SCL-37Recommended
Keep no state inside the app process
Keep sessions, uploads and pending jobs outside the app process and its local disk. Any copy of the app can then stop or start without losing them.
How to do it
Store sessions in the database or a shared store such as Redis or Valkey. Store uploads in object storage, as Upload straight to storage with a signed address shows. Put background work in a queue that survives a restart. Treat local disk as scratch space that the next deploy may wipe. Then a one-step deploy or a second server loses nothing. Each copy also shuts down cleanly, so no request in flight is lost.
Examples
JavaScript example
// Express: sessions live in Redis, so any copy of the app can serve any request
import session from 'express-session';
import { RedisStore } from 'connect-redis';
app.use(session({
store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}));
Python example
# Django settings: sessions in the database and uploads in object storage, never on the app's disk
SESSION_ENGINE = "django.contrib.sessions.backends.db"
STORAGES = {
"default": {"BACKEND": "storages.backends.s3.S3Storage"},
"staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"},
}
How to check
Sign in, upload a file and start a background job on a test server. Deploy a fresh copy of the app on a new machine and stop the old one. The session, the file and the job are all still there.
Sources
Related rules
- Run two copies of the app in staging in Scaling
- Pinning each visitor to one server in Scaling
- Upload straight to storage with a signed address in API design
- Deploy in one step and roll back by itself in Engineering
- Shut down cleanly in Operations
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/stateless-app-servers/stateless-processes">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.