Two Object-Store Primitives Are Enough for Concurrent Writers

Itzma has no server. Any process that can reach the bucket can write, and any process can query. People hear that and ask who decides the order of writes. Nobody does. The bucket does, through two operations it already has.

The first is put-if-absent: create this key, and fail if it exists. The second is conditional update: overwrite this object only if it still has the contents I last read. S3 and in-memory stores have both. Everything else in the write path is built from those.

The log

A write is a batch of upserts or deletes. Validation runs first, so one bad document refuses the whole call before any request goes out. Then the batch becomes one object, written at the next sequence number with put-if-absent.

If two writers pick the same number, one succeeds and the other gets “already exists”. That loser doesn’t need to know who won. It looks past that entry and tries the next number. After 32 lost races in a row it stops and returns WriteContention, with nothing written.

So the order of writes is the order of the keys. Two concurrent writers get distinct sequence numbers, and for the same document id the higher one wins. The log stays contiguous, which is what lets a reader promise it sees entries 1 through n and never a later entry without an earlier one.

One object that changes

A log alone would make every query read every entry. So there’s a manifest that lists the live segments, how far the log has been folded into them, and which segments have an index. It’s the only object that’s ever overwritten, and only by conditional update.

A compactor reads the manifest, builds a new segment, writes it, then asks to swap in a manifest that includes it. If someone else changed the manifest in the meantime, the swap fails, and the compactor retries on the new contents. Writers never read the manifest at all, so they never wait on a compaction.

Making a repeat harmless

Compaction can crash at any point, and two compactors can run at once. The answer is to make the outputs boring. A segment’s bytes depend only on its range of log entries, the schema and the format level. Rerun the work and the same bytes come out. When an object already exists at a key, the library compares bytes before reusing it, and treats a mismatch as corruption instead of picking one.

That’s why a query returns the same documents before, during and after a compaction, even when a compactor is killed or two race. The tests check it by failing every operation in turn, once as a plain error and once as a lost acknowledgement, then restarting on a fresh handle and comparing against a naive replay.

Where this stops being enough

Put-if-absent settles who gets a sequence number. It can’t tell a retry from a new write. If a put fails after the request was sent, the batch may or may not have landed, and a plain retry can append it twice. For upserts and deletes the end state is usually the same. It isn’t if a newer write to the same id got in between.

Keyed writes narrow that. The key goes in the log entry’s header, and a retry looks for it from a known sequence number on, returning the original sequence number if it finds it. That still doesn’t stop two submits of one key running at the same time, and it doesn’t help if garbage collection has already fenced the original. I’d rather list those than pretend the problem is closed.

Local disk is the other edge. LocalFileSystem has no conditional update, so a namespace there can be created, written and queried, but compaction and indexing fail. A store counts as supported when it passes a conformance suite that checks these two operations (and a few more behaviours Itzma relies on), not just because it implements ObjectStore.

Who runs the maintenance

The log needs folding and old segments need deleting, and with no server nobody is obviously in charge. The library starts no background task in Rust. In Python it starts a loop by default, and the processes that share a namespace agree on one maintainer through a lease. The lease is one object in the bucket, renewed every 15 seconds and taken over by another process after 60 seconds of silence.

The lease decides nothing about correctness. Two maintainers at once were always safe, because every output is reproducible and every swap is conditional. It only stops processes from doing the same work twice. I like keeping it that way: a lease that expires at the wrong moment costs some wasted work, not a wrong answer.