Features
- In-memory database
- Multi-version concurrency control
- Rich transaction support with rollbacks
- Multiple concurrent readers without locking
- Multiple concurrent writers without locking
- Support for serializable, snapshot isolated transactions
- Atomicity, Consistency, Isolation, and optional Durability from ACID
- Optional persistence with configurable modes:
- Support for synchronous and asynchronous append-only logging
- Support for periodic full-datastore snapshots
- Support for fsync on every commit, or periodically in the background
- Support for LZ4 snapshot file compression
Quick start
use ;
Manual cleanup and garbage collection
Background worker threads perform cleanup and garbage collection at regular
intervals. These workers can be disabled through DatabaseOptions by setting
enable_cleanup or enable_gc to false. When disabled, the tasks can be
triggered manually using the run_cleanup and run_gc methods.
use ;
Persistence modes
SurrealMX supports optional persistence with two modes:
Full persistence (AOL + Snapshots) - Default
Provides maximum durability by logging every change to an append-only log and taking periodic snapshots.
use ;
use Duration;
Snapshot-only persistence
Provides good performance with periodic durability by taking snapshots without logging individual changes.
use ;
use Duration;
Configuration Options
AOL Modes
AolMode::Never: Disables append-only logging entirely (default)AolMode::SynchronousOnCommit: Writes changes to AOL immediately on every commit (maximum durability)AolMode::AsynchronousAfterCommit: Writes changes to AOL asynchronously after every commit (better performance)
Snapshot Modes
SnapshotMode::Never: Disables snapshots entirely (default)SnapshotMode::Interval(Duration): Takes snapshots at the specified interval
Fsync Modes
FsyncMode::Never: Never calls fsync - fastest but least durable (default)FsyncMode::EveryAppend: Calls fsync after every AOL append - slowest but most durableFsyncMode::Interval(Duration): Calls fsync at most once per interval - balanced approach
Compression Support
CompressionMode::None: No compression applied to snapshots (default)CompressionMode::Lz4: Fast LZ4 compression for snapshots (reduces storage size)
Advanced Configuration Example
use ;
use Duration;
Trade-offs:
- AOL + Snapshots: Maximum durability, slower writes, larger storage
- Snapshot-only: Better performance, risk of data loss between snapshots, smaller storage
- Synchronous AOL: Immediate durability, slower commit times
- Asynchronous AOL: Better performance, small risk of data loss on system crash
- Frequent fsync: Higher durability, reduced performance
- LZ4 Compression: Smaller storage footprint, slight CPU overhead
See the Durability guarantees section for detailed information about ACID durability levels.
Durability guarantees
SurrealMX provides different levels of durability (the "D" in ACID) depending on the persistence configuration:
In-memory only mode (No persistence)
When persistence is disabled (the default), SurrealMX provides no durability guarantees. All data is lost when the process terminates, crashes, or the system shuts down. This mode is ideal for:
- Caching and temporary data storage
- Development and testing
- Scenarios where data can be reconstructed from other sources
Maximum durability (AOL with fsync)
For maximum durability that survives system crashes and power failures, use synchronous AOL with fsync on every append:
use ;
With this configuration:
- Changes are written to the AOL immediately on commit
fsync()is called to ensure data reaches physical storage- Transactions are fully durable once
commit()returns successfully - Data survives process crashes, system crashes, and power failures
Configurable durability levels
Different persistence configurations provide different durability guarantees:
AOL Modes:
AolMode::SynchronousOnCommit: Changes written to AOL immediately on commit. Durable after commit returns (if combined with appropriate fsync mode).AolMode::AsynchronousAfterCommit: Changes written to AOL asynchronously. Small window where recent commits may be lost on sudden system crash.AolMode::Never: No AOL logging. Changes only persisted via snapshots.
Fsync Modes:
FsyncMode::EveryAppend: Callsfsync()after every AOL write. Maximum durability but slowest performance.FsyncMode::Interval(Duration): Callsfsync()periodically. Durability guaranteed after the interval passes.FsyncMode::Never: Never callsfsync(). Relies on OS to flush data. Risk of data loss if OS crashes before flush.
Snapshot Modes:
SnapshotMode::Interval(Duration): Takes periodic snapshots. Without AOL, only data from the last snapshot is durable.SnapshotMode::Never: No snapshots. Must use AOL for any durability.
Durability guarantees summary:
| Configuration | Survives Process Crash | Survives System Crash | Performance |
|---|---|---|---|
| No persistence | ❌ | ❌ | Fastest |
| Snapshot-only | ⚠️ (last snapshot) | ⚠️ (last snapshot) | Fastest |
| Async AOL + No fsync | ⚠️ (mostly) | ⚠️ (mostly + OS buffers) | Very fast |
| Async AOL + Interval fsync | ⚠️ (mostly) | ⚠️ (mostly + since last fsync) | Very fast |
| Async AOL + Every fsync | ⚠️ (mostly) | ⚠️ (mostly) | Very fast |
| Sync AOL + No fsync | ✅ | ⚠️ (OS buffers) | Fast |
| Sync AOL + Interval fsync | ✅ | ⚠️ (since last fsync) | Fast |
| Sync AOL + Every fsync | ✅ | ✅ | Slow |
Choose the configuration that best balances your durability requirements against performance needs.
Historical reads
SurrealMX's MVCC (Multi-Version Concurrency Control) design allows you to read data as it existed at any point in time. This enables powerful use cases like:
- Audit trails: See what data looked like at specific timestamps
- Time-travel debugging: Examine application state at the time of an issue
- Consistent reporting: Generate reports based on a snapshot of data from a specific point in time
- Conflict resolution: Compare different versions of data to understand changes
use Database;
Available historical read methods:
get_at_version(key, version): Read a single key's value at a specific versionkeys_at_version(range, skip, limit, version): Get keys in range at a specific versionscan_at_version(range, skip, limit, version): Get key-value pairs at a specific versiontotal_at_version(range, skip, limit, version): Count keys at a specific version
Isolation levels
SurrealMX supports two isolation levels to balance between performance and consistency guarantees:
Snapshot Isolation (Default)
Provides excellent performance with strong consistency guarantees. Transactions see a consistent snapshot of the database as it existed when the transaction began.
- Read consistency: All reads within a transaction see the same consistent view
- Write isolation: Changes from other transactions are not visible until they commit
- No dirty reads: Never see uncommitted changes from other transactions
- No non-repeatable reads: Reading the same key multiple times returns the same value
use Database;
Serializable Snapshot Isolation
Provides the strongest consistency guarantee by detecting read-write conflicts and aborting transactions that would violate serializability.
- All Snapshot Isolation guarantees: Plus additional conflict detection
- Read-write conflict detection: Prevents phantom reads and write skew
- Serializable execution: Equivalent to running transactions one at a time
- Higher abort rate: More transactions may need to retry due to conflicts
use ;
When to use each isolation level:
- Snapshot Isolation: Most applications, high-performance scenarios, read-heavy workloads
- Serializable Snapshot Isolation: Financial applications, inventory management, any scenario requiring strict serializability
Range operations
SurrealMX provides powerful range-based operations for scanning, counting, and iterating over keys. All range operations support:
- Forward and reverse iteration
- Skip and limit parameters for pagination
- Historical versions for time-travel queries
- Efficient range scans using the underlying B+ tree structure
Basic range scanning
use Database;
Pagination and reverse iteration
use Database;
Historical range operations
use Database;
Available range operation methods:
Current version:
keys(range, skip, limit)/keys_reverse(...): Get keys in rangescan(range, skip, limit)/scan_reverse(...): Get key-value pairs in rangetotal(range, skip, limit): Count keys in range
Historical versions:
keys_at_version(range, skip, limit, version)/keys_at_version_reverse(...)scan_at_version(range, skip, limit, version)/scan_at_version_reverse(...)total_at_version(range, skip, limit, version)
Range parameters:
range: Rust range syntax ("start".."end") - start inclusive, end exclusiveskip: Optional number of items to skip (for pagination)limit: Optional maximum number of items to returnversion: Specific version timestamp for historical operations
Project History
Note: This project was originally developed under the name memodb. It has been renamed to surrealmx to better reflect its evolution and alignment with the SurrealDB ecosystem.