open atlas
↑ Back to track
System Design Case Studies SDC · 04 · 07

Location & realtime: reading the code and config

Read real geo-query code, a pub/sub routing config, a routing-engine setup, and Redis sorted-set commands, then do the reasoning: spot the single-cell bug, the broadcast blow-up, the re-contraction trap, and the O(n) rank query.

SDC Senior ◷ 14 min
Level
FoundationsJuniorMiddleSenior

The bugs in these systems live in the code, not the prose: a query that reads one cell, a publish that goes to everyone, a routing setup that re-preprocesses on every traffic tick, a rank query that scans. Read each snippet, run it in your head, and pick the answer a senior engineer would commit to.

Practise the design-review loop: locate the access pattern in the code, match it to the right structure, and choose the change the architecture actually supports.

Snippet 1 — the proximity query

def nearby(lat, lng, radius_m):
    cell = h3.geo_to_h3(lat, lng, res=9)         # the user's own cell
    candidates = store.get_by_cell(cell)         # only this one cell
    return [p for p in candidates
            if haversine(lat, lng, p.lat, p.lng) <= radius_m]
Quiz

What's the bug in nearby(), and how do you fix it?

Snippet 2 — the location publisher

def on_ping(user_id, lat, lng):
    store.set(user_id, (lat, lng), ttl=30)
    for conn in all_connections:          # every connected client
        conn.send({"user": user_id, "lat": lat, "lng": lng})
Quiz

At 2M pings/second this loop melts the cluster. What's the structural fix?

Snippet 3 — the routing engine setup

def update_traffic(edge_id, new_travel_time):
    graph.set_weight(edge_id, new_travel_time)
    graph.build_contraction_hierarchy()   # runs on every traffic update
    return graph
# called ~thousands of times per second from the probe stream
Quiz

Why is this catastrophic, and what's the right architecture?

Snippet 4 — the rank query

-- a player's rank on the leaderboard
SELECT COUNT(*) + 1 AS rank
FROM scores
WHERE score > (SELECT score FROM scores WHERE player_id = :id);
-- called per player, millions polling their own rank
Quiz

Why does this query scale badly, and what replaces it?

Recall before you leave
  1. 01
    Two code smells that signal a proximity or fan-out bug, and their fixes?
  2. 02
    Why is re-running contraction on every traffic update wrong, and why is a SQL rank query slow — what replaces each?
Recap

Every failure in this unit is visible in the code and structural, not a tuning detail. A proximity query that reads one cell misses the place across the boundary — query the neighbor ring and filter by exact distance. A location handler that sends each ping to all connections is a quadratic broadcast — publish by region cell and filter at the subscriber edge. A routing engine that rebuilds the contraction hierarchy on every traffic tick is impossible at continental scale — separate structure from weights so traffic overlays live. And a SQL rank query (COUNT(*) WHERE score > mine) is an O(n) scan per poller — replace it with a sorted set whose ZREVRANK is O(log n). The senior habit is the same each time: read the access pattern off the code, then choose the data structure or routing that fits it — never the version that hides an O(n) or a re-computation on the hot path.

Something unclear?

Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.