Overview
From ~/workspace/source/accounts-db/src/accounts_db.rs:1:Architecture Components
The accounts database consists of several key components:Core Structures
Storage Architecture
Append-Only Storage
Accounts stored in append-only files (AppendVec):- Memory-mapped files for fast access
- Immutable once written
- Multiple files per slot
- Accounts never modified in place
Storage Layout
AppendVec Format
Each AppendVec contains:Storage Recycling
Old storage files recycled when:- Dead Accounts - No live accounts remain
- Low Occupancy - Most accounts updated elsewhere
- Ancient Storage - Very old storage files
- Compaction - Combined with other storage
Account Indexing
The accounts index (~/workspace/source/accounts-db/src/accounts_index.rs:1) maps pubkeys to storage locations.Index Structure
Account Info
- Multiple slots per pubkey (fork history)
- Sorted by slot number
- Most recent slot typically used
- Ref count for garbage collection
Index Sharding
Index sharded for parallelism:Disk Index
Index can spill to disk:- In-Memory - Hot accounts in RAM
- On-Disk - Cold accounts on disk
- LRU Policy - Keep recently used in memory
- Async Loading - Load from disk as needed
Account Cache
Recently modified accounts cached in memory:Cache Operations
- Store - Write to cache, async flush to disk
- Load - Check cache first, then disk
- Flush - Write cached accounts to AppendVec
- Evict - Remove from cache after flush
Write Cache
Default cache size (~/workspace/source/accounts-db/src/accounts_db.rs:98):Account Hashing
Accounts hashed for verification:Hash Computation
Lattice Hash (LtHash)
Used for incremental hashing:- Homomorphic hash function
- Add/remove accounts without rehashing
- Efficient for large account sets
- Used in snapshots
Accounts Hash
Full accounts hash computed for:- Snapshot creation
- Cluster consensus on state
- State verification
- Fork comparison
Snapshot System
Full Snapshots
Complete state at a slot:- Freeze Bank - No more modifications
- Hash Accounts - Compute full hash
- Write Storage - Copy AppendVec files
- Write Metadata - Bank state and hash
- Compress Archive - Create tar.zst file
Incremental Snapshots
Changes since last full snapshot:- Only modified accounts
- Much smaller than full snapshot
- Faster to create and load
- Chain with full snapshot
Snapshot Loading
- Download - Get snapshot from peer or disk
- Decompress - Extract archive
- Load Accounts - Mmap storage files
- Build Index - Scan storage and index
- Verify Hash - Ensure correctness
- Resume - Continue from snapshot slot
Background Services
Accounts Background Service
Runs cleanup operations:- Snapshot Generation - Create periodic snapshots
- Shrink Storage - Compact underutilized storage
- Clean Accounts - Remove old versions
- Purge Slots - Delete rooted-out forks
Clean Process
Remove old account versions:- Identify Dead - Find superseded accounts
- Remove from Index - Update index
- Mark Storage - Track dead bytes
- Schedule Shrink - If occupancy low
Shrink Process
Compact sparse storage:- Select Candidates - Find low-occupancy storage
- Read Alive Accounts - Load still-referenced accounts
- Write New Storage - Append to new file
- Update Index - Point to new locations
- Delete Old Storage - Remove old file
Ancient Append Vecs
Very old storage files:- Combined into larger files
- Reduces file count
- Improves space efficiency
- Special handling for durability
Read-Only Accounts Cache
Cache for read-only accounts:- Faster program loading
- Reduced disk contention
- Better performance for hot programs
Account Locks
Transactions lock accounts during execution:Lock Rules
- Write Lock - Exclusive access
- Read Lock - Shared access
- Validation - Before transaction execution
- Release - After batch complete
Scanning Accounts
Scan Operations
Iterate over all accounts:- Snapshot creation
- Hash computation
- Rent collection
- Account analysis
Parallel Scanning
Scan in parallel using shards:Performance Optimizations
Memory Mapping
- Zero-copy account reads
- Kernel manages caching
- Efficient for large datasets
- OS handles page faults
Parallel Operations
- Sharded index structure
- Parallel account scanning
- Concurrent storage access
- Lock-free where possible
Cache Hierarchy
- Write Cache - Recent modifications
- Read Cache - Popular read-only accounts
- OS Page Cache - Memory-mapped files
- Disk - Persistent storage
Batch Processing
- Flush multiple accounts together
- Amortize I/O overhead
- Better disk utilization
- Reduced write amplification
Tiered Storage
Optimized storage for different access patterns:Configuration
AccountsDbConfig
Testing Config
Error Handling
Storage Errors
- Disk Full - No space for new storage
- Corruption - Storage file damaged
- I/O Error - Disk read/write failure
Recovery Strategies
- Retry operations
- Load from snapshot
- Reconstruct from ledger
- Request from peers
Monitoring
Metrics Tracked
- Storage usage per slot
- Cache hit/miss rates
- Clean/shrink performance
- Scan latency
- Flush time
Stats Reporting
Key Files
- Accounts DB: ~/workspace/source/accounts-db/src/accounts_db.rs
- Accounts Index: ~/workspace/source/accounts-db/src/accounts_index.rs
- Accounts Cache: ~/workspace/source/accounts-db/src/accounts_cache.rs
- Account Storage: ~/workspace/source/accounts-db/src/account_storage.rs
- Append Vec: ~/workspace/source/accounts-db/src/append_vec.rs