What actually happens between sending an UPDATE and getting UPDATE 1 back?
Ten steps, animated, with an index and without.
Press Play, or step with the arrows. Switch to Without the index, or Side by side, to see what one missing index changes.
Most system design courses start with how a web request travels. This lesson does the same for the database: one UPDATE, from the driver to the disk and back, with the numbers we measured on real servers.
A real PostgreSQL runs in this page, with a users table of 50,000 rows. Run the steps in order and watch the plan change.
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM users WHERE email = 'user42000@example.com';
CREATE INDEX users_email ON users (email);
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM users WHERE email = 'user42000@example.com';
SELECT ctid, xmin, name FROM users WHERE id = 42; UPDATE users SET name = 'Asha' WHERE id = 42; SELECT ctid, xmin, name FROM users WHERE id = 42;
The steps are the same in every engine. The names and the files change.
| Step | PostgreSQL | MySQL, InnoDB | MongoDB, WiredTiger |
|---|---|---|---|
| Connection | A process per connection | A thread per connection | A thread per connection |
| Plan | Planner, from table statistics | Optimizer, from index statistics | Tries candidate plans, caches the winner |
| Find the row | Index, then the table page | The primary key B-tree holds the row; a secondary index gives the key, then a second lookup | Index, then the document in the collection file |
| Old version | Stays in the table page until VACUUM | Goes to the undo log until purge | Kept in the cache while a reader needs it |
| Log flushed at commit | WAL | Redo log and binlog | Journal, with j: true or w: "majority" |
| Commit wait, Notebook 2 | 0.236 ms | 0.556 ms | 0.954 ms |
| To replicas | WAL stream | Binlog | Oplog |
| Data file written | Later, at a checkpoint | Later, by the page cleaner | Later, at the 60 second checkpoint |
Commit times are from one client on the Notebook 2 runner. Your disk will give different numbers; the order between the settings is what carries over.
For a lookup by an indexed key, most of the trip is two network hops and one log flush. Reading a few pages is cheap, especially when they are already in memory.
Two things turn this into a slow query. A missing index turns 4 pages into 19,231. A lock held by another transaction turns step 4 into a wait with no upper limit except your lock_timeout. When a query is slow, ask which of the ten steps it is stuck in.
BEGIN; EXPLAIN (ANALYZE, BUFFERS) UPDATE users SET plan = 'pro' WHERE email = 'asha@example.com'; ROLLBACK; -- Index Scan or Seq Scan, and Buffers: shared hit=… read=…
-- Who is waiting on a lock right now (step 4) SELECT pid, wait_event_type, wait_event, state, now() - query_start AS running, query FROM pg_stat_activity WHERE wait_event_type = 'Lock';
-- MySQL: the plan (estimates only) EXPLAIN UPDATE users SET plan = 'pro' WHERE email = 'asha@example.com';
Paste a plan into the EXPLAIN reader to see which step costs the most. MySQL only runs EXPLAIN ANALYZE on SELECT and multi-table UPDATE and DELETE, so a single-table UPDATE gets plain EXPLAIN here. Run the same WHERE clause as a SELECT to see the actual cost.
The same mechanism, indexes and plans, what a commit promises, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. With default settings, when UPDATE returns, what is on disk?
The table file catches up at the next checkpoint. The WAL is enough to bring the change back after a crash.
2. In Notebook 3, how many pages did PostgreSQL read to find one user by email without an index, on 1,000,000 rows?
Every page of the 150 MB table. With the index it was 4.
3. With default asynchronous replication, when does the replica receive the change?
That gap is why a failover to an asynchronous replica can lose acknowledged commits.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.