NanoDB is a lightweight, persistent key-value store built from scratch in C. Designed as an educational prototype, it demonstrates the core concepts behind high-performance databases like Redis and Bitcask.
NanoDB is not just another wrapper around sqlite. It is a custom implementation of a Log-Structured Database.
Traditional databases often struggle with write performance because they need to seek random locations on the disk to update records. NanoDB solves this by using an Append-Only Log architecture, ensuring
NanoDB employs a hybrid storage model combining Disk and RAM:
- Concept: Data is never overwritten. Updates are simply appended to the end of the database file (
.db). - Benefit: Sequential writes are significantly faster than random writes, maximizing disk throughput.
- Persistence: Every
SETcommand is flushed to disk immediately, ensuring durability against crashes.
- Concept: An in-memory index maps keys to their exact byte offset in the file.
- Hashing: Uses the DJB2 algorithm to efficiently hash string keys to integer indices.
-
Performance: Lookups are
$O(1)$ (constant time). The server computes the hash, jumps (fseek) to the file location, and reads the value.
- Communication: Operates over TCP sockets on port
8888. - Interface: Custom text-based protocol similar to Redis (e.g.,
+OK,:1).
- GCC Compiler
- Linux/Unix environment (Socket API)
-
Clone and Compile
# Compile the server source code gcc server.c storage.c -o nanodb_server -
Start the Server
./nanodb_server
Output:
NanoDB Server started on port 8888...
You can interact with NanoDB using netcat (nc) or by writing a custom TCP client.
Open a new terminal window:
nc localhost 8888| Command | Syntax | Description | Example |
|---|---|---|---|
| SET | SET <key> <value> |
Store a key-value pair. | SET user:101 alice |
| GET | GET <key> |
Retrieve value by key. | GET user:101 |
| EXISTS | EXISTS <key> |
Check if a key exists. | EXISTS user:101 |
| STATS | STATS |
Show server statistics. | STATS |
| QUIT | QUIT |
Disconnect from server. | QUIT |
SET user:1 shashank
> +OK
GET user:1
> shashank
EXISTS user:1
> :1
STATS
> Total Keys: 1
server.c- Main entry point. Handles TCP connections and protocol parsing.storage.c- Core database logic. Implementsdb_set,db_get, and file handling.db.h- Header file defining theNanoDBstruct and function prototypes.main.c- A standalone CLI test runner (optional usage).
