A production-inspired, multithreaded in-memory key-value database built with Modern C++17 and Linux system programming.
Mini Key-Value Store is a production-inspired backend systems project that implements the core components of an in-memory database from scratch using Modern C++17.
The goal of the project is not to compete with production databases, but to understand how they work internally by building the fundamental pieces yourself.
The server supports multiple concurrent clients over TCP, executes commands using a thread pool, stores data in memory, persists writes through an Append-Only Log (AOF), restores state during startup, supports key expiration (TTL), snapshots the database to disk, and performs graceful shutdown using POSIX signal handling.
This project was built as a systems programming exercise to gain hands-on experience with networking, concurrency, synchronization, persistence, and backend architecture.
---
The server accepts TCP client connections, parses incoming commands, dispatches work to a fixed-size thread pool, and executes operations on a thread-safe sharded storage engine. Write operations are persisted through an Append-Only Log (AOF), snapshots are generated for faster recovery, and TTL management removes expired keys in the background.
./build/app
FATAL: ThreadSanitizer: unexpected memory mapping 0x61b36c730000-0x61b36c735000
If the above error comes paste the below command and then rerun
echo 0 | sudo tee /proc/sys/kernel/randomize_va_spaceDetects memory errors: heap overflow, use-after-free, double free, memory leaks.
cmake -DUSE_ASAN=ON ..
make
./build/appcmake -DUSE_UBSAN=ON ..
make
./build/app- POSIX socket-based TCP server
- Line-based text protocol
- Multiple concurrent client connections
- Per-client receive buffers
- Graceful connection handling
- Thread-safe in-memory key-value database
std::unordered_mapbased storage- 16-shard architecture to reduce lock contention
- Reader-writer synchronization using
std::shared_mutex - Constant average-time (
O(1)) key lookups
- Fixed-size thread pool
- Producer-consumer task queue
std::mutexstd::shared_mutexstd::condition_variable- RAII-based synchronization
- Append-Only Log (AOF)
- Crash recovery
- Snapshot generation
- Log compaction
- TTL support
- Lazy expiration
- Background cleanup thread
- Configurable maximum memory limit
- Automatic eviction when memory limit is reached
- FIFO / Random eviction policy
- Memory accounting for stored entries
- Graceful shutdown using
SIGINT - Proper thread cleanup
- RAII resource management
- Clean socket shutdown
- Modern C++17
- CMake
- Linux
- GDB
- AddressSanitizer
- ThreadSanitizer
mini-kv-store/
│
├── inc/
│ ├── network/
│ ├── database/
|
├── src/
│ ├── network/
│ ├── database/
│ └── main.cpp
│
├── tests/
│
├── data/
│ ├── appendonly.log
│ └── snapshot.db
│
├── CMakeLists.txt
├── README.md
└── .gitignore
- Linux
- C++17 compatible compiler (GCC or Clang)
- CMake 3.16+
- Make
git clone https://github.com/arham6606/Mini_kv_store.git
cd Mini_kv_storemkdir build
cd build
cmake ..
make./build/appThe server starts listening on:
127.0.0.1:6379
Open another terminal.
nc 127.0.0.1 6379| Command | Description | Example |
|---|---|---|
PING |
Check server availability | PING |
SET |
Store a key-value pair | SET name Arham |
GET |
Retrieve a value | GET name |
DEL |
Delete a key | DEL name |
SIZE |
Return total number of keys | SIZE |
EXIT |
Close the client connection | EXIT |
$ nc 127.0.0.1 6379
PING
PONG
SET name Arham
OK
GET name
Arham
EXISTS name
1
SIZE
1
DEL name
1
GET name
(nil)
EXIT
The server supports graceful termination using SIGINT.
Pressing Ctrl + C:
- Stops accepting new client connections
- Notifies worker threads to exit
- Waits for active threads to finish
- Closes all open sockets
- Flushes pending persistent data
- Releases allocated resources through RAII
- Terminates cleanly without leaving orphaned threads
The current implementation focuses on understanding backend infrastructure concepts. Possible future enhancements include:
- Event-driven networking with
epoll - Non-blocking sockets
- RESP-compatible protocol
- LRU/LFU eviction policies
- Replication
- Authentication
- Configuration file support
- Metrics endpoint
- Performance benchmarking
- Asynchronous disk I/O
| Category | Technology |
|---|---|
| Language | C++17 |
| Platform | Linux |
| Networking | POSIX Sockets |
| Concurrency | std::thread, std::mutex, std::shared_mutex, std::condition_variable, std::unique_lock |
| Storage | std::unordered_map |
| Build System | CMake |
| Debugging | GDB, AddressSanitizer, ThreadSanitizer |
This project was built to gain hands-on experience with:
- Linux system programming
- TCP/IP networking
- Concurrent programming
- Thread synchronization
- Producer-consumer architecture
- Thread pools
- In-memory database design
- Append-only persistence
- Snapshot-based recovery
- Key expiration (TTL)
- Storage sharding
- Modern C++ resource management (RAII)
This project is licensed under the MIT License.
See the LICENSE file for details.