~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zlay

atproto relay in zig · mirror of https://tangled.org/zat.dev/zlay

0
0
138 lines · 6.9 KB Raw History

zlay collection index on zkv

The current decision and revised measurements are in the audit report. The observations below describe the initial prototype and are retained as history.

The pilot replaces the collection index only. The relay event log and Postgres are outside this experiment. All workloads used local scratch directories and synthetic DIDs. The follow-up write-path investigation also replays a finite, already captured atproto-bench fixture. No relay was started, no upstream was queried, and no production or heavypad resources were used.

build and footprint

Zig 0.17.0 on an Apple M5 Pro. Linux x86-64 GNU target, zio, ReleaseSafe. The first build in each worktree reused downloaded packages and the host Zig global cache. The RocksDB build overlapped with some native pilot compilation. Treat wall times as first observations.

BuildFirst build wall timeELF file bytesELF loadable file bytes
RocksDB at f3346e9160.47 s165,469,36815,193,404
Initial zkv port42.14 s19,017,6804,192,972
zkv with frame and page batches—19,037,6244,197,532

Loadable bytes sum ELF PT_LOAD file sizes. They are neither stripped artifact size nor resident memory. The workload processes used about 12.5 MiB peak RSS with RocksDB and 5.7–6.4 MiB with zkv. These are index-only processes, separate from the relay binary measurement.

offline index workload

The benchmark inserts 8,192 pairs across 4,096 DIDs and two collections, repeats 4,096 existing pairs, verifies 3,000 cursor pages of 100 DIDs, and removes all pairs for 512 DIDs. It checks the reverse index for every DID afterward. Each row is the median of three sequential passes, in microseconds per listed operation. In batch mode the add and duplicate columns are amortized per pair. The delete workload still removes one DID per call.

VariantNew pairDuplicate pairPage of 100Remove all for one DID
RocksDB, default WAL4.985.4819.0311.05
RocksDB, synced WAL36.9837.7818.6442.98
zkv, one commit per pair464.77454.677.16453.05
zkv, duplicate elision461.441.045.11449.98
RocksDB, default WAL, batch 641.413.0119.0111.17
RocksDB, synced WAL, batch 642.213.9018.8242.51
zkv, batch 64 and duplicate elision8.991.104.72489.24

The current zlay RocksDB binding leaves sync disabled. The synced control turns it on. zkv uses its standard DirStore commit and explicit maintenance after 256 unflushed segments or 4 MiB. The engines have different publication and sync paths. Batch boundaries match in the 64-pair runs. The zkv duplicate check is intentional application behavior and skips writes only when both index keys exist.

One short concurrent test starts 1,000 verified page reads and 1,024 single-pair writes together. The median per-run scan p99 was 13.0 µs for zkv, 21.5 µs for default RocksDB and 25.3 µs for synced RocksDB. Write p99 was 555 µs, 9.5 µs and 51.7 µs respectively. The loops run at their own speeds and can finish at different times. This is a contention smoke test, with a fixed arrival-rate test still needed to characterize backlog and maintenance tails.

edge cases and decision

The pilot uses two key prefixes in one database and commits both directions together. It rejects an existing RocksDB directory. Tests cover cursor paging when one DID is a prefix of another, idempotent writes, concurrent producers, deletion, maintenance and reopen, and discarding a page or replacement after an invalid key. Backfill now commits a page before advancing its Postgres cursor. Resync replaces all collections for a DID in one batch.

Keep production on RocksDB. The smaller build and fast cursor reads justify the offline pilot. The current zkv integration still puts one file publication per novel live pair on the critical path when a frame contributes one pair. The small synthetic data set and coarse mutex do not establish headroom for a live relay.

Before considering a rollout:

  1. zlay: route live producers through a bounded index owner and coalesce writes across frames without losing acknowledgment or ordering. Keep duplicate elision, page batches, and atomic resync in the adapter. Replay a bounded local fixture at fixed arrival rates and measure relay lag, queue depth, and read/write tails through maintenance. Include long scans and tombstone bursts. The pilot uses the threaded pool I/O for the index and its mutex. Test executor stalls with the zio relay path before sharing it with live ingestion or API fibers.
  2. zkv: measure the local commit path by phase. Evaluate an append-and-sync local journal or group-commit adapter only if batching still misses zlay's latency or throughput target. Preserve acknowledged-batch recovery and fencing. Avoid changing default object-store semantics for this integration.
  3. zkv: add local crash coverage around file publication, including the first commit into a newly created directory and an ambiguous failure after linking a segment. DirStore syncs the staged file and its immediate parent, so the ordering of newly created parent directories deserves explicit validation.
  4. zkv: verify a local read-view contract before replacing the coarse mutex. Can independent readers see a well-defined committed generation while maintenance runs, and what happens when collection races a scan? If the existing refresh and retry contract cannot serve the API, consider a pinned read view or bounded retention.
  5. zlay: validate migration from an offline export or a rebuild with generation-specific progress. A new index must not inherit Postgres backfill cursors that describe the old index. Verify both directions, remove/recreate ordering, and restart after kill during commit or maintenance. Keep a separate directory and a RocksDB rollback path.

Stop if bounded batching and explicit recovery cannot meet the same service contract without changing zkv's durability or shifting unbounded work into zlay's relay threads.

reproduce

The zkv dependency is the sibling checkout at eca4fd1. The RocksDB benchmark lives on codex/zkv-pilot. The zkv pilot is codex/zlay-zkv. For the first two zkv variants, build at cbceb11 and d2ea8bc respectively.

zig build -Doptimize=ReleaseSafe -Dbackend=zio -Dtarget=x86_64-linux-gnu
zig build index-bench -Doptimize=ReleaseSafe
scratch=$(mktemp -d)
mkdir "$scratch/index"
zig-out/bin/index-bench "$scratch/index" --batch64 --mixed

Use a fresh scratch directory per invocation. Add --sync to the RocksDB driver for the synced WAL control. Omit --batch64 for single-pair writes and --mixed for the single-threaded trace.

Single-pair samples · Batch samples · Concurrent samples