SierraDB: 用于事件的不可变数据库
A high-performance, distributed event store designed for scalable event sourcing applications
SierraDB is a modern, horizontally-scalable database specifically built for event sourcing workloads. It combines the simplicity of Redis protocol compatibility with the distributed architecture principles of Cassandra/ScyllaDB, providing developers with a powerful foundation for building event-driven systems.
Want to understand how SierraDB works under the hood? Read the detailed blog post: Building SierraDB: A Distributed Event Store Built in Rust
The post covers the architecture decisions, watermark system for consistency, distributed consensus, and the journey of building an event store from scratch.
Get SierraDB running with Docker and start storing events immediately:
…That's it! You're now running a distributed event store and can append/read events using any Redis client.
# Run with persistent data
docker run -p 9090:9090 -v ./data:/app/data tqwewe/sierradbcargo install sierradb-server
sierradb --dir ./data --client-address 0.0.0.0:9090# Connect with any Redis client
redis-cli -p 9090
# Append events to streams
EAPPEND order-456 OrderCreated EXPECTED_VERSION empty PAYLOAD '{"total":99.99,"items":["laptop"]}'
# Read events back
ESCAN orders - + COUNT 100
# Subscribe to real-time events
ESUB orders FROM LATESTSince SierraDB uses the RESP3 protocol, you can also connect with any Redis-compatible client:
import redis
# Connect to SierraDB
client = redis.Redis(host='localhost', port=9090, protocol=3)
# Append an event (use 'empty' for new streams, or a version number for existing ones)
result = client.execute_command('EAPPEND', 'user-123', 'UserCreated',
'EXPECTED_VERSION', 'empty',
'PAYLOAD', '{"name":"John","email":"[email protected]"}')
# Read events from stream
events = client.execute_command('ESCAN', 'user-123', '-', '+', 'COUNT', '100')SierraDB organizes data using a three-tier hierarchy designed for optimal performance and scalability:
SierraDB is optimized for three primary read patterns:
Each segment file is accompanied by specialized index files and bloom filters for maximum query performance.
SierraDB operates as a distributed cluster similar to Cassandra/ScyllaDB:
One of SierraDB's most powerful features is its subscription system, designed specifically for event sourcing projections:
Perfect for building:
SierraDB offers extensive configuration options. Key settings include:
…See crates/sierradb-server/src/config.rs for the complete configuration reference.
SierraDB implements a comprehensive set of RESP3 commands for event operations:
EAPPEND - Append Event to StreamAppend an event to a stream with expected version control.
Note: By default, EXPECTED_VERSION must be empty (for new streams) or a specific version number. To allow any or exists, set append.strict_versioning = false in your configuration.
Syntax:
EAPPEND [EVENT_ID ] [PARTITION_KEY ] [EXPECTED_VERSION ] [TIMESTAMP ] [PAYLOAD ] [METADATA ]Parameters:
stream_id - Stream identifier to append the event toevent_name - Name/type of the event event_id (optional) - UUID for the event (auto-generated if not provided)partition_key (optional) - UUID to determine event partitioningexpected_version - Expected stream version (empty for new streams, or version number)timestamp (optional) - Event timestamp in millisecondspayload (optional) - Event payload datametadata (optional) - Event metadataExamples:
EAPPEND my-stream UserCreated EXPECTED_VERSION empty PAYLOAD '{"name":"john"}' METADATA '{"source":"api"}'
EAPPEND orders OrderPlaced EXPECTED_VERSION 5 PAYLOAD '{"order_id":"12345"}'EMAPPEND - Multi-Stream Transactional AppendAppend multiple events across different streams within the same partition atomically.
Syntax:
EMAPPEND [EVENT_ID ] [EXPECTED_VERSION ] [TIMESTAMP ] [PAYLOAD ] [METADATA ] [ ...]Parameters:
partition_key - UUID that determines which partition all events will be written tostream_id, event_name, and optional EVENT_ID, EXPECTED_VERSION, TIMESTAMP, PAYLOAD, METADATAExamples:
EMAPPEND 550e8400-e29b-41d4-a716-446655440000 stream1 EventA EXPECTED_VERSION empty PAYLOAD '{"data":"value1"}' stream2 EventB EXPECTED_VERSION 3 PAYLOAD '{"data":"value2"}'Note: All events succeed or fail together as a single atomic transaction.
EGET - Get Event by IDRetrieve an event by its unique identifier.
Syntax:
EGET Parameters:
event_id - UUID of the event to retrieveExamples:
EGET 550e8400-e29b-41d4-a716-446655440000ESCAN - Scan Stream EventsScan events in a stream by version range.
Syntax:
ESCAN [PARTITION_KEY ] [COUNT ]Parameters:
stream_id - Stream identifier to scanstart_version - Starting version number (use "-" for beginning)end_version - Ending version number (use "+" for end, or specific number) partition_key (optional) - UUID to scan specific partitioncount (optional) - Maximum number of events to returnExamples:
ESCAN my-stream 0 100 COUNT 50
ESCAN my-stream - + PARTITION_KEY 550e8400-e29b-41d4-a716-446655440000ESVER - Get Stream VersionGet the current version number for a stream.
Syntax:
ESVER [PARTITION_KEY ]Parameters:
stream_id - Stream identifier to get version forpartition_key (optional) - UUID to check specific partitionExamples:
ESVER my-stream
ESVER my-stream PARTITION_KEY 550e8400-e29b-41d4-a716-446655440000ESUB - Subscribe to Stream EventsSubscribe to events from one or more streams with real-time delivery.
Syntax:
# Single stream
ESUB [PARTITION_KEY ] [FROM ] [WINDOW ]
# Multiple streams
ESUB [PARTITION_KEY ] [PARTITION_KEY ] ... [FROM LATEST | FROM | FROM MAP =...] [WINDOW ]Parameters:
stream_id - Stream identifier(s) to subscribe topartition_key (optional) - UUID for specific partitionFROM (optional) - Starting position (LATEST, version number, or MAP for per-stream positions)WINDOW (optional) - Maximum number of unacknowledged eventsExamples:
ESUB user-123 # Single stream, latest, no window
ESUB user-123 WINDOW 100 # Single stream, latest, window 100
ESUB user-123 FROM 50 WINDOW 100 # Single stream, from version 50
ESUB user-1 user-2 user-3 FROM MAP user-1=10 user-2=20 user-3=30 WINDOW 50Note: Establishes a persistent connection for real-time event delivery.
EPSCAN - Scan Partition EventsScan events in a partition by sequence number range.
Syntax:
EPSCAN [COUNT ]Parameters:
partition - Partition selector (partition ID 0-65535 or UUID key)start_sequence - Starting sequence number (use "-" for beginning)end_sequence - Ending sequence number (use "+" for end, or specific number)count (optional) - Maximum number of events to returnExamples:
EPSCAN 42 100 200 COUNT 50
EPSCAN 550e8400-e29b-41d4-a716-446655440000 - + COUNT 100EPSEQ - Get Partition SequenceGet the current sequence number for a partition.
Syntax:
EPSEQ Parameters:
partition - Partition selector (partition ID 0-65535 or UUID key)Examples:
EPSEQ 42
EPSEQ 550e8400-e29b-41d4-a716-446655440000EPSUB - Subscribe to Partition EventsSubscribe to events from one or more partitions with real-time delivery.
Syntax:
# All partitions
EPSUB * [FROM LATEST | FROM | FROM MAP = =... [DEFAULT ]] [WINDOW ]
# Single partition
EPSUB [FROM ] [WINDOW ]
# Multiple partitions
EPSUB ,, [FROM LATEST | FROM | FROM MAP = =... [DEFAULT ]] [WINDOW ]Parameters:
partition_id - Partition identifier(s) (* for all, comma-separated list for multiple)FROM (optional) - Starting position (LATEST, sequence number, or MAP for per-partition positions) WINDOW (optional) - Maximum number of unacknowledged eventsExamples:
EPSUB * # All partitions, latest
EPSUB * WINDOW 100 # All partitions, latest, window 100
EPSUB * FROM 1000 WINDOW 100暂无开放 Issues,或尚未同步最近议题。