CheckpointPolicy determines when the in-memory tree state is materialized to data files on disk. Checkpoints enable WAL truncation and allow cold tree levels to be mmap’d instead of kept in memory.
Definition
Defined insrc/storage/checkpoint.rs:40
Variants
Manual
Default policy. No automatic checkpoints occur. The application must call tree.checkpoint() explicitly.
Use when:
- You have natural checkpoint points (e.g., end of epoch)
- You want full control over checkpoint timing
- You’re okay with unbounded WAL growth until manual checkpoint
- Development and testing scenarios
EveryNEntries(u64)
Automatically triggers a checkpoint after every N WAL entries (insertions).
Number of WAL entries between automatic checkpoints.Recommended values:
10_000- Frequent checkpoints, low memory, longer WAL recovery100_000- Balanced approach1_000_000- Infrequent checkpoints, higher memory, shorter WAL recovery
- You want predictable checkpoint frequency
- WAL size needs to be bounded
- Recovery time needs to be predictable
MemoryThreshold(usize)
Automatically triggers a checkpoint when uncheckpointed in-memory chunks exceed the specified byte threshold.
Memory threshold in bytes. When uncheckpointed tree data exceeds this, a checkpoint is triggered.Recommended values:
268_435_456(256 MiB) - Frequent checkpoints1_073_741_824(1 GiB) - Balanced4_294_967_296(4 GiB) - Infrequent checkpoints
- You want to bound memory usage
- Insert patterns are irregular
- Memory pressure is a concern
OnClose
Checkpoint is only performed when the tree is gracefully closed via tree.close().
Use when:
- Tree lifetime is short
- You want minimal checkpoint overhead during operation
- Acceptable to replay full WAL on next open
- Testing or ephemeral workloads
Usage Examples
Manual Checkpoints
Entry-Based Checkpointing
Memory-Based Checkpointing
OnClose Pattern
Checkpoint Process
When a checkpoint occurs:- WAL flush: All buffered entries are fsynced
- Snapshot capture: Current tree state is captured atomically
- Data file writes: New chunks written to
data/level_N/shard_XXXX.dat - Metadata update:
checkpoint.metawritten atomically - WAL truncation: WAL is reset to just the header
- Tiering: Eligible levels are remapped to mmap’d files (see TieringConfig)
Waiting for Checkpoints
With automatic checkpoint policies, you can wait for background checkpoints:Policy Comparison
| Policy | Checkpoint Timing | WAL Growth | Recovery Time | Memory Usage | Use Case |
|---|---|---|---|---|---|
Manual | Application-controlled | Unbounded | Proportional to WAL | Grows with tree | Full control |
EveryNEntries(N) | Every N inserts | Bounded | ≤N entries | Bounded | Predictable checkpoints |
MemoryThreshold(B) | Memory-based | Variable | Variable | Bounded to B | Memory-constrained |
OnClose | On close only | Entire session | Full WAL replay | Grows with tree | Short-lived trees |
Performance Considerations
Checkpoints are I/O intensive operations. Consider:
- Checkpoint duration: Proportional to uncheckpointed data (typically 100ms-10s)
- Write amplification: Each checkpoint writes all new chunks to disk
- Background vs blocking: Automatic policies use background threads (non-blocking)
Measuring Checkpoint Performance
Default Value
Related
- TieringConfig - Controls mmap behavior after checkpoint
- RotorTree::checkpoint() - Manual checkpoint method
- RotorTree::wait_for_checkpoint() - Wait for background checkpoint
- RotorTreeConfig - Full configuration structure
