#systemDesign #programming Find Nearby Friends System Design Features Users able to see their nearby online friends Friends within 5 miles of the user Friends become inactive after 10-20 minutes of inactivity High Level Design How does this work? Backend receives location updates from all active users For each user: Find all active friends Calculate distance between user and friend Push update to friend's device if friend is online Things to note: Web Socket connection between backend and user A Web Socket connections between backend server and each of the friend's device Let's Do Some Math 10M Active Users Each user updates location every 30 seconds ~334K updates per second On average a user has 400 friends 10% of those 400 friends are online So, 334K 400 10% = 14 million location updates per second That's a lot of connections and updates to push!! We need a more sustainable and stateful way to push these updates to clients. Redis Pub/Sub to handle updates What is this Redis Pub / Sub system? Similar to Kafka You can have multiple topics Producers can write messages to these topics Consumers can read from these topics Kafka Topics = Redis Channels Except, Redis channels are significantly cheaper to create/maintain/delete. Each user has their own dedicated channel/topic User's location updates pushed to their own channel/topic User's friends subscribe to user's channel Location updates from user pushed to subscriber Subscriber sends user's location update to friend using persistent websocket connection Should we always send updates to friends? NO! We only send updates to friend's clients when friend is nearby We need a way to quickly access friend's location data Enter: Cache! How does the cache work? Every location update for a user is stored in cache User ID --> (lat, long) TTL of 10 minutes If no location for a user in cache, we conclude that user is inactive Before sending an update to a friend If friend's location not in cache Friend is inactive So no update sent If friend's location present in cache Calculate distance between user and friend Forward update if within 5 miles Should We Store Location Data Persistently? YES Location data is useful for Product We are storing it in cache for easy access We need a more persistent storage for future uses Things to keep in mind: Users send location updates every 30 seconds or so Very high write throughput Required database: Can handle high write throughput Availability over consistency Can handle huge volume of data Can easily scale horizontally Answer: Cassandra! Schema: <user_id, lat, long, timestamp> Let's do a final recap! References
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-