Project 14: Memory-Mapped File Database

A simple embedded database using mmap() for zero-copy persistence.

Quick Reference

Attribute Value
Primary Language C
Alternative Languages N/A
Difficulty Level 4 (Expert)
Time Estimate See main guide
Knowledge Area Databases, File Systems
Tooling See main guide
Prerequisites See main guide

What You Will Build

A simple embedded database using mmap() for zero-copy persistence.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • File growth → Remapping when database grows
  • Crash consistency → msync and fsync
  • Concurrent access → Record locking + mmap

Key Concepts

  • Map the project to core concepts before you code.

Real-World Outcome

$ ./mmapdb create mydata.db --size=100MB
Database created: mydata.db (100MB)

$ ./mmapdb insert mydata.db user:1 '{"name":"Alice"}'
Inserted at offset 0

$ ./mmapdb insert mydata.db user:2 '{"name":"Bob"}'
Inserted at offset 256

$ ./mmapdb get mydata.db user:1
{"name":"Alice"}

# Kill the process abruptly
$ ./mmapdb insert mydata.db user:3 '{"name":"Charlie"}' &
$ kill -9 $!

# Data still persists (mmap wrote to file)
$ ./mmapdb get mydata.db user:3
{"name":"Charlie"}

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: UNIX_IPC_STEVENS_VOL2_MASTERY.md
  • Primary references are listed in the main guide