atproto relay implementation in zig zlay.waow.tech
Zig 99.4%
Dockerfile 0.5%
Other 0.1%
46 2 0

Clone this repository

https://tangled.org/zzstoatzz.io/zlay https://tangled.org/did:plc:xbtmt2zjwlrfegqvch7fboei/zlay
git@tangled.org:zzstoatzz.io/zlay git@tangled.org:did:plc:xbtmt2zjwlrfegqvch7fboei/zlay

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

zlay#

an AT Protocol relay in zig. subscribes to every PDS on the network, verifies commit signatures, and serves the merged event stream to downstream consumers via com.atproto.sync.subscribeRepos.

live instance: zlay.waow.techmetrics

design#

  • direct PDS crawl — the bootstrap relay (bsky.network) is called once at startup for the host list via listHosts, then all data flows directly from each PDS. no fan-out relay in between.

  • optimistic signature validation — on signing key cache miss, the frame passes through immediately and the DID is queued for background resolution. the first commit from an unknown account is unvalidated; all subsequent commits are verified against the cached key. >99.9% cache hit rate after warmup.

  • inline collection index — indexes (DID, collection) pairs directly in the event processing pipeline using RocksDB with two column families: rbc for collection-to-DID lookups and cbr for DID-to-collection cleanup. serves listReposByCollection from the relay process — no sidecar. the index design draws on fig's work on lightrail, which uses adjacent keys from CAR slices to enumerate collections.

  • one OS thread per PDS — predictable memory, no garbage collector. ~2,750 threads is fine; most are blocked on websocket reads. thread stacks are set to 2 MB (zig's default is 16 MB).

endpoints#

endpoint method
com.atproto.sync.subscribeRepos WebSocket
com.atproto.sync.listRepos GET
com.atproto.sync.getRepoStatus GET
com.atproto.sync.getLatestCommit GET
com.atproto.sync.listReposByCollection GET
com.atproto.sync.listHosts GET
com.atproto.sync.requestCrawl POST

getRepo is not implemented — the relay does not serve full repository exports.

dependencies#

dependency purpose
zat AT Protocol primitives (CBOR, CAR, signatures, DID resolution)
websocket.zig WebSocket client/server
pg.zig PostgreSQL driver
rocksdb-zig RocksDB bindings

build#

requires zig 0.15 and a C/C++ toolchain (for RocksDB).

zig build                          # build
zig build test                     # run tests
zig build -Doptimize=ReleaseSafe   # release build

configuration#

variable default description
RELAY_PORT 3000 WebSocket firehose port
RELAY_HTTP_PORT 3001 HTTP API port
RELAY_UPSTREAM bsky.network bootstrap relay for initial host list
RELAY_DATA_DIR data/events event log storage
RELAY_RETENTION_HOURS 72 event retention window
COLLECTION_INDEX_DIR data/collection-index RocksDB collection index path
DATABASE_URL PostgreSQL connection string
RELAY_ADMIN_PASSWORD bearer token for admin endpoints

see docs/deployment.md for production deployment and docs/backfill.md for collection index backfill.

numbers#

metric value
connected PDS hosts ~2,750
memory ~2.9 GiB
throughput ~600 events/sec typical