Zero-Downtime Python Deployments with uWSGI --touch-reload
Use uWSGI's --touch-reload with an atomic symlink swap to deploy Python WSGI code updates without dropping connections. Covers configuration, deployment procedure, health checks, and rollback.
01 Aug 2025, 19:48 UTC

The Problem: Deploying Code Without Dropping Connections
When you update a Python WSGI application behind uWSGI, a naive restart kills in-flight requests and creates a brief window where the service is unavailable. The --touch-reload option solves this by letting the master process gracefully respawn workers when a designated file's modification time changes. Workers finish their current requests before exiting, and new workers start with the updated code—all while the master keeps the listening sockets open.
Prerequisites
- A running uWSGI instance with a master process and at least one worker (
--master --workers N). - The deployment user must have write permission on the touch file (or its parent directory).
- Your application must load code at worker startup. If you rely on long-lived in-memory caches or module-level singletons that persist across requests, those will be lost on reload—plan for warm-up or externalize state.
- uWSGI 2.0+ (the behavior described here has been stable since early 2.x releases).
Configure the Touch File
Add --touch-reload to your uWSGI command line or configuration file, pointing to a path that your deployment process can write:
# uwsgi.ini
[uwsgi]
master = true
workers = 4
module = myapp.wsgi:application
socket = /run/uwsgi/app.sock
# Touch file lives on a persistent filesystem, not /tmp (which may be cleaned)
touch-reload = /var/lib/uwsgi/myapp.reload
Start uWSGI normally. The master will stat() the file periodically (default interval ~1 second) and trigger a graceful reload when the mtime changes.
Atomic Deployment Procedure
The key to zero-downtime is making the code swap and the touch atomic from uWSGI's perspective:
- Stage the new code in a separate directory (e.g.,
/opt/myapp/releases/v1.2.3/). - Switch the active symlink atomically:
Your uWSGI config should referenceln -sfn /opt/myapp/releases/v1.2.3 /opt/myapp/current/opt/myapp/currentviachdiror Python path. - Touch the reload file only after the symlink is updated:
touch /var/lib/uwsgi/myapp.reload
Because the symlink swap is atomic, no worker sees a half-written codebase. The touch triggers the master to start replacing workers one by one.
Expected Checks After Reload
Verify the reload completed cleanly:
- Worker PIDs changed: Run
ps aux | grep uwsgibefore and after; old worker PIDs should disappear, new ones appear. - uWSGI logs show the cycle: Look for lines like:
*** touching reload file /var/lib/uwsgi/myapp.reload *** [pid: 12345] worker 1 exiting (graceful) [pid: 12346] worker 2 exiting (graceful) spawned uWSGI worker 1 (pid: 12400) spawned uWSGI worker 2 (pid: 12401) - Health endpoint returns 200: Hit your
/healthzor equivalent endpoint before and after the touch. It should never return 5xx or time out during the transition. - Touch file mtime updated:
stat /var/lib/uwsgi/myapp.reloadconfirms the trigger fired.
Common Pitfalls and Mitigations
Long-Running Requests Block Reload
Workers wait for the current request to finish before exiting. A stuck request (e.g., a 30-second external API call) delays the entire reload. Add --harakiri 30 (or a value appropriate for your SLA) so the master kills workers that exceed the limit, allowing the reload to proceed.
Memory Spike During Overlap
Old and new workers coexist briefly. If your application imports heavy modules (large ML models, big dependency trees) at import time, memory usage can double momentarily. Mitigations:
- Lazy-load heavy imports inside request handlers or background threads.
- Use
--lazy-appsso each worker loads the app independently (default in recent uWSGI), but be aware this increases baseline memory. - Provision headroom: if each worker uses 200 MB, plan for 2× during reload.
Master-Level Changes Require Full Restart
--touch-reload only respawns workers. Changes to socket bindings, uWSGI binary, master-only plugins, or --master-fifo commands need a full master restart (SIGTERM then restart).
Don't Touch on Every File Change
If you use a file watcher (e.g., inotifywait) during development, avoid pointing it at the production touch file. Touch only after the full deployment artifact is in place.
Recovery Options
Roll Back to Previous Version
- Point the symlink back:
ln -sfn /opt/myapp/releases/v1.2.2 /opt/myapp/current - Touch the reload file again:
touch /var/lib/uwsgi/myapp.reload - Verify health endpoint and logs as above.
This works because the rollback is just another code swap—the same graceful worker replacement applies.
Full Master Restart (Last Resort)
If the master process itself is wedged or you changed master-level config:
# Send SIGTERM to master (graceful shutdown)
kill -TERM $(cat /run/uwsgi/master.pid)
# Or force kill if stuck
kill -KILL $(cat /run/uwsgi/master.pid)
# Then restart via your process manager (systemd, supervisord, etc.)
systemctl restart uwsgi-myapp
Verification Checklist for Production Use
| Check | Command / Method | Pass Criteria |
|---|---|---|
| Worker PID turnover | ps -o pid,ppid,cmd -C uwsgi before/after | Old PIDs gone, new PIDs present, same count |
| Zero 5xx during reload | Load test or tail access logs | No 502/503/504 spikes |
| Health endpoint version | curl -s /healthz | jq .version | Returns new version after touch |
| Log sequence | grep -E 'touching reload|worker.*exiting|spawned uWSGI worker' /var/log/uwsgi/app.log | Clean graceful cycle for each worker |
Limitations
- Does not reload the master process—socket changes, uWSGI upgrades, or plugin loads need a full restart.
- Relies on filesystem mtime granularity (typically 1 second). Rapid successive touches may be coalesced.
- Not a substitute for blue/green or canary deployments when you need traffic splitting or instant rollback without code swap.
For most Python WSGI services, --touch-reload with an atomic symlink swap gives you true zero-downtime deploys using only uWSGI's built-in machinery—no external orchestration required.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.