Skip to content
← All work

Event ingestion gateway

Axiom

An HTTP gateway in Go that takes app events, answers in milliseconds and streams them in batches to a Rust append-only log. Load tested at 74,648 requests a second with a P99 under 10 ms.

  • Go
  • Rust
  • Protocol Buffers
  • FastHTTP
Axiom's request path (fasthttp, API key auth, JSON parse, Protobuf encode, enqueue, with 401, 400 and 503 exits) and its asynchronous delivery path (a 50,000-slot queue, 10 batch workers, 6 TCP senders and a Rust memory-mapped event log).
req/s in the peak 10 s window
74.6kreq/s in the peak 10 s window
P99 latency at that peak
9.60 msP99 latency at that peak
requests in 61 s, 0 failed
2.93Mrequests in 61 s, 0 failed
req/s average over the run
48.4kreq/s average over the run

Why I built it

Analytics and audit pipelines start with the same problem: many small JSON events arrive over HTTP, and the client wants an answer immediately. Axiom is an experiment in how far one machine can push that front door when the request path stays short and everything slow sits behind a queue.

What it does

  • Accepts POST /api/v1/events/ with an X-API-Key header on port 8000, bodies up to 1 MB.
  • Gives each event a ULID, checks its event type and encodes it as Protobuf.
  • Answers 201 with the event_id as soon as the event is queued, before it is stored.
  • Batches events and streams them over TCP to a separate Rust engine that appends them to a log on disk.
  • Pushes back with 503 and Retry-After: 5 when the queue is full, rather than letting memory grow.

How it works

The request passes three middlewares: recovery, a Zap logger, then auth, which looks the API key up in an in-memory map behind an RWMutex. The handler parses the body with bytedance/sonic and validates only the event type (1 to 64 characters of [A-Za-z0-9_-]). The event gets a ULID and is serialised into a Protobuf message taken from a sync.Pool, with tenant_id, id, event_type, timestamp and the original raw_body.

Enqueueing copies those bytes into a pooled buffer and does a non-blocking send on a bounded channel of 50,000 slots. If the send would block, the client gets 503; otherwise it gets 201 straight away.

Behind the queue, 10 batch workers collect up to 1,000 events or flush every 10 ms, whichever comes first. Six senders each hold a persistent TCP connection with TCP_NODELAY and write a whole batch with one vectored write (net.Buffers). Each event on the wire is framed as a 4-byte big-endian length followed by the Protobuf bytes. The sender then waits for a 1-byte ack, with a 5 s timeout.

The engine is a separate Rust service. It reads frames, batches them and appends each one as [len][crc32][data] to a 1 GB memory-mapped log, then sends the ack. On startup it runs a recovery scan over the log that checks every record against its CRC. On SIGINT or SIGTERM the gateway drains the queue and any open batches before it exits.

Bytes at each hop. Frame: a 4-byte big-endian length, then the Protobuf fields tenant_id, id as a ULID, event_type of 1 to 64 characters, timestamp and raw_body with the original JSON. Batch: up to 1,000 frames go out in one writev using net.Buffers with TCP_NODELAY, then the sender waits for a 1-byte ack with a 5 second timeout; an unreachable engine means the batch is dropped, so delivery is at-most-once. Record: the engine appends length, crc32 and data to a 1 GB memory-mapped log, checks CRCs in a recovery scan on startup and acks after the mmap write, before any fsync.

Why this stack

The gateway is mostly plumbing between stages, and Go's channels and goroutines are that plumbing. fasthttp reuses its request objects, sonic parses JSON quickly, and pooling the Protobuf messages and byte buffers keeps the remaining allocations down, so the garbage collector has less to do at high request rates.

Protobuf over a raw TCP stream keeps the wire format small and the framing trivial: a length prefix and the bytes. Plain length-prefixed frames let up to 1,000 events go out in one syscall, and the ack protocol is one byte that the gateway fully controls.

The hard part

Deciding what the 201 promises. Answering before storage is what makes the latency numbers possible, but it moves the failure modes behind the response. Most of the design is about keeping that gap bounded: a fixed-size queue that pushes back with 503 instead of growing, batches capped by both size and time, and a graceful shutdown that drains what is already accepted.

Results

A k6 stress test (scripts/stress_test.js) ramped from 1k toward a 100k req/s target against localhost on a single machine. It sent 2,933,632 requests in about 61 s with none failed, an average of 48.4k req/s. The busiest 10-second window reached 74,648 req/s with a P95 of 5.26 ms and a P99 of 9.60 ms.

Latency here is time to the 201, not time to storage. The load generator and the gateway shared one machine, so these numbers show the gateway's ceiling on that box, not a networked deployment.

What I'd do differently

  • Delivery is at-most-once today. If the engine is unreachable, a batch is dropped and not retried. A retry with backoff and a bounded spill buffer would make it at-least-once.
  • The engine acks after the mmap write, not after fsync. A crash can lose acknowledged events still in the page cache. A periodic or per-batch fsync before the ack is the trade-off to measure.
  • Rate limiting is in the README but not in the code. Per-key limits belong in the auth middleware.
  • Only the event type is validated. A schema per event type would catch bad payloads at the edge.
  • API keys are two hardcoded test keys. They should come from a store with rotation.
  • The Unix domain socket transport exists but isn't wired in. For a gateway and engine on the same host it would skip the TCP stack.