ThermalKV is a Go key-value store implementing hot in-memory storage with durable persistence and manual cold storage migration. It demonstrates TTL expiration, WAL durability, snapshot recovery, cold storage indexing, and a simple TCP client/server interface.
- Live Documentation: ThermalKV DOCs
- Latest Release: v0.1.0 Release
- Thread-safe in-memory key-value store.
- Per-key TTL expiration.
- Background expiration cleaner.
- Write-Ahead Logging (WAL) durability.
- Snapshot persistence for faster restarts.
- Manual cold storage migration (
COOLcommand). - Automatic cold storage compaction.
- Lazy cold-key restoration on access.
- TCP server/client architecture.
- Graceful shutdown with final snapshot save.
Below is the design and architecture diagram of ThermalKV:
- Active keys are stored in memory.
- Supports fast GET/SET/DEL operations.
- Tracks TTL and last-access times.
- Keys can be manually cooled with
COOL <key>. - Cold entries are persisted to
data/cold.dat. - Cold-key lookup restores valid values back into hot memory.
cmd/server/main.go
- Creates WAL and coldstore manager.
- Initializes the store.
- Recovers state from snapshot, WAL, and cold index.
- Starts TTL cleaner, snapshot loop, cooling worker, and compaction worker.
- Listens on TCP port
8080.
cmd/client/main.go
- Interactive TCP client for sending commands.
- Connects to
localhost:8080. - Reads and prints server responses.
internal/store
Handles core database operations:
- SET, GET, DEL
- TTL registration and expiry
- Cold key migration and lazy restore
- Snapshot export
- Recovery from WAL and snapshot data
- Cold storage compaction
internal/persistence
Handles disk persistence:
- WAL write and replay
- Snapshot save and load
internal/coldstore
Handles:
- Cold data append storage.
- Cold index management.
- Loading cooled keys on demand.
- Cold data compaction.
internal/recover
Handles:
- Snapshot recovery.
- WAL replay for
SET,DEL, andEXPIRE. - Cold index recovery.
internal/ttl
Implements:
- Min-heap expiry queue.
- Background expiration cleanup.
internal/store/compact.go
- Initiates cold storage compaction.
| File | Purpose |
|---|---|
data/wal.log |
Write-Ahead Log |
data/snapshot.dat |
Snapshot persistence |
data/cold.dat |
Cold storage append-only file |
| Command | Description |
|---|---|
SET <key> <value> |
Store or update a value |
GET <key> |
Retrieve a value |
DEL <key> |
Delete a key |
TTL <key> <seconds> |
Set expiration if the key exists |
COOL <key> |
Move a key into cold storage |
COMPACT |
Manually trigger cold storage compaction |
COUNT |
Number of hot keys in memory |
EXISTS <key> |
Check whether a key exists |
KEYS |
List all hot keys |
INFO |
Store statistics |
EXIT |
Close client connection |
SET user1 jenil
OK :)
GET user1
jenil
TTL user1 30
OK :)
EXISTS user1
true
COOL user1
OK :)
GET user1
jenil
The final GET restores the cooled key back into memory.
The server persists mutations to WAL and periodically saves snapshots.
Write-Ahead Log entries are appended to data/wal.log.
Recorded operations:
SETDELEXPIRE
WAL logs are replayed during recovery.
Snapshots save the in-memory state to data/snapshot.dat, reducing recovery time.
On startup the server:
- Loads the latest snapshot.
- Replays WAL entries from
data/wal.log. - Recovers the cold storage index.
- Starts the TTL cleaner.
- Go-based concurrent key-value store.
- RWMutex synchronization.
- Heap-based TTL expiry scheduling.
- Background expiration cleaner.
- WAL and snapshot durability.
- Manual cold storage migration with lazy restoration.
- Automatic cold storage compaction.
- TCP client/server interface.
ThermalKV/
β
βββ assets/
β βββ architecture.png
β
βββ cmd/
β βββ server/
β βββ client/
β
βββ internal/
β βββ coldstore/
β βββ model/
β βββ persistence/
β β βββ snapshot/
β β βββ walpkg/
β βββ recover/
β βββ server/
β βββ store/
β β βββ compact.go
β βββ ttl/
β
βββ data/
β βββ wal.log
β βββ snapshot.dat
β βββ cold.dat
β
βββ tests/
βββ go.mod
βββ README.md
- Go 1.26.2
go run ./cmd/servergo run ./cmd/clientgo test ./...- Latest Release:
v0.1.0 - Status: Active development. The core storage, replication-ready state machine, and persistence layer are stable.
- Hot in-memory storage
- TTL expiration
- WAL persistence
- Snapshot recovery
- Manual cold storage migration
- Automatic cold storage compaction
- Lazy cold restore
- TCP server/client
- Background expiration cleanup
- Automatic hot-to-cold migration
- Warm storage layer
- Access-frequency tracking
- Disk indexing
- Replication support
- Metrics and observability
ThermalKV is a learning-focused database project that explores durability, recovery, memory efficiency, and data temperature management. It combines WALs, snapshots, TTLs, cold storage, and a simple network interface into a compact Go implementation for experimentation and extension.
