felixkwekuco@gmail.com

SimKV: A Simple Log-Structured Hash Table Key-Value Store - 29/06/2025

An introduction to building a simple, immutable key-value store using Log-Structured Hash Table architecture. Learn the core concepts behind modern database systems like LevelDB and RocksDB.

SimKV: A Simple Log-Structured Hash Table Key-Value Store

Introduction

SimKV is a simple, immutable key-value store implementation based on Log-Structured Hash Table (LSHT) architecture. This project demonstrates the core concepts behind modern database systems like LevelDB, RocksDB, and Cassandra, providing a hands-on learning experience for understanding how these powerful, complex systems work under the hood.

What is a Log-Structured Hash Table?

A Log-Structured Hash Table (LSHT) is a storage architecture that combines the benefits of log-structured storage with hash-based indexing for fast lookups. Here’s how it works:

Core Principles

  1. Append-Only Log: All writes are appended to the end of a log file, making writes extremely fast (no random seeks)
  2. In-Memory Index: A hash table in memory maps keys to their positions in the log file
  3. Immutable Records: Once written, records are never modified in place - updates create new versions
  4. Tombstone Markers: Deletions are marked with special “tombstone” flags rather than removing data

Why This Architecture?

Record Structure

Each record in SimKV follows a binary format optimized for fast serialization and deserialization:

┌─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┐
│ Timestamp   │ Key Length  │ Value Length│ Tombstone   │ Key Data    │ Value Data  │
│ (4 bytes)   │ (4 bytes)   │ (4 bytes)   │ (1 byte)    │ (variable)  │ (variable)  │
└─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┘

Field Descriptions

Why Variable Lengths?

Since we don’t know the size of keys and values in advance, we store their lengths in the header. This allows us to:

  1. Skip Records: When iterating, we can calculate the next record’s position
  2. Validate Data: Ensure we read the correct amount of data
  3. Handle Different Sizes: Support keys and values of any reasonable size

Architecture Overview

SimKV consists of several key components:

Storage Engine

The core storage engine manages:

Query Layer

Handles user operations:

Index Structure

An in-memory hash table maps string keys to file offsets:

type RecordStore struct {
    mu    sync.RWMutex
    File  *os.File
    index map[string]int64  // key -> file offset
}

Key Features

1. Thread-Safe Operations

All operations are protected by mutexes to ensure thread safety:

func (s *RecordStore) Put(key, value []byte) error {
    s.mu.Lock()
    defer s.mu.Unlock()
    // ... implementation
}

2. Crash Recovery

On startup, the index is rebuilt by scanning the log file:

func (s *RecordStore) buildIndex() error {
    var offset int64
    for {
        record, err := s.readData(offset)
        if err == io.EOF {
            break
        }
        // Rebuild index from log
    }
}

3. Tombstone Support

Deletions are handled by writing tombstone markers:

func (s *RecordStore) Delete(key []byte) error {
    record := &Record{
        Key:       key,
        Tombstone: true,  // Mark as deleted
        Timestamp: uint32(time.Now().Unix()),
    }
    // Write tombstone to log
}

Usage Examples

Basic Operations

// Create a new store
store, err := NewRecordStore()
if err != nil {
    log.Fatal(err)
}
defer store.Close()

// Store data
err = store.Put([]byte("name"), []byte("Felix"))
err = store.Put([]byte("age"), []byte("16"))

// Retrieve data
value, err := store.Get([]byte("name"))
fmt.Println(string(value)) // Output: Felix

// Delete data
err = store.Delete([]byte("age"))

// Iterate through all records
iter, err := store.Iterator()
for iter.Next() {
    record, err := iter.Record()
    fmt.Printf("Key: %s, Value: %s\n", string(record.Key), string(record.Value))
}

Building and Running

Prerequisites

Build the Project

# Clone the repository (if applicable)
git clone <repository-url>
cd simkv

# Build the executable
go build -o simkv .

# Or build for specific platforms
GOOS=linux GOARCH=amd64 go build -o simkv-linux .
GOOS=darwin GOARCH=amd64 go build -o simkv-mac .

Run the Project

# Run the example program
./simkv

# Expected output:
# Felix
# Key: age, Value: 16

Development

# Run tests (if available)
go test ./...

# Run with race detection
go run -race .

# Format code
go fmt ./...

# Check for issues
go vet ./...

File Organization

simkv/
├── main.go          # Example usage and entry point
├── store.go         # Core storage engine implementation
├── scan_store.go    # Iterator implementation
├── go.mod           # Go module definition
└── README.md        # This file

Future Enhancements

The current implementation is a foundation that can be extended with:

  1. Compaction: Remove old versions and tombstones to reclaim space
  2. Multiple Log Files: Support for multiple log files (SSTables)
  3. Bloom Filters: Fast non-existence checks
  4. Compression: Reduce storage requirements
  5. Transactions: ACID guarantees
  6. Persistence: Better crash recovery mechanisms

Learning Value

This project demonstrates several important database concepts:

References

For deeper understanding of key-value store internals and database design:

Conclusion

SimKV provides a hands-on introduction to the internals of modern key-value stores. While simple, it demonstrates the core principles that power systems like LevelDB, RocksDB, and Cassandra. The append-only log structure, in-memory indexing, and immutable records are fundamental concepts in modern database design.

By understanding these concepts through implementation, you’ll have a solid foundation for working with and contributing to more complex database systems.


Note: This is a learning project. For production use, consider established solutions like LevelDB, RocksDB, or BadgerDB.