S3 If-Match Is Not a Version Check

Part 3 of a series on commit, written while building CambiumDB. Part 1: What Is a Commit, Really? Part 2: Commit Protocols: A Literature Tour.
In part 1 I argued that any store with a conditional write can hold a commit. That's true, but a conditional write is only as good as the thing it compares. CambiumDB's first commit protocol compared the wrong thing.
The setup
The first version of CambiumDB was a cache in front of S3, and S3 was the commit point. Every file had a version number, stored in the object's S3 metadata. Writing a file went like this:
HEADthe object to get its current version and ETag. If the caller asked for a specific version (X-If-Version: 5) and the object is at a different one, stop with a conflict.PUTthe new content with version + 1 andIf-Match: <etag>.
If another writer got there between the two steps, the object's ETag would have changed and S3 would reject the PUT with a 412. One winner, the other told no. That's the conditional-write authority from part 1.
Everything above S3 trusted the version. The in-process cache, Redis and the listing index all only accept an entry whose version is newer than the one they hold, so they can't go backwards.
The bug
For a normal (non-multipart) upload, an S3 ETag is an MD5 hash of the object's bytes. It identifies content, not a write. If-Match doesn't ask "has anyone written this object since I read it?" It asks "does this object still have these bytes?"
So the commit point checked the bytes, and everything else trusted the version. When those two disagree, things break.
When the bytes come back
Writer C runs HEAD: version 3, content X, ETag E(X). It computes new content Z and is about to PUT it as version 4. Before that PUT lands, two other writes go through: Y as version 4, then X again as version 5. The content is X again, so the ETag is E(X) again, and C's If-Match: E(X) passes.
S3 now holds Z, stamped version 4. A moment ago the object was at version 5. Now it says 4, and there are two different version 4s: Y and Z.
Everything that trusted the version breaks, with no error anywhere:
- A cache holding version 5 (X) rejects Z as older, and keeps serving X.
- A cache holding version 4 (Y) doesn't see anything newer either, and keeps serving Y.
- A client that read Y at version 4 can send
X-If-Version: 4, pass the version check against Z, and overwrite a write it never saw.
The window is small: the two writes have to land between C's HEAD and its PUT. But a slow writer under load is exactly the case a commit protocol exists for.
When the bytes never change
There's a simpler way to see the same flaw, and it's the test that caught it. Two writers HEAD the object (version 5, content X, ETag E), and both PUT X back unchanged, as version 6. The first PUT leaves the ETag at E, because the bytes didn't change. So the second writer's If-Match: E passes too, and both are told their write from version 5 won.
On its own, that loses nothing: the content is the same either way. But a conditional write promises exactly one winner, and this one handed out two. It also takes two lines to test, which made it the easiest way to prove If-Match was checking bytes, not writes.
Both cases break part 1's second promise: a commit has one answer.
Finding it
The test that caught it was the same-content case: two writers, same bytes, at the same time, then check that exactly one won. It also mattered what it ran against. LocalStack 3.0 silently ignores conditional headers, so every conditional test passes against it no matter what. CambiumDB's integration tests now run on SeaweedFS, which enforces them. A backend that ignores preconditions loses updates without any error, so CambiumDB checks for it at startup: it makes two create-only PUTs to the same name and refuses to start if the second one succeeds.
The fix
If the ETag identifies bytes, make every write's bytes unique.
Every object CambiumDB stores is now framed: 4 magic bytes, then a fresh 16-byte random nonce, then the content. That's a fixed 20-byte header, stripped on read. Callers never see it, and sizes they get back are content sizes. Framed objects carry a format marker in their S3 metadata, and an unknown format value fails closed, as an error, never as garbage.
Now no two writes produce the same ETag, even when they store the same content. Writing X a second time gives it a new ETag, so C's If-Match fails in the first case, and the second writer gets a 412 in the other.
This works because CambiumDB is the only thing that reads its bucket. The objects are a database's storage format, not files for other tools.
What I didn't do
The obvious alternative is a distributed lock: take a lock, HEAD, PUT, release. I rejected it, for the reason in part 2. A lock holder can pause past its lease, and the only thing that can stop a stale writer is a check the storage does. On S3, that check is the conditional write. So a lock would have needed the very fencing that was broken.
I did add an in-process lock per path. It's there only to save wasted round trips when two requests in the same process race. Correctness still comes from the conditional write.
The bugs it uncovered
Fixing this surfaced more bugs, and they were all the same shape:
- The cache tiers could re-cache an entry after evicting a stale one, bringing old data back.
- One
HEADresult was shared across tiers, which was itself a race. Each tier now checks for itself.
Every fix had the same root cause, the information leakage from part 1: several layers each kept their own idea of the current version, and they drifted apart. The ETag bug was that leak in its purest form. The commit point and the caches each had a different idea of what "unchanged" meant.
That's why the next post isn't about another patch. It's about stepping back and designing the storage layer a second time, around one owner and one log: Design It Twice: Cache Tiers vs a Log on Object Storage.