State retention limits for idle Reflex websocket sessions on long-lived backends
0 reputation · 29 Sept 2024, 16:41 UTC
0 reputation · 29 Sept 2024, 16:41 UTC
Reflex keeps server-side state in the backend process memory by default, and the framework does not offer scale-to-zero semantics because that state is lost when the process terminates. For a low-traffic workload, a small self-hosted instance can be cheaper than managed hosting, but the practical limits of the default state manager over weeks or months of sparse traffic are unclear. In particular, idle websocket connections and their associated state objects may linger unless explicit cleanup or expiration is configured, which could lead to gradual memory accumulation on a constrained VM.
Redis-backed state managers externalize session state and make restarts survivable, but they introduce another service to run and tune, including key expiration settings. The trade-off between a single in-memory instance and a Redis-backed deployment is not obvious when traffic is minimal.
Does the default Reflex state manager garbage-collect state for websocket sessions that have been idle for extended periods, or does that state remain resident until the backend restarts? Is there a documented expiration or cleanup mechanism for low-traffic single-instance deployments that avoids adding Redis? What operational limits should be assumed for memory growth when a Reflex app serves only occasional requests over a long-lived process?
35275 reputation · 30 Sept 2024, 03:26 UTC
The default Reflex state manager retains server-side state, including that associated with idle WebSocket sessions, directly within the backend process memory. There is no documented, automatic, time-bound garbage collection mechanism for this idle session state in a single, long-lived instance without explicit configuration.
The operational memory growth limit cannot be definitively stated without knowing the size and complexity of the state objects being retained. To refine the recommendation, please confirm if your application utilizes any custom state serialization or object types that might have known, bounded lifetimes.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.