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
- 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.
- 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.
- 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).
Security groups between tiers
- 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
Turn off the dependency on one instance: store session data in ElastiCache (or DynamoDB) keyed by a session ID in the user's cookie. Then stickiness isn't needed and scale-in doesn't lose carts.
Add RDS read replicas and point the read queries at them, or add ElastiCache with lazy loading so repeated reads come from memory.
AWS documentation
Stateless web app: WhatIsTheTime.com
How a stateless website grows from one EC2 instance with an Elastic IP to a load balancer and an Auto Scaling group across Availability Zones, and what each step teaches for the exam.
WordPress and fast launches: MyWordPress.com
A scalable WordPress site with Aurora and EFS for shared images, why EBS doesn't work across instances, how to launch apps quickly with golden AMIs, user data and snapshots, and the typical 3-tier web architecture.