Home›Practice›Labs
Labs · things to click and break

Labs

What happens if I pull the plug, or flood a replica, or lock one row?

Four small simulations. Each one is a model of the behavior, not a benchmark, so the numbers are round on purpose. Each lab starts with a question. Make a guess before you open the answer, then break things and see whether you were right. Two more labs live on the Staff Path: a B+ tree and an MVCC timeline. For the other engines: a Redis console and a vector search lab.

Lab 1 · crash recovery

Pull the plug, then watch the restart

Commit a few transactions. Take a checkpoint at some point. Open a transaction and leave it. Then crash the server and restart it. The restart replays only the log written after the last checkpoint.

You turn the log-flush box off, commit three transactions, and crash before a second passes. How many of the three survive?

It depends on the engine. PostgreSQL flushes the WAL every wal_writer_delay, 200 ms by default and never more than about 600 ms after a commit, so within a second a flush almost certainly ran and the three survive. MySQL flushes the redo log once a second with innodb_flush_log_at_trx_commit = 0, so all three can be lost if the crash lands before that flush. MongoDB has no such setting: durability there is the write concern, and a write with j: false can be lost until the journal flushes, about 100 ms by default. The lab below models one shared flush on its One second passes button, so read it as the MySQL case.


    

Try this: turn the log-flush box off, commit three times, crash, and read which commits were lost. They were acknowledged to the client, so the application believed they were safe. Then run it again with the box on.

The same event, side by side in the real engines: Compare, scenario 2.

Lab 2 · replica lag

Writes arrive faster than the replica applies them

Lag is a queue. Set the write rate and the apply rate and see how big the queue gets, how long the replica is behind, and whether it can ever catch up.

Writes arrive at 1,500 per second and the replica applies 1,200. After 10 minutes, how far behind is it?

300 a second for 600 seconds is 180,000 transactions behind, about two and a half minutes of work at 1,200 a second. It keeps growing for as long as the overload lasts, and it only shrinks once writes drop below 1,200.

transactions waiting
replica is behind by
to catch up if writes drop by half

A real replica does not apply at a fixed rate. Apply speed depends on the shape of the work, so measure it on your own workload with the commands in Compare, scenario 3.

Lab 3 · lock pile-up

One transaction holds one hot row

A single transaction takes a lock on a row that many requests want. Every request that needs that row waits, and every waiting request holds a connection. Watch how long it takes for the pool to run out.

40 requests a second need the same row, one transaction holds its lock for 5 seconds, and the pool has 100 connections. Does the pool survive?

No. 40 times 5 is 200 waiting sessions. The pool is gone after 2.5 seconds, and from then on every request waits, including the ones that never touch that row.

sessions waiting when the lock is released
until the pool is exhausted
to drain the queue afterwards

The bars show waiting sessions over the time the lock is held. Red bars are past the pool size. This is the most common shape of an outage that starts as "one slow query".

Lab 4 · page count

Scan every page, or walk the index

The same lookup, two ways. A scan reads every page of the table. An index lookup reads a handful. The squares are the table, scaled to fit.

10 million rows of 200 bytes in 8 KB pages. How many pages does a scan read, and how many does an index lookup read?

About 250,000 pages for the scan. About 4 for the index lookup: 3 index pages and the table page that holds the row.

pages read in this run
pages in the table
to scan
at a cold, sequential scan
pages for an index lookup

Part 2 of 4 · Practice · 9 pagesAll pages

Can I see it happen? Simulations, an index you build yourself, a real PostgreSQL in your browser, experiments run on real servers, and tools for your own database: a query coach, an EXPLAIN reader, a config checker and a health check.