Systems Performance · Zero Background Needed · Visual Guide + 8 Interactive Labs · Free
Lock-Free Ring Buffers & Order Books: What They Are & How They Work
Whenever a game audio mixer, Linux io_uring kernel queue, or financial stock exchange passes millions of messages a second between CPU cores, an ordinary operating-system mutex lock is far too slow: putting a waiting thread to sleep and waking it up burns microseconds, and allocating memory with new triggers unpredictable pauses. Lock-free concurrency solves this by passing messages through fixed-size circular arrays called ring buffers using tiny atomic CPU instructions—no locks, no heap allocations. You will start from how CPU cores actually trade memory in 64-byte cache lines and build all the way up to a real price-time order book matching buyers and sellers without a single pause.
- Zero background needed
- Starts with a plain-English visual guide comparing L1 CPU caches, 64-byte cache lines, RAM, and OS mutex locks—plus a gentle walkthrough of what a Bid/Ask order book actually does.
- Real teaching before every quiz
- Every lesson explains every term in plain words, traces a tiny concrete example step by step, and lets you operate the interactive simulator before asking you to predict.
- Offline browser & Node checks
- Explore all 8 interactive labs in your browser, then solve the 8 TODO engine exercises locally with
node build-lab.cjsand worker-thread Atomics tests.
Three Ways In · Layer 3 of 3 · Node.js 22+ · Zero Dependencies
Build With Your Coding Agent — 5 Rungs + Planted Traps
Build a zero-allocation exchange matching engine with your coding agent across five rungs: price-time priority matching, O(1) arena slot cancellation, single-producer single-consumer lock-free rings, cache-line-padded generational handles, and deterministic snapshot + tail journal replay. Each rung gives you an architect prompt for Claude Code, Codex, or Cursor, a planted bug that unguided agents write, and a deterministic test suite that catches it.
- Price-time order book: Limit, market, and IOC matching with partial fills at resting maker price — Planted trap: Executing a crossing limit order at the incoming taker's limit price instead of resting maker price.
- O(1) arena cancel: Preallocated Doubly-linked slot pool with O(1) cancel and modify rules — Planted trap: Letting a quantity increase or price change keep its old time priority in the queue.
- Lock-free SPSC ring: SharedArrayBuffer ring buffer verified across a real Node Worker thread — Planted trap: Publishing the write cursor before payload fields finish writing, or allowing a full ring to wrap.
- Padded generational handles: 64-byte cache-line-separated cursors and generation-tagged order handles — Planted trap: Reusing a freed slot without bumping its generation, letting a stale cancel kill a new order.
- Snapshot + journal replay: Monotonic sequence journal with exact snapshot + tail crash recovery — Planted trap: Replaying
seq >= snapshot.seqinstead ofseq > snapshot.seqand filling an order twice.
unzip agent-track.zip && cd lockfree-orderbook-lab/agent-track
node --test test/*.test.mjs
# Point a rung's import at your own module in src/mine/ and run:
node --test test/rung-01.test.mjs
Download 5-Rung Agent Track (agent-track.zip) Read AGENT-TRACK.md on GitHub Open agent-track/README.md on GitHub
Inside the course
- Start Here — What & Why: Why game audio threads, kernel networking (
io_uring), and stock exchanges avoid OS mutex locks, how CPU cores bounce 64-byte cache lines, and how buyers (bids) meet sellers (asks) in an order book. - Lessons 1–3 — Cache Lines & Safe Handoffs: Stop two CPU cores from fighting over the same 64-byte chunk (false sharing), wrap tickets around a fixed power-of-two ring buffer, and order writes safely with release/acquire fences.
- Lessons 4–5 — Multiple Writers Without Locks: Claim slots with Compare-And-Swap (CAS), space out colliding retries with backoff, and prevent stale pointer reuse (the ABA bug) using epoch guards.
- Lessons 6–8 — Zero-Allocation Order Book & Tail Latency: Match limit and market orders from a preallocated pool by best price then arrival time, enforce backpressure across two rings, and diagnose P99 latency spikes.
Clone & build your own engine
git clone https://github.com/itsHabib/free-to-learn.git
cd free-to-learn
git switch --detach 7dba0d743224c10a707307769782b0db3f9a2a1a
cd courses/lockfree-orderbook-lab
node build-lab.cjs start
node build-lab.cjs check 1OS mutex lock sleep: ~10,000 ns
Cross-core cache bounce: ~50 ns
Local L1 cache hit: ~1 ns
Separate 8-byte cursors by 56 bytes → 4.8× faster.
Zero mutexes. Zero heap allocations.
8 interactive labs + Node Atomics tests.