PSGI application and sidecar proxy: which layer should own connection draining for zero-downtime deploys?
0 reputation · 16 Sept 2022, 13:38 UTC
0 reputation · 16 Sept 2022, 13:38 UTC
A PSGI/Plack application runs behind Starman, Twiggy, or Corona — each implements graceful shutdown differently (SIGQUIT, SIGTERM with timeout, Coro semantics). None drains WebSocket or long-poll connections by default. The team wants zero-downtime deployments without rewriting every server adapter.
Adding a sidecar proxy (Envoy or HAProxy) centralizes drain logic but introduces new infrastructure, health-check complexity, and TLS termination decisions. Extending each Plack server with a custom /drain endpoint keeps logic in-process but requires maintaining server-specific signal handling and coordination across workers. The PSGI spec defines no shutdown contract, so neither approach is portable.
33775 reputation · 17 Sept 2022, 00:50 UTC
Proxy owns the drain sequence. The Plack server should not. Put drain control in Envoy or HAProxy and keep the PSGI app server version-agnostic. A single in-process drain protocol cannot be made portable across Starman, Twiggy and Corona without server-specific forks.
A sidecar proxy can stop sending new requests to the old instance while letting in-flight HTTP requests complete, independent of the PSGI server implementation. Moving drain logic into the proxy centralizes it, avoids rewriting every server adapter, and keeps the application portable.
Expose a simple health-check endpoint, e.g. /health, that returns HTTP 200 only after the worker pool has bound its port and is ready to serve PSGI requests. The proxy adds new instances to rotation only after health checks pass and drains old instances before the old process is stopped.
/health.One diagnostic detail that changes the recommendation: do you have persistent WebSocket or long-poll connections in production? If yes, the proxy drain timeout must exceed the maximum expected connection lifetime, otherwise those connections will be dropped.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.