📝 Problem Description
Design a real-time feature that shows "X users are viewing this page right now" commonly seen on e-commerce and booking sites. This creates urgency and social proof. Handle accuracy, scale, and edge cases.
👤 Use Cases
1.
User wants to opens product page so that sees count of other viewers
2.
User wants to leaves the page so that count decreases for others
3.
System wants to user idle for 5 minutes so that removes from viewer count
✅ Functional Requirements
- •Show real-time viewer count per page
- •Update count when users join/leave
- •Handle page refreshes gracefully
- •Idle timeout (5 min of inactivity)
⚡ Non-Functional Requirements
- •Count updates within 2 seconds
- •Support 100K concurrent viewers per page
- •Handle 10M total concurrent users
- •Accurate within ±10%
⚠️ Constraints & Assumptions
- •Anonymous users should count
- •Must handle browser tab switches
- •Should not impact page load time
📊 Capacity Estimation
👥 Users
10M concurrent users across all pages
💾 Storage
10GB (page counts and session tracking)
⚡ QPS
Heartbeats: 100K/sec, Count reads: 50K/sec
📐 Assumptions
- • 10M concurrent users
- • 1M unique pages being viewed
- • Heartbeat every 30 seconds per user
- • Average page view: 2 minutes
💡 Key Concepts
CRITICAL
Session Heartbeats
Periodic pings from client to confirm presence. No heartbeat = user left.
HIGH
Redis Sorted Sets
Use ZADD with timestamp as score, ZRANGEBYSCORE to count active sessions.
MEDIUM
Approximate Counting
For very high traffic pages, use HyperLogLog or probabilistic counting.
💡 Interview Tips
- 💡Start with the presence detection mechanism
- 💡Discuss the heartbeat pattern
- 💡Emphasize the Redis data structures
- 💡Be prepared to discuss exact vs approximate counting
- 💡Know the tradeoffs between accuracy and scale
- 💡Understand the WebSocket/polling decision