Measurements for carrying the AT Protocol on Dagr.
README.md

atproto-dagr #

Measurements for carrying the AT Protocol on Dagr. The plan and the results live in RethinkingDagr/spec/46-atproto-on-dagr.md (https://codeberg.org/mzaks/dagr/src/branch/main/spec/46-atproto-on-dagr.md); this repo holds the capture tools, the harnesses, and a Dagr Jetstream you can run ("Try it" below). Like otlp-dagr, it depends on Dagr; Dagr does not depend on it.

The blog series AT Protocol on Dagr tells the story in twelve short posts, from why to try it to what is still open.

Why Dagr for the AT Protocol #

The same data, carried by Dagr instead of DAG-CBOR and JSON:

  • Smaller. A like, repost or follow as Dagr is 0.30–0.42× its DAG-CBOR, a post 0.66×. A Jetstream event is 200 B on the wire uncompressed, 30% less than the 287 B Jetstream sends after zstd. A repository exports at 47% of its CAR.
  • Cheaper to serve. On live firehose traffic a Dagr Jetstream spends 6.8 µs per event, against 35.7 µs for Jetstream's path (5.3×): the records are converted once and then forwarded, never rendered or compressed. Replaying the archive costs about 0.1 µs per event, where re-encoding and compressing JSON costs 26 µs (up to 260×). A collection subscription reads only that collection's blocks (up to 165×).
  • Cheaper to consume. A client reads what it needs straight from the bytes: a like's subject in 0.19 µs, against 3–5 µs for JSON (17–28×).
  • Faster where trust is checked. Dagr-native firehose frames are hashed in place and verify 1.8–2× faster. A relay's inversion check runs 3.4× faster. A PDS applies a write's ops 2.9× faster, and a whole write, from the PDS through the consumer, is 4.5× cheaper with the signature set aside.

Without giving anything up:

  • Lossless. All 875,701 records of an hour of the firehose convert to Dagr and back to their exact DAG-CBOR bytes, so every CID is kept.
  • Drop-in. Jetstream's own v1 and v2 endpoints are served from the Dagr archive, so unmodified Jetstream clients work and get byte-identical records.
  • Durable. The service survives crashes. Killed again and again mid-stream, it kept every event exactly once and its consumers resumed without a gap.
  • Checked byte for byte. Go, Rust and Python write the same bytes. Every result here is gated on real network data, not synthetic samples.
  • Easy to look at. Every record lexicon, the firehose frames and the Jetstream events are plain Dagr schemas. Browse them in your browser with dagr ui --open ("Try it" below).

The measurements behind each number are in spec/46.

An app that sends Dagr over the AT Protocol today, with no protocol change: examples/wrapped-app (blog Part 12).

Try it: a Dagr Jetstream on the live network #

This runs the Dagr Jetstream of spec/46 §26–§32 against the real Bluesky firehose:

  • it subscribes to a relay and converts every record to Dagr as it arrives;
  • it archives the events in sealed, indexed segments and resumes from them after a restart;
  • it serves them three ways: as Dagr, and on Jetstream's own v1 and v2 endpoints, so an existing Jetstream client works unchanged.

You need Python ≥ 3.9, Go (the module asks for 1.26.10; with GOTOOLCHAIN=auto, the default, Go fetches it itself) and git. No corpus or capture is needed.

pip install 'dagr-cli>=2026.11.9'          # the Dagr code generators (or: uv tool install dagr-cli)
git clone https://tangled.org/mzaks.bsky.social/atproto-dagr && cd atproto-dagr
scripts/generate.sh                         # generates the Go code the programs build on (not committed)

cd go
go run ./firehose -serve -listen 127.0.0.1:6008 -bridge-dir ../data

Browse the schemas. Each schema directory opens in Dagr's web UI, where you can read every node, union and enum and the lexicon it came from. Run it from the repository root (no generated code needed):

(cd schema/build && dagr ui --open)       # the 21 app.bsky / com.atproto record lexicons, plus GenericRecord
(cd firehose/schema && dagr ui --open)    # firehose frames, the commit, Jetstream events, the archive index and log
(cd appview/schema && dagr ui --open)     # the AppView's view graph and partitions

The service prints listening on 127.0.0.1:6008. It stores its state in ../data and stops cleanly on Ctrl-C. Run it again and it resumes where it stopped: the relay from the last message it stored, the cursors without a gap. curl 127.0.0.1:6008/metrics shows how far it is, as JSON.

Read it from TypeScript. ts/ is a client of the Dagr stream in about 60 lines: every new post as it is made, with its author and record key, and once a minute how many events went by. Its readers are generated by dagr build (scripts/generate.sh runs it). With the service running, you need Node.js 22 or later:

cd ts && npm install
npm run feed               # the live feed; its argument is the service, default http://127.0.0.1:6008
npm run feed -- --ws       # the same, over WebSocket: /dagr/subscribe speaks both
npm run feed -- --sink     # the stream as a DataSink (/dagr/sink, JetWire); add --ws for WebSocket
npm run verify             # 2,000 posts from the Dagr stream == the service's Jetstream JSON
npm run bench              # reading the same events as Dagr and as JSON (spec/46 §35)
npm run bench:transport    # bare frames and the JetWire sink, over HTTP and WebSocket, and JSON (§35.1, §35.2)

Left running, the archive grows by roughly 4–5 GB a day of firehose. Add -retain 72h (or -retain-segments N) to delete old segments; a client whose cursor is older than what is kept gets Jetstream's answer (v2 CursorTooOld for a seq, an OutdatedCursor notice for a timestamp) or starts at the oldest event kept. A restart reads only the segments' indexes, so it takes well under a second however much is stored.

Read it as Dagr. dagrtail follows the Dagr stream and prints one line per event. It reads each event in place, with the generated readers: no JSON, and nothing decoded that it does not print.

go run ./dagrtail -host 127.0.0.1:6008                                    # everything, from the live tip
go run ./dagrtail -host 127.0.0.1:6008 -collections app.bsky.feed.like    # likes, with the post each one likes
go run ./dagrtail -host 127.0.0.1:6008 -cursor 0                          # replay from the first event stored

The stream itself is GET /dagr/subscribe?cursor=N&collections=a,b: each event is a LEB128 length followed by a JetEvent, a header-less packed Dagr graph (schema/jetstream.py). Its record is the record's own Dagr graph, its lexicon's (schema/bsky_d7_all.py) or a GenericRecord, and its CID is the hash of those bytes.

Or point any Jetstream client at it. These are Jetstream's real endpoints, framed by Jetstream's own encoders (§31): /subscribe (v1) and /xrpc/network.bsky.jetstream.subscribeEvents (v2, with its dictionary-zstd option).

websocat 'ws://127.0.0.1:6008/subscribe?wantedCollections=app.bsky.feed.post'   # v1 JSON, as from Jetstream
// the official Jetstream Go client, unchanged: github.com/bluesky-social/jetstream
c, err := jetstream.Subscribe("http://127.0.0.1:6008",
	jetstream.WithCollections([]string{"app.bsky.feed.post"}))
if err != nil {
	log.Fatal(err)
}
for batch, err := range c.Events(ctx) {
	if err != nil {
		continue
	}
	for _, e := range batch.Events() {
		fmt.Println(e.Seq, e.DID, e.Commit.Collection, e.Commit.Rkey)
	}
}

What to expect:

  • Converting a record to Dagr costs about 1.4 µs, against 2.2 µs for Jetstream to render it as JSON.
  • A Dagr client reads an event in about 0.2 µs, where a JSON client spends 3–5 µs.
  • A Dagr event is about 200 B raw, against 287 B for Jetstream's compressed JSON.
  • Converted back, every record gives its original DAG-CBOR bytes and CID (§30.1).

Measurements and gates: spec/46 §26 (the server), §28 (the converter), §29 (the bridge), §31 (the Jetstream endpoints), §32 (restarts), §32.1 (retention, restarts on a large archive).

Not yet: Jetstream's archive download APIs (planSnapshot / getSegment, the client's WithAfterSeq backfill; use a cursor instead) and v1's options_update.

Step 0 — corpus + DAG-CBOR baseline #

python3 -m venv .venv && .venv/bin/pip install websockets cbor2 zstandard

# capture (resumes across disconnects; run under caffeinate on a laptop)
.venv/bin/python capture/ws_capture.py firehose \
    wss://relay1.us-east.bsky.network/xrpc/com.atproto.sync.subscribeRepos corpus/firehose-1h.frames 3600
.venv/bin/python capture/ws_capture.py jetstream \
    wss://jetstream2.us-east.bsky.network/subscribe corpus/jetstream-1h.frames 3600

# full repos: named DIDs, or a sample of DIDs seen in a capture
.venv/bin/python capture/fetch_repos.py corpus/repos did:plc:…
.venv/bin/python capture/fetch_repos.py corpus/repos --sample 30 corpus/firehose-1h.frames

# split into a flat corpus (records / MST nodes / commits / frames), print per-collection stats
.venv/bin/python analysis/corpus.py corpus/c --repos corpus/repos --firehose corpus/firehose-1h.frames

# baselines
.venv/bin/python analysis/sizes.py corpus/c            # raw / zstd stream / zstd + dictionary
(cd go && go run ./baseline ../corpus/c)               # indigo cbor-gen decode
(cd rust/baseline && cargo run --release -- ../../corpus/c)   # serde_ipld_dagcbor + atrium-api

corpus/ and vendor/ (the lexicons, the canonical bsky lexicons and the spec site, cloned) are not committed.

# Jetstream on Dagr (spec/46 §8, §16): sizes, round-trip gated; --out keeps the events for §26's gate 2
.venv/bin/python analysis/jetstream_dagr.py corpus/jetstream-1h.frames ~/dev/RethinkingDagr \
    vendor/jetstream/internal/subscribe/dictionaries/{legacy_subscribe,subscribe_events_20260811}.zdict \
    --out corpus/jetstream-dagr-generic.bin

# a Dagr Jetstream (spec/46 §26): the firehose's records as Dagr (GenericRecord where no lexicon
# covers one), then server + JetLog stream + client against Jetstream's own encoder, three gates
.venv/bin/python analysis/frame_records.py corpus/firehose-1h.frames 0 corpus/frame-records-1h.map ~/dev/RethinkingDagr
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -jetstream 100000 \
    -jet-events ../corpus/jetstream-dagr-generic.bin \
    -jet-json-dict ../vendor/jetstream/internal/subscribe/dictionaries/legacy_subscribe.zdict \
    -jet-dagr-dict ../corpus/jetevent.zdict ../corpus/firehose-1h.frames)   # + the archive vs Jetstream's segments (§26.2)

# the Go record converter (spec/46 §28): generate it, then gate it on every record of the hour
PYTHONPATH=~/dev/RethinkingDagr python3 -m dagr.importers.lexicon_convert schema/bsky_d7_all.py \
    --generic schema/jetstream.py --go schema/build/conv --go-gen atproto
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -convert-all ../corpus/firehose-1h.frames)

# the live bridge (spec/46 §29): the real firehose in, a Dagr Jetstream out, for N seconds
(cd go && go run ./firehose -bridge 600 -bridge-dir ../corpus/bridge \
    -jet-json-dict ../vendor/jetstream/internal/subscribe/dictionaries/legacy_subscribe.zdict)

# the relay role's block typing, gated and timed (spec/46 §29.1); Dagr records back to DAG-CBOR (§30)
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -relay-bench 50000 ../corpus/firehose-1h.frames)
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -reverse-all ../corpus/firehose-1h.frames)

# the Jetstream-compatible face (spec/46 §31): v1 + v2 endpoints, gated with Jetstream's own client
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -jetcompat 100000 ../corpus/firehose-1h.frames)

# the service (spec/46 §32): resumes from its store, serves Dagr + Jetstream v1/v2, /health, /metrics
(cd go && go run ./firehose -serve -bridge-dir ../corpus/service -listen 127.0.0.1:6008)
# its gate: a fake relay, crash points and SIGKILLs, consumers reconnecting through every crash
(cd go && go run ./firehose -restart-test 100000 -restart-kills 8 -bridge-dir /tmp/dagr-svc ../corpus/firehose-1h.frames)
# retention and fast restarts (spec/46 §32.1): the retention gate; a restart timed on a copy of an
# archive, before and after -reindex (which gives segments written before 2026-10-26 time ranges)
(cd go && go run ./firehose -retention-test 60000 -bridge-dir /tmp/dagr-ret ../corpus/firehose-1h.frames)
(cd go && go run ./firehose -store-bench ../corpus/bridge-soak -bridge-dir /tmp/dagr-store)
(cd go && go run ./firehose -reindex ../data)

# the same experiment on Linux, on an EC2 machine next to the relay: aws/README.md
# (aws/bin/up, deploy, run-live, fetch, down)

# an AppView on the live Dagr Jetstream (spec/46 §33): with a service running (-serve above),
# catch up on its archive from cursor 0, follow live with queries interleaved, then gate the
# answers against a batch rebuild of the same cursors
(cd go && go run ./appview -live http://127.0.0.1:6008 -live-from 0 -live-secs 3600 -live-dir /tmp/appview-live)
# its logs (spec/46 §33.1–§33.3): sync calls timed; small appends under a saturated disk, write(2)
# against a mapping; the service's -wal-sync full | barrier | async (async: synced off the publishing path)
(cd go && go run ./appview -syncbench /tmp/syncbench && go run ./appview -writebench /tmp/writebench)
(cd go && go run ./firehose -serve -wal-sync async -bridge-dir ../corpus/service -listen 127.0.0.1:6008)

# one write end to end (spec/46 §27): PDS, relay, Jetstream, consumer, CBOR + JSON vs Dagr
(cd go && go run ./firehose -record-map ../corpus/frame-records-1h.map -e2e 100000 \
    -jet-json-dict ../vendor/jetstream/internal/subscribe/dictionaries/legacy_subscribe.zdict ../corpus/firehose-1h.frames)

corpus/jetevent.zdict is a zstd dictionary trained (Python zstandard.train_dictionary, 112,640 B, the size of Jetstream's v1 dictionary) on the last 10% of corpus/jetstream-dagr-generic.bin.

Schemas #

schema/bsky.py and schema/bsky_d7_all.py are generated from the lexicons in vendor/ by Dagr's lexicon importer; never edit them by hand. Both read and extend the one code lock, schema/lexicon.lock.json (spec/46 §3.3), which pins every union, enum and field position: commit it with them. Regenerating from unchanged lexicons gives the same bytes.

LEX="python3 -m dagr.importers.lexicon vendor/atproto/lexicons vendor/bsky/lexicons \
     --all-records --prefix app.bsky --prefix com.atproto --lock schema/lexicon.lock.json"
export PYTHONPATH=~/dev/RethinkingDagr
eval "$LEX -o schema/bsky.py"                                                  # strings: the baseline
eval "$LEX --typed-formats cid,datetime,did,at-uri,nsid,mime,language -o schema/bsky_d7_all.py"   # the one in use
(cd schema/build && python3 -m dagr.cli build)                                 # Go packages → gen/go
# the lexicon helpers (spec/46 §21: D8 / D10 / D11), Go and Rust, next to gen/
python3 -m dagr.importers.lexicon_helpers schema/bsky_d7_all.py \
    --go schema/build/lexview/go --go-gen atproto \
    --rust schema/build/lexview/rust --rust-gen atproto --rust-gen-path ../../gen/rust
printf '\nreplace atproto => ../../gen/go\n' >> schema/build/lexview/go/go.mod
  • bsky_d7_all.py is the schema everything runs on: cid, datetime, did and at-uri are typed (each a {typed, string} union, so any string round-trips; spec/46 §3.2, D7), and NSIDs (an at-uri's collection too), blob MIME types and languages are knownValues unions whose cases come from the lexicons or ISO 639-1 (spec/46 §18). schema/build builds it for Go (go/go.mod maps package atproto to gen/go), and schema/jetstream.py (hand-written: the JetEvent graph) imports it.
  • bsky.py keeps every format a string. It is the baseline analysis/convert.py measures by default (--schema bsky_d7_all for the typed one) and has no build of its own.
path what
capture/ws_capture.py verbatim WebSocket capture, u64 time_ns + u32 len + bytes per frame
capture/fetch_repos.py DID → PDS → com.atproto.sync.getRepo CAR
analysis/corpus.py CAR / frame splitter, block classifier, $type-vs-collection check
analysis/sizes.py size baseline per collection
analysis/convert.py DAG-CBOR → Dagr → data model, the step-2 gate; --out writes the converted records
analysis/jetstream_dagr.py Jetstream events re-encoded as Dagr JetEvents, round-trip gated, sized against Jetstream's JSON
go/baseline indigo decode baseline
rust/baseline Rust decode baseline
# The firehose as a lossless Dagr transport (spec/46 §20)
.venv/bin/python analysis/firehose_probe.py corpus/firehose-1h.frames 20000   # how much is recomputable CIDs
(cd firehose/schema && python3 -m dagr.cli build)                             # the Frame graph → Go
python3 firehose/schema/nsids.py                                              # go/firehose/nsids.go
(cd go && go run ./firehose ../corpus/firehose-1h.frames)                     # every level, rebuilt and compared
# Dagr-native repositories (spec/46 §22) and the relay's inversion check (§23)
.venv/bin/python analysis/repo_records.py corpus/repos corpus/native ~/dev/RethinkingDagr   # records → Dagr
(cd go && go run ./firehose -repos ../corpus/repos -records ../corpus/native)              # build, gate, measure
(cd go && go run ./firehose -invert 20000 ../corpus/firehose-1h.frames)                    # indigo / CBOR / Dagr

go/dmst is a fork of indigo's atproto/repo/mst (MIT / Apache 2.0) whose node encoding is a codec.