Open-source project
erikgrinaker/toydb avatar
erikgrinaker/toydb

toyDB: a distributed SQL database in Rust you can read end to end

Distributed SQL database in Rust, written as an educational project

7,310 stars623 forksRustApache-2.0

At a glance

What is it?
toyDB is an educational distributed SQL database written in Rust, with Raft consensus, MVCC snapshot isolation and a pluggable storage engine. It is built to be understood, not to be fast, and the README says so directly.
Who is it for?
Adopt toyDB if you want to read a working distributed SQL database rather than operate one: the architecture guide, the testscripts and the EXPLAIN output are the point. Do not put it in front of production traffic, because the README lists performance, scalability and availability as non-goals.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 52 days ago.
What is it written in?
Mainly Rust, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 23, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • erikgrinaker/toydb7,310 ★ · +26
Star history
Repository2026-09-232026-11-23
erikgrinaker/toydb7.3k7.3k

26 Stars in the last 61 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The problem toyDB solves is reading a distributed SQL database, not running one

Production distributed SQL databases are large enough that the core ideas disappear into the machinery. toyDB takes the opposite position. The README describes it as "a simple illustration of the architecture and concepts behind distributed SQL databases", built from scratch as an educational project. The stated goals are simple, understandable, functional and correct, and the same paragraph names performance, scalability and availability as non-goals, adding that "shortcuts have been taken where possible". That is an unusual thing for a database README to admit, and it is the most useful sentence in the repository.

The audience follows from that. This is for engineers who want to see how Raft, MVCC and a SQL planner fit together in one codebase they can finish reading. It is not for anyone choosing a database for an application. The author's own framing supports that: the README says the project was first written in 2020 to learn about database internals, then rewritten after several years spent building real distributed SQL databases at CockroachDB and Neon. The rewrite is aimed at explanation, not at competing with the systems the author worked on.

Raft on the bottom, MVCC in the middle, a SQL engine on top

The README describes the architecture as "fairly typical for a distributed SQL database: a transactional key/value store managed by a Raft cluster with a SQL query engine on top". Each layer is a separate module in src/, and the architecture guide in docs/architecture/index.md walks through them.

Raft provides linearizable state machine replication, which is what lets a five-node cluster agree on an ordered log of writes. MVCC sits inside the storage layer and gives ACID transactions with snapshot isolation, implemented in src/storage/mvcc.rs. The storage engine is pluggable through src/storage/engine.rs, with a BitCask backend and an in-memory backend. Above that, the query engine is iterator based, with heuristic optimization in src/sql/planner/optimizer.rs and a SQL parser in src/sql/parser/parser.rs.

The EXPLAIN output in the README is the clearest evidence that the layers are real rather than sketched. A query joining movies, genres and studios produces a tree that starts with Remap, Order, Projection and Aggregate nodes. That is a plan tree from a working planner, not a diagram.

Starting a five-node cluster and running your first query

The README gives the usage path directly. You need a Rust compiler installed. From the repository root, the cluster script builds and starts five nodes on ports 9601 to 9605 for SQL and 9701 to 9705 for Raft, with data under cluster/*/data/.

bash
./cluster/run.sh

The script prints the ports as each node starts, then reports an election. In the README's example output, node 2 starts a new election for term 1.

With the cluster running, the toysql client connects to node 1 on localhost:9601. The README builds it with cargo run --release --bin toysql.

bash
cargo run --release --bin toysql

The client prints a connection banner and a toydb> prompt. The README's first session creates a table, inserts three rows and selects them back.

sql
CREATE TABLE movies (id INTEGER PRIMARY KEY, title VARCHAR NOT NULL);
INSERT INTO movies VALUES (1, 'Sicario'), (2, 'Stalker'), (3, 'Her');
SELECT * FROM movies;

You should see the three rows echoed back in insertion order. From there, docs/examples.md walks through joins, aggregates and transactions, and docs/sql.md is the reference for the dialect. The client accepts !help for its own instructions.

What the README does not promise, and where that bites

The honest limitation is stated up front: performance, scalability and availability are non-goals. That rules out a set of uses. Durability and recovery behaviour under real failure are not what this codebase was written to demonstrate. Anyone expecting to run toyDB as a service is working outside what the project claims to cover.

The benchmark section reinforces the point. The workload tool runs read and write workloads against a cluster, and the README shows a read workload reaching roughly 14,000 transactions per second with p50 latency around 1.2ms on the author's machine. Those numbers are illustrative of the tool, not a capacity claim, and the README opens the section by saying toyDB "is not optimized for performance". Treat the benchmark as a way to generate load and watch the cluster behave, not as a comparison against anything.

There is also a practical constraint in the manifest. Cargo.toml sets edition 2024 and publish = false, and the repository pins a toolchain via the rust-toolchain file. If your compiler predates edition 2024 support, the build fails before you reach any database code.

How toyDB differs from DuckDB and CockroachDB

DuckDB, CockroachDB, SurrealDB and GlueSQL are the usual comparisons, and the comparison is worth making precisely because toyDB loses all of them on capability.

DuckDB is an embedded analytical database. It runs in your process and is optimized for scanning and aggregating large local datasets. toyDB is a networked, replicated OLTP-shaped system: five processes, Raft between them, a client connecting over a port. If your problem is analytical queries over a file, DuckDB is the tool and toyDB is not in the conversation.

CockroachDB is the closest architectural relative, and the README names it because the author worked there. Both are distributed SQL databases with consensus-replicated transactional storage. The difference is everything around the core: CockroachDB carries the operational surface, tuning and correctness work that toyDB explicitly declines to take on. toyDB keeps the shape and drops the weight, which is what makes it readable and what makes it unsuitable for production.

SurrealDB and GlueSQL solve different problems. SurrealDB is a multi-model database with its own query language, and GlueSQL is a SQL layer meant to be embedded over a storage backend. Neither is a from-scratch teaching implementation of Raft plus MVCC plus a planner, which is the specific thing toyDB is.

Tests, licence and what maintenance looks like

toyDB uses Goldenscripts for testing. The README lists scripted scenarios for Raft cluster behaviour, MVCC transactions, SQL execution and end-to-end runs, and notes that cargo test runs them. The pattern is worth understanding before you change anything: a goldenscript records events and output, then asserts that later runs match. Editing a Raft timing constant or a planner rule will change captured output, so a failing test may mean the golden file needs review rather than that the code is broken.

The licence is Apache-2.0, declared in both the LICENSE file and the license field of Cargo.toml. Apache-2.0 permits commercial use and modification and includes an explicit patent grant, but it also carries notice and attribution obligations when you redistribute. That is the general shape of the licence, not advice about your situation; if you plan to ship modified toyDB code, read the LICENSE file and talk to someone qualified.

On maintenance: the last push to the default branch was on 2026-10-02, which is recent. The repository is not archived. The repository has no published GitHub releases, and Cargo.toml sets publish = false, so there is no crate to depend on and no versioned upgrade path. You take the code at a commit and you own your fork. For an educational codebase that is a reasonable arrangement, but it means there is no upstream to pull a fix from.

Editorial conclusion

Adopt toyDB if you want to read a working distributed SQL database rather than operate one: the architecture guide, the testscripts and the EXPLAIN output are the point. Do not put it in front of production traffic, because the README lists performance, scalability and availability as non-goals. Before you spend a weekend on it, check that your Rust toolchain matches rust-toolchain and that the edition 2024 requirement in Cargo.toml is supported by the compiler you have installed.

Frequently asked questions

What is toyDB written in, and what does it use for consensus?

toyDB is written in Rust and uses Raft for linearizable state machine replication. The README lists Raft, MVCC-based snapshot isolation, pluggable storage and a SQL interface as its main features.

How do I run a toyDB cluster locally?

With a Rust compiler installed, run ./cluster/run.sh from the repository root. The script starts five nodes on ports 9601 to 9605 for SQL and 9701 to 9705 for Raft, and the README shows one of them starting an election for term 1.

Is toyDB suitable for production use?

No. The README states that performance, scalability and availability are non-goals and that shortcuts have been taken where possible. It is intended as a simple and understandable educational implementation.

What SQL features does toyDB support?

The README says toyDB supports most common SQL features including joins, aggregates and transactions. The SQL reference in docs/sql.md documents the dialect, and docs/examples.md walks through the features.

How does toyDB store data?

The storage engine is pluggable through src/storage/engine.rs, with a BitCask backend and an in-memory backend. The cluster script places data under cluster/*/data/.

Official sources

  1. erikgrinaker/toydb on GitHub
  2. Issues
  3. License: Apache-2.0
  4. README
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/erikgrinaker-toydb.svg)](https://hysenlabs.com/projects/erikgrinaker-toydb)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/erikgrinaker-toydb.svg)](https://hysenlabs.com/projects/erikgrinaker-toydb)

Was this useful?