Connection Draining and Graceful Shutdown
What you'll learn: How to safely remove backend targets from service without interrupting active user requests.
The Core Problem
When you need to remove a target (like an EC2 instance) from behind a load balancer—perhaps for maintenance, updates, or scaling down—you face a critical challenge: that target may be handling active connections. If you yank it out immediately, users get broken requests and error messages.
What is Connection Draining?
Connection draining is a load balancer feature that gives your target a "grace period" to finish handling existing requests before fully removing it from service. During this period:
- The load balancer stops sending new requests to the draining target
- Existing connections are allowed to complete naturally
- Once all connections finish (or a timeout expires), the target is fully removed
Think of it like a restaurant closing: when it's time to close, you stop seating new customers but let existing diners finish their meals peacefully.
How It Works
When you deregister a target or mark it unhealthy:
- The load balancer enters "draining" mode for that target
- Active requests have a configurable timeout window (e.g., 300 seconds)
- New requests route to healthy targets only
- Monitoring shows the target as "draining" status
This is also called graceful shutdown because the transition happens smoothly without dropping traffic.
Configuration Essentials
You typically configure:
- Draining timeout: how long to wait (0–3600 seconds)
- Deregistration delay: time before full removal
- Connection tracking to monitor active requests
Key Takeaway: Connection draining ensures zero-downtime deployments and maintenance by letting active requests finish before removing targets from service, preventing user-facing errors during infrastructure changes.