Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

toydb

> 数据库
Open source

Distributed SQL database in Rust, written as an educational project

7.3K stars0 likes1 views
WebsiteGitHub

About

Distributed SQL database in Rust, written as an educational project

toyDB

Distributed SQL database in Rust, built from scratch as an educational project. Main features:

  • Raft distributed consensus for linearizable state machine replication.

  • ACID transactions with MVCC-based snapshot isolation.

  • Pluggable storage engine with BitCask and in-memory backends.

  • Iterator-based query engine with heuristic optimization and time-travel support.

  • SQL interface including joins, aggregates, and transactions.

toyDB is intended to be simple and understandable, and also functional and correct. Other aspects like performance, scalability, and availability are non-goals -- these are major sources of complexity in production-grade databases, and obscure the basic underlying concepts. Shortcuts have been taken where possible.

I originally wrote toyDB in 2020 to learn more about database internals. Since then, I've spent several years building real distributed SQL databases at CockroachDB and Neon. Based on this experience, I've rewritten toyDB as a simple illustration of the architecture and concepts behind distributed SQL databases.

Documentation

  • Architecture guide: a guided tour of toyDB's code and architecture.

  • SQL examples: walkthrough of toyDB's SQL features.

  • SQL reference: reference documentation for toyDB's SQL dialect.

  • References: research materials used while building toyDB.

Usage

With a Rust compiler installed, a local five-node cluster can be built and started as:

…

A command-line client can be built and used with node 1 on localhost:9601:

$ cargo run --release --bin toysql
Connected to toyDB node n1. Enter !help for instructions.
toydb> CREATE TABLE movies (id INTEGER PRIMARY KEY, title VARCHAR NOT NULL);
toydb> INSERT INTO movies VALUES (1, 'Sicario'), (2, 'Stalker'), (3, 'Her');
toydb> SELECT * FROM movies;
1, 'Sicario'
2, 'Stalker'
3, 'Her'

toyDB supports most common SQL features, including joins, aggregates, and transactions. Below is an EXPLAIN query plan of a more complex query (fetches all movies from studios that have released any movie with an IMDb rating of 8 or more):

…

Architecture

toyDB's architecture is fairly typical for a distributed SQL database: a transactional key/value store managed by a Raft cluster with a SQL query engine on top. See the architecture guide for more details.

Tests

toyDB mainly uses Goldenscripts for tests. These script various scenarios, capture events and output, and later assert that the behavior remains the same. See e.g.:

  • Raft cluster tests
  • MVCC transaction tests
  • SQL execution tests
  • End-to-end tests

Run tests with cargo test, or have a look at the latest CI run.

Benchmarks

toyDB is not optimized for performance, but comes with a workload benchmark tool that can run various workloads against a toyDB cluster. For example:

…

The available workloads are:

  • read: single-row primary key lookups.
  • write: single-row inserts to sequential primary keys.
  • bank: bank transfers between various customers and accounts. To make things interesting, this includes joins, secondary indexes, sorting, and conflicts.

For more information about workloads and parameters, run cargo run --bin workload -- --help.

Example workload results are listed below. Write performance is atrocious, due to fsync and a lack of write batching in the Raft layer. Disabling fsync, or using the in-memory engine, significantly improves write performance (at the expense of durability).

Workload BitCask BitCask w/o fsync Memory
read 14163 txn/s 13941 txn/s 13949 txn/s
write 35 txn/s 4719 txn/s 7781 txn/s
bank 21 txn/s 1120 txn/s 1346 txn/s

Debugging

VSCode and the CodeLLDB extension can be used to debug toyDB, with the debug configuration under .vscode/launch.json.

Under the "Run and Debug" tab, select e.g. "Debug executable 'toydb'" or "Debug unit tests in library 'toydb'".

Credits

The toyDB logo is courtesy of @jonasmerlin.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rustdatabasedistributedmvccraft

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库