01. Making a Graph Database
Why a Graph Database? And Why in C?
I mean, just look at the graph databases out there right now: Neo4j, Memgraph, and whatever the hell needs half a gig of JVM overhead just to start up. They are way too bloated for me who just wants a literal graph that can store data inside a tight, space-constrained environment.
So instead of wrestling with an oversized server I didn't need (or use Obsidian like a lot of wave riding tech bros), I decided to just build my own from scratch in pure C. No AI writing it for me, no frameworks, just seeing how far I could get over a few days on a whim.
Now most database tutorials either hand you SQLite/Postgres and call it a day, matter of fact I find a lot of blogs just saying to use those as a "graph", or I'd end up finding simple build a toy in-memory hash map in Python tutorials that live on memory with shitty IO.
I wanted neither. I wanted persistent graph storage, nodes, typed directed edges, and string payloads, where the bytes on disk are the data structures in memory. No SQL parser, no JSON serialization tax, no malloc fragmentation, and no third-party libraries.
Ground Rules
Before writing a single line of C, I set a few constraints for myself (you know I hate myself) to keep the scope tight and actually finishable instead of turning into another abandoned side project:
- Pure C + POSIX syscalls only: Standard library headers,
open,lseek,mmap,msync,munmap. Zero external dependencies. - No
mallocinside the engine: Once the file is mapped into virtual memory, every node, edge, and string lives inside fixed, predictable page offsets. - Fast common case, graceful overflow: Most nodes in real graphs only have a handful of edges, while a few hub nodes have dozens or hundreds. Optimize for small degree without capping high-degree nodes.
- Ship it as a real
.solibrary: Not a messymain.ctest script—compile down to a proper shared library (libc-mmap-graph.so) with a clean header that another language or C app can actually call. - NO AI ALLOWED FOR THIS PROJECT: All written manually baby.
Why mmap Instead of read() / write()?
I like mmap, got an issue with that? It be cool!
But seriously, if you build a persistent graph the naive way, opening a file means reading bytes off disk, parsing them, calling malloc thousands of times to rebuild structs, converting file offsets into RAM pointers, and then serializing the whole mess back out when saving. A lot of unnecessary overhead.
Why do all that work in userspace when the Linux kernel's virtual memory subsystem already knows how to map disk pages into your address space? (dudes working on the kernel spent decades optimizing the page cache, might as well let it do its job.)
With mmap(..., PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0):
- The kernel reserves a contiguous virtual address range backed directly by the file.
- Pages are faulted into RAM lazily by the OS only when we touch them.
- Instead of storing 64-bit memory pointers (which break every time the file is remapped at a different base address by the way), everything inside the file references
uint32_tindices (node_id,bucket_id,slot_id) relative to page offsets. - Reading a node is literally pointer arithmetic:
base_ptr + page_offset + (node_id * sizeof(GraphNode)).
Of course, mmap is not magic. I had to plan my byte layout and free-space tracking carefully upfront, or else I'd have ended up with a fragmented file that's unresizable without invalidating offsets.
Wrapping Up
With the motivation and rules locked in, the real issue was figuring out how to slice a raw 500 MiB file into pages, nodes, edges, and variable-length strings without writing a full general-purpose heap allocator. Next post covers that exact design plan.
Written on: 2026-08-21