Skip to content

Zero-Downtime Deploys with Traefik on a t4g.micro

Most blue-green deployment guides assume you have a load balancer and multiple servers. Here’s how MealPal does it on a single t4g.micro with Traefik.

Traditional deployments have a brief downtime window when the old container stops and the new one starts. For an app with streaming chat (SSE), even a few seconds of downtime means dropped connections and a bad user experience.

The Solution: Traefik as a Local Load Balancer

Section titled “The Solution: Traefik as a Local Load Balancer”

Traefik runs as a reverse proxy in Docker Compose, watching container labels for routing rules. The deploy script:

  1. Determines which slot is active (blue or green)
  2. Starts the new container in the inactive slot
  3. Waits for the health check to pass
  4. Updates Traefik labels to route traffic to the new container
  5. Gracefully stops the old container
Terminal window
# Simplified deploy logic
if is_active "blue"; then
deploy_to "green"
switch_traffic "green"
stop "blue"
else
deploy_to "blue"
switch_traffic "blue"
stop "green"
fi

The most important detail: docker compose up --no-recreate. Without this flag, Docker Compose will recreate the Traefik container when bringing up the new backend, causing a brief outage.

  1. Traefik watches Docker labels — no config file reload needed
  2. Health checks are essential — never route traffic to an unhealthy container
  3. --no-recreate is critical — prevents proxy restarts during deploy
  4. Traefik can crash during Let’s Encrypt renewal — add a cron job to restart it