Sub topic 13
Safe deployments with the circuit breaker
This chapter assumes you can register a task definition, create an ECS service on Fargate behind an Application Load Balancer, and read the service's event log. If any of that is unfamiliar, review the chapters on services and health checks first.
How a rolling update works
An ECS service uses the rolling update deployment type by default. When you register a new task definition revision and update the service, ECS starts tasks from the new revision and stops tasks from the old one. Two settings control the pace:
minimumHealthyPercentis the share ofdesiredCountthat must stay healthy during the deployment. The default is 100.maximumPercentis the most tasks, old and new together, that may run at once. The default is 200.
With a desired count of 2 and the defaults, ECS starts two new tasks first. Only after they pass their health checks does it stop the two old ones. This keeps full capacity during the rollout, and you pay for the extra tasks briefly.
The problem the circuit breaker solves
Suppose the new image crashes on startup. Without protection, ECS keeps launching replacement tasks, and each one fails. The deployment stays in progress and consumes capacity and money. Your old tasks keep serving traffic, but the service never converges.
The deployment circuit breaker detects this. It counts tasks of the new deployment that fail to reach a steady state. A task fails if it stops before it becomes healthy, or if the load balancer marks it unhealthy and it is replaced. When the failure count crosses a threshold, ECS marks the deployment FAILED. With rollback=true, it then redeploys the last deployment that completed successfully.
The threshold scales with the service. It is half of the desired count, with a minimum of 3 and a maximum of 200 failed tasks. For a service with a desired count of 2, three failures trip the breaker.
Enabling it
You enable the circuit breaker in the deployment configuration. The create-service example earlier in this tutorial already did so. To turn it on for an existing service:
aws ecs update-service \
--cluster demo \
--service web \
--deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true}"To deploy a new revision, pass the task definition:
aws ecs update-service \
--cluster demo \
--service web \
--task-definition web-app:2
aws ecs wait services-stable --cluster demo --services webThe wait command returns once the service is stable. If the deployment fails and rolls back, the service still becomes stable, but on the old revision. Check which revision is running rather than assuming the update succeeded:
aws ecs describe-services \
--cluster demo \
--services web \
--query "services[0].deployments[].{status:status,rollout:rolloutState,taskDef:taskDefinition}"A rolloutState of COMPLETED means the deployment succeeded. FAILED means the breaker tripped.
Common mistakes
- No health signal. The breaker can only react to failures ECS can see. If your container has no health check and the target group check is too lenient, a broken app can look healthy.
- Grace period too short. If
healthCheckGracePeriodSecondsis shorter than your app's startup time, the load balancer marks healthy tasks as failed. The breaker then counts them and may roll back a good release. - Rollback to a bad baseline. Rollback restores the last completed deployment. If that revision depended on something you have since removed, such as a secret or a database column, the rollback can fail too.
Exercise
A service has a desired count of 4 and the circuit breaker is enabled with rollback. A new revision fails to start. After the breaker trips, which revision serves traffic, and how can you confirm it from the CLI?
Show answer
The last successfully completed revision serves traffic, because rollback redeploys it. Confirm with aws ecs describe-services and inspect deployments[].rolloutState and taskDefinition. The failed deployment shows FAILED, and the running task definition is the earlier revision.