Deployment strategies: rolling, blue-green, canary, feature flags and zero downtime on Kubernetes
Recreate, rolling, blue-green, canary, shadow and feature flags compared on rollback, cost and database compatibility; expand/contract migrations so two versions can run side by side; the Kubernetes a backend developer needs (probes, maxSurge/maxUnavailable, resources, PodDisruptionBudget), Spring Boot graceful shutdown and health groups, and what to watch during a canary.
On this page
1. The problem
Every rolling deploy of 6 pods produces 40–60 502 errors: new pods get traffic before they finish starting, old pods are killed mid-request. Another time, a migration renames phone to phone_number, and the 3 remaining old pods break immediately.
2. Key idea
Strategies differ in rollback speed, extra machines, and how long both versions run together. Both versions always share one database, so "rollback" brings back code, not schema.
| Strategy | Rollback | Extra cost | Use when | Don't use when |
|---|---|---|---|---|
| Recreate | redeploy the old version | 0 | the versions can't run side by side | downtime isn't acceptable |
| Rolling | kubectl rollout undo |
a few pods | the default | high-risk changes |
| Blue-green | switch the router: seconds | double during the switch | you need instant rollback | resources are tight |
| Canary | weight back to 0 | small | risky changes with enough traffic | 20 requests/s: 1% isn't enough to conclude |
| Feature flag | turn the flag off: instant | near 0 | separating deploy from release | flags live forever and nobody cleans up |
2.1 How do you change schema while two versions run?
Expand/contract across releases: (1) add the new column, code writes both; (2) backfill old data; (3) code reads the new column; (4) stop writing the old column; (5) drop it only after an observation period. A migration must work with the code version right before it. APIs and messages too: adding a field is fine, removing or changing meaning takes a full cycle.
2.2 What does each probe check?
| Probe | On failure | Should check |
|---|---|---|
| Startup | restart past the threshold | like liveness, with more time |
| Readiness | removed from the Service, no restart | internal state, plus dependencies the pod is useless without |
| Liveness | container restart | internal state only (deadlock, stuck) |
3. In Spring Boot and Kubernetes
# application.properties
management.endpoint.health.probes.add-additional-paths=true # /livez, /readyz on the main port
management.endpoint.health.group.readiness.include=readinessState,db
server.shutdown=graceful # SIGTERM: stop accepting, wait for in-flight requests
spring.lifecycle.timeout-per-shutdown-phase=30s
# Deployment
strategy: { rollingUpdate: { maxSurge: 1, maxUnavailable: 0 } } # always enough pods available
minReadySeconds: 10
lifecycle: { preStop: { exec: { command: ["sleep", "10"] } } } # wait for endpoints to update
terminationGracePeriodSeconds: 45 # > 10 + 30SIGTERM and removal from endpoints happen in parallel; without preStop the pod stops accepting connections while the Ingress still sends to it. Other languages: Go must catch SIGTERM and call server.Shutdown(ctx); Node.js calls server.close().
Canary: what to watch and when to stop
Compare the canary with the old version at the same time, on signals users can feel:
| Signal | Example stop threshold |
|---|---|
| 5xx rate | canary > old + 0.5 percentage points |
| p99 latency | canary > 1.2 × old |
| SLO burn rate | > 10 over 10 minutes |
Burn rate = error rate / (1 − SLO): a 99.9% SLO allows 0.1% errors, so a canary at 0.5% burns at 5. Compute the needed request count first: at 20 requests/s, a 1% canary gets only 120 requests in ten minutes, and one error is already 0.8%.
Sessions in pod memory disappear on deploy; move them to Redis or JDBC rather than relying on sticky sessions.
4. Working with AI
Hand to AI: writing Deployment, Service and PDB manifests; splitting a column rename into expand/contract steps; writing Prometheus queries for canary criteria.
Check yourself in AI-written config:
- Liveness doesn't check the database or other services.
- There's a
preStopsleep andterminationGracePeriodSecondsexceeds the sleep plus shutdown time. - No migration drops or renames a column in the same release as the code that stops using it.
- Services with 2+ pods have a PodDisruptionBudget.
5. Exercises
Exercise 1: Count pods in a rolling update
A Deployment with replicas: 10 uses the defaults maxSurge: 25%, maxUnavailable: 25%. During rollout, what's the maximum pod count and the minimum available? The service runs at 80% CPU and the AI left the defaults. Should you change them?
Solution 1
- 25% of 10 is 2.5:
maxSurgerounds up to 3,maxUnavailablerounds down to 2. At most 13 pods, at least 8 available. - At 80% CPU, 8 pods carrying the load of 10 puts each at 100%. Switch to
maxSurge: 1, maxUnavailable: 0: always 10 available. - The cost: one pod at a time, waiting for ready, so rollout takes about 10 startup cycles; if the cluster has no room for an 11th pod, the rollout stalls.
Exercise 2: Split the full_name column
customer has full_name; a new requirement needs first_name and last_name, and the service rolls out over 6 pods. The AI proposes one migration: add the columns, backfill, drop full_name, shipped with the new code. Review it and write a release plan, with where each step can roll back to.
Solution 2
A single migration like that breaks the old pods immediately (full_name is gone) and can't be rolled back. Split it:
- R1 expand: add both columns nullable; code writes all three, still reads
full_name. Rolling back to R0 is safe. - R2 backfill: a job splits names for old rows (
WHERE first_name IS NULL) and logs uncertain splits. Safe. - R3 switch reads: read the new columns, still write all three, behind a feature flag. Rolling back to R2 is safe.
- R4 stop writing
full_name: from here you can only roll back to R3. - R5 contract: after a week of observation, drop
full_name; move reports, ETL and CDC still reading it first.
Summary
- Rolling by default, canary for risky changes, blue-green when you need instant rollback.
- Both versions always run together on one database: expand/contract across releases.
- You can roll back code, not a
DROP COLUMN. - Readiness removes traffic, liveness restarts; liveness doesn't check the database.
- Pod shutdown: removal from endpoints,
preStop, SIGTERM, wait for in-flight requests.
Further reading: Kubernetes for backend developers.
Self-compiled notes from official documentation and hands-on practice.
Practice interview questions on this topic
- How do readiness, liveness and startup probes differ? How do you configure them for Spring Boot?
- How do rolling, blue-green and canary deployments differ? How do feature flags help deployments?
- How do you deploy Spring Boot on Kubernetes without dropping a request? What do you check when every deploy shows 502s?
- How do you change a schema with zero-downtime deploys, where do migrations run, and how do you roll back?
Comments
Community rules