open atlas
↑ Back to track
System Design Case Studies SDC · 05 · 09

Data & money: code and config reading

Read real code and config from the data-and-money cases, then pick the senior fix: a metric label, an idempotent charge, a lost-update wallet debit, and an atomic multi-night reservation.

SDC Senior ◷ 14 min
Level
FoundationsJuniorMiddleSenior

Money bugs live in the code, not the prose: a label on a metric, a non-atomic read-then-write, a charge with no idempotency key, a per-night decrement outside a transaction. Read each snippet, trace the failure, and pick the fix a senior engineer would commit.

Practise the loop you run in a design review on money or correctness-critical data: spot the hazard in the code, name it, and choose the change the mechanism actually requires.

Snippet 1 — the metric label

# request handler instrumentation
REQUESTS = Counter("http_requests_total", labels=["route", "status", "user_id"])

def handle(req):
    REQUESTS.labels(route=req.route, status=resp.status, user_id=req.user_id).inc()
Quiz

What's wrong with this instrumentation, and what's the fix?

Snippet 2 — the charge call

def checkout(order):
    # client retries this call on timeout
    resp = psp.charge(amount=order.total, source=order.card)
    ledger.record(order.id, resp)
    return resp
Quiz

A client retries checkout() after a timeout. What happens, and what's the fix?

Snippet 3 — the wallet debit

def debit(wallet_id, amount):
    bal = db.query("SELECT balance FROM wallets WHERE id = ?", wallet_id)
    if bal >= amount:
        db.execute("UPDATE wallets SET balance = ? WHERE id = ?", bal - amount, wallet_id)
        return "ok"
    return "insufficient"
Quiz

Two concurrent debits hit the same wallet. What's the bug, and the single-statement fix?

Snippet 4 — the multi-night reservation

def reserve(room_type, nights):  # nights = [Dec31, Jan1, Jan2]
    for night in nights:
        db.execute(
          "UPDATE inventory SET booked = booked + 1 "
          "WHERE room_type = ? AND date = ? AND booked < total",
          room_type, night)
    return "reserved"   # each night updated independently, no transaction
Quiz

The per-night UPDATE is conditional (good), but the loop has no transaction. What can go wrong, and the fix?

Recall before you leave
  1. 01
    How do you read a wallet-debit or reservation snippet for the lost-update / partial-write hazard?
  2. 02
    How do you spot the idempotency and cardinality hazards in code?
Recap

Every data-and-money bug is readable straight off the code. An unbounded metric label (user_id) forks a series per value and OOMs the TSDB — series cardinality, not request rate, is the constraint, so drop it from labels. A charge the client retries with no idempotency key double-charges when the first response is lost — pass a client-minted key with an atomic check-and-store. A wallet debit that reads the balance then writes it back has a read-write gap that two concurrent debits exploit (the lost update) — collapse it into one atomic conditional UPDATE. And a multi-night reservation that decrements each night outside a transaction can leave a partial reservation — wrap the whole range in one all-or-nothing transaction. The senior habit is the same across all four: find the hazard, name it, and make the operation bounded (cardinality) or atomic (idempotency, lost update, partial write) — the fix the mechanism requires, not the one that hides it.

Connected lessons

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.