Designing Stateless Applications with Externalised State
This lesson explains why cloud-native compute should be stateless, where each kind of state belongs, and how to make an existing application stateless.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why statelessness enables scaling, Spot capacity and self-healing.
- Choose a managed store for sessions, files, workflow progress and configuration.
- Describe the steps to remove local state from an existing application.
Why statelessness matters
A stateless tier can send any request to any instance, task or function. That enables horizontal scaling, Spot capacity, automatic replacement of unhealthy instances, zero-downtime deployments and multi-Region designs. State kept locally, such as in-memory sessions, shopping carts, files on local disk or workflow progress held in a process, is lost when the compute goes away and forces sticky sessions, which unbalance load and still lose data on failure.
Where state goes
The following table maps common kinds of state to the services that hold them.
| State | Where to keep it | Why |
|---|---|---|
| Web sessions | ElastiCache (Valkey or Redis OSS) or DynamoDB with TTL | Fast shared access and automatic expiry |
| Carts and user profiles | DynamoDB | Durable, scalable key-value access |
| Uploads and generated files | Amazon S3 (or EFS for shared POSIX access) | Durable shared storage |
| Workflow progress | Step Functions or Lambda durable functions | Managed checkpoints and retries |
| Configuration and feature flags | AppConfig or Parameter Store | Change without redeploying |
Local disks and memory may still hold caches that can be rebuilt. Conditional writes and optimistic locking in DynamoDB prevent lost updates when several instances change the same item.
Where state belongs
Making an existing application stateless
-
Find the state
Inventory sessions, files, caches and in-process jobs that live on instances.
-
Externalise it
Move each kind of state to the matching managed store.
-
Remove affinity
Disable sticky sessions once sessions are external.
-
Make compute disposable
Use Auto Scaling or ECS services with health checks, then add Spot and scale-in.
A ticketing site
A ticketing site runs on EC2 with sticky sessions; seats being purchased are held in instance memory, and generated PDF tickets are written to local disk. Scale-in events lose purchases.
The architect moves sessions and seat holds to DynamoDB with TTL (so abandoned holds expire), writes tickets to S3 and returns presigned URLs, and disables stickiness on the target group.
With no local state, the Auto Scaling group can use Spot Instances for most capacity and scale in aggressively after sales, and instances that fail health checks are replaced without losing anyone's purchase.
What must be done before sticky sessions can be removed from a load balancer?
Show answer
Session state must be moved out of the instances into a shared store, such as ElastiCache or DynamoDB, so that any instance can serve any request.
Exam Tips
Remember for the exam
- Users lose sessions when instances are replaced: move sessions to ElastiCache or DynamoDB and remove stickiness.
- Files written by one instance needed by another: S3 or EFS, not local disks.
- Stateless tiers are the precondition for Spot, scale-in and self-healing.
- Expire temporary state automatically: DynamoDB TTL or cache TTLs.