KayaDB: Writing a Distributed Database from Scratch

6 min read rust kayadb distributed-systems raft database jepsen open-source

Distributed systems are the Everest of software engineering. Everyone sees the summit, very few climb it. I decided to climb. The result: KayaDB, a Raft-based distributed key-value storage engine written in Rust and being prepared for production.

This post is the log of that climb.

Why did I write my own database?

Honest answer: porque puedo. (Because I can.)

Serious answer: etcd exists, TiKV exists, FoundationDB exists. But none of them were what I wanted. What I wanted:

  • Zero operational overhead. Download a binary, run it. No config file, no YAML hell.
  • Embeddable. It should work as a library. You should be able to write kayadb = "0.1" and embed it in your own application.
  • Jepsen-tested. No claim from a distributed system can be taken seriously unless it has passed Kyle Kingsbury's tests.
  • Rust. C++ performance without dealing with manual memory management.

Nothing gave me all of these at once. So I built it.

Architecture

At the heart of KayaDB sits the Raft consensus algorithm. Raft is the most understandable consensus protocol for leader election, log replication and safety guarantees in distributed systems (the "understandable by humans" version of Paxos).

Three core layers:

┌─────────────────────────────────┐
│         Client API (gRPC)       │
├─────────────────────────────────┤
│      Raft Consensus Layer       │
│  ┌─────────┐  ┌──────────────┐  │
│  │ Leader  │  │  Follower x2  │  │
│  │ Election│  │  Log Repl.   │  │
│  └─────────┘  └──────────────┘  │
├─────────────────────────────────┤
│     Storage Engine (LSM-Tree)   │
│  ┌─────────┐  ┌──────────────┐  │
│  │ MemTable│  │  SSTables    │  │
│  └─────────┘  └──────────────┘  │
└─────────────────────────────────┘

Storage engine: LSM-tree (Log-Structured Merge-tree). Optimized for write-heavy workloads. Accumulate in the MemTable, flush to disk as an SSTable once it hits a certain size, run compaction in the background. A simpler, easier to follow version of what RocksDB does.

Raft layer: Elects a leader, replicates logs, advances the commit index. Standard Raft: no surprises, follows the spec to the letter.

Client API: gRPC for now. A native TCP protocol is coming later.

From M1 to M13: 13 milestones, 6 months

I run the project in milestones. Each milestone takes a few weeks and produces something concrete.

M1-M3 (January 2026): The basic storage engine. Single-node, memory-only. put(key, value) and get(key) work. Not exciting, but it's the foundation.

M4-M6 (February): Persistence. WAL (Write-Ahead Log), MemTable flush, the SSTable format. For the first time we stopped losing data on restart. I can't describe that feeling: code you wrote brings back the data of a program you shut down.

M7-M9 (March): The Raft implementation. The hardest part. I read the Raft paper roughly 17 times. Leader election, log replication, snapshotting: each one is a world of its own. The biggest headache: network partition simulation. Writing the tests was harder than the implementation.

M10-M12 (April-May): Jepsen tests. Kyle Kingsbury's famous distributed systems testing framework. Linearizability, partition tolerance, clock skew. It tests all of it. And on the first run... red. Everything was red.

M13 (June 2026): Production prep. And then that moment: 6/6 Jepsen gates green.

Getting green from Jepsen is like being knighted in the distributed systems world. It means "your system keeps data consistent even when the network splits, the disk dies and the clock drifts."

Dancing with Jepsen

Writing Jepsen tests is nothing like writing normal tests. In a normal test you say "this input gives this output." In Jepsen you ask "in this chaotic scenario, is the system's behavior linearizable?" The difference is like testing a building with an earthquake simulation.

A Jepsen test does the following:

  1. Starts a cluster (usually 5 nodes)
  2. Sends random operations (append, read, cas)
  3. Meanwhile, it brings in the nemeses:
    • partition-random-node: Randomly splits the network
    • kill-random-node: Kills a random node
    • clock-skew: Shifts the system clock
    • pause-random-node: Freezes a node (SIGSTOP)
  4. At the end of the test, it analyzes the full operation history
  5. It answers the question "is this history linearizable?"

If the answer is "no," you have violated the consistency guarantee somewhere. And believe me, on the first try the answer is always "no."

What was my biggest mistake? Skipping fsync.

I confess: I had forgotten to call fsync() when writing the WAL. I was writing data with write(), and it sat in the page cache, not on disk. When a node crashed... the WAL was empty. Jepsen caught it right away: "Your system has no durability guarantee."

Fix: fsync() after every WAL write. Did performance drop? Yes, by 30%. But we stopped losing data. That was the trade-off.

Another mistake: implementing the Read Index optimization wrong. In Raft, reads have to go through the leader, but writing a log entry for every read is expensive. The Read Index technique makes reads safe without writing to the log. But I had calculated the lease duration wrong: a partitioned leader could still think "I'm the leader" and return stale reads. Jepsen: red.

Fix: Lease-based leader check + heartbeat confirmation before every read.

I caught 11 Jepsen bugs like this. Each one a separate lesson.

What did I learn?

1. There is no "works on my machine" in distributed systems. Code that doesn't work under a network partition, a disk failure or clock skew doesn't work. Jepsen rubs your face in it.

2. Reading the Raft paper isn't enough, you have to implement it. You only understand how many edge cases leader election has, or what a sentence on page 5 of the paper really means, while implementing it.

3. fsync is everything. Durability = fsync. There's no other way. However much you batch, sooner or later you have to make that system call.

4. Test infrastructure matters more than product code. KayaDB has more lines of test code than product code. Network partition simulations, fault injection, chaos engineering: all of it lives on the test side.

What's next?

M14: Jepsen test suite final + prod deployment. Goal: run KayaDB on a real server under real traffic, not on my own machine.

After that:

  • gRPC API stabilization
  • Rust client library (kayadb-client)
  • Go client library (maybe)
  • Publishing benchmarks (compared against etcd and TiKV)

Try it

git clone https://github.com/Tuntii/KayaDB
cd KayaDB
cargo build --release
./target/release/kayadb --id node1 --peers node2:5001,node3:5001

3 nodes, 3 terminals, and your Raft cluster is ready.

GitHub: github.com/Tuntii/KayaDB Documentation: tuntii.github.io/KayaDB

Distributed systems are hard. But doable. And when you pull it off, you've solved one of the most fundamental problems in computer science with your own hands. That feeling is priceless.

In the next post I'll talk about Hyperembed: a pure-Rust BERT embedding inference engine.

← Blog