Deployment and Scaling

Learn about statelessness, health checks, and version compatibility through independent order releases, instance scaling, and rolling updates.

🎯 Introduction

Suppose orders adds an order note, while users, inventory, and payments remain unchanged. Must all four stop and redeploy? If order traffic suddenly grows, can we increase only order capacity?

These are the benefits of independent deployment and scaling. Using orders, this article explains releasing a version, adding instances, and minimizing interruptions during updates. Other courses cover Docker, Nginx, and Kubernetes configuration; here we focus on the microservices deployment process.


🧱 Why an Order-Only Change Can Ship Independently

Suppose these versions are running, and this release adds only an optional note field to orders:

ServiceBeforeAfter
Usersv1.0v1.0, unchanged
Ordersv1.0v1.1
Inventoryv1.0v1.0, unchanged
Paymentsv1.0v1.0, unchanged

With its own entry point, runtime configuration, and deployable unit, orders can replace only its own running instances. Other services continue without restarting.

Independent deployment does not mean arbitrary changes cannot affect others. Verify two things first:

  1. New orders remains compatible with existing inventory, payment, and other APIs.
  2. Any order schema changes are planned so both old and new instances can use the database.

For example, a nullable note column can usually be added through a compatible migration before deploying code that uses it. Database migration and application release are separate actions requiring coordination.


📈 Scaling Only Orders as Traffic Grows

Scaling out adds running instances of one service. Orders can grow from one to three instances, all running the same program.

courseComponents.mermaid.loading

The balancer assigns each request to one available instance; it does not execute the same request on all three. New instances must join the traffic-eligible list through registration or platform mechanisms. “Service Communication and Discovery” covered the principles.

Why multiple instances should be stateless

Suppose A handles a request and stores order progress only in a JavaScript variable. A later request reaches B, which cannot read A's memory and may lose that progress.

Store business state needed across requests and instances in appropriate shared persistent storage such as MySQL. A, B, and C can then continue using the same order record.

Stateless does not mean orders have no state. It means required shared business state does not depend on memory exclusive to one instance.

Adding instances does not guarantee proportional performance gains. If the database or inventory is already saturated, extra order instances may add downstream pressure. Use metrics to locate bottlenecks before and after scaling.

🔄 Why Not Stop Every Instance During a Release?

Suppose orders has old instances A and B and needs v2. Stopping both before starting replacements can leave requests with nowhere to go.

A rolling update gradually replaces old instances while preserving enough available capacity wherever possible.

StageServing instancesWhat happens
Before releaseA(v1), B(v1)Both old instances handle requests
Start new versionA(v1), B(v1)C(v2) starts without receiving new requests
New instance readyA(v1), B(v1), C(v2)C passes checks and joins traffic distribution
Replace first old instanceB(v1), C(v2)A stops accepting new requests, drains in-flight work, and exits
Start another new instanceB(v1), C(v2), D(v2)D joins after becoming ready
Release completeC(v2), D(v2)B exits gracefully, leaving only new instances

This illustrates the principle. Platforms may use different startup and replacement orders or limit extra capacity during rollout.

Two points matter:

  • Add new instances to traffic distribution only after they are ready.
  • After old instances stop accepting new requests, let in-flight requests finish before exiting.

This reduces interruption without guaranteeing zero downtime. Startup times, connection shutdown, timeouts, and capacity still matter.


🩺 Determining Whether a New Instance Can Receive Traffic

A running process may still be initializing configuration or database connections and unable to handle orders.

Deployments commonly distinguish two checks:

CheckQuestionDeployment role
LivenessCan the process still run normally?Determine whether an unhealthy instance needs restarting
ReadinessCan it handle new requests now?Determine whether it receives traffic

C can pass liveness while initialization is incomplete, but should not pass readiness. Let it receive requests only after preparation finishes.

Likewise, remove an exiting instance from new-request routing before draining existing requests and stopping.

Do not make a transient database disruption repeatedly fail liveness and trigger restart loops. Kubernetes and other deployment courses cover concrete probe configuration; here we distinguish responsibilities.


🧩 Keeping Old and New Versions Compatible

During a rollout, A(v1) and C(v2) may run together. Deleting fields v1 still needs can break it.

A common approach is expand, migrate, then contract. Consider an order column currently called remark, to be renamed note. Do not delete remark while old code still reads it.

  1. Expand: add note while retaining remark. Arrange compatible reads and writes so both versions receive correct notes.
  2. Migrate: gradually release code using note, migrate existing data, and verify correct reads and writes during coexistence.
  3. Contract: after all old instances exit and no code depends on remark, end compatibility handling and delete the old column.

The key is the sequence: make both versions work, switch gradually, then remove the old form.

If the release merely adds an optional note field, there is usually no need to delete another column just to demonstrate migration. Add the field compatibly, then release. Renaming, deleting, or changing field semantics requires a fuller transition.

Compatibility also applies to HTTP APIs and business event formats, not just databases. Do not remove fields while callers still depend on them.


🛠 Checks Before and After Deployment

WhenCheck
Before releaseVerify API and schema compatibility; prepare a failure response plan
Starting new instancesConfirm readiness before business traffic
Replacing old instancesStop new assignments and handle in-flight requests gracefully
After releaseWatch errors, P95 latency, and other metrics for regressions
On failureChoose application rollback or forward fixes based on schema compatibility; do not blindly revert schemas already used by new code

These checks are tool-independent. Docker Compose, Nginx, and Kubernetes provide implementation mechanisms; developers still need to plan sequence and compatibility.


❓ Knowledge Check

Q1: With three order instances, does one creation request execute three times?

No. Normal balancing assigns one request to one instance. Instances share different requests. Duplicate requests caused by network retries still require business-level handling.

Q2: Does statelessness prohibit Redis?

No. It avoids relying on one process's private memory for shared state. Appropriate external MySQL or Redis storage is consistent with that principle.

Q3: Can every old instance stop as soon as a new process starts?

Startup alone is insufficient. Verify readiness and capacity, then gradually stop old instances accepting new requests and drain their in-flight work.


🧪 Exercise

Orders currently has A and B running v1. Version v2 adds optional note; inventory and payments do not change.

  1. Explain why only orders needs releasing and which database preparation comes first.
  2. List a gradual replacement sequence and when new instances begin receiving requests.
  3. If traffic requires a third instance, explain how it joins traffic and why order state cannot remain only in one instance's memory.

Acceptance criteria: No simultaneous restart of all services; new instances receive traffic only when ready; old instances drain in-flight requests; coexistence does not break because of note.


🎉 Congratulations on completing this article! You now understand independent releases, adding instances as needed, and maintaining availability during version transitions.