Bird
Raised Fist0
HLDsystem_design~7 mins

Sticky sessions in HLD - System Design Guide

Choose your learning style10 modes available
Problem Statement
When a load balancer distributes user requests randomly across multiple servers, the user's session data may be lost or inconsistent because subsequent requests go to different servers without shared session state. This causes failures in applications that rely on session persistence, such as shopping carts or login states.
Solution
Sticky sessions solve this by making the load balancer route all requests from the same user to the same backend server. This is done by tracking a session identifier, often stored in a cookie or IP address, so the server handling the first request continues to handle all subsequent requests from that user, preserving session state locally.
Architecture
User Client ├──────▶
Load Balancer
→Load Balancer
↓
Server A

This diagram shows a load balancer routing user requests to backend servers. Sticky sessions ensure requests from the same user always go to the same server, preserving session state.

Trade-offs
✓ Pros
→
Preserves user session state without requiring shared session storage.
→
Simplifies application design by keeping session data local to one server.
→
Reduces latency by avoiding external session stores.
✗ Cons
→
Can cause uneven load distribution if some servers get more sticky sessions.
→
Limits fault tolerance; if a server goes down, users lose their session.
→
Scaling is harder because sessions are tied to specific servers.
Use sticky sessions when session state is stored locally and shared session storage is not available or too costly, especially for moderate traffic systems under 10,000 concurrent users.
Avoid sticky sessions in very large scale systems with thousands of servers or when high availability and fault tolerance require session data to be shared or replicated.
Real World Examples
Amazon
Uses sticky sessions in some legacy parts of their e-commerce platform to maintain shopping cart state without a centralized session store.
Netflix
Avoids sticky sessions by using stateless services and external session stores, but used sticky sessions early on to simplify session management.
Uber
Uses sticky sessions in their dispatch system to keep user requests routed to the same backend for consistent state during a ride.
Alternatives
Distributed session store
Stores session data in a centralized or distributed cache accessible by all servers, allowing any server to handle any request.
Use when: Choose when you need high availability and fault tolerance with many servers and cannot afford session loss.
Token-based stateless sessions
Encodes session state in tokens (like JWT) sent with each request, eliminating server-side session storage.
Use when: Choose when you want to scale horizontally without session affinity and can tolerate stateless session design.
Summary
Sticky sessions route all requests from a user to the same server to preserve session state.
They simplify session management but can cause uneven load and reduce fault tolerance.
Alternatives like distributed session stores or stateless tokens offer better scalability and availability.

Practice

(1/5)
1. What is the main purpose of sticky sessions in system design?
easy
A. To keep a user connected to the same server during their session
B. To distribute user requests evenly across all servers
C. To store session data in a shared database
D. To encrypt user data during transmission

Solution

  1. Step 1: Understand sticky sessions concept

    Sticky sessions ensure that a user's requests go to the same server to maintain session state.
  2. Step 2: Compare with other options

    Options B and C describe load balancing and shared storage, not sticky sessions. To encrypt user data during transmission is about security, unrelated here.
  3. Final Answer:

    To keep a user connected to the same server during their session -> Option A
  4. Quick Check:

    Sticky sessions = same server connection [OK]
Hint: Sticky sessions keep user-server link constant [OK]
Common Mistakes:
  • Confusing sticky sessions with load balancing
  • Thinking session data is always shared
  • Assuming sticky sessions encrypt data
2. Which method is commonly used to implement sticky sessions in a load balancer?
easy
A. Randomly assigning servers per request
B. Encrypting all session data
C. Caching data in a CDN
D. Using IP hashing to route requests

Solution

  1. Step 1: Identify sticky session implementation methods

    IP hashing is a common way to ensure requests from the same user go to the same server.
  2. Step 2: Eliminate unrelated options

    Encrypting data and caching in CDN do not control session routing. Random assignment breaks stickiness.
  3. Final Answer:

    Using IP hashing to route requests -> Option D
  4. Quick Check:

    Sticky sessions use IP hashing [OK]
Hint: IP hashing helps keep sessions sticky [OK]
Common Mistakes:
  • Confusing encryption with session routing
  • Thinking CDN caching manages sessions
  • Assuming random server assignment keeps sessions sticky
3. Consider a load balancer using cookie-based sticky sessions. If a user's first request is routed to Server A, where will the second request go?
medium
A. Server B
B. Server A
C. Random server
D. Load balancer itself

Solution

  1. Step 1: Understand cookie-based sticky sessions

    The load balancer sets a cookie to identify the server handling the user's session.
  2. Step 2: Follow the cookie routing logic

    Subsequent requests with the cookie go to the same server, Server A in this case.
  3. Final Answer:

    Server A -> Option B
  4. Quick Check:

    Cookie sticky sessions route to same server [OK]
Hint: Cookie keeps user on original server [OK]
Common Mistakes:
  • Assuming requests go to different servers
  • Thinking load balancer handles requests directly
  • Confusing random routing with sticky sessions
4. A system uses IP hashing for sticky sessions but users report inconsistent session behavior. What is the most likely cause?
medium
A. Users have dynamic IP addresses changing frequently
B. Session data is stored in a shared database
C. Load balancer is not configured for SSL
D. Cookies are disabled in user browsers

Solution

  1. Step 1: Analyze IP hashing limitations

    IP hashing depends on stable IP addresses to route requests consistently.
  2. Step 2: Identify cause of inconsistency

    Dynamic IPs cause requests to appear from different IPs, breaking stickiness.
  3. Final Answer:

    Users have dynamic IP addresses changing frequently -> Option A
  4. Quick Check:

    Dynamic IPs break IP hashing sticky sessions [OK]
Hint: Dynamic IPs break IP hashing stickiness [OK]
Common Mistakes:
  • Blaming shared database for session routing
  • Confusing SSL with session stickiness
  • Assuming cookies affect IP hashing
5. You design a web app with multiple servers storing session data locally. To ensure users always reach the same server, which approach is best and why?
hard
A. Use IP hashing but allow fallback to any server if IP changes
B. Use a shared database for session storage and random load balancing
C. Use sticky sessions with cookie-based routing to bind users to servers
D. Disable sticky sessions and rely on client-side session storage

Solution

  1. Step 1: Understand session storage setup

    Sessions stored locally require users to connect to the same server to access their data.
  2. Step 2: Evaluate approaches for consistent routing

    Cookie-based sticky sessions reliably bind users to servers, ensuring session continuity.
  3. Step 3: Compare other options

    Shared DB removes need for stickiness but contradicts local storage. IP hashing can fail with IP changes. Client-side storage shifts session responsibility.
  4. Final Answer:

    Use sticky sessions with cookie-based routing to bind users to servers -> Option C
  5. Quick Check:

    Local sessions need sticky cookie routing [OK]
Hint: Local sessions need cookie sticky routing [OK]
Common Mistakes:
  • Choosing shared DB when sessions are local
  • Relying on IP hashing despite IP changes
  • Ignoring server session storage needs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard