What Is a Commit, Really?

Reading time:2031 words, 11 min read
Last edited:
What Is a Commit, Really?

Part 1 of a series on commit, written while building CambiumDB.

Why I'm reading about commit again

CambiumDB started as a side project inspired by a use case at Homa. The idea was simple enough: put a cache in front of S3. An in-process cache, Redis behind it, pub/sub to invalidate, and S3's conditional writes as the point where a write actually lands. I borrowed a lot of the shape from JuiceFS.

Then the bugs started. The first one was subtle enough that it gets its own post later in this series. The short version: two writers could both be told they won the same write. I fixed it. Then I fixed the next one, and the one after that. Somewhere around the third fix I got frustrated and realized we had an information leakage problem.

When the same bug keeps showing up in different places, it's rarely a coding mistake. It's a design problem. John Ousterhout's A Philosophy of Software Design lists information leakage as a red flag: the same design decision reflected in multiple modules. The underlying idea, information hiding, goes back to David Parnas in 1972. In CambiumDB the leaked decision was "what is the current version of this path?" The in-process cache, Redis's Lua scripts, the listing index and the S3 commit path each answered it on their own. Every module had to get it right, so every fix in one exposed the gap in the next.

Once I'd named the problem, the fix followed: one place decides what "current" means. But it also made me realize how long it had been since I'd read much about commit—not since Understanding Distributed Systems. So I went back to brush up, from two-phase commit (2PC) to the systems being built on object storage today. The punchline fits in one sentence:

Every commit is one ordered decision record, and any store with a conditional write can hold it.

That sounds like a slogan. It isn't. Once you break the problem into a handful of plain pieces, it's close to a proof, and that's what this post does. The next post walks through the literature and maps each system back to this claim.

Why care

If you mostly build on top of Postgres or MySQL, this can all sound academic. Databases solved commit decades ago, right? Inside one database, mostly yes. But two things still bite.

Problem 1: a race inside one database. You can use a perfectly good database in a way that races. Two people try to buy the last seat. Each request reads "1 seat left," checks it, and inserts a booking. Most databases default to an isolation level weaker than serializable—Postgres, Oracle and SQL Server all default to Read Committed—so both transactions can read "1 left" and both insert. Each one commits atomically and durably, and the result is still wrong. The common fixes:

  • Do the check and the write in one statement: UPDATE seats SET sold = true WHERE id = 12 AND sold = false, then look at the row count.
  • Let a unique constraint reject the second booking.
  • Lock the row you read with SELECT ... FOR UPDATE.
  • Run the transaction at SERIALIZABLE and retry when it's rejected.

Problem 2: one write, two systems. The moment a write touches two systems, no database is committing for you. A card is charged, the service crashes before saving the order, and the customer has a receipt for nothing. The charge lives at the payment provider and the order lives in your database. Neither can commit for the other. This is the dual-write problem, and it shows up anywhere one action has to land in two places: your database and a queue, a cache and S3. The common fixes:

  • Idempotency keys, so retrying the charge can't charge twice.
  • The outbox pattern: write the order and an "event to send" in the same database transaction, and let a separate process deliver it.
  • Sagas: a sequence of local commits, each with a compensating step if a later one fails.

What getting it right looks like: a rejected git push. You've already seen this one work. Someone pushed first, and Git refuses your push because the branch moved since you last looked. That's not a bug. It's the fix, in miniature: one of you wins, the other is told so, and nobody walks away with a confirmation that isn't true.

You could memorize every one of these scenarios and its fix. Or you could learn the one pattern underneath them. The seat race, the half-finished checkout and the rejected push are all the same question: who decides, and where is that decision written down? Once you can see that question, you start recognizing it everywhere, including in places nobody gave you a checklist for. The rest of this post builds that pattern from scratch.

The pieces

To reason about any of this you only need five things.

A writer holds state in memory, which a crash wipes. It sends messages, which can be lost or late, to a durable store, which survives crashes.

  • A writer is anything that wants to change state: an API server, a database node, a CLI.
  • Memory is where a writer keeps what it's working on. It's fast, and a crash wipes it.
  • A durable store is anything that keeps its contents through a crash: a disk, a database, S3.
  • A crash stops a process at an arbitrary point. When it comes back, whatever was only in memory is gone.
  • A message is how pieces talk to each other. Messages can be lost, delayed, or arrive after the sender has given up.

With those pieces, "committed" has a precise meaning. When a system tells you something is committed, it's making two promises:

  1. It survives. No crash, anywhere, can make it un-happen.
  2. It's the only answer. It can't also be aborted, and nobody can be told otherwise.

Everything below follows from trying to keep both promises.

One writer

Start with the simplest case: one writer, one store, one change. Call it T1.

The writer does its work in memory. At some point it has to make the change real. Where exactly does T1 become committed?

One writer: T1 is committed at the moment its decision record becomes durable. A crash before that leaves no record, which means no.

Promise 1 says it has to survive a crash, so it can't be the moment the writer decides in memory. A crash would erase that. It also can't be the moment the user is told "done." If the writer crashes after telling the user but before saving anything, the user has been lied to.

So the commit point is the moment a record of the decision reaches durable storage. Before that, a crash leaves no record, and the change never happened. After it, the record is there, and the change happened, whether or not anyone was told yet.

Notice what "no record" means. If the store has nothing for T1, the answer is no. Missing means aborted. That sounds trivial, but it's useful: you never have to write down a "no," only a "yes."

Two writers

Add a second writer, and both want the last seat.

Two writers. Without an authority both are told they won and one write is silently lost. With one authority, the first write wins and the second is told no.

If each writer just writes its own record, they both succeed. The store ends up holding whichever write landed last, and the other writer was told "ok" for a change that no longer exists. That breaks promise 2: two different answers, both reported as committed.

Something has to pick. There has to be one authority that sees both attempts, accepts one, and tells the other no. The authority's choice is itself a decision, so by the one-writer argument it has to be written down durably too. The record now says not just "T1 committed" but "seat 12 belongs to A."

That's the first half of the claim, already: a commit is one decision record, written by one authority.

When the authority crashes

The authority is now the important piece, which makes it the weak point. What if it crashes?

Where the decision record lives. In two-phase commit one coordinator holds it, so its crash blocks everyone. With consensus a majority holds the same record, so one crash changes nothing.

This is the problem behind most of the commit literature. In two-phase commit, the authority is one coordinator. It collects votes, writes the decision, and tells everyone. If it crashes at the wrong moment, the participants have voted, are holding locks, and can't learn the outcome until it comes back. They're stuck. Gray described this as 2PC "blocking."

The fix is to store the same record on more than one machine and treat it as decided once a majority has it. Paxos and Raft do exactly that. With three replicas, any two of them hold the decision, so losing one changes nothing.

The fix doesn't change what's being stored, though. It's still one decision record. It's just stored on several disks instead of one. Gray and Lamport made this point formally in 2004: two-phase commit is the same protocol as Paxos Commit when you tolerate zero failures. Consensus buys you availability. It doesn't buy you a different kind of commit.

Many decisions

Real systems make more than one decision. Each one is a record, and the records have to come in an order. If T2 read something T1 wrote, T1 has to come first, and every reader has to agree on that.

Many decisions form an ordered log. Committed means the entry is in the log; the next slot is the one place writers compete.

Put the records in order and you have a log. "Committed" now means "in the log." Writers only compete for one thing: the next slot. Whoever claims slot #6 first wins it. Everyone else sees #6 is taken and tries again at #7, against the new state.

That's the full claim, first half: every commit is one ordered decision record. Only three things differ between systems: who the authority is (one machine, or a majority), where the record lives, and how much work it takes to get there.

What can hold it

Now the second half. If a commit is "claim the next slot in an ordered log," what does a store need in order to hold that log?

It needs to be durable, which most stores are. And when two writers race for the same slot, it needs to pick exactly one of them and tell the other no. That's an operation every developer has used without naming it: a conditional write on one name. "Create this, but only if it doesn't exist yet." Or "replace this, but only if it hasn't changed since I read it."

Any store with a conditional write can hold the record: two writers try to create the same name, the store accepts the first and rejects the second.

You've seen it everywhere:

  • open() with O_CREAT | O_EXCL fails if the file already exists.
  • A unique key in a database rejects the second insert.
  • etcd and ZooKeeper have compare-and-swap.
  • git push refuses a non-fast-forward.
  • S3 has If-None-Match: * (create only if absent) and If-Match (replace only if unchanged).

Any of these can play the authority. The store itself breaks the tie, so the writers don't have to agree on anything among themselves. That's the second half of the claim: any store with a conditional write can hold it.

There's a catch, and it's what the rest of this series is about. Each conditional write is a round trip to the store. On S3, one name sustains roughly 15 of those per second. You can't put every user's write through that one at a time. Every real system on top of it batches: one owner collects writes and commits them as a group. That's where the next post picks up.

So

A commit isn't a protocol. It's one ordered, durable record of a decision, written by one authority. Two-phase commit, Paxos, Raft, and every table format on S3 are all different answers to the same two questions: who gets to write that record, and what happens when they disappear.