Skip to Content

Stateful web app: MyClothes.com

Where to keep session state in a scalable shopping site (sticky sessions, cookies, ElastiCache or DynamoDB), scaling reads with read replicas and caching, Multi-AZ, and security groups between tiers.

The app

MyClothes.com is an online shop. Users browse, put clothes in a shopping cart and check out, and hundreds of them shop at the same time. The site must scale horizontally and keep the web tier as stateless as possible, but the cart and user details are state: they must not disappear when a request lands on a different server.

It starts from the stateless architecture: Route 53, an ALB across AZs and an Auto Scaling group.

Where to keep the shopping cart

  1. Sticky sessions. Turn on ELB stickiness: each user keeps going to the same instance, which holds their cart in memory. Simple, but if that instance is terminated, the cart is lost, and load can become uneven.
  2. Client-side cookies. The browser sends the cart in a cookie with each request, so any instance can serve it: the web tier is stateless. But requests get heavier, cookies must stay under 4 KB, and users can tamper with them, so the server must validate them.
  3. Server-side sessions. The cookie holds only a session ID. The instances store the session data in ElastiCache (sub-millisecond reads), or in DynamoDB. Any instance can read any session; this is the usual answer.

Storing and reading data

  • User data (addresses, orders) goes in Amazon RDS: one primary takes the writes.
  • Scaling reads: most traffic is reading the catalog. Add up to 15 RDS read replicas and send reads there.
  • Or cache the reads with lazy loading in ElastiCache: the app checks the cache first and queries RDS only on a miss, then stores the result. Less load on RDS, but cache invalidation needs care.
  • Surviving disasters: the ALB is already Multi-AZ; turn on RDS Multi-AZ (standby in another AZ) and ElastiCache Multi-AZ (replicas with automatic failover).

The final MyClothes.com: users reach an Application Load Balancer across AZs, which sends requests to EC2 instances in an Auto Scaling group in three AZs; the instances keep sessions and cached reads in ElastiCache, write to an RDS primary with Multi-AZ, and read from RDS read replicas fed by replication The final MyClothes.com: users reach an Application Load Balancer across AZs, which sends requests to EC2 instances in an Auto Scaling group in three AZs; the instances keep sessions and cached reads in ElastiCache, write to an RDS primary with Multi-AZ, and read from RDS read replicas fed by replication

State moved out of the web tier.

Security groups between tiers

Security groups for MyClothes.com: the ALB security group allows HTTPS 443 from anywhere; the EC2 security group allows traffic only from the ALB security group; the ElastiCache and RDS security groups allow traffic only from the EC2 security group Security groups for MyClothes.com: the ALB security group allows HTTPS 443 from anywhere; the EC2 security group allows traffic only from the ALB security group; the ElastiCache and RDS security groups allow traffic only from the EC2 security group

Each tier only talks to the one in front of it.
  • ALB: HTTP/HTTPS open to 0.0.0.0/0.
  • EC2: inbound only from the ALB's security group.
  • ElastiCache and RDS: inbound only from the EC2 security group, on their ports (6379, 3306…).

Referencing security groups instead of IP ranges keeps working as instances come and go.

What this lecture covered

  • 3-tier web apps: web (ALB), application (EC2 in an ASG) and data (ElastiCache, RDS).
  • ELB sticky sessions, and cookies to make the web tier stateless.
  • ElastiCache for sessions (alternative: DynamoDB) and for caching RDS data, with Multi-AZ.
  • RDS for user data, read replicas for scaling reads, Multi-AZ for disaster recovery.
  • Tight security with security groups referencing each other.

Real-world examples

  • A ticketing site stores session IDs in cookies and sessions in ElastiCache with a 30-minute TTL, so a sale-day scale-out to 60 instances doesn't log anyone out.

Exam tips

  • "Users lose their session (cart, login) when instances scale in" → store sessions in ElastiCache or DynamoDB, not on the instance.
  • "Stateless web tier" → session data outside the instances (cache, database, or signed cookies).
  • "Read-heavy RDS" → read replicas, or ElastiCache in front.
  • "Database tier reachable only from the app tier" → database security group allows the app tier's security group.

Common traps

  • "Sticky sessions make the app highly available." The session still dies with its instance. Use them only when the session can't be externalized.
  • "Read replicas give high availability." That's Multi-AZ. Replicas scale reads (though one can be promoted).
  • "Store the whole cart in a cookie, it's stateless." It works for small data, but cookies are limited to about 4 KB and must be validated.

Check yourself

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt