Designing systems that handle millions of concurrent location updates and generate personalized feeds is one of the most challenging aspects of system design interviews. Whether you are modeling Uber's driver-rider matching or LinkedIn's activity feed, the core challenges involve geospatial indexing, real-time state management, and efficient data retrieval. In this post, we break down the architecture, algorithms, and trade-offs required to solve these problems effectively.
Geospatial Matching for Ride-Sharing
The core problem in ride-sharing is finding the nearest available driver to a rider within a specific radius. While a brute-force approach comparing every driver to every rider is $O(N^2)$, it is infeasible for global scale. Instead, we utilize geospatial data structures like GeoHash, H3, or R-Trees.
GeoHash is a popular choice because it converts lat/long coordinates into a string, effectively mapping 2D coordinates into a 1D sorted index. This allows us to use standard database indexes (like Redis Sorted Sets or Elasticsearch) to perform range queries efficiently.
function findNearestDrivers(riderLat, riderLon, radiusKm, driverCache) {
// 1. Convert rider location to GeoHash
const centerHash = geoHash.encode(riderLat, riderLon, 8);
// 2. Get neighboring cells to ensure boundary coverage
const neighbors = geoHash.neighbors(centerHash);
// 3. Query drivers in these cells
let candidates = [];
for (const hash of neighbors) {
// Retrieve drivers stored in this geohash bucket
const driversInCell = driverCache.getDriversByHash(hash);
candidates.push(...driversInCell);
}
// 4. Filter by exact distance and sort
return candidates
.map(driver => ({
driver,
distance: haversine(riderLat, riderLon, driver.lat, driver.lon)
}))
.filter(item => item.distance <= radiusKm)
.sort((a, b) => a.distance - b.distance)
.map(item => item.driver);
}
Social Feed Generation Strategies
Social media feeds face the opposite problem: high write frequency but specific read patterns. Two primary architectures exist: Fan-out on Write and Fan-out on Read.
Fan-out on Write: When a user posts, the system pushes the post ID to the notification/inbox tables of all their followers. This makes reads very fast ($O(1)$ lookup) but writes expensive. If a celebrity with 10 million followers posts, the write operation must update 10 million inbox rows. This is suitable for normal users but problematic for celebrities.
Fan-out on Read: The inbox is not pre-computed. When a user opens their feed, the system queries the databases of all their friends to fetch recent posts, merges them, sorts by timestamp, and returns the top N. Writes are cheap, but reads are complex and latency increases as the friend graph grows. This is better for "power users" or celebrities.
In practice, hybrid models are used. Normal users use Fan-out on Write. Celebrities use Fan-out on Read. The feed service merges these two sources dynamically.
Optimization and Consistency Trade-offs
For ride-sharing, consistency is less critical than availability and freshness. Stale driver locations (up to 1-2 seconds old) are acceptable. Use in-memory stores like Redis or Memcached for driver locations, flushing to persistent databases asynchronously. For social feeds, idempotency is key. Ensure that duplicate posts are not injected into the feed multiple times during retries. Use unique post IDs as keys in your distributed cache.
Additionally, consider pagination for both systems. For ride-sharing, limit the search radius. For social feeds, use cursor-based pagination (storing the last timestamp seen) rather than offset-based, as it performs better on large datasets with frequent inserts.
Conclusion
Mastering system design for geospatial and social graphs requires balancing data structure choices with operational scale. GeoHashing transforms complex spatial queries into simple range scans, while hybrid fan-out strategies for social feeds ensure consistent user experience across diverse follower counts. In interviews, clearly articulate your assumptions, propose a scalable data model, and discuss the trade-offs of your chosen algorithms. Focus on how your design handles hotspots, failure modes, and read/write ratios to demonstrate senior-level thinking.