Python FastAPI Async Variable Sharing
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Sharing variables in an async FastAPI application sounds simple until concurrency, multiple workers, and request isolation enter the picture. A plain global variable may seem to work in development but break under load or across processes. The right design depends on whether the state is request-local, process-local, or truly shared across the whole deployment.
Know What “Shared” Means in FastAPI
There are several different scopes of state:
- per-request state
- per-process in-memory state
- cross-process shared state
These are not interchangeable.
For example:
- request-local data belongs in function scope or request state
- process-local caches can live in app-level objects
- cross-instance coordination belongs in Redis, a database, or another external system
If you skip this distinction, race conditions and stale data are almost guaranteed.
Use app.state for Process-Local Shared Objects
FastAPI exposes app.state for application-level state inside one process.
This works for simple demos, but it is only process-local. If you run multiple workers, each worker gets its own counter.
Protect Mutable Shared State with asyncio.Lock
Even within one process, async endpoints can interleave access. If multiple requests mutate the same value, use a lock.
This prevents two coroutines from updating the same variable concurrently in inconsistent ways.
Use Dependency Injection for Shared Services
Instead of sharing raw variables, it is often better to share a service object that owns its own synchronization rules.
This is easier to test and extend than scattering globals across modules.
Do Not Use In-Memory Variables for Cross-Worker Coordination
If you launch FastAPI with multiple workers, each process has separate memory. That means this does not create truly shared state:
With four workers:
- each worker has its own
app.state - each worker has its own globals
- requests routed to different workers see different values
If you need one shared counter or cache across the deployment, use an external store.
Use Redis or a Database for Real Shared State
For shared mutable state across workers or instances, move the state out of process memory.
This works consistently across multiple workers and multiple application instances.
Use request.state for Per-Request Data
If data should live only during one request, attach it to request.state, not to application globals.
This avoids accidental cross-request leakage.
Common Pitfalls
One common mistake is using a plain global variable for shared mutable state and assuming async code makes it automatically safe.
Another issue is forgetting that multiple FastAPI workers do not share memory, so in-process counters or caches drift immediately.
A third mistake is storing request-specific information in app.state, which leaks data across requests.
Summary
- Decide whether your state is request-local, process-local, or deployment-wide.
- Use
app.statefor simple process-local shared objects. - Protect mutable async state with
asyncio.Lock. - Prefer service objects over raw global variables for maintainability.
- Use Redis or another external store when state must be shared across workers or instances.
Related reading
- Python file-based queue that is process-safe
- Python geventbottle. Querying an API. How to use gevent to prevent timeout locks?
- Python Kafka multiprocess vs thread
- Python requests - threads/processes vs. IO
- Python FastAPI building a single-threaded queue of jobs after API call
- Python find a duplicate in a container efficiently
- Python retrieve several URLs via select.epoll
- Python rewrite a looping numpy math function to run on GPU
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.