The signature and its nearby causes#
A service behind a Network Load Balancer holds long-lived TCP connections: a database proxy, a message broker, a streaming feed. Short requests come through a deploy without errors, but clients that hold one connection for minutes or hours drop it, or stop receiving data and hang until their own timeout fires. When ECS replaces a task, it deregisters the old target. The load balancer stops sending new connections to a deregistering target and lets existing connections drain for the deregistration delay. DeregisterTargets · ECS load balancer considerations
Three causes are close together:
- The load balancer closed the connections.
deregistration_delay.connection_termination.enabledis true, so connections still open when the delay ends are closed. This is the setting doing its job; the client has to reconnect. - The connection outlived the old task. Termination is off, so the load balancer keeps forwarding an active connection to a deregistered target that stays healthy. The connection then ends with the task's process when ECS stops it.
- The connection went idle. The listener's
tcp.idle_timeout.secondsended an idle flow. This happens with or without a deploy.
The first two are deploy behavior and differ in who ends the connection. The third is a listener setting that a deploy only makes visible. HTTP 502 and 503 errors while ECS drains Application Load Balancer targets are a separate question, and other routing signatures are in the signature table.
Read the drain settings#
The target group holds the delay and the termination switch. The delay ranges from 0 to 3600 seconds and defaults to 300. Connection termination defaults to true for new UDP and TCP_UDP target groups and to false for others. A separate attribute, target_health_state.unhealthy.connection_termination.enabled, closes connections to targets that become unhealthy and defaults to true. Use a read-only profile and replace the uppercase placeholders. TargetGroupAttribute · describe-target-group-attributes
aws elbv2 describe-target-group-attributes \
--profile PROFILE --region REGION --target-group-arn TARGET_GROUP_ARN \
--query 'Attributes[?starts_with(Key,`"deregistration_delay"`) || starts_with(Key,`"target_health_state"`)]' \
--output json --no-cli-pager
aws elbv2 describe-listener-attributes \
--profile PROFILE --region REGION --listener-arn LISTENER_ARN \
--query 'Attributes[?Key==`"tcp.idle_timeout.seconds"`]' \
--output json --no-cli-pager
The listener's TCP idle timeout ranges from 60 to 6000 seconds and defaults to 350. Then read the old task definition's stop timeout. On Fargate, stopTimeout defaults to 30 seconds and can't exceed 120. On EC2, an unset value falls back to the agent's ECS_CONTAINER_STOP_TIMEOUT, then to 30 seconds. ListenerAttribute · ContainerDefinition
aws ecs describe-task-definition \
--profile PROFILE --region REGION --task-definition OLD_TASK_DEFINITION_ARN \
--query 'taskDefinition.containerDefinitions[].{name:name,stopTimeout:stopTimeout}' \
--output json --no-cli-pager
Line the drops up with the task timeline#
The separating read is time. Take the times your clients logged a disconnect and set them beside the old task's own timestamps. stoppingAt is when the task moved from RUNNING to STOPPING, and stoppedAt is when it reached STOPPED. Task fields · describe-tasks
aws ecs describe-tasks \
--profile PROFILE --region REGION --cluster CLUSTER --tasks OLD_TASK_ARN \
--query 'tasks[].{deployment:startedBy,definition:taskDefinitionArn,stopping:stoppingAt,executionStopped:executionStoppedAt,stopped:stoppedAt,code:stopCode,reason:stoppedReason}' \
--output json --no-cli-pager
While the drain is still running, describe-target-health shows the old target as draining with reason Target.DeregistrationInProgress, which means the delay has not expired. On the load balancer side, ActiveFlowCount per target group shows how many flows were open, and TCP_ELB_Reset_Count counts resets the load balancer generated. The reset metrics are published per load balancer and Availability Zone, not per target group. TargetHealth · NLB CloudWatch metrics
aws cloudwatch get-metric-statistics \
--profile PROFILE --region REGION --namespace AWS/NetworkELB \
--metric-name TCP_ELB_Reset_Count \
--dimensions Name=LoadBalancer,Value=LB_DIMENSION \
--start-time START --end-time END --period 60 --statistics Sum \
--query 'sort_by(Datapoints,&Timestamp)[].[Timestamp,Sum]' \
--output json --no-cli-pager
aws cloudwatch list-metrics --namespace AWS/NetworkELB gives the exact LoadBalancer value. Swap in ActiveFlowCount with an added Name=TargetGroup,Value=TG_DIMENSION dimension for flows by group.
What each pattern means#
| When clients dropped | Termination | Likely cause | Next step |
|---|---|---|---|
| About one delay after the target began draining, together | true | The load balancer closed the remaining connections | Make clients reconnect on close, or lengthen the delay up to 3600 seconds |
At the old task's stoppingAt to stoppedAt | false | Connections outlived the drain and ended with the process | Close or hand off connections from the application on shutdown |
| Soon after the target began draining | either | A short delay, possibly 0 | Raise the delay |
At intervals near tcp.idle_timeout.seconds, deploy or not | either | Listener idle timeout | Send keepalives more often than the idle timeout, or raise it |
AWS lists three ways to make sure existing connections close during deregistration: enable connection termination, make the target unhealthy before deregistering it, or close client connections periodically. ECS recommends a stop timeout longer than the deregistration delay, and a delay longer than the client's connection timeout, so clients time out and reconnect while the old task drains. On Fargate the 120-second stop timeout limit means following that order requires a delay below 120 seconds. Edit NLB target group attributes · ECS connection draining
An illustrative fixture: two timelines#
This fixture is fictional. The service ticker streams prices over TCP port 7000 behind ticker-nlb, and clients hold one connection each for hours. Deployment ecs-svc/2211 replaces ticker:57 with ticker:58. ECS begins draining the first old target at 10:00:00 UTC.
In the first timeline, the target group has a 300-second delay and termination enabled. Every client on that target logs a disconnect between 10:05:00 and 10:05:05, one delay after draining began. The load balancer closed the connections as configured. Clients that reconnected at once landed on a registered target; one client library waited 60 seconds before retrying, and that wait was the outage its users saw. The fix is in the client's reconnect logic.
In the second timeline, the delay is 30 seconds and termination is off. The old target leaves draining at 10:00:30, but its connections stay active, so the load balancer keeps forwarding them. The old task's stoppingAt is 10:00:31 and stoppedAt is 10:01:01, 30 seconds later, matching its stopTimeout. Clients disconnect between those two times, and some see nothing until their keepalive notices. The connections ended with the process, so the change is a shutdown handler that closes client connections on SIGTERM and lets them reconnect to a new task.
Change the attributes and confirm on the next deploy#
This command changes the target group. Choose the delay from how long your clients need to reconnect. Connections can outlive any delay, so a delay sized to the longest connection only postpones the close. modify-target-group-attributes
# Change: close remaining connections at the end of a 90-second drain
aws elbv2 modify-target-group-attributes \
--profile PROFILE --region REGION --target-group-arn TARGET_GROUP_ARN \
--attributes Key=deregistration_delay.timeout_seconds,Value=90 \
Key=deregistration_delay.connection_termination.enabled,Value=true \
--output json --no-cli-pager
On the next deploy, record the time each old target starts draining and the time clients disconnect. With termination on, disconnects should cluster at the end of the delay. Check that each client reconnected to a new task within its retry interval; the new deployment's startedBy value identifies the replacement tasks.
Where the reading stops#
The load balancer's records are aggregate. Reset counts have no target group dimension, ActiveFlowCount does not say which flows belonged to which target, and metrics are reported only while traffic flows. The disconnect times have to come from the clients, so log them with the remote address. For QUIC traffic the delay is fixed at 300 seconds. A connection that the load balancer keeps forwarding to a draining target can also leave a client on the old version after the deploy, which can look like traffic on the wrong target group. If the new targets never became healthy, the drain is not the problem: check the security group path for NLB targets. Edit NLB target group attributes
Questions#
What does connection termination on deregistration do on an NLB?
When the target group attribute deregistration_delay.connection_termination.enabled is true, the Network Load Balancer closes connections that are still open on a deregistered target at the end of the deregistration delay. When it is false, which is the default for TCP target groups, an active connection to a target that stays healthy can keep flowing after the delay.
Why do NLB connections stay on a target after it is deregistered?
Without connection termination, the load balancer can keep sending traffic on an existing, non-idle connection to a deregistered target that is still healthy. The connection ends when the client or the target closes it, or when the task stops.
Should the ECS stop timeout be longer than the deregistration delay?
ECS recommends a stop timeout longer than the deregistration delay, and a delay longer than the client's connection timeout. On Fargate the stop timeout can't exceed 120 seconds, so that order needs a delay shorter than 120 seconds.