Skip to content
← All work

Personal · systems engineering labs · 2026In progress · lab 1 of 7 solved

MYENGINEER

Seven small systems with real production bugs: a slow dashboard, an oversold flash sale, stale replica reads, a cache stampede, double charges, broken sharding and a server that stays down after a spike. You run each one, watch it fail with real numbers, fix it, and an automated check tries to break your fix. I've solved lab 1.

Who wrote whatI designed MYENGINEER as the way I wanted to learn, and asked Claude to build it to that spec: seven labs, the checks, a step-by-step guide for lab 1 and a one-command CLI, in plain Python with no dependencies. Solving the labs is mine to do. My rule is that the first attempt at each lab happens without AI; Claude is allowed in afterwards as a tutor that explains and sets experiments. For lab 1 I chose the indexes and wrote the single-query fix for Top Customers myself. After I'd found both bugs in the daily revenue query, I asked the tutor for the corrected version. A handoff document in the repo records who did what in every session.

How every lab goes
  1. Run it and read the numbers
  2. Read the query plan
  3. Change one thing
  4. Measure again
  5. The check tries to break the fix

How it works

Everything goes through one command, python3 lab.py. run 01 replays a fixed workload and prints latency, statements per request and “work” per request; adding --explain shows the query plan for each statement. check 01 is the acceptance test. hint 01 reveals one hint at a time, guide 01 walks lab 1 in ten steps, and play 01 opens a SQL prompt on the lab database that prints rows, timing and the plan. notes 01 is a senior-engineer debrief that only opens once you've passed, and journal 01 starts an investigation note.

Each lab is one folder: the incident written as a chat from the on-call channel, the one file you change (app.py), a script that shows the symptoms, the check, the hints, and a reference fix with its debrief.

The checks are built to reject the plausible-but-wrong fixes: a threading lock across four processes, “read from the primary for one second after a write”, or a new idempotency key on every retry.

Why “work”, not milliseconds

Lab 1's harness charges 0.5 ms of network time per SQL statement and kills any request after 2 seconds. It also counts work: thousands of SQLite virtual-machine steps, roughly the rows touched. A fast laptop hides a full table scan; work doesn't. Every fix below was judged on work.

Terminal output of lab.py run 01 before the fix: My Orders at 900k work, daily revenue at 1,665k, Top Customers timing out at 2000 ms after 79 statements
Before: Top Customers times out at 2 seconds, having managed 79 of the 1,978 queries it needed.
Terminal output of lab.py run 01 after the fix: My Orders 0k work, daily revenue 2k, Top Customers one statement at 369k
After: one statement per request, and work down by orders of magnitude.

Lab 1, the slow dashboard

The incident: Top Customers times out, the daily revenue report is “sluggish”, and My Orders got slower after the shop passed 300,000 orders. Nothing was deployed.

  • My Orders. The query scanned the whole orders table and then sorted it. An index on (user_id, created_at) puts the equality column first and the sort column second, so the database reads at most 20 rows that are already in order and stops. Work fell from about 900,000 to under 1,000. For the heaviest buyer, with 324 orders, it went from about 3,400 steps to about 200, and it stays flat as their history grows.
  • Daily revenue. I found two bugs. The first crashed. The second didn't, and that's the one I'm glad I hit: Python's isoformat() writes dates with a T, the database stores them with a space, and in a text comparison a space sorts before a T. The query returned 0 instead of 8,249,922, a confident wrong number with no error. The other problem was that date(created_at) = ? wraps the indexed column in a function, so no index can help. Rewritten as a half-open range from midnight to the next midnight, work fell from about 1,665,000 to about 2,000.
  • Top Customers. One query per customer: 1,978 round trips for one country. I rebuilt it step by step in the playground (join, filter, group, sum, order, limit) and wrote it as a single query, tie-break included.
  • The surprise. My new (status, created_at) index made Top Customers worse. The planner started driving the query from status = 'paid', but 85% of orders are paid, so that index barely narrows anything. Work hit about 2.5 million against a 1 million budget. Running ANALYZE gave the planner real statistics, the plan flipped, and the maximum fell to 710,000. All ten checks passed.
Query plans before and after: SCAN orders and USE TEMP B-TREE become SEARCH orders USING INDEX
The query plans: full scans and a temporary sort become index searches.
Terminal output of lab.py check 01 with all ten expectations passing
The acceptance check, all ten passing.

What I took from it

  • Ask whether a cost is bounded or grows with the data.
  • Put equality columns first in an index, then the sort or range column.
  • Keep indexed columns bare in a WHERE clause.
  • N+1 is a round-trip problem, not an index problem.
  • Every new index can change every plan on its table, and statistics can change a plan with no deploy.
  • A fast wrong finance number is worse than a slow right one.

The seven labs

#LabThe incidentWhat you learnWhat the check enforces
01The slow dashboardTop Customers times out at 300k ordersIndexes, composite order, query plans, N+1, sargable predicates, planner statisticsCorrect results; no full scan of orders; no temporary sort; at most 2 round trips; work budgets per page
02The flash sale900 pairs sold, 100 in stock, 99 still “left”Race conditions, lost updates, atomic conditional updates, why in-process locks fail across 4 serversNever oversold; stock never negative; stock and orders balance; every charged customer has exactly one order
03Read replicas“I saved my name and it changed back”Replication lag, read-your-writes, monotonic reads, log sequence numbersNobody sees their old name after saving; no going back in time; primary serves ≤25% of reads; p99 ≤150 ms
04The cache stampedeDB at 100% CPU with a 98% cache hit rateTTLs, the thundering herd, single-flight, jitterPeak DB concurrency within limit; about one query per product per TTL; no price older than 1.5 s; p99 ≤200 ms
05The double charge19 customers charged twice after retriesUnknown outcomes, idempotency keys, retryable vs final errors, backoff with jitterNobody charged twice; every valid order charged; a declined card is never retried; no retry storm
06Sharding75% of users “not found” after a deployStable hashing, the cost of resharding, consistent hashing, virtual nodesSame key, same shard in every process; balanced within 1.25×; adding a shard moves ≤25% of keys
07Overload0% success after the spike endedQueueing, Little's law, goodput, load shedding, deadlines, backpressure≥99% ok before the spike; goodput ≥75% of capacity during it; ≥95% ok after; ≤2% wasted work

Design drills

Alongside the labs there are five paper design drills, 45 minutes each: a URL shortener, a ticket sale, a news feed, a wallet ledger and a rate limiter. After each one, review asks the questions a staff engineer would. The wallet drill asks how you'd answer “what was this balance at 14:03 last March, and why?” and points back to labs 2, 3, 5 and 6. estimate is a back-of-the-envelope quiz with new numbers every time.

Next

Write up lab 1 in my own words in the journal, redo it from scratch in a week with no guide and no AI, then lab 2: the flash sale, where four processes sell the same 100 pairs of sneakers and a simple lock can't save you.

NextLedger API→

07 · Contact

Write to me.

I'm open to software engineering and data roles, and to freelance work. If you have a process that still runs on copy and paste, I'd like to hear about it.