LeetBuild

Reads that tell the truth

The caller's own data, counters read live, caches keyed by everything that varies, no verdict below a minimum sample, pages by cursor, and a partial result marked partial.

A read that looks right and is not

The read endpoint at the end of a course looks like the easy step: one GET that loads a record. It is where a system tells people what is true, and they act on it: an operator staffs a queue from its depth, a team ships the variant the report calls the winner. A read that is wrong in a plausible way does more damage than one that fails, because nobody checks a number that looks fine.

The smallest instance:

@app.get("/v1/jobs/{job_id}")
def read_job(job_id, cache):
    body = cache.get(f"job:{job_id}")          # whose job? which version?
    if body is None:
        body = summarise(store.load(job_id))   # any tenant's job, by id alone
        cache.set(f"job:{job_id}", body, ex=600)
    return body                                 # "done" because the segments look done

Every line answers a request, and every line can lie:

  • Whose data. The id is the only key, so a caller who guesses another tenant's id reads it.
  • How old. The body is ten minutes old, and nothing in the answer says so.
  • Which copy. The cache key leaves out the tenant and the version, so two callers who should see different answers share one.
  • What it means. summarise decides "done" from a derived view instead of the record that owns the state.

An honest read answers four questions correctly: who may see this, how fresh it is, what the number is measured over, and how complete the answer is. When the answer to the last two is "not enough" or "not all of it", the read says so in the response instead of rounding up to a confident answer.

How it shows up in the courses

The read step of nearly every debugging course starts from a handler that serves its demo perfectly. The flaws only show under conditions the demo does not have: a second tenant, a busy writer, a skewed distribution, a dependency that answered half the question.

None of these crash. Each one returns HTTP 200 with a well-formed body. The checks that catch them set up the condition the demo lacks and compare the answer with what is actually true.

Reads scoped to the caller

The first question is whose data this is. The answer comes from the credential, never from the path, the query or the body: the tenant in the authenticated header, the user in the token. Every lookup carries that scope, so a record that belongs to someone else is simply not found:

tenant = auth.tenant(request)                    # 401 when there is none
job = store.get(tenant, job_id)                  # the scope is part of the lookup
if job is None:
    return 404, {"error": "not_found"}           # missing and foreign look the same

Two details matter:

  • Foreign answers like missing. A 403 for another tenant's id tells the caller the id exists, which is enough to enumerate customers or confirm a leaked id. A 404 with the same body as an unknown id tells them nothing.
  • Scope inside the record, not only around it. A ticket's internal notes, another user's labels, a reporter's identity: some fields of a record the caller may read are still not theirs. The projection is built per role in the response builder, not filtered by the client.
which status code
HTTP status codes a service uses, grouped by family, with retry guidance2xx · success200 OK200OK201 Created201Created202 Accepted202Accepted204 No Content204No Content304 Not Modified304Not Modified4xx · the client is wrong400 Bad Request400Bad Requestno retry401 Unauthorized401Unauthorizedno retry403 Forbidden403Forbiddenno retry404 Not Found404Not Foundno retry409 Conflict409Conflictno retry422 Unprocessable422Unprocessableno retry429 Too Many Requests429Too Many RequestsRetry-After5xx · the server is wrong500 Internal Server Error500Internal Server Errorbackoff502 Bad Gateway502Bad Gatewaybackoff503 Service Unavailable503Service UnavailableRetry-After504 Gateway Timeout504Gateway Timeoutbackoff
Pick the family first (success, client error, server error), then the most specific code in it: clients branch on the family, humans and logs branch on the code. Hover, click or tab to a code.

Pop quiz

GET /v1/runs/r-91 belongs to tenant B. Tenant A asks for it. Which answer leaks the least?

Live counters and the source of truth

A read is only as true as the copy it reads. Systems keep many copies of one fact: the ledger a reservation path decrements, a nightly snapshot of it, a cached body, a list of the segments written so far. Exactly one of them decides; the rest are views that lag it.

The read wantsThe source of truthA copy that drifts
GPUs free on a nodethe ledger the placement path writesa counter cached at the last dashboard refresh
comments in a threadthe thread's counter, incremented on writethe length of the page just served
whether a job is donethe job record's state"every segment I can see is present"
a queue's depththe queue itself, nowthe routing records ever written to it
spend this windowthe window key the limiter enforcesa total over all windows

The rules follow from the table:

  1. Read the counter the write path writes. If the enforcement path decrements stock:{sku}, the read reports stock:{sku}, not a sum over orders that someone recomputes later.
  2. Read it on every request. A counter is cheap to read (one GET, one HGETALL); caching it saves microseconds and costs correctness. When a dashboard can tolerate age, the read returns updated_at with the value.
  3. Read related numbers together. capacity and allocated read in two round trips can straddle a write and produce a free count that never existed. One HMGET, one Lua script or a MULTI returns them from one instant.
  4. Derive status from the record that owns it. "Done" is a state the job record holds, set by the code that knows every chunk finished. Counting visible segments says done when a failed chunk simply left no segment.
  5. Clamp what can drift. A counter that drifted above capacity reports free: 0 and a drift flag, never free: -3.
HMGET node:n7 capacity allocated     → 8, 11       one instant, one reply
free = max(0, 8 - 11) = 0                         and report drift: allocated > capacity

Caches keyed by everything that varies

A cache is a function from a key to a value. It is correct only when the value depends on nothing but the key. Before writing a cache line, list what the answer depends on, and put every item in the key:

The answer depends onSo the key carriesLeave it out and
the tenant{tenant}tenant B reads tenant A's cached body
the version (model, prompt, policy){version}a rollout serves the old version's answer
the window or range{window}the 1-hour figure answers the 24-hour question
the newest input{latest_turn} or a content digesta new message never reaches the context
the caller's role{role}the operator's view reaches the customer
context:{session}                         wrong: one entry per session forever
context:{tenant}:{session}:{last_turn}    right: a new turn is a new key, the old one ages out

A key that includes the newest input is self-invalidating: nobody has to remember to delete the old entry, because no reader will ask for it again, and its TTL reclaims the memory. A TTL alone is not an invalidation strategy; it bounds how long a wrong answer can live. Pick it from the cost of being stale (a config that must reflect a promotion within seconds gets seconds), and set it in the same command that writes the value.

cache-aside: miss, fill with TTL, hit until expiry
Requests between the application, Redis and the origin storeAppRedisOriginuser:42absentTTL —GET user:42(nil)
cold key. First request: the key is not in Redis, so GET returns nil — a cache miss. The app must go to the origin.

The caches you do not run

The Redis cache is yours. The browser cache, the CDN and every proxy between them are not, and they obey only the response headers (RFC 9111):

HeaderWho may store the responseUse it for
public, max-age=60the browser and every shared cachethe same bytes for everyone: a published product page
private, max-age=30the user's own browser onlyone user's view, fine to reuse briefly
no-storenobodypersonal or sensitive data, explanations of one user's decision
no-cacheanyone, but revalidate before every reusedata that must be checked each time, cheap to confirm with an ETag

public on a personal response is the expensive mistake: the CDN stores the first user's answer under the URL and serves it to the next user who asks the same URL. If the URL does not carry the user (and it should not, since identity comes from the credential), every user shares one entry. Personal data gets private at least, no-store when it must not sit on disk at all. A shared, public response that changes with a version names the version in its ETag, so revalidation notices a publish.

Pop quiz

GET /v1/flags/checkout/evaluation explains why the user in the bearer token got a flag value. It is sent with Cache-Control: public, max-age=60. What goes wrong behind a CDN?

Statistics that earn their verdict

A read that reports a number is making a claim about a population. Four habits keep the claim honest.

Median, not mean, for skewed figures. Waits, latencies and sizes have long tails. Five tickets waited 30, 40, 45, 50 and 60 seconds and one has waited two hours:

mean   = (30 + 40 + 45 + 50 + 60 + 7200) / 6 = 1237.5 s   about 20 minutes
median = (45 + 50) / 2                       =   47.5 s

The mean describes no ticket in the queue. The median says what a typical customer waits; the oldest item, reported separately, says what the worst one waits. Report both, and name the method.

A minimum sample before a verdict. Three ratings out of three positive is "100 percent satisfied" and means nothing. Below a floor chosen in advance (50 ratings, 200 scored samples, 500 pairs), the read answers pending or insufficient_data with the count it has, not a score.

An interval, not a point. Variant B converted 60 of 1000 users, control 50 of 1000:

lift  = 0.060 - 0.050 = 0.010
se    = sqrt(0.05 * 0.95 / 1000 + 0.06 * 0.94 / 1000) ≈ 0.0102
95 %  = 0.010 ± 1.96 * 0.0102 = [-0.010, 0.030]

The interval crosses zero: B may be better, the same or worse. A report that prints "+20 % lift" without the interval invites a decision the data does not support.

Sample ratio mismatch blocks the verdict. An experiment configured 50/50 that counts 10 000 users in control and 9 600 in treatment has lost users somewhere: a redirect that drops slow clients, a crash in one arm, a bot filter applied unevenly. A chi-square test on the counts against the configured split catches it:

expected = 9800 each
chi2     = (200² / 9800) + (200² / 9800) ≈ 8.16      p ≈ 0.004

With a p-value that small the split is broken, and every comparison downstream compares two populations that differ in more than the treatment. Microsoft's experimentation platform requires every test to pass this check before anyone looks at its effects (Fabijan et al.).

Pop quiz

An experiment report has 12 000 exposures in control and 11 200 in the variant, configured 50/50, and the variant's lift is significant. What should the read answer?

Pagination by cursor, with caps

An offset says "skip the first N items". Between two requests the list changes, and N now points somewhere else:

Sequence of calls for this stepclientcomments APIthread indexother usersGET ?offset=0 (page 1,newest first)ZREVRANGE 0 19c100 … c81two new comments: c101,c102GET ?offset=20 (page 2)ZREVRANGE 20 39c82 and c81 again, thenc80 … c63
— request- - reply· · fire-and-forget

Inserts at the head repeat items; deletes skip them. Offsets are also slow on large sets, since the store walks past every skipped row. A cursor says "continue after this item":

page 1: ZREVRANGEBYSCORE thread:t1 +inf -inf LIMIT 0 20     → ..., c81 (score 1731400081)
cursor: the last item's sort key, opaque to the client
page 2: ZREVRANGEBYSCORE thread:t1 (1731400081 -inf LIMIT 0 20

The exclusive bound ( resumes strictly after the last item served, whatever arrived since. Ties on the sort key need a tie-breaker (score, then id) or a cursor that encodes both.

The rest of a good page:

  • A cap on the page size. limit defaults to something modest and is capped (100 by default, 500 at most, for instance). An uncapped limit turns one request into a table scan.
  • An exact has_more. Fetch limit + 1 items; there is a next page only if the extra one exists. Guessing from a full page leaves a final empty page or misses one.
  • Totals from the counter, not the page. comment_count comes from the thread's counter. The page length is 20 on page 1 and 7 on the last page.
  • An opaque cursor. Encode it (base64 of the sort key, signed if it must not be forged) so clients do not do arithmetic on it, and so you can change its contents later (Slack's account of moving from offsets to cursors makes the same case).
  • A bounded range. Time-series reads require from and to and refuse a span past the limit with 400, before the store is asked.

Pop quiz

A sync endpoint pages a change log 1000 entries at a time. The starter returns next_cursor = offset + 1000. What fails when another device writes during a sync?

Partial, pending, stale and degraded, said out loud

The most tempting lie is the tidy one: drop what did not fit, default what did not answer, and return something that looks complete. An honest read keeps the shape of what it does not know:

StateWhat happenedWhat the read says
pendingthe work has not finished202 or status: "pending" with progress, never a result
partialsome parts failed or are missingpartial: true and the list of gaps (failed shards, missing parent spans, uncovered categories)
stalethe data is older than its boundstale: true and the age or updated_at
degradeda fallback answeredthe source (fallback, cached), and the share of answers it produced
truncatedthe answer hit a captruncated: true and the cap
gonethe result expired410 with the metadata that is left
spans = assembly.load_trace(tenant, trace_id)
ids = {s.span_id for s in spans}
gaps = sorted({s.parent_id for s in spans if s.parent_id and s.parent_id not in ids})
return {"spans": spans, "gaps": gaps, "partial": bool(gaps)}   # every span, and what is missing

Each of these costs a field or two. Leaving them out moves the cost to the reader, who learns about the gap during an incident review or a customer escalation. The same rule covers verdicts: a red-team run with incomplete coverage does not report pass, a batch with a failed shard is not completed, and a purge that some edges have not confirmed is accepted, not propagated.

The fix and its trade-offs

Put together, an honest read handler has a recognisable order:

  1. Identify the caller from the credential; 401 without one.
  2. Validate the request: bounded range, capped page size, a window from a fixed set; 400 before any read.
  3. Look up inside the caller's scope; 404 for missing and foreign alike.
  4. Read the source of truth: the live counter, the record that owns the state, related values in one command.
  5. Compute honestly: the right denominator, the median for skewed figures, an interval, a sample floor and a ratio check before any verdict.
  6. Describe completeness: pending, partial, stale, degraded, truncated, with what is missing.
  7. Cache with a full key and the right headers: tenant, version, window and latest input in the key, a TTL bounded by the cost of staleness, private or no-store for personal data.

What it costs:

ChoiceCostWhy it is usually worth it
live countersone extra round trip per readcounters are O(1); a stale capacity figure misplaces real work
fine-grained cache keyslower hit ratio, more entriesevery miss is correct; TTLs reclaim the old entries
no-store on personal readsno browser or CDN reusepersonal reads are rarely hot; a leak is permanent
sample floors and intervalsslower verdicts, more "inconclusive"a wrong winner ships and stays shipped
cursorsno "jump to page 37"clients that walk a list need completeness, not page numbers

An expensive figure (a percentile over millions of rows) is precomputed on the write path, or cached with its updated_at, never replaced by a cheap figure that looks like it.

Proving the fix

The demo already passes, so a proof has to recreate the condition the demo lacks and assert on outcomes, not on the code's shape:

  • Two tenants. Seed the same id shape for tenants A and B; read B's record as A and compare the response, status and body, byte for byte with the response for an id that does not exist.
  • A write between reads. Read the counter, perform the write the enforcement path performs, read again: the second read moves by exactly the write. Do the same with the cache warm, so a stale entry would show.
  • A key per variant. Warm the cache as tenant A or version 1, read as tenant B or version 2, and assert the answer belongs to B or version 2. Read the response headers and assert private or no-store on every personal route.
  • Skew and small samples. Seed one outlier and assert the reported figure is the median. Seed fewer samples than the floor and assert no verdict, with the count. Seed a 50/50 experiment with a broken split and assert no winner.
  • Pages under writes. Read page 1, insert at the head and delete from the middle, read on to the end, and assert every surviving item appeared exactly once, has_more was false only on the last page, and no page exceeded the cap.
  • Something missing. Fail one shard, drop one parent span, let one fallback answer, age the data past its bound, and assert the read names each: partial, gaps, degraded, stale.

A good proof fails against the starter, passes after the fix, and asserts facts a user depends on: "tenant A cannot tell B's job exists", "no winner on a broken split".

Pop quiz

Which test proves that a stock read uses the live counter rather than a cached copy?

Practise it

98 steps start from a starter that hides this failure mode, in 96 courses.

Sources