What makes a Python service horizontally scalable
standardadvancedA Python service becomes horizontally scalable by removing everything that pins a request to one specific process: no in-memory session state (use an external store), no module-level mutable globals holding request-specific data, and no assumption that "the next request" lands on the same worker as the last one — every worker process (across every machine) must be able to serve any request identically.
Think of it as
A stateful worker is a specific employee who remembers your conversation — if they go home, the next employee has no idea who you are. A horizontally scalable worker is any interchangeable employee at any counter, because everything they need to help you (your session, your cart) is written down in a shared filing system (Redis, the database) any counter can read. Adding more counters (worker processes, more machines) only helps once no counter is special.
What we're doing: Contrast a stateful handler that only works if the same worker serves every request from a user, against a stateless handler backed by an external store that any worker can serve identically.
- 1
- _local_cart_cache lives in one worker process's memory — a second worker process (a second machine, or even a second process on the same machine) has an entirely separate, empty dict.
- 15
- shared_store models an external store like Redis — every worker process reads and writes the SAME underlying data, which is what makes it safe for any worker to handle any request.
worker A view: ['widget']
worker B view (SAME store): ['widget', 'gadget']Why this works: Both calls go through add_to_cart_scalable with the same shared_store, modeling two different worker processes (or two different machines) both reaching the same external state — worker B's call correctly sees worker A's earlier item because the cart lives in a store both can reach, not in either worker's own memory. _local_cart_cache would have given worker B an empty list instead, silently losing the first item the moment a second worker (or a restart) was involved.
Assuming "it works on my machine with one worker" proves a service is horizontally scalable
Wrong
Better
What you see: The bug is invisible in local development (one worker, every request naturally lands on the same process) and only appears in production once real concurrent traffic gets load-balanced across multiple workers or machines — appearing as intermittent, hard-to-reproduce "missing data" reports.
Why: Statelessness is not observable from a single-worker test — it only fails once a second worker (or a second machine) is actually involved, which is exactly the condition horizontal scaling introduces. Testing locally with more than one worker process is what surfaces this class of bug before production traffic does.
Remember: Externalize every piece of state a worker would otherwise hold in memory (session, cart, job context) into a shared store every worker can reach — statelessness is what makes adding more workers or machines actually increase capacity instead of causing random, worker-dependent bugs.
See also: statelessness as a precondition · moving state out · worker and thread models · avoiding hidden global dependencies · queue concepts

