sled

sled sled

sled is an embedded database that provides an API similar to a threadsafe BTreeMap<[u8], [u8]>. It is designed to offer ACID transactions, zero-copy reads, atomic operations, and much more, making it suitable for various applications requiring embedded databases.

Key Features

  • API similar to BTreeMap: Provides a familiar API for key-value storage.
  • ACID Transactions: Supports serializable transactions for atomic operations.
  • Zero-Copy Reads: Efficient data retrieval with zero-copy reads.
  • Write Batches: Supports batching multiple writes for efficiency.
  • Key Prefix Subscription: Subscribe to changes on key prefixes.
  • Multiple Keyspaces: Allows defining and using multiple keyspaces.
  • Merge Operators: Customizable merge operations for specific key-value logic.
  • Iterators: Forward and reverse iterators over key ranges.
  • ID Generator: A crash-safe monotonic ID generator capable of generating 75-125 million unique IDs per second.
  • Compression: Supports zstd compression (optional feature).

Quick Start

Basic Usage

let tree = sled::open("/tmp/welcome-to-sled")?;
 
// insert and get, similar to std's BTreeMap
let old_value = tree.insert("key", "value")?;
 
assert_eq!(
  tree.get(&"key")?,
  Some(sled::IVec::from("value")),
);
 
// range queries
for kv_result in tree.range("key_1".."key_9") {}
 
// deletion
let old_value = tree.remove(&"key")?;
 
// atomic compare and swap
tree.compare_and_swap(
  "key",
  Some("current_value"),
  Some("new_value"),
)?;
 
// block until all operations are stable on disk
tree.flush()?;

Advanced Features

  • Serializable Transactions: Atomically read and write multiple keys in multiple keyspaces.
  • Subscribe to Key Changes: React to changes in specific key prefixes.
  • Merge Operators: Customize how values for specific keys are merged.

Performance

sled offers LSM tree-like write performance combined with B+ tree-like read performance. It can handle over a billion operations in under a minute on 16 cores with a mix of read and write operations.

Considerations

  • Durability: By default, sled automatically fsyncs every 500ms. This can be configured or managed manually with flush or flush_async.
  • Transactions: Transactions are optimistic and should not interact with external state or perform IO unless idempotent.
  • Endianness: Store numerical keys in big-endian form to ensure correct lexicographic ordering.
  • Single Instance: sled does not support multiple open instances. Keep sled open for the duration of your process.

Architecture

sled is built on a lock-free tree structure atop a lock-free pagecache, designed for high concurrency and efficiency. It employs modern b-tree techniques like prefix encoding and suffix truncation to reduce storage costs.

Minimum Supported Rust Version (MSRV)

sled supports Rust 1.62 and above.

Similar Projects