yagdb is an experimental, in-memory graph database written in Rust. It implements a subset of the Cypher query language, features disk persistence via Write-Ahead Logging (WAL) and snapshots, and provides an HTTP API for query execution. It also supports compiling to WebAssembly (WASM) for use in browsers. The project was largely built as an exercise in using AI-tooling, and at this time should not be trusted for anything beyond goofing off.
- Cypher Query Support: Implements a subset of Cypher using the
nomparser combinator library.- CREATE: Creates new nodes and relationships.
- Nodes:
CREATE (n:Person {name: 'Alice', age: 30}) - Relationships:
CREATE (a:User)-[:KNOWS]->(b:User) - Mixed:
CREATE (a:Person {name: 'Alice'})-[:KNOWS]->(b:Person {name: 'Bob'})
- Nodes:
- MATCH: Matches patterns in the graph.
- Nodes:
MATCH (n:Person) - Relationships:
MATCH (a)-[r:KNOWS]->(b) - Variable Length Paths:
MATCH (a)-[*1..3]->(b) - Bound Variables to Paths:
MATCH p = (a)-[*1..3]->(b)
- Nodes:
- WHERE: Filters results based on conditions. Supports AND, OR, NOT, and comparison operators (
=,!=,<,>,<=,>=,=~).- Property comparison:
MATCH (n:Person) WHERE n.age > 25 - Regex match:
MATCH (n:Person) WHERE n.name =~ '(?i)^a.*' - Boolean logic:
MATCH (n:Person) WHERE n.age > 25 AND NOT n.name = 'Bob'
- Property comparison:
- RETURN: Specifies what to include in the query result. Supports aliasing.
- Variables:
RETURN n, r - Properties:
RETURN n.name, n.age - Aliasing:
RETURN n.name AS personName - All variables:
RETURN * - Aggregations:
RETURN COUNT(n), COLLECT(n.name), UNIQUE(n.age)
- Variables:
- WITH: Chains query parts together, allowing you to manipulate the output before it is passed to the following clauses. Similar syntax to
RETURN.MATCH (n:Person) WITH n, n.age AS age WHERE age > 25 RETURN n.name
- ORDER BY: Sorts the result set. Supports ASC and DESC.
MATCH (n:Person) RETURN n ORDER BY n.age DESC
- LIMIT: Restricts the number of rows in the result set.
MATCH (n:Person) RETURN n LIMIT 10
- CREATE INDEX ON: Creates an index on a specific node label and property for faster lookups.
CREATE INDEX ON :Person(name)
- SET: Updates properties on existing nodes or relationships.
MATCH (n:Person {name: 'Alice'}) SET n.age = 31
- MERGE: Matches an existing pattern or creates it if it doesn't exist.
MERGE (n:Person {name: 'Charlie'})
- DELETE: Deletes nodes or relationships.
MATCH (n:Person {name: 'Alice'})-[r]-() DELETE n, r
- UNWIND: Expands a list into a sequence of rows.
UNWIND [1, 2, 3] AS x RETURN x
- CALL: Executes a subquery.
MATCH (n:Person) CALL { WITH n MATCH (n)-[:KNOWS]->(m) RETURN m } RETURN n, m
- Expressions:
- Property Access:
n.name - Literals: Strings (
'Alice'), Numbers (42,3.14), Booleans (true,false) - Variables:
n,r,p - Functions:
rand()is supported.
- Property Access:
- PROFILE: Prefixing a query with
PROFILEoutputs the execution plan instead of executing the query.PROFILE MATCH (n:Person) RETURN n
- CREATE: Creates new nodes and relationships.
- Graph Engine: Native Rust graph execution engine utilizing Depth-First Search (DFS) for path finding.
- Persistence: Fast binary serialization of the database state and Write-Ahead Log (WAL) entries using
bincodeandserde. Automatically recovers state upon startup. - HTTP Server: An asynchronous API server using
axumandtokio. Exposes a POST endpoint to execute queries. - Observability: Exposes Prometheus metrics at the
GET /metricsendpoint for production monitoring, tracking query counts, duration, and graph sizes. - Clustering (Optional): Can be compiled with the
clusterfeature to enable distributed consensus usingopenraft. Extra experimental. - Disk Storage: Experimental support for managing large graphs by caching nodes and serving from disk.
- WASM Support: Can be compiled to the
wasm32-unknown-unknowntarget for browser embedding.
- Rust toolchain (stable)
To start the HTTP server, simply run:
cargo runThe server will start listening on 127.0.0.1:3000 by default. It will automatically create graph.bin and wal.bin in the current directory for persistence.
To compile and run with Raft cluster support:
cargo run --features clusterYou can interact with yagdb using HTTP POST requests to the /query endpoint. The body of the request should be the raw Cypher query string.
Create nodes and a relationship:
curl -X POST -H "Content-Type: text/plain" -d "CREATE (a:User {name: 'Alice'})-[r:KNOWS]->(b:User {name: 'Bob'})" http://127.0.0.1:3000/queryMatch and return nodes:
curl -X POST -H "Content-Type: text/plain" -d "MATCH (a:User)-[r:KNOWS]->(b:User) RETURN a, b" http://127.0.0.1:3000/queryCreate an index:
curl -X POST -H "Content-Type: text/plain" -d "CREATE INDEX ON :User(name)" http://127.0.0.1:3000/queryProfile a query:
curl -X POST -H "Content-Type: text/plain" -d "PROFILE MATCH (a:User {name: 'Alice'}) RETURN a" http://127.0.0.1:3000/queryYou can also use yagdb as an embedded library in your own Rust applications.
use yagdb::graph::Graph;
fn main() {
// Start an in-memory database without persistence for testing
let mut g = Graph::new();
// Execute queries
g.execute("CREATE (n:Person {name: 'Alice'})").unwrap();
let result = g.execute("MATCH (n:Person) RETURN n").unwrap();
println!("{}", result);
}Run the test suite using standard Cargo commands:
cargo testTo test cluster features specifically:
cargo test --features clusteryagdb includes a benchmark suite using the criterion crate.
cargo benchTo build the project for WASM:
wasm-pack build --target webIn WASM mode, disk persistence (WAL, snapshots) and the HTTP server are conditionally disabled. The library exposes a wasm_bindgen function execute_query(query: &str) -> String that operates on a globally shared in-memory graph.
To transition yagdb into a production-ready system, the following features would have to be implemented:
- Strongly Typed Properties: Transitioning from string-only properties to native support for integers, floats, booleans, and dates to improve performance and data integrity.
- Authentication & Authorization: Implementing user roles, credentials, and access control for the API.
- Transactions: Adding support for multi-statement ACID transactions with commit and rollback capabilities.
- Expanded Cypher Support: Adding advanced Cypher clauses like
OPTIONAL MATCH,REMOVE, and string/math functions. - TLS/SSL Encryption: Securing the HTTP server and Raft node-to-node communication.
- Graph Algorithms: Built-in procedures for PageRank, Shortest Path, and Community Detection.
- Backup & Restore Tooling: Dedicated utilities for point-in-time recovery, snapshot management, version migration, and hot backups.