so support multi-device aggregation
Reads: batch presence lookups when apps open — ~100M DAU × ~5 opens/day, each fetching ~200 friends ≈ ~500K–1M lookup-equivalents/se
Storage
REST for snapshot queries
WebSocket for real-time plaan
/heartbeat API (every 30sec.)
Clients publish their statuses via WatchingService (deviceID, status, time).
WatchingService publishes status changes to Hub (PresenceEvents WebSocket)
AvailabilityService monitor is pulling data from PresenctEvents to check who is online and what is their status
. In a real system, a user's Wi-Fi blips for 3 seconds and their presence would show offline→online→offline, spamming every watcher with events. You need a TTL/grace buffer on the server: don't declare offline until the heartbeat misses for ~60s. This is arguably the core trick of presence systems.
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
Presence Service — the liveness gate (highest value, pick this one)
device:{userId}:{deviceId} with ~60s expiry, refreshed on each heartbeat). Offline fires only when the TTL expires — no offline event, no push.device_count per user (INCR on connect, DECR on TTL expire/disconnect). User-level offline only when count hits 0 — so a phone dying doesn't flip the laptop user offline.2. Fan-out Hub — delivery to watchers
presence:{userId}) that watchers subscribe to; updates routed only to subscribers.3. WebSocket Gateway — connection lifecycle, heartbeats, and disconnects; scale-out via connection hash.
Plus a minor line: transitions append to Kafka for analytics — off the hot path.