Java and Spring Level 3 This level · 27 of 56
Hard java spring

How do you deploy with zero downtime when the DB change is backward-incompatible?

The 30 second answer

I split the change into expand, migrate and contract. First I add the new column or table and keep the old one. Then I deploy code that writes to both, backfill the data, and switch reads to the new structure. Only in a later release do I drop the old column. During a rolling update both versions run together, so every step must work with both.

Could you say this in an interview?

Split it into expand, migrate, contract (parallel change). Expand: add the new column or table, keep the old one. Deploy code that writes both and reads the old. Backfill. Deploy code that reads the new. Contract: drop the old column in a later release. During a rolling update, old and new versions run at the same time, so every step must work with both. Follow-up: How do you rename a column safely? (Add the new one, dual-write, backfill, switch reads, then drop; never rename in place.)

Alternate interview scenarios

"How do you deploy without downtime?"

If there is no schema change, the question is about traffic, not data. I use a rolling update with maxUnavailable set to zero, so new pods come up before old ones go away. A new pod gets traffic only after its readiness probe passes, and Spring Boot exposes liveness and readiness through Actuator health groups. When an old pod stops, Spring Boot graceful shutdown stops taking new requests and lets in-flight ones finish within spring.lifecycle.timeout-per-shutdown-phase. That timeout must fit inside the pod's termination grace period. I also make sure the release is backward compatible with the version still running, and I use a canary or blue-green switch when I want a fast rollback. The schema case on top of that is expand, migrate and contract, as above.


Sources (4)
  1. Martin Fowler: Parallel changemartinfowler.com
  2. Amazon ECS rolling update deploymentsdocs.aws.amazon.com
  3. Kubernetes: Deployments (rolling update, maxUnavailable)kubernetes.io
  4. Spring Boot reference: Graceful shutdowndocs.spring.io
Previous
Next