Design: Online Presence System
Design covers real-time presence tracking, status updates, and querying presence. Does not cover user authentication system, messaging, or notification delivery.
Functional Requirements
Non-Functional Requirements
Jump into concepts and practice - no test required
Client Devices (Web/Mobile)
|
| WebSocket / API Requests
v
+-------------------+
| API Gateway |
+-------------------+
|
| REST API / WS
v
+-------------------+ +-------------------+
| Presence Service |<----->| Message Broker |
+-------------------+ +-------------------+
|
| Cache (Redis) for fast presence data
v
+-------------------+
| Persistent Store |
| (NoSQL DB) |
+-------------------+connect, heartbeat, and disconnect to track user activity.message_send relates to sending chat messages, not presence tracking.message_send -> Option Auser_last_seen = 0
current_time = 10
heartbeat_interval = 5
if current_time - user_last_seen <= heartbeat_interval:
status = 'online'
else:
status = 'offline'def update_status(last_seen, current_time, heartbeat=10):
if current_time - last_seen > heartbeat:
return 'online'
else:
return 'offline'