Engineering · Sep 2025 – now
Nerve
A real-time crypto exchange, built layer by layer.
- Role
- Solo
- Status
- Running locally · deploying to nerve.sameerkr.dev
- Services
- 12
- Built in
- ~1 month, 17 h days
Overview
A microservice exchange: an in-memory Go matching engine with a write-ahead log, an order book with limit/market orders, cancels and modifies, market-making bots, OHLCV candles in three cache tiers, and live depth, ticker and order events streamed over WebSockets.
Why it exists
I wanted to know how an exchange really works, so I built every layer myself, without AI coding tools. Order matching and performance engineering were new to me; I learned them while building.
Product film
How it works
- 1
The client hits an Express API gateway, which validates with Zod and calls the order service over gRPC. The order service calls the Go matching engine, also over gRPC.
- 2
Each symbol runs in a single-goroutine actor that owns its order book, so matching needs no locks. Every command is written to a WAL before it's acknowledged, and replayed on restart.
- 3
Engine events are batched to Kafka. Consumers write orders and trades to Postgres, build OHLCV candles, and feed indicators.
- 4
Candles live in three tiers: in-memory L1, Redis L2 (capped lists), ClickHouse L3 aggregated on the fly from the trades table.
- 5
Depth, ticker and per-user order events fan out through Redis pub/sub to a WebSocket server. Each stream has a buffered channel, so a slow client never blocks the engine.
Architecture
Hard parts
An ack that wasn't durable
The actor wrote to a buffered WAL writer and replied before fsync. A crash in that window lost an order the client thought was placed. Fix: sync the WAL before replying, for place, cancel and modify.
Retry ambiguity over gRPC
On a timeout, the order service can't tell whether the engine saw the order. Blind retries were rejected as duplicates. A GetOrderStatus RPC lets it check first and retry only if the order is unknown.
Slow consumers vs a hot loop
Writing to gRPC streams from the matching loop let one slow client stall every symbol. Each stream now gets a buffered channel (cap 2048) and its own drain goroutine.
Candles without double writes
Writing candles to ClickHouse as well as trades created drift. Trades are the single source of truth; L3 candles are aggregated from them on read.
By the numbers
- 12
- Services
- ~1 month, 17 h days
- Built in
- None
- AI coding tools
Stack
- Go
- TypeScript
- gRPC
- Protobuf / buf
- Kafka
- Redis
- Postgres
- ClickHouse
- Prisma
- WebSocket
- Docker
- Turborepo
Read the write-up
The ack that wasn't durable: a WAL bug in my matching engine
How Nerve's Go matching engine told clients an order was placed before it was safe on disk, and the two fixes that closed the gap.