open atlas
↑ Back to track
System Design Foundations SD · 08 · 08

Building blocks: code and config reading

Read real limiter code, an ID config, a bloom-filter sizing call, and a geohash query, then pick the answer a senior engineer would commit to: the lost-update race, the index-fragmenting key, the saturated filter, and the missed boundary neighbour.

SD Senior ◷ 14 min
Level
FoundationsJuniorMiddleSenior

Building-block bugs hide in the code, not the prose: a read-then-write that isn’t atomic, a primary key that fragments the index, a filter sized for the wrong N, a query that forgets the neighbour cells. Read each snippet, find the flaw, and pick the fix a senior engineer would commit to.

Practise the review loop you run on real building-block code: locate the operation, check whether it’s atomic / time-ordered / correctly sized / boundary-aware, and choose the change the mechanism actually requires.

Snippet 1 — the limiter increment

def allow(key, limit):
    n = redis.get(key) or 0          # read
    if int(n) < limit:
        redis.set(key, int(n) + 1)   # modify-write (separate op)
        return True
    return False
Quiz

This limiter runs on many servers against one Redis. Under concurrency, what bug does the read-then-write introduce, and what's the fix?

Snippet 2 — the primary key

-- high-volume events table, InnoDB (clustered PK)
CREATE TABLE events (
  id   UUID DEFAULT gen_random_uuid()  PRIMARY KEY,  -- random UUIDv4
  body JSONB,
  ts   TIMESTAMPTZ
);
Quiz

At millions of inserts/hour, this clustered random-UUID primary key hurts. What's the problem and the minimal change?

Snippet 3 — the bloom filter sizing

# sized once at startup
bf = BloomFilter(expected_items=1_000, fp_rate=0.01)
# ...later, in production, it ingests every event key
for key in stream:        # millions of keys over time
    bf.add(key)
Quiz

The filter was sized for 1,000 items but ingests millions. What happens, and what stays guaranteed?

Snippet 4 — the proximity query

def nearby(lat, lng):
    cell = geohash_encode(lat, lng, precision=6)   # ~1 km cell
    return db.query(
        "SELECT * FROM drivers WHERE geohash LIKE %s", cell + '%'
    )   # only the rider's own cell
Quiz

This returns no driver even when one is parked just across the street. What's wrong and what's the fix?

Recall before you leave
  1. 01
    How do you spot and fix the distributed-limiter race in code?
  2. 02
    Why does a random-UUID clustered primary key hurt insert throughput, and the minimal fix?
  3. 03
    What happens when a bloom filter is sized for too few items, and what's preserved?
Recap

Every building-block decision in this unit can be read straight off the code. A limiter that does a separate read then write across a fleet has a lost-update race — collapse it to an atomic INCR or Lua script. A random-UUID clustered primary key fragments the B-tree at high insert rates — switch to a time-ordered UUIDv7 for right-edge inserts. A bloom filter sized for too few items saturates (false-positive rate → 100%, but never false negatives) — rebuild for the true N. And a geohash proximity query that scans only the rider’s cell hits the boundary problem — include the 8 neighbour cells, then rank by exact distance. The senior habit is the same each time: find the operation, name the invariant it must hold (atomic / time-ordered / correctly sized / boundary-aware), and choose the fix that restores it.

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.