Build a high-performance, lock-free relational database engine optimized for ECS (Entity Component System) game engines in Rust. The system prioritizes zero-copy data access, CPU cache efficiency, atomic transactions, and seamless client synchronization through delta-based replication.
Title: ECSDb - High-Performance Entity Component System Database
Mission: Provide game developers with a fast, developer-friendly database that understands ECS architecture natively, eliminating impedance mismatch between database operations and game engine requirements.
Key Differentiators:
- Lock-free atomic operations without mutex overhead
- Double-buffer architecture with delta-only write propagation
- Zero-copy field access via unsafe casting
- Native multi-client replication
- Embedded crate deployment with optional web dashboard
- Entities Table: Central registry of all game entities with unique IDs and versions
- Component Tables: One table per component type, storing dense arrays of component data
- Relation Definitions: Foreign key constraints linking components to entities
- Metadata Tables: Schema definitions, field type information, and cardinality hints
- Row Format: Vec buffer with fixed-width field packing
- Field Alignment: Fields packed contiguously without padding waste
- Memory Layout: Column-oriented for cache efficiency within related entity groups
- Casting Strategy: User-defined fields cast via unsafe pointer operations after bounds checking
- One-to-Many: Entity → Multiple Components
- Many-to-One: Components → Single Entity
- Optional Relations: Sparse component storage for entities without certain components
- Relation Integrity: Referential constraints enforced at transaction boundaries
- Atomicity: All-or-nothing for CRUD operations using write-ahead logging
- Consistency: Schema validation and referential integrity checks pre-commit
- Isolation: Double-buffer model isolates readers from writers
- Durability: Parallel disk persistence with snapshot mechanism
- Default Behavior: All operations wrapped in implicit transactions
- Explicit Transactions: Multi-operation grouping with rollback capability
- No Locks: Lock-free via atomic operations and message passing
- Conflict Resolution: Last-write-wins with vector clock tracking for distributed writes
- Read Buffer: Current committed state, accessible to all readers
- Write Buffer: In-progress changes, atomic swap on transaction commit
- Delta Storage: Write buffer tracks only changed rows/fields (not full table copy)
- Copy-on-Write Semantics: Shared immutable snapshots avoid duplication
- Direct Casting: User types cast from raw buffer pointers post-validation
- Lifetime Management: Borrowed references tied to buffer lifetime
- Type Safety: Compile-time field offset validation where possible
- Alignment Guarantees: Unsafe code verified for correct memory access
- Dense Packing: No field padding; custom alignment handling
- Sparse Components: Entities missing components use bitmap indices
- Allocation Minimization: Pre-allocated buffer growth with exponential scaling
- Reuse Pools: Deleted entity IDs recycled with version counters
- Atomic Swaps: Readers switch buffers via atomic pointer swap
- Message Passing: Write operations queue through MPSC channel
- Single Writer: Dedicated write thread processes transaction log sequentially
- Memory Ordering: Acquire/Release semantics for cross-thread visibility
- Write-Ahead Logging (WAL): Operations logged before buffer modification
- Batch Processing: Accumulated writes committed in configurable intervals
- Atomic Commit: All-or-nothing buffer swap with version bump
- Rollback Capability: WAL replay from checkpoint on failure
- Delta-Based Sync: Only changed rows/fields transmitted to clients
- Full Initial Sync: Complete dataset on first client connection
- Incremental Updates: Efficient patch application for remote changes
- Conflict Resolution: Server-authoritative with timestamp/version tracking
- Binary Serialization: Compact binary format for over-wire transmission
- Compression: Optional zstd compression for large deltas
- Batching: Multiple deltas grouped into single network frame
- Acknowledgment: Client ACK prevents redundant retransmission
- Eventual Consistency: Clients eventually catch up to server state
- Causal Ordering: Related operations maintain dependency order
- Broadcast Ordering: Updates from single session strictly ordered
- Idempotent Operations: Safe replay of duplicate delta packets
- Async I/O: Non-blocking writes using Tokio background tasks
- Snapshot Format: Point-in-time database dumps for quick recovery
- WAL Archival: Transaction logs preserved for point-in-time recovery
- Compaction: Periodic merging of snapshots and WAL
- TOML Format: Human-readable schema definitions
- Version Tracking: Schema evolution with migration tracking
- Metadata Storage: Table structure, field types, constraints
- Embedded Configuration: Schema compiled into binary or loaded at runtime
| Feature | Description | Priority |
|---|---|---|
| Entity Management | Create, read, update, delete entities with version tracking | P0 |
| Component Storage | Dense component tables with zero-copy access | P0 |
| Entity-Component Relations | Foreign key references maintaining referential integrity | P0 |
| Atomic Transactions | Lock-free ACID transactions with WAL | P0 |
| Double Buffering | Read/write buffer separation with atomic swaps | P0 |
| Delta Tracking | Efficient change log for replication | P0 |
| In-Memory Operation | Full database in RAM with zero serialization overhead | P0 |
| Disk Persistence | Asynchronous snapshot and WAL writing | P0 |
| TOML Schema | Schema definition and migration in TOML format | P0 |
| Rust API | Type-safe, ergonomic database operations | P0 |
| Feature | Description | Priority |
|---|---|---|
| Client Replication | Multi-client delta sync via network | P1 |
| Web Dashboard | Tauri 2 + Vue 3 schema editor and data viewer | P1 |
| Batch Operations | Bulk insert/update/delete with transaction grouping | P1 |
| Query Optimization | Spatial indices, relation graph indexing | P2 |
| Hot Reload | Schema changes without full database restart | P2 |
| Backup/Restore | Point-in-time recovery from snapshots | P2 |
| Metrics Collection | Performance monitoring and latency tracking | P2 |
| Event Streaming | Pub/sub for component changes | P2 |
┌─────────────────────────────────────────────────────┐
│ Application Layer (Game Engine) │
├─────────────────────────────────────────────────────┤
│ Rust Database API │
├─────────────────────────────────────────────────────┤
│ ┌─────────────────────────────────────────────┐ │
│ │ Transaction Engine (Single Writer) │ │
│ │ ├─ WAL Logger │ │
│ │ ├─ Delta Tracker │ │
│ │ ├─ Conflict Resolver │ │
│ │ └─ Replication Broadcaster │ │
│ └─────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Double Buffer Storage Layer │ │
│ │ ┌──────────────────────────────────────┐ │ │
│ │ │ Active Read Buffer (Vec<u8> per tbl) │ │ │
│ │ └──────────────────────────────────────┘ │ │
│ │ ┌──────────────────────────────────────┐ │ │
│ │ │ Write Buffer (Delta changes) │ │ │
│ │ └──────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────┐ │
│ │ Persistence Layer (Async I/O) │ │
│ │ ├─ Snapshot Manager │ │
│ │ ├─ WAL Archive │ │
│ │ └─ Compaction Worker │ │
│ └─────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────┤
│ Schema Manager (TOML Parser & Validator) │
├─────────────────────────────────────────────────────┤
│ Network Layer (Optional: Client Replication) │
├─────────────────────────────────────────────────────┤
│ Dashboard UI (Optional: Tauri + Vue 3) │
└─────────────────────────────────────────────────────┘
core::schema
- Schema parsing and validation
- TOML serialization/deserialization
- Field type system and constraints
- Migration tracking
core::storage
- Vec buffer management
- Memory layout calculation
- Field offset computation
- Alignment guarantee verification
core::entity
- Entity ID generation and versioning
- Entity registry table
- Component archetype tracking
- Sparse set membership
core::transaction
- Transaction state machine
- Write-ahead logging
- Conflict detection
- Atomic commit protocol
core::replication
- Delta calculation and encoding
- Binary serialization format
- Broadcast message queuing
- Client synchronization protocol
core::persistence
- Snapshot creation and restoration
- WAL file management
- Async I/O coordination
- Compaction scheduling
Database Handle
- Entry point for all operations
- Configuration and initialization
- Table access and iteration
- Transaction management
Table Interface
- Type-safe component table wrapper
- Row insertion/update/deletion
- Entity filtering and queries
- Batch operations
Transaction Builder
- Fluent API for multi-step operations
- Automatic rollback on panic
- Resource cleanup guarantees
Buffer Manager
- Allocates and resizes Vec buffers
- Tracks active/inactive buffer state
- Manages buffer swap coordination
- Monitors memory pressure
Field Codec
- Converts user types to/from bytes
- Handles alignment and padding
- Validates field layout at runtime
- Provides unsafe casting wrappers
Application
↓
API Call (Insert/Update/Delete)
↓
Transaction Builder
↓
Serialize to WAL Entry
↓
Queue to Write Channel
↓
Write Thread (Receives via MPSC)
↓
Modify Write Buffer
↓
Update Delta Tracker
↓
[Commit Batch Trigger or Explicit Commit]
↓
Atomic Swap: Write Buffer → Read Buffer
↓
Bump Version Number
↓
Broadcast Deltas to Replication Clients
↓
Queue Snapshot/WAL Async Flush
↓
Return Success to Application
Application
↓
Query API Call
↓
Acquire Read Buffer Reference
↓
Calculate Field Offset
↓
Unsafe Cast to User Type (T)
↓
Return Borrowed Reference (Lifetime Tied to Buffer)
↓
Application Uses Data
↓
Drop Borrowed Reference → Release Buffer Lock
Write Commit (Server)
↓
Delta Tracker Records Changes
↓
Serialize Delta Batch
↓
Broadcast to All Connected Clients
↓
Client Receives Delta
↓
Validate Against Local Schema
↓
Apply Changes to Local Buffer
↓
Send ACK Back to Server
Read Operations:
- Multiple readers access read buffer simultaneously
- No locks; atomic reference counting ensures buffer validity
- Readers progress independently
Write Operations:
- Single writer thread processes all mutations
- Writes queued via MPSC channel (lock-free queue)
- Application threads don't block; returns immediately
Commit Phase:
- Atomic pointer swap for buffer switch (1 CPU instruction)
- All readers atomically see new committed state
- Version number increment signals update availability
- Acquire Ordering on read buffer pointer load (ensures delta visibility)
- Release Ordering on buffer swap (ensures all writes visible to readers)
- Sequential Consistency within single transaction (per-thread program order)
[database]
name = "game_db"
version = "1.0.0"
[tables.entities]
primary_key = "id"
[[tables.entities.fields]]
name = "id"
type = "u64"
constraint = "unique"
[[tables.entities.fields]]
name = "version"
type = "u32"
auto_increment = true
[tables.transform]
parent_table = "entities"
[[tables.transform.fields]]
name = "entity_id"
type = "u64"
foreign_key = "entities.id"
[[tables.transform.fields]]
name = "position_x"
type = "f32"
[[tables.transform.fields]]
name = "position_y"
type = "f32"
[[tables.transform.fields]]
name = "position_z"
type = "f32"
[tables.health]
parent_table = "entities"
[[tables.health.fields]]
name = "entity_id"
type = "u64"
foreign_key = "entities.id"
[[tables.health.fields]]
name = "hp"
type = "u32"
[[tables.health.fields]]
name = "max_hp"
type = "u32"Supported types:
- Primitives: u8, u16, u32, u64, i8, i16, i32, i64, f32, f64, bool
- Fixed Arrays:
[T; N](stored inline) - Enums: Enumerated types with discriminant
- Structs: User-defined composite types (if layout is well-defined)
- Strings: Fixed-size byte arrays or references
- Schema Versioning: Track version number with each table definition
- Migration Tracking: Log transformations applied to schema
- Backward Compatibility: Support field additions and deprecations
- Zero-Downtime Updates: Hot schema reload without stopping writes
| Metric | Target | Notes |
|---|---|---|
| Insert Latency | <1μs | Single row, non-batch |
| Update Latency | <1μs | Field-level updates |
| Delete Latency | <1μs | Logical delete with version |
| Read Latency | <100ns | Zero-copy access with cast |
| Query Throughput | >1M ops/sec | Sequential table scans |
| Replication Lag | <10ms | Delta broadcast to clients |
| Snapshot Time | <100ms | Full database dump |
| Memory Overhead | <10% | Schema, metadata, indices |
| Disk Write Latency | Background | Non-blocking async I/O |
- Schema parser and TOML support
- Entity table with ID generation
- Component storage with Vec buffers
- Basic CRUD operations
- Double buffer implementation
- Transaction state machine
- Write-ahead logging
- Delta tracking system
- Atomic commit protocol
- Referential integrity checks
- Sparse component handling
- Lock-free write queue (MPSC)
- Memory efficient buffering
- Field codec system
- Snapshot creation/restoration
- WAL archival and replay
- Async I/O integration (Tokio)
- Compaction worker
- Crash recovery
- Benchmark suite
- Client connection management
- Delta serialization format
- Network broadcast mechanism
- Conflict resolution
- Full-sync protocol
- Incremental sync
- Client library
- Tauri 2 app skeleton
- Vue 3 schema editor
- Data viewer component
- Query builder UI
- Integration testing
- Performance profiling
- Documentation
- Language: Pure Rust, no unsafe except verified critical paths
- Testing: Unit tests >80% coverage, integration tests for all features
- Documentation: Inline code docs, API guide, architecture ADRs
- Benchmarking: Criterion benchmarks for all hot paths
- Library Distribution: Published to crates.io
- Embedded Deployment: Zero external dependencies beyond Tokio
- Dashboard Distribution: Standalone Tauri executable
- Configuration: Environment variables + TOML config files
- Module Organization: Clear separation of concerns
- Error Handling: Custom error types with context
- Logging: Tracing integration for observability
- Version Compatibility: Semver compliance
- MVP database performs 1M ops/sec with <1μs latency
- Zero-copy reads verified via profiling (no extra allocations)
- Atomic transactions pass ACID compliance tests
- Lock-free write queue passes contention benchmarks
- Multi-client replication syncs within 10ms
- Schema TOML format matches spec, auto-validates
- Dashboard launches with Tauri, loads/edits schemas
- All major code paths covered by tests
- Public documentation complete and code examples work
| Risk | Probability | Impact | Mitigation |
|---|---|---|---|
| Memory safety issues | Medium | High | Extensive testing of unsafe code, use MIRI |
| Lock-free complexity | Medium | High | Thorough code review, concurrency tests |
| Delta serialization bugs | Medium | Medium | Property-based testing, fuzzing |
| Schema evolution issues | Low | High | Comprehensive migration tests |
| Replication inconsistency | Medium | High | Conflict resolution tests, server authority |
| Performance regression | Medium | High | Continuous benchmarking in CI |