In smollerGraph I build a graph database from scratch in C++, a small one you can read, run and understand from end to end. The focus is on what happens behind the query, from how nodes and relationships are laid out on disk and how the database follows them from one to the next, to how a Cypher pattern becomes a plan that runs.
Why graphs
Graphs sit at the core of many of the problems we solve through engineering, from knowledge graphs for search and AI, healthcare, routes and maps, to file systems, distributed systems, the batch jobs and ETL loads of the enterprise and the entities in a game engine. They matter a lot in cybersecurity too, where tools such as BloodHound find attack paths through an organisation's accounts and machines.
Despite that, most of us never get to see the inside of a graph database, and I created this series out of my interest in complex software engineering at scale and the practices around it. After years at Morgan Stanley and Google building large distributed systems, which rest on databases most of us treat as black boxes, I wanted a codebase small enough that you can follow a query all the way down to the bytes it reads.
Learning from Neo4j and Kùzu
Before we write code of our own, the series looks in depth at two graph databases, Neo4j, a server written in Java that keeps the graph as fixed-size records pointing at one another, and Kùzu, an embedded database in C++ from research at the University of Waterloo, with columns and compressed sparse row adjacency. smollerGraph learns from both and is measured against both as it grows, on questions in the style of LDBC's Social Network Benchmark at laptop scale.
From memory to disk
The code starts out as a throwaway graph kept in memory, a small knowledge graph about animals with a breadth-first walk and tests around it. From there, episode by episode, we replace a piece of what sits underneath while the tests keep checking the answers.
The graph moves into files of 4 KiB pages, with fixed-size records for nodes and relationships, so it outlives the program, and then a buffer pool keeps those pages in memory, with frames, pins, dirty pages and the clock algorithm deciding which page goes next. A B+ tree, kept in pages through that same buffer pool, finds a node by name, so opening a graph no longer means reading every node, and a parser and a planner turn a subset of Cypher into a plan and the rows it returns.
Later on, properties join the nodes and relationships in chains of records of their own, so queries can ask about them with WHERE. Labels and relationship types become tokens, numbers kept in place of their names as in Neo4j, which makes room for a relationship to sit in two chains, one for the node it starts at and one for the node it ends at, so it can be followed either way. The planner then starts from whichever node of a pattern it can find most quickly and walks from there in both directions.
Measuring as we go
Early on, we put a benchmark together on a million relationships, with the same two-hop question asked of smollerGraph, Neo4j and Kùzu. It wasn't quite fair, though, as smollerGraph timed a walk straight through the graph while the other two got a query in Cypher, so in episode eleven all three answer the same two questions in Cypher, with parsing and planning included.
On those, smollerGraph answers the two-hop question in a median of about 28 microseconds, where Neo4j takes nearly a millisecond and Kùzu about 1.6 milliseconds. I'd take that with a pinch of salt, as Neo4j's times still include a trip over the network protocol to a container and Kùzu's a call from Python, while ours are measured inside the process. smollerGraph also does a small fraction of what either of them does, so what the numbers mostly show is that the layout on disk works, which is what matters at this stage.
Beyond storage
Once the storage was in place, the series moved on to what people do with a graph, starting with shortest paths, found by a breadth-first search from both ends that meets in the middle and written as shortestPath in Cypher the way Neo4j writes it. Cheapest paths along weighted relationships came next, with Dijkstra's algorithm, written the way Kùzu writes them.
Nodes get embeddings with FastRP, the way Neo4j's Graph Data Science library computes them, so you can ask which nodes are most similar to another. A small graph neural network, a GraphSAGE in PyTorch, then learns which species are threatened from their relatives in a graph of mammals and birds from Wikidata, and the predictions go back into the database for Cypher to ask about.
The most recent episode on applications takes a made-up company's Active Directory as a graph, with Cypher that follows several relationship types at once, so smollerGraph finds the paths from an ordinary account to Domain Admins, the way BloodHound does.
Around the database
A fair share of the series goes into the engineering around the code, with cpplint, clang-tidy and Abseil's Status keeping the codebase honest, a page in the browser that draws the graph with D3 and shows the record on disk for every node, and a deploy to a VPS with Docker Compose and Caddy. Doxygen generates the API docs from the comments already in the headers and CI publishes them, the reasons behind our design choices are written up as architecture decision records, and the library is packaged so another CMake project can fetch it and run a query in a single call.
The library also compiles unchanged to WebAssembly with Emscripten, so the explorer runs the whole database in a browser tab, on a graph it builds from Wikidata's facts as the page opens, and you can play with it with nothing to install.
Where it's heading
smollerGraph is heading towards a knowledge graph, with facts kept as nodes and the relationships between them, and it already plays a part in my other two series. mvGPT, the small GPT I'm building from scratch, has learned to write Cypher to ask it for facts, and nanoMMO keeps the game content in it as a graph, with a test in CI that fails whenever a new player can no longer reach a quest, a monster, an item or a skill. Transactions and a log, which keep a database correct when things go wrong, are still ahead.
Following along
The code is on GitHub together with the API docs, and all the steps of all the episodes are tagged in the repository, so you can check out the code wherever you are in a video and run it yourself. New episodes go up on my YouTube channel, and the series' website has a page per episode, with links to browse the code at every step and see what changed since the step before.