--- title: Afterkey sdk: static app_file: demo/index.html license: apache-2.0 short_description: A C++17 change feed for the current state of each key tags: - cpp - multi-agent - streaming - benchmarks colorFrom: gray colorTo: gray thumbnail: https://huggingface.co/spaces/pgg-engineering/afterkey/resolve/main/art/share.png --- # Afterkey Afterkey is a header-only C++17 library for dashboards and workers that need the current state of a producer. Each reader saves a revision number. On its next poll, it gets one row per key written since that revision, ordered by the key’s last write. The row holds the latest value or marks a deletion. ![After revision 1, key 3 is set to 20, key 7 is set to 11, and key 3 is deleted. The poll returns key 7 with value 11 at revision 3, then the deletion of key 3 at revision 4.](https://huggingface.co/spaces/pgg-engineering/afterkey/resolve/main/art/afterkey-register.svg) If a worker publishes its available slot count ten times between polls, the dashboard receives just the last count. [Open the two-reader demo](https://huggingface.co/spaces/pgg-engineering/afterkey) · [Download the C++17 kit (v0.1.0)](https://huggingface.co/spaces/pgg-engineering/afterkey/resolve/main/releases/afterkey-0.1.0.zip) · [Reproduce the benchmarks](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/bench/environment.md) ## Run the C++17 example The Space ships Afterkey 0.1.0. Its `since` call delivers every current row written after the reader’s saved revision. Download the kit and run the example with a C++17 compiler. ```sh curl -fsSLO https://huggingface.co/spaces/pgg-engineering/afterkey/resolve/main/releases/afterkey-0.1.0.zip unzip -q afterkey-0.1.0.zip cd afterkey-0.1.0 c++ -std=c++17 -O2 -Iinclude examples/quickstart.cpp -o /tmp/afterkey-demo /tmp/afterkey-demo ``` The reader last checked at revision 1. Key 3 is set to 20, key 7 is updated to 11, then key 3 is deleted. The poll returns two rows in revision order. The deletion supersedes the earlier value of 20. ```text 3 7 11 4 3 deleted ``` For bounded pages, the [newer GitHub kit](https://github.com/pgg898200-ship-it/streamsieve/tree/main/afterkey#read-a-bounded-page) adds `since_page`, which limits the number of rows delivered per call. For an existing service, copy [include/afterkey.hpp](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/include/afterkey.hpp) into its include path. The header has no third-party dependencies. Each callback receives a revision, a dense key and a borrowed pointer to the absolute value. A null pointer means deletion. Advance the cursor only after your consumer has successfully applied the entire batch. Retrying after a partial failure can redeliver rows, so apply values and tombstones idempotently. ## Which workload benefits? When writes concentrated on 16 of 1,048,576 populated keys, Index took **5.60 ns per operation** and a cursor-indexed append log took **11.45 ns**. When writes spread uniformly across 16,384 keys, the log was faster at **121.63 ns** against **421.46 ns** for Index. Each run used 16 staggered readers on a shared AMD EPYC 9V74 VM. One operation is a write or a poll, and one poll can deliver many rows. | Synthetic workload | Afterkey Index (ns/op) | Bounded Journal (ns/op) | Cursor log (ns/op) | `std::list` (ns/op) | | --- | ---: | ---: | ---: | ---: | | 1M populated keys, 16 hot | 5.60 | 20.47 | 11.45 | 6.42 | | 16K populated keys, 16 hot | 4.41 | 13.09 | 10.41 | 6.06 | | 16K populated keys, uniform writes | 421.46 | 134.77 | 121.63 | 546.34 | The table shows medians across five or nine trials. Timings include the callback and exclude initialization. With uniform writes, Journal’s contiguous traversal was faster than Index in this workload. CPU-time medians, sample ranges, the full-map scan and additional cases are in the [results CSV](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/bench/results.csv). With **4,096 independently lagging readers**, a second benchmark measured each synchronous catch-up call on one thread. For a moving hot set, p99 was **1.67 µs** for Index and **4.99 µs** for the cursor log. With uniform writes, p99 was **417.86 µs** and **75.92 µs** respectively. The timer includes the consuming callback. Queueing, network and producer time are excluded. [Latency harness](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/bench/fanout.cpp) · [Raw percentiles](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/bench/fanout.json) The public replays check exact ordered outputs. On the FreeInference session trace an hourly reader received 64 rows for 1,375 recorded completions. On the Paris Vélib’ trace an hourly reader received 895,673 rows for 5,143,865 recorded writes. At one-minute intervals only 94 Paris writes were superseded. Poll schedules were simulated. These counts describe the work a reader can skip. [Sources, checksums and replay commands](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/bench/replay.md) ## Contracts and costs | | Index | Journal | | --- | --- | --- | | Header | `afterkey.hpp` | `afterkey_journal.hpp` | | Write | O(1) worst-case bookkeeping | O(1) amortized, O(capacity) on compaction | | Read | O(1 + distinct keys touched since cursor) | Scans up to min(2 × capacity, writes since cursor) entries, plus a seek for older cursors | | Storage | Dense slots, two 32-bit neighbor indices per slot | Dense slots plus a bounded chronological key journal | Both implementations retain one current payload per live key and tombstones for erased keys. Their own storage is allocated at construction. Payloads and callbacks can allocate. Use an application-level dense ID mapping for strings or sparse IDs. Every put and erase advances the revision, including unchanged values and repeated erases. The index rejects future cursors, out-of-range keys and callback writes. Payloads must be nothrow-move-constructible and nothrow-destructible. Run reads and writes on one event loop or serialize access externally. Cursors belong to one live instance. On process restart generate a new instance identifier, replace client state, and replay from zero. A client expanding its subscription also needs a fresh snapshot of added keys. For audit trails, increments, commands and intermediate transitions, retain a separate event log. ## Integrations - The [StreamSieve example](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/examples/streamsieve.cpp) publishes absolute scores and checks that a dashboard and a slower observer both reconstruct the underlying native state. - [ZeroMQ catch-up](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/examples/zmq_catchup.cpp) serves independent filtered readers over REQ/REP, including tombstones and reconnects to the same server instance. Its topic masks filter after index traversal. Build instructions are in the example. The multi-topic slow-consumer case was described in [libzmq issue 1688](https://github.com/zeromq/libzmq/issues/1688). - [Randomized reference tests](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/tests/test_afterkey.cpp) exercise tombstones, callbacks, independent readers and historical cursors across compactions. Run `c++ -std=c++17 -O2 tests/test_afterkey.cpp -o /tmp/test-afterkey && /tmp/test-afterkey`. ## Prior art Kubernetes already moves changed nodes to the head of a generation-ordered list and stops its snapshot walk when it reaches an old generation. CouchDB provides a durable document changes feed with latest-change semantics. Chronicle Ring offers conflation for low-latency messaging. Afterkey packages this pattern as a small, in-process C++ component with dense storage, ordered tombstones and reproducible crossover measurements. The measured competitors above are in-process C++ implementations. [Kubernetes scheduler source](https://cos.googlesource.com/third_party/kubernetes/+/4f5c634fcfa498e3634c9b6b31cc4ca4c6a1203c/pkg/scheduler/backend/cache/cache.go) · [CouchDB changes feed](https://docs.couchdb.org/en/stable/api/database/changes.html) · [Chronicle Ring](https://chronicle.software/products/chronicle-ring) Afterkey source is licensed under [Apache-2.0](https://huggingface.co/spaces/pgg-engineering/afterkey/blob/main/LICENSE). Trace datasets retain their respective licenses.