WRITE · TRACE · UNDERSTAND

Project: apply atomic versioned index updates and revocations

State has tenant,integer version,documents mapping and seen event-digest mapping.

Read lesson: Project: apply atomic versioned index updates and revocations →

Your experiment

State has tenant,integer version,documents mapping and seen event-digest mapping. Require expected_version==state.version. Work on a deep copy. Events contain tenant,event,id,positive integer revision,op=upsert/delete; upsert also needs string text and list of string roles. Reject unknown event fields and cross-tenant events. Canonical JSON SHA-256 identifies an event: identical event replay is ignored, changed replay rejected. Every new document revision must exceed the previous revision, including tombstones. Store revision,deleted,text,sorted unique roles; deletion clears text/roles but retains revision. Advance state version once if any new event applied. Errors leave the original state unchanged.

Python runs in your browser. Your draft stays on this device.

What to look for

Review replay identities, CAS retries, ACL revocation and stale resurrection. A content digest is a consistency key, not approval of a document's instructions. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: apply atomic versioned index updates and revocations works

The idea

A search index is a copy of documents that keep changing: they are edited, their permissions change, and they are deleted. update_index applies a batch of such events so that the index can never end up half updated or move backwards.

Four rules do the work. The caller must name the version it read, and a mismatch is rejected, which is compare-and-swap. All changes go into a deep copy that is returned only if every event is valid, so a failure leaves the original untouched. Each event is fingerprinted with SHA-256 over canonical JSON: an exact replay is ignored, and a replay with different content is an error. A deletion leaves a tombstone that keeps its revision number, so an older upsert cannot bring the document back.

How it works

  1. Reject the call unless expected_version equals state['version'], then deep-copy the state into candidate.
  2. Validate each event: known fields only, the same tenant, non-empty ids, an op of upsert or delete, a positive integer revision, and text plus string roles for an upsert.
  3. Hash the event's canonical JSON and look its event id up in seen: skip on an identical digest, raise on a different one.
  4. Require the revision to exceed the stored revision of that document, including when the stored entry is a tombstone.
  5. Store revision, deleted, text and sorted unique roles; a delete clears the text and roles but keeps the revision.
  6. Record the digest, and after the loop raise version by one if anything new was applied.

What it costs

Time
O(state + batch)
Space
O(state)

The deep copy costs the size of the whole state on every call, a simple way to get atomicity; real indexes get the same effect with write-ahead logs or immutable segments.

Where it is used

  • Elasticsearch gives each document a _seq_no and _primary_term, and a write sent with if_seq_no fails if the document changed in between.
  • Apache Cassandra records deletes as tombstones so that replicas still holding older data cannot bring the deleted value back.
  • Amazon DynamoDB supports conditional writes that succeed only if an attribute, such as a version number, still has the expected value.

Watch out for

  • Editing state in place: an error halfway through the batch then leaves some events applied and others not.
  • Removing a deleted document's entry entirely: without the tombstone's revision, a late event carrying an old revision recreates it.
  • Hashing str(event) or JSON with unsorted keys, so one event can produce two digests and an honest retry looks like a conflict.