Module 13 Review: Real-Time Systems
1. Key Concepts Summary
In this module, we explored how to build systems that react instantly to user actions.
- Latency Matters: For “Real-Time”, we often trade Strong Consistency for Availability and Low Latency (AP systems).
- Stateful vs Stateless: WebSockets introduce stateful connections, making scaling harder (Need Sticky Sessions or Pub/Sub).
- Concurrency: Handling millions of concurrent writes (Likes, Sales) requires moving locks out of the Database and into Memory (Redis).
2. Quick Reference Cheat Sheet
| Problem | Solution | Trade-off |
|---|---|---|
| Counting at Scale | Sharded Counters (Redis) | Harder to get “Exact” count instantly. |
| Real-Time Feed | WebSockets (2-way) or SSE (1-way) | Stateful servers require intricate load balancing. |
| Leaderboard | Redis Sorted Sets (Skiplists) | RAM usage grows with user count. |
| Flash Sale | Redis Atomic Counters (Lua) | If Redis crashes without AOF, stock count is lost. |
| Thundering Herd | Jitter (Random Backoff) | Slightly slower recovery time. |
| Overselling | Lua Scripting (Atomic Check-and-Set) | Complex to debug Lua scripts. |
| Hot Partition | Virtual Buckets / Consistent Hashing | Resharding is complex. |
| C10M Problem | Kernel Tuning / Ephemeral Ports | Requires deep OS knowledge. |
| Approximate Counting | HyperLogLog (Probabilistic) | ~0.81% error rate. |
3. Flashcards
Test your knowledge by clicking the cards to flip them.
Wait! How do we scale WebSockets?
Use Redis Pub/Sub! When Server A receives a message for User B (on Server C), it publishes to a channel that Server C is listening to.
Why Redis ZSET for Leaderboards?
Because it uses a Skiplist, allowing O(log N) inserts and O(log N) rank retrieval. SQL sorting is O(N log N) or worse on inserts.
Pessimistic vs Optimistic Locking?
Pessimistic (SELECT FOR UPDATE) blocks others. Optimistic (Version check) allows others but fails on commit. Optimistic is better for low contention; Redis is better for high contention.
Long Polling vs SSE?
Long Polling is a "Hack" (Request...Wait...Response). SSE is a standard "Stream" (Request...Data...Data...). SSE is cleaner but unidirectional.
How to handle "Approximate" Counts?
Use HyperLogLog! It estimates cardinality with ~0.81% error using only ~12KB memory, saving massive amounts of RAM compared to Hash Sets.
What is a "Thundering Herd"?
When a large number of processes/users wake up simultaneously (e.g., after a crash) and attack the server. Solution: Add random Jitter to reconnect logic.
What is the "Funnel Architecture"?
A strategy to filter traffic at edge/cache layers before it hits the DB. CDN blocks bots -> Rate Limiter drops excess -> Redis Atomic handles inventory -> SQL stores order.
Why use Lua Scripts in Redis?
To ensure Atomicity. Redis runs the entire script as a single blocking operation, preventing race conditions (like checking stock and decrementing it separately).
What is the C10M Problem?
Handling 10 Million concurrent connections. Requires bypassing standard Kernel bottlenecks (using techniques like Kernel Bypass, or extreme tuning of File Descriptors and Ephemeral Ports).
What is a CRDT?
Conflict-Free Replicated Data Type. A data structure (like G-Counter) that can be updated independently on multiple replicas and always merges to a correct, consistent state.
4. Final Assessment
If you can explain how to prevent overselling 100 iPhones to 1 Million users without crashing the DB, you are ready for the interview!