A highly-optimized, single-node, mini-Kafka broker built from scratch in Rust. This system leverages a thread-per-core architecture powered by io_uring via the tokio-uring framework, bypassing traditional OS thread context switches and synchronization overhead.
io_uringAsynchronous System Calls: Uses direct Linux kernel ring buffers for both Disk WAL I/O (File::write_at/File::read_at) and network I/O (TcpStream::read/TcpStream::write).- Kernel-User Ownership Passing: Implements strict data ownership transfer where memory buffers (
Vec<u8>) are passed to the kernel and reaped asynchronously, achieving zero-copy properties. - Single-Thread Hot Path: The server runs entirely inside a single CPU thread event loop. Cross-client synchronization and tail-log notifications are achieved using thread-local asynchronous coordination (
tokio::sync::watchandtokio::sync::Mutex), avoiding standard thread-locking contention. - Append-Only Write-Ahead Log (WAL): Messages are sequentially packed with a 4-byte length header and flushed directly to disk.
The broker communicates over TCP using a simplified binary framing protocol:
On initial connection, a client must send a single byte designating its role:
0x01: Producer Client0x02: Consumer Client
Once handshaking as a Producer, the client continuously streams records:
- Request Format:
[4-Byte Big-Endian Length] [Payload Bytes] - Response ACK: The server appends the record to the WAL and immediately returns an
[8-Byte Big-Endian Physical Offset]representing the exact byte location of the message.
Once handshaking as a Consumer, the client requests a stream of records starting from a specific point:
- Request Format:
[8-Byte Big-Endian Starting Offset] - Response Stream: The server tails the WAL and streams back matches:
[4-Byte Big-Endian Length] [Payload Bytes]. When it reaches the end of the log, it yields and awaits notifications of new writes.
- OS: Linux (Kernel 5.1 or newer is required for basic
io_uringsupport, 5.6+ is highly recommended for full socket support). - Rust: Modern stable toolchain (
edition = "2021").
The single binary supports four distinct operational modes via command-line arguments.
To build the highly-optimized release binary:
cargo build --releaseThis produces a standalone executable at ./target/release/ringlog. You can copy this binary and run it anywhere on a compatible Linux environment:
# To run the self-contained simulation demo:
./target/release/ringlog
# To run the standalone broker server:
./target/release/ringlog server
# To run the interactive producer client:
./target/release/ringlog producer
# To run the real-time consumer client starting from physical offset 0:
./target/release/ringlog consumer 0For quick development and testing, you can use cargo to compile and run:
This runs a fully self-contained simulation of the broker, producer, and consumer all running concurrently inside the same thread-local event loop:
cargo runTo start the standalone broker server listening on port 12000:
cargo run -- serverCreates/appends to a persistent file commit.log in your working directory, automatically preserving/restoring the write offsets.
In a new terminal window, connect to a running broker server and dynamically type messages to publish them to the WAL:
cargo run -- producerSimply type your message in the console and hit Enter. The client will print the assigned physical byte offset ACK returned from the server.
In a new terminal window, connect to a running broker server to stream all historical and newly landing messages:
cargo run -- consumer [starting_offset]If starting_offset is omitted, it defaults to 0 (replaying the entire log).