← Portfolio

Building a SQL Database Engine in C

A walkthrough of building a lightweight SQLite-style database from scratch in C, covering page management, B-tree storage, and a command-line REPL.

0

Building a SQL Database Engine in C

This project is a lightweight SQLite-style database engine written from scratch in C.

Motivation

I wanted to deeply understand how databases actually work under the hood — not just how to use them, but how they persist data, manage memory, and parse commands.

Architecture

The engine is built around three core components:

1. The REPL
A read-eval-print loop that accepts SQL-like commands and routes them to the appropriate handler.

2. The Pager
Manages reading and writing fixed-size pages (4KB) to disk. Pages are cached in memory using a simple LRU strategy.

3. B-Tree Storage
Data rows are stored in a B-tree structure for O(log n) lookups. The implementation supports leaf and internal nodes with proper splitting.

Key Lessons

  • Memory layout matters enormously in C. Careful struct packing saved ~30% storage per row.
  • Persistence is harder than it looks — write ordering and fsync are critical for durability.
  • The gap between "works" and "correct" is wide when managing raw disk I/O.

Current State

The engine supports basic INSERT and SELECT with single-table persistence. Work is ongoing to add UPDATE, DELETE, and multi-table joins.

Source: GitHub

Discussion

Sign in to leave a comment.

No comments yet. Be the first.