Lesson 8 of 10 Serverless and API Architecture

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.

StateWhere to keep itWhy
Web sessionsElastiCache (Valkey or Redis OSS) or DynamoDB with TTLFast shared access and automatic expiry
Carts and user profilesDynamoDBDurable, scalable key-value access
Uploads and generated filesAmazon S3 (or EFS for shared POSIX access)Durable shared storage
Workflow progressStep Functions or Lambda durable functionsManaged checkpoints and retries
Configuration and feature flagsAppConfig or Parameter StoreChange 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.

Match the pairs

Where state belongs

Exam skills 1.1.2

Match each kind of state with a suitable external store.

Log in to try this activity Interactive activities are free with an account.

Making an existing application stateless

  1. Find the state

    Inventory sessions, files, caches and in-process jobs that live on instances.

  2. Externalise it

    Move each kind of state to the matching managed store.

  3. Remove affinity

    Disable sticky sessions once sessions are external.

  4. Make compute disposable

    Use Auto Scaling or ECS services with health checks, then add Spot and scale-in.

Worked example

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.

Check yourself

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.