LSM-Tree (Log-Structured Merge Tree) is a data structure designed for high write throughput and efficient reads. This API server provides a simple key-value store based on the LSM-Tree architecture.
LSM-Tree (Log-Structured Merge Tree) is a write-optimized data structure commonly used in modern key-value stores such as LevelDB, RocksDB, and Cassandra. It is designed to deliver:
High write throughput by buffering writes in memory and flushing them to disk in batches.
Efficient reads by organizing data into sorted files and using indexes and Bloom filters to reduce disk I/O.
Instead of writing data directly to disk for every operation, LSM-Trees first store data in memory (e.g., a MemTable) and periodically flush it to disk as immutable sorted files called SSTables (Sorted String Tables). This minimizes random writes and maximizes sequential disk access.
You can easily start the API server using Docker and Makefile:
make upThe server will start on port 8080 by default.
To stop the server, run:
make downcurl -X PUT http://localhost:8080/api/put \
-H "Content-Type: application/json" \
-d '{"key": "user:1", "value": "Alice"}'Response:
{
"status": "success",
"message": "Key 'user:1' stored successfully"
}curl http://localhost:8080/api/get/user:1Response:
{
"key": "user:1",
"value": "Alice",
"found": true
}curl -X DELETE http://localhost:8080/api/delete \
-H "Content-Type: application/json" \
-d '{"key": "user:1"}'Response:
{
"status": "success",
"message": "Key 'user:1' deleted successfully"
}curl http://localhost:8080/api/statusResponse:
{
"active_memtable_size": 3,
"immutable_count": 1,
"sstable_stats": {
"level_0": 2,
"level_1": 1
},
"message": "LSM-Tree service is running"
}curl http://localhost:8080/healthResponse:
{
"status": "healthy",
"service": "mini-lsm-table"
}curl -X POST http://localhost:8080/api/recoveryResponse:
{
"status": "success",
"message": "Recovery completed successfully"
}The API returns appropriate HTTP status codes and JSON error messages:
{
"error": "Key cannot be empty"
}200 OK: Successful operation400 Bad Request: Invalid request format or missing required fields404 Not Found: Key not found (for GET operations)405 Method Not Allowed: Incorrect HTTP method500 Internal Server Error: Server-side error
By default, the server stores data in a temporary directory that persists between runs:
- Location:
/tmp/mini_lsm_api/ - Structure:
wal/: Write-Ahead Log filessstables/: SSTable files organized by levels
The API server is built on top of a LSM-Tree storage engine with the following components:
- MemTable: In-memory sorted tree for recent writes
- SSTable: Sorted String Tables for persistent storage
- WAL: Write-Ahead Log for durability
- Compaction: Background process to merge and optimize SSTables
- Block Index: Efficient key lookup within SSTables
- Bloom Filter: Probabilistic data structure to avoid unnecessary disk reads