Real Time Updates hard
You need a 'Who is Online' feature for 10 million users. Storing 'Online' status in a SQL DB with `UPDATE users SET last_seen = NOW()` is too slow. What is the standard high-scale pattern?
- A Broadcast a message to everyone when someone joins.
- B Use a global hash map in the application memory.
- C Log every request to a file and parse it.
- D Use Redis Sorted Sets (ZSET) or Keys with TTL. Clients send a heartbeat every minute to refresh the TTL. If the key expires, they are considered offline.
Answer: D. Use Redis Sorted Sets (ZSET) or Keys with TTL. Clients send a heartbeat every minute to refresh the TTL. If the key expires, they are considered offline.
Redis keys with TTL (Time To Live) automatically handle the 'Offline' detection (eviction). You don't need to write 'User went offline'; the system just forgets them if they stop heartbeating.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- When a mobile app is closed/backgrounded, it cannot keep a WebSocket open. How does it receive real-time alerts (like 'New Message')?
- For a human user, what is generally considered the maximum acceptable latency for an interaction to feel 'Real-Time' (e.g., typing in a Google Doc)?
- If you want to update a news feed on a website every time a new article is published, what is the simplest (but least efficient) 'Real-Time' technique?
- In a persistent real-time connection, what is the purpose of a 'Heartbeat' or 'Ping/Pong' message?
- Why is Short Polling considered 'Chatty'?
- Server-Sent Events (SSE) are ideal for which type of real-time application?