Question 11 of 11 · Set 1 · 12 marks
Open in ChatGPTDiscuss the design principles of scalable web-based systems. Explain load balancing, server replication, caching, and session management in distributed web environments.
Introduction
A scalable web system serves more users by adding machines rather than by buying a bigger one. That requires the system to hold as little per-server state as possible, to spread requests, and to keep the shared data consistent.
Design principles
- Statelessness. Every request carries what it needs, so any server can answer it. Nothing in memory ties a user to a machine.
- Partition by function. Separate the web tier, the application tier and the data tier, so each scales on its own bottleneck.
- Cache at every layer. Browser, CDN, reverse proxy, application and database caches each remove work from the layer behind them.
- Asynchrony. Long work goes to a queue and is done by a worker, so the request path stays short.
- Graceful degradation. When a dependency is slow, serve a cached or reduced response rather than blocking.
Load balancing
- DNS round robin: cheap, no health awareness, used for geographic distribution.
- Layer 4 (TCP): forwards connections without inspecting them: fast, protocol-agnostic.
- Layer 7 (HTTP): routes by path, header or cookie, and can retry an idempotent request on another backend.
- Health checks remove a failed backend from the pool; session affinity is avoided where possible, because it pins load and loses state on failure.
Server replication
- Replicas of the web tier behind the balancer, each stateless, so N replicas give N times the throughput and tolerate N − 1 failures.
- Data replication differs: a primary takes writes and replicas serve reads, which scales reads but not writes, and introduces replication lag that the application must tolerate.
- Autoscaling adds replicas on a metric; the danger is a herd: scaling up on a metric that scales up further.
Caching
| Layer | Stores | Invalidated by |
|---|---|---|
| Browser | Response per URL | Cache-Control, expiry |
| CDN | Objects at the edge | TTL, purge |
| Reverse proxy | Rendered pages, API responses | TTL, purge |
| Application (Redis) | Computed results | Write-through or TTL |
| Database | Buffer pool, query cache | Write |
The hard problem is invalidation, so most systems choose a TTL and accept brief staleness.
Session management
- A shared store (Redis) with a session id in a cookie, so any server can read the session.
- A signed token (JWT) for fully stateless auth, with a short expiry and a revocation list for logout.
- Sticky sessions only as a last resort, because they defeat the point of replication.
What breaks first
- The database: solved by read replicas, caching, and sharding.
- Chatty calls between services: solved by batching and coarser APIs.
- Shared mutable state: solved by partitioning the keyspace so each key has one owner.
Conclusion
Scalability is the absence of per-server state plus a cache in front of every expensive thing. Load balancing spreads work, replication adds capacity, caching removes work, and session management is the one place where state is allowed: as long as it lives somewhere shared.