Lesson 24 / Unit 6: Production Operations
Draining Deploys
A 30-second kill timer ends a ten-minute call mid-sentence. How to drain LiveKit workers and SFU nodes on SIGTERM, one node at a time, with the timeouts in order.
A web service deploys by replacing instances behind a balancer, and nobody notices. Try that on a voice fleet and every node you replace takes its live calls with it.
The framework knows how to wait. Your supervisor usually does not. A 30-second kill timer in front of a worker holding a ten-minute call ends that call mid-sentence, whatever the drain setting says.
Drain Behaviour
Draining means a node stops taking new calls, lets its existing calls end on their own, and only then exits. Every zero-downtime voice deploy is this one primitive applied per tier.
Both LiveKit components drain natively on a termination signal. What differs is how long they wait and what can cut the wait short.
| Component | Signal | While draining | Exits when |
|---|---|---|---|
| Agent worker | SIGTERM, SIGINT | Accepts no new jobs; running jobs continue | Jobs finish, or drain_timeout passes |
| LiveKit server (SFU) | SIGTERM, SIGINT, SIGQUIT | Active rooms run and admit participants; joins to new rooms rejected | Every participant has disconnected |
Sources: agent server options and downscaling and draining in the LiveKit docs.
Shutdown Timeouts
A worker in production (start) mode drains on SIGTERM: it stops accepting jobs and waits for running ones, up to drain_timeout. The default is one hour. The deploy only works when every timer above it is longer.
Two decisions make it real.
The drain timeout must exceed your longest reasonable call. A five-minute timeout with calls that can run to ten minutes kills the tail mid-sentence, so if you have lowered it, raise it back above your call length:
from livekit.agents import AgentServer
# Longest reasonable call: 10 minutes. Drain for 15.server = AgentServer(drain_timeout=900) # seconds in PythonThe supervisor must be told to wait longer than the drain. A systemd unit or Kubernetes pod with a 30-second kill timer overrides the framework’s patience with its own, and the drain you configured never actually runs. Both end in SIGKILL.
# systemd unit: must exceed drain_timeout[Service]TimeoutStopSec=960# Kubernetes pod spec: must exceed drain_timeoutspec: terminationGracePeriodSeconds: 960TimeoutStopSec is documented in systemd.service. The order to check is: longest call, then drain_timeout, then the supervisor timeout, each longer than the last.
This is also where the agent shutdown leak stops being theory: a worker that never reports job completion never finishes draining, and your deploy pipeline hangs on a bug you shipped weeks earlier. If a drain takes exactly drain_timeout every time, suspect that before you suspect long calls.
Termination Hooks
To drain an SFU node, stop new room placement on it, let existing rooms end naturally, then terminate. A multi-node deployment with Redis makes this workable, because new rooms route to the remaining nodes while this one empties (see multi-node LiveKit with Redis).
LiveKit server does the first two steps itself on SIGTERM. The risk is the layer above it terminating the instance early, and on an Auto Scaling group a termination lifecycle hook holds the instance in a wait state while it drains:
aws autoscaling put-lifecycle-hook \ --lifecycle-hook-name drain-calls \ --auto-scaling-group-name sfu-asg \ --lifecycle-transition autoscaling:EC2_INSTANCE_TERMINATING \ --heartbeat-timeout 900The hook does not execute the drain by itself. Wire its event to an instance-side agent or orchestration handler with permission to send heartbeats and complete the lifecycle action. Then use this sequence:
- The hook fires and the instance enters its wait state.
- On the instance, stop the LiveKit service so it receives SIGTERM and starts draining. The server exits when its last participant leaves, so the unit’s
TimeoutStopSecmust also outlast your longest call. - Watch the node’s active-call count, the same custom metric from autoscaling on active calls. If calls are still running near the heartbeat timeout, extend it with
aws autoscaling record-lifecycle-action-heartbeat. - When the node reports zero calls, release it:
aws autoscaling complete-lifecycle-action \ --lifecycle-action-result CONTINUE \ --lifecycle-hook-name drain-calls \ --auto-scaling-group-name sfu-asg \ --instance-id "$INSTANCE_ID"Two limits from the AWS lifecycle hook docs matter here. The heartbeat timeout ranges from 30 to 7,200 seconds, and termination hooks have a finite global timeout too; repeated heartbeats cannot wait forever. A timeout can allow termination while calls remain. The drain guard should prevent routine termination above zero, but cannot guarantee survival of a crash, expired hook or forced termination. Test those recovery paths separately.
Rolling Replacement
Roll one node at a time and drain each one fully before terminating it. Your longest call sets the deploy clock. The CI pipeline does not.
One node at a time: stop new work, wait for active calls to finish, check the node holds zero calls, then replace it.
Open full size- Pick the traffic trough. Deploy when the fewest calls are in flight.
- Hold headroom for the roll. The fleet is one node short throughout, so the remaining nodes must carry the trough without crossing the per-node cap from the capacity model.
- Drain one node. Signal it, stop new work, wait out its longest call.
- Confirm zero. The node reports zero active calls before anything terminates it.
- Replace and verify. The new node registers and takes calls before the next one drains.
- Know the duration. Measure how long a drain actually takes on your traffic, and budget the deploy as that number times the node count.
Deploy Exercise
Test the drain path under load before you need it: deploy during a load test and count dropped calls, and the number you get is either zero or a defect of the kind covered in silent failures. A controlled test limits the customer impact. Finding it during a campaign costs the campaign.
The stakes are bigger than one deploy. Voice fleets that cannot deploy safely stop deploying, and then the real cost arrives: the fix that waits three weeks for a maintenance window. Drain discipline is what lets infrastructure that holds phone calls keep the deploy cadence of a web service.
Next: the last number that decides whether you keep running any of this yourself, self-hosted LiveKit cost per minute.
Common questions
What happens when a LiveKit agent worker receives SIGTERM?
In production mode it enters a draining state: it stops accepting new jobs and waits for active jobs to finish, up to drain_timeout, which defaults to one hour (seconds in Python, milliseconds in Node.js). Then it closes connections and exits.
How does a self-hosted LiveKit server drain?
On SIGTERM, SIGINT or SIGQUIT with participants connected, it enters draining mode: active rooms keep running and can still admit participants, joins to new rooms are rejected, and the server shuts down once every participant has left.
Why do calls still drop even with drain_timeout configured?
Usually a supervisor kills the process first. A systemd TimeoutStopSec, a Kubernetes terminationGracePeriodSeconds or an Auto Scaling lifecycle hook timeout shorter than the drain ends in SIGKILL or termination, whatever the framework was waiting for.
We do this work for teams shipping voice agents: Self-Hosted Voice Agent .
Edit this lesson on GitHub ↗Discussion
Questions, corrections and war stories welcome. Sign in with GitHub to comment.