v0.7.0 MIT Rust 2024 Work in progress

A MongoDB-style document database, written in Rust.

ferrite_db runs as a standalone server that clients connect to over TCP. It is a one-person project, started as a way to learn Rust, and it is still a work in progress.

$ curl -fsSL https://raw.githubusercontent.com/LeSynz/ferrite_db/main/install.sh | sh
View source
JSON lines over TCP
$ cargo install ferrite_db
$ ferrite_db  # 127.0.0.1:7878, data in ./ferrite_data
$ nc 127.0.0.1 7878
{"cmd":"use","db":"shop"}
{"cmd":"insert","collection":"users","doc":{"name":"Dylan","age":25,"address":{"city":"Leeds"}}}
{"cmd":"find","collection":"users","filter":{"age":{"$gt":18}},"sort":{"age":-1},"limit":10}
What it does

Documents, queries, aggregation, transactions and a log that survives a crash.

Documents and queries

  • Nested documents with dates and ObjectIds.
  • MongoDB-style filters, update operators and array operators, including positional $, $[] and $[name] with arrayFilters.
  • Sort, skip, limit and projection.
  • findOneAndUpdate, findOneAndDelete, replace, distinct and upserts.

Aggregation

$match, $group, $project, $lookup (also with let and a sub-pipeline), $unwind, $facet, $bucket, $bucketAuto, $graphLookup, $out, $merge and more.

Indexes

Equality, range, compound and unique. A sort with a limit is read straight off the index.

Transactions

  • Begin, commit and abort on one database.
  • Optimistic: a conflict means retry.
  • All writes go to the log as one record, so a crash leaves all or none.

Change streams

Watch with a $match pipeline, and resumeAfter tokens.

Replication

One leader, with followers that copy its log byte for byte. Followers serve reads and watch and refuse writes. There is no automatic failover, and you promote a follower by hand.

Security

  • SCRAM-SHA-256 users with roles (read, readWrite, admin) per database and per collection.
  • Token login for admins, TLS, and TLS client certificates with live certificate reload.
  • Limits per IP, per line, per connection and on memory.

Durability

  • Append-only binary log with a checksum on every record, replayed at startup, compacting itself.
  • Three sync policies: every 100 ms (default), group (a write is acknowledged only once it is on disk, and clients share one fsync), and every write.

Wire

JSON lines (so nc works) or a binary format, picked by the first byte of the connection. The binary format is not BSON.

Deployment

A .deb with a systemd unit and /etc/ferrite/ferrite.conf, plus an install.sh. A memory limit defaults to 70% of RAM or the cgroup limit.

Numbers

Measured against MongoDB.

One run on one machine, 100,000 small documents, MongoDB's normal settings. With every write fsynced, MongoDB is faster.

All results →
get by _id
Ferrite213k/s
MongoDB39.8k/s
insert, 8 clients
Ferrite161.6k/s
MongoDB41.5k/s
RAM for 100k documents · lower is better
Ferrite45 MB
MongoDB240 MB
Node & Bun

ferrite-db on npm

TypeScript first, with no runtime dependencies. Schemas, types inferred from the schema, hooks, populate and timestamps.

$ npm install ferrite-db 0.5.0
users.ts
import { connect, model, t, type Infer } from "ferrite-db";

const User = model("users", {
  name: t.string(),
  email: t.string().unique(),
  age: t.int().optional(),
  tags: t.array(t.string()),
});

const db = await connect("ferrite://root:pw@localhost/shop");
await User.create({ name: "Ada", email: "[email protected]", tags: ["admin"] });
const adults = await User.find({ age: { $gt: 18 } }).sort({ name: 1 }).limit(10);
Desktop viewer

ferrite_viewer

A Tauri 2 app for browsing, editing, indexes, aggregate, users and grants. It keeps connection passwords in the system keychain and works over TLS. It is a separate repo, installed per user with install.sh or as a .deb.

0.1.0 · no release yet Repository →
Ferrite Viewer showing documents in the users collection
Known limits

What it doesn't do yet.

FerriteDB is a learning project and has had one review. It is not production ready.

Memory

All data lives in RAM, so the ceiling is about 2.5M typical documents in 2 GB.

Replication

There is no failover. Three known gaps: a follower deletes databases the leader lacks, a resync reads empty meanwhile, and resume has no log id.

Drivers and query planning

There are no drivers except the npm package, and no query planner.

Compatibility

It is not wire-compatible with MongoDB, so MongoDB drivers will not connect.

Platform

The memory limit is Linux only.