Design It Twice: Cache Tiers vs a Log on Object Storage

Reading time:2201 words, 12 min read
Last edited:
Design It Twice: Cache Tiers vs a Log on Object Storage

Part 4 of a series on commit, written while building CambiumDB. Part 1: What Is a Commit, Really? Part 2: Commit Protocols: A Literature Tour Part 3: S3 If-Match Is Not a Version Check

John Ousterhout's A Philosophy of Software Design has a chapter called "Design It Twice." The advice is simple: before you commit to a design, sketch a second, very different one and compare them. Your first idea is rarely your best, and you can't tell until you have something to compare it with.

In theory, that's great advice. In practice, it only works when the problem is clearly defined. Most first designs do involve brainstorming a few options. But a lot of software engineering is learning about the problem as you build it. You have to implement something and test it to find the sharp corners, the right API ergonomics, and the requirements nobody wrote down. So when I say "design it twice," I've come to accept it usually means the "aha" moment about the right design comes after the first one has been running for a while.

That's what happened with CambiumDB. It didn't start as CambiumDB. It started as my own side project: a prototype for a very different problem, inspired by a use case at Homa. It never served a production need, but for the problem it was built for, it was well tested. Homa went a different direction, and I kept working on it for fun. Since then the use case and goals have kept moving, and the design has had to move with them.

So I didn't design it twice up front. I built the first design, lived with it through two rounds of correctness fixes (part 3 is one of them), and only then sat down and designed it a second time. The bugs were the research. I couldn't have written the comparison below before I knew where the first design broke.

What CambiumDB has to do

Both designs get scored against the same requirements, so here they are:

  • Agent shared memory. Many agents read and write paths at the same time. They need multi-path preconditions ("write X only if Y is still at version 7"), a record of every conflict, and an op log for audit and rollback.
  • Field nodes and a hub. Field nodes work disconnected and sync to a central S3-compatible store, possibly on-prem. Every record needs provenance, so that when a node is compromised, the hub can find what it touched and replay without it.
  • A deterministic simulator. The core should be something a simulator can drive with injected faults, so the tricky parts can be tested, and later shown in blog posts.

Design A: cache tiers over S3

This is what I built, and what's still running. It borrowed a lot from JuiceFS: an in-process cache (L1), Redis as a shared cache (L2), a Redis listing index, pub/sub to invalidate other instances, and S3 as the source of truth. A write commits with a conditional PUT on the object's ETag, as described in part 3.

Design A: four places each enforce their own version rule: the in-process cache, Redis, the Redis listing index and S3. Design B: one namespace owner keeps the index, versions are log sequence numbers, and each batch is one create-only PUT of the next log segment.

Redis ended up doing six jobs: the L2 cache, the listing index, L1 invalidation, a hot-path write counter, version-guard Lua scripts, and a write-behind queue that no longer receives writes. A write costs two S3 calls plus four or five round trips to Redis. A batch write is a series of separate commits, and if one fails, the earlier ones stay committed.

The red flags

Looking back, the design told me what was wrong. Two of Ousterhout's red flags were all over it.

Information leakage. The rule "a version only moves forward" was enforced in four places: once in Rust for L1, once in each of two Lua scripts for Redis, and once by the conditional PUT in S3. Every one had to get it right, and every bug I fixed was two of them disagreeing. Part 1 calls the same thing several layers each thinking they're the authority.

Special-general mixture. A database should store and return data. Mine also contained TTLs, a write-frequency tracker that kept hot paths out of L1, and a dead-letter queue. Those are caching policies, and they were mixed into the code that decides what's committed.

The comparison with JuiceFS is the clearest way to see the mistake. JuiceFS makes Redis authoritative for metadata and uses S3 only for immutable data blocks. Nothing else is authoritative, so there's nothing to keep coherent. I made S3 authoritative and Redis a cache of it. None of the systems I surveyed in part 2 do that, and it's exactly where my bugs came from.

Design A′: the cheap fix

The obvious second design is the first one without Redis. Keep the per-object conditional writes on S3. Each instance caches in memory and on local disk, and checks freshness with a conditional GET (If-None-Match, which returns 304 if nothing changed). Listing uses S3's LIST. Deletes leave a tombstone, so a path's version never resets.

That fixes a lot. Redis and its roughly 1,300 lines go away, and with them the whole class of bugs where tiers disagree. But it doesn't meet the requirements. S3's conditional write covers one object, so there's still no way to commit several paths atomically. The op log would be a second write per operation, which brings back the question of what happens when one of the two writes lands and the other doesn't.

A′ is a good incremental step, but it fixes the design's bugs, not its fit.

Design B: one owner, one log

The second real design is the shape every object-storage system in part 2 converged on, and it's part 1's claim built literally.

  • One owner per namespace. Requests for a namespace route to its owner. The owner keeps an in-memory index of every path's version and location.
  • Group commit into a log. The owner batches every op that arrives while a write is in flight, and writes the batch as the next log segment (log/<ns>/0007) with a create-only PUT. An op is acknowledged once its segment exists.
  • Versions are log positions. A path's version is the sequence number of its last write. It can't go backwards, and a delete is just another op. The bugs from part 3 can't happen, because there's no second idea of "current" to drift from.
  • Ops carry requirements and updates. "Path P is at version 7" and "path Q doesn't exist" are checked against the owner's index at commit time, the way Iceberg's REST catalog and Aurora DSQL's adjudicator work. A batch is one segment, so multi-path atomicity is free. A failed requirement becomes a typed conflict record in the log, not just an HTTP 409.
  • The log is the database. The index is derived from it, so the op log, audit and point-in-time restore come for free: restoring to sequence n means replaying to n.

Design B's commit: the owner batches ops that arrive while a PUT is in flight and writes them as the next segment with a create-only PUT. An old owner that wakes up and tries the same slot finds it taken, so it is fenced.

Fencing comes from the same create-only write. If an old owner pauses and comes back, its write targets a slot the new owner already created, and it loses. That's SlateDB's writer epoch and turbopuffer's broker from part 2. If a PUT's response is lost, the owner reads the slot back and checks whether its own (owner, epoch, batch) stamp is there, instead of guessing.

The storage interface shrinks to almost nothing: create a key only if it's absent, get, list, and delete for garbage collection. That's a deep module in Ousterhout's sense: a small interface over very different backends (S3, local disk with O_CREAT | O_EXCL, an in-memory store for tests and the simulator). ETags, the nonce frame from part 3, retries and how each backend spells "already exists" all stay behind it.

Scoring them

Criterion A: cache tiers A′: no Redis B: one owner, one log
Simple correctness model ❌ four version rules ➖ one, with tombstones ✅ one log; versions are positions
Multi-path atomic writes ❌ impossible with per-object CAS ❌ same ✅ one batch is one segment
Op log, rollback, provenance ❌ a second write per op ❌ same ✅ the log is the store
Write latency (one path) ➖ HEAD + PUT + ~5 Redis round trips ➖ HEAD + PUT ➖ batch window + one PUT
Write throughput ✅ any instance, any path ✅ same ➖ serialized per namespace
Read latency (hot) ✅ ~µs in L1, ~1 ms in Redis ➖ per-instance caches ➖ µs at the owner, local disk elsewhere
Disconnected field nodes ❌ needs Redis and S3 reachable ➖ still needs central CAS ✅ local log, upload on reconnect
Information hiding ❌ version rules leak across tiers ➖ ✅ one deep module behind a small API
Operations ➖ stateless, but Redis required ✅ stateless, S3 only ➖ S3 only, but routing and owner failover
Simulator ❌ correctness lives in Lua, Redis, S3 ➖ ✅ the owner is a pure state machine

What B costs

B wins on correctness and requirements. It isn't free, and the honest list of what I'd give up matters as much as the scores.

  1. Any instance can no longer write any path. Today, stateless instances write anything and S3 decides races. In B, writes for a namespace go to its owner, which means request routing, and seconds of write unavailability while a failed owner's lease expires. Every system in part 2's object-storage family pays this.
  2. A ceiling per namespace. Independent paths never contend in A. In B they share their namespace's commit stream. Group commit makes each commit carry many ops, but a hot namespace has a limit, and scaling out means more namespaces, not more writers.
  3. A shared warm cache. In A, one instance's fetch from S3 warms Redis for everyone. In B each instance warms its own cache, and a new one starts cold.
  4. Instant freshness everywhere. Pub/sub invalidation is near-instant (when it works). In B, instances other than the owner are as fresh as their last read of the log, unless they ask the owner.
  5. Familiarity. "A Redis cache in front of S3" is easy to explain. "Namespace owners with leases over a log on S3" is the same shape as SlateDB and turbopuffer, but fewer people have run one.

A few things look like losses and aren't. A's 9 MB write limit was headroom under Upstash's 10 MB request limit, the managed Redis I was using; Redis itself allows far larger values. Its listings can be incomplete, while the owner's index is complete. And today, if Redis is down at startup, the service is down.

The numbers

I measured the parts of B that the comparison depended on before deciding:

  • One create-only PUT of 4 KiB, p50: 26.5 ms on S3 Standard, 7.4 ms on S3 Express One Zone, 1.6 ms on a local SeaweedFS.
  • Group commit with a 10 ms batch window, acknowledged p50/p99: 44/83 ms on S3 Standard, 17/27 ms on Express. That's about the same as A's HEAD + PUT for one path, and much better for a write that touches several.
  • Serial commits per namespace: about 40 per second on S3 Standard and 138 on Express, each carrying a whole batch. That's well above the ~15 per second quoted in part 1, which came from a benchmark of a different operation, compare-and-swap on one object. Part 5 has the details.
  • Cold start at 1 million paths (load the checkpoint, replay the rest of the log): 1.09 s on S3 Standard, 0.72 s on Express.

The protocol is the same on every backend. Only the latency changes, so the batch window is a setting, not a code path.

What survives from A

Most of A's code goes. Some of the hardest-won parts don't:

  • The startup probe. CambiumDB refuses to start on a backend that doesn't enforce conditional writes. It caught LocalStack and an older SeaweedFS ignoring them. B depends on create-only writes even more than A did, so the probe carries over unchanged and grows into a conformance suite every backend has to pass.
  • Reading back after an ambiguous write. A's commit id becomes B's (owner, epoch, batch) stamp in every segment.
  • Auth and the HTTP/gRPC transports, unchanged.

The nonce frame from part 3 mostly doesn't survive. B never overwrites anything, and content-addressed blobs are supposed to have identical bytes. The lesson does: know exactly what your conditional write compares.

The roughly 250 lines of evict-and-recache logic and their 30-odd tests are gone too. That code was careful repair work for a problem the design created. It still paid for something: without writing it, I wouldn't have known which problem B had to make impossible.

Where it stands

The engine for B exists as a library with its own simulator: owners, group commit, requirements and conflict records, fencing, and recovery, all in a sans-IO core. The simulator runs that core in sweeps of a thousand seeds, with storage faults, crashes and a stale second owner, and any failing seed replays exactly. It isn't wired into the service yet, and owner leases and failover are the next slice. The design comparison and the engine design are in the repo if you want the details.