← Back to list

Blue Green & Canary Releases for FastAPI with Labels and Probes

Modern web teams want new features live without breaking a sweat or a session, and FastAPI with Docker makes that feel almost easy. Blue…

Tera Byte 26 in Write A Catalyst · 2025-10-19 15:36 · 30 claps · 4.7 min read paywalled
#fastapi-deployment #blue-green-deploy #canary-release #caddy-reverse-proxy #gunicorn-uvicorn
Open on Medium ↗
Wiki topics: STP · Startups & Venture ☁️ · DevOps & Cloud

Blue Green & Canary Releases for FastAPI with Labels and Probes

Generated by sora.chatgpt.com

Generated by sora.chatgpt.com

Modern web teams want new features live without breaking a sweat or a session, and FastAPI with Docker makes that feel almost easy. Blue green and canary releases give you two friendly ways to swap versions with grace. Add smart labels and health probes, put Caddy in front as a lean reverse proxy, choose uv or gunicorn for the app server, and wire the whole thing into CI CD with observability from the first request. The result is a service that rolls forward with confidence and rolls back without drama.

Blue green keeps two identical environments ready, one serving traffic and one waiting in the wings. When a new FastAPI image is built, you start it as green while blue keeps handling users. Caddy points the world at blue, then after readiness checks pass you switch the router to green in one atomic change. Because both stacks exist at once, database migrations and cache warming can be rehearsed before any switch, which turns scary deploys into a calm choreography.

Canary adds nuance when you want to watch real traffic flow through the new image before a full cutover. With Caddy or an ingress controller in a cluster, you can send a small slice of traffic to the canary by label or header match and watch metrics breathe in real time. If error rates stay quiet and latency holds steady, you increase the slice until the old version sits idle. If anything drifts, you return the slice to zero and try again after a quick fix.

Labels make the dance reliable and scriptable. Tag containers or pods with app name, version, role, and color so your CI CD jobs and your router know exactly where to send probes and traffic. Something like app FastAPI, role api, version v1 two three, color green keeps selectors readable for both humans and automation. Caddy upstream definitions or Docker Compose services can target by label, which avoids brittle references to container ids.

Probes tell you when to trust a release. Liveness probes answer the question can the process recover or should it be restarted, while readiness probes answer the question can this instance take traffic now. In FastAPI, expose a lightweight route such as slash healthz that returns dependency checks and maybe build info. Keep it fast and side effect free so the probe itself never becomes the problem. In Docker healthcheck or in orchestration, set reasonable intervals and timeouts, and make the readiness probe slightly stricter than liveness so routing happens only when dependencies are truly awake.

FastAPI runs well under uvicorn or gunicorn with uvicorn workers. The uv runner has become a speedy way to install and execute Python apps and can simplify local and CI environments by avoiding heavy virtualenv steps. For production density, gunicorn with multiple uvicorn workers gives predictable concurrency. Keep worker counts tied to CPU cores and memory limits, and prefer async paths in FastAPI to keep the event loop clear. No matter which you choose, expose a single clear port for Caddy to proxy.

Caddy shines here because its config is compact and its automatic TLS is a gift. Use a site block that defines two upstream pools, one for blue and one for green, selected by a route that you can flip with a single config reload. For canary, define a matcher that routes a percentage or a cookie group to the canary upstream. Because Caddy reloads without dropping connections, users never feel the switch. Access logs and request duration metrics from Caddy also feed nicely into your observability story.

CI CD ties it all together. A pipeline triggers on a main branch push, builds a Docker image, runs unit and contract tests, pushes to a registry, and deploys to the green environment with labels. The pipeline then polls the readiness probe and runs smoke tests through Caddy against a scoped route that only hits green. If all checks pass, the pipeline flips the router to green and marks the release. For canary, the stages gradually raise the traffic percentage, pausing to evaluate error budgets and p ninety five latency before each nudge.

Observability proves that your hunches are right. Export Prometheus metrics from FastAPI for request counts, status codes, and latency buckets. Add structured logs with trace ids so a single user action can be followed from Caddy to the app and back to the client. Dashboards that compare blue and green or stable and canary side by side make trends obvious. Alerting should key on rate error duration not just absolute errors, so small spikes in a tiny canary do not page the team at night.

Stateful dependencies demand care during a switch. Plan database migrations with backward compatible steps so both versions can run together for a while. Use feature flags to hide code paths that need new columns until after the migration lands. For caches, warm the green instances using a replay of common keys, then point the app at a shared store to avoid cold starts. File uploads and background jobs should mark their producer build id so consumers can be upgraded with the same cadence.

Rollbacks should be as simple as a router change. Keep at least one prior image readily available and keep configuration and secrets versioned alongside code. If a canary reveals an issue, drop its traffic to zero, keep it running for debugging if resources allow, and open a follow up fix branch with diagnostics attached. Post release reports should include what the probes and metrics said, not just whether the switch succeeded.

Security belongs in the path, not bolted on later. Caddy can terminate TLS and enforce modern ciphers. The app should verify bearer tokens or sessions at the edge and again inside sensitive routes. Docker images should be scanned before deployment and pinned by digest to ensure you are running exactly what CI built. Environment variables that hold credentials should come from a secrets store and never from a repo.

When you put these pieces together, deployments become a quiet routine. FastAPI serves brisk responses behind Caddy, uv or gunicorn offers steady concurrency, Docker packages the app predictably, and CI CD pushes with guardrails. Labels point traffic where it should go, probes confirm that each instance is healthy, and observability lets you see the moment something wanders. Blue green for decisive switches and canary for cautious ramps let you pick the tempo that fits your team and your users, and they reward that choice with calm, repeatable releases that feel almost boring in the best possible way.

Your turn. What will you try on your next release and why Drop your plan or a lesson learned below. If this article earned your time, a quick clap and a follow go a long way.


메타데이터
post_id
bfdf7526b271
slug
blue-green-canary-releases-for-fastapi-with-labels-and-probes-bfdf7526b271
url
https://medium.com/write-a-catalyst/blue-green-canary-releases-for-fastapi-with-labels-and-probes-bfdf7526b271
canonical_url
https://medium.com/write-a-catalyst/blue-green-canary-releases-for-fastapi-with-labels-and-probes-bfdf7526b271
author_url
https://medium.com/@terabyte26
status
ok
fetched_at
2026-08-24 23:22:41