Files
weaseldb/design.md
2025-08-15 20:39:45 -04:00

5.3 KiB

WeaselDB Design Overview

Project Summary

WeaselDB is a high-performance write-side database component designed for systems where reading and writing are decoupled. The system focuses exclusively on handling transactional commits with optimistic concurrency control, while readers are expected to maintain their own queryable representations by subscribing to change streams.

Architecture Overview

Core Components

1. Arena Allocator (src/arena_allocator.hpp)

  • Ultra-fast memory allocator (~1ns per allocation vs ~20-270ns for malloc)
  • Lazy initialization with geometric block growth (doubling strategy)
  • Intrusive linked list design for minimal memory overhead
  • Memory-efficient reset that keeps the first block and frees others
  • STL-compatible interface via ArenaStlAllocator

Key features:

  • O(1) amortized allocation
  • Proper alignment handling for all types
  • Move semantics for efficient transfers
  • Requires trivially destructible types only

2. Commit Request Parser (src/commit_request.{hpp,cpp})

  • High-performance JSON parser using weaseljson library
  • Streaming parser support for incremental parsing of network data
  • Arena-based string storage for zero-copy string handling
  • Base64 decoding for binary key/value data
  • Comprehensive validation of transaction structure

Parser capabilities:

  • One-shot parsing for complete JSON
  • Streaming parsing for network protocols
  • Parse state management with error recovery
  • Memory-efficient string views backed by arena storage

3. Configuration System (src/config.{hpp,cpp})

  • TOML-based configuration using toml11 library
  • Structured configuration with server, commit, and subscription sections
  • Default fallback values for all configuration options
  • Type-safe parsing with validation

Configuration domains:

  • Server: bind address, port, request size limits
  • Commit: request ID validation, retention policies
  • Subscription: buffer management, keepalive intervals

Data Model

Transaction Structure

CommitRequest {
  - request_id: Optional unique identifier
  - leader_id: Expected leader for consistency
  - read_version: Snapshot version for preconditions
  - preconditions[]: Optimistic concurrency checks
    - point_read: Single key existence/content validation
    - range_read: Range-based consistency validation
  - operations[]: Ordered mutation operations
    - write: Set key-value pair
    - delete: Remove single key
    - range_delete: Remove key range
}

Memory Management

  • Arena-based allocation ensures efficient bulk memory management
  • String views eliminate unnecessary copying of JSON data
  • Zero-copy design for binary data handling
  • Automatic memory cleanup on transaction completion

API Design

The system implements a RESTful API with three core endpoints:

  1. GET /v1/version: Retrieve current committed version and leader
  2. POST /v1/commit: Submit transactional operations
  3. GET /v1/subscribe: Stream committed transactions (implied)
  4. GET /v1/status: Check commit status by request_id (implied)

Performance Characteristics

Memory Allocation

  • ~1ns allocation time vs standard allocators
  • Bulk deallocation eliminates individual free() calls
  • Optimized geometric growth uses current block size for doubling strategy
  • Alignment-aware allocation prevents performance penalties

JSON Parsing

  • Streaming parser handles large payloads efficiently
  • Incremental processing suitable for network protocols
  • Arena storage eliminates string allocation overhead
  • SIMD-accelerated base64 decoding using simdutf for maximum performance

Design Principles

  1. Performance-first: Every component optimized for high throughput
  2. Memory efficiency: Arena allocation eliminates fragmentation
  3. Zero-copy: Minimize data copying throughout pipeline
  4. Streaming-ready: Support incremental processing
  5. Type safety: Compile-time validation where possible
  6. Resource management: RAII and move semantics throughout

Testing & Benchmarking

The project includes comprehensive testing infrastructure:

  • Unit tests using doctest framework
  • Performance benchmarks using nanobench
  • Memory allocation benchmarks for arena performance
  • JSON parsing validation for correctness

Build targets:

  • test_arena_allocator: Arena allocator functionality tests
  • test_commit_request: JSON parsing and validation tests
  • weaseldb: Main application demonstrating configuration and parsing
  • Various benchmark executables for performance testing

Dependencies

  • weaseljson: High-performance streaming JSON parser
  • simdutf: SIMD-accelerated UTF-8 validation and base64 encoding/decoding
  • toml11: TOML configuration file parsing
  • doctest: Lightweight testing framework
  • nanobench: Micro-benchmarking library

Future Considerations

This write-side component is designed to integrate with:

  • Leader election systems for distributed consensus
  • Replication mechanisms for fault tolerance
  • Read-side systems that consume the transaction stream
  • Monitoring systems for operational visibility

The modular design allows each component to be optimized independently while maintaining clear interfaces for system integration.