Overview
The Accounts DB library provides persistent storage for account data with memory-mapped files, concurrent access, and efficient indexing. It supports single-threaded append operations with many concurrent readers.AccountsDb
TheAccountsDb struct is the main interface for account storage and retrieval.
Architecture
- Accounts are stored in memory-mapped files called
AccountsFile - Each file stores accounts for a single slot
- A shared index tracks file locations and offsets for each account
- Global atomic
write_versiontracks commits to the data store - Only the index requires write locks; storage operations are lock-free
Core Configuration
Configuration for AccountsDb behavior and performance tuning.Fields:
flush_threads: Number of threads for flushing accountsancient_append_vecs: Whether to use ancient append vecs optimizationstorage_paths: Directories for account storagecache_limit_bytes: Maximum size of the account cache
ACCOUNTS_DB_CONFIG_FOR_TESTING: Optimized for testsACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: Optimized for benchmarks
Account Operations
Load an account by pubkey, considering ancestor relationships.Parameters:
ancestors: Set of ancestor slots to searchpubkey: The account’s public key
Option<(AccountSharedData, Slot)>: Account data and the slot it was found in, or None
Store multiple accounts to the database at a given slot.Parameters:
slot: The slot number for these accountsaccounts: Slice of pubkey and account data tuples to store
Store accounts to the cache (for uncommitted transactions).Parameters:
slot: The slot numberaccounts: Accounts to cache (None indicates account deletion)
Scanning and Indexing
scan_accounts
fn<F>(&self, ancestors: &Ancestors, scan_func: F, config: &ScanConfig) -> ScanResult<()> where F: FnMut(Option<(&Pubkey, AccountSharedData, Slot)>)
Scan all accounts, calling a function for each one.Parameters:
ancestors: Ancestor slots to includescan_func: Function called for each accountconfig: Scan configuration (sorting, limits, etc.)
ScanResult<()>: Ok on success, or error if scan was aborted
get_filtered_indexed_accounts
fn(&self, index_key: &IndexKey, filter: F, config: &ScanConfig, byte_limit: Option<usize>) -> ScanResult<Vec<(Pubkey, AccountSharedData)>>
Get accounts from a secondary index with filtering.Parameters:
index_key: The secondary index to query (e.g., by program owner)filter: Predicate function to filter accountsconfig: Scan configurationbyte_limit: Optional byte limit for scans
ScanResult<Vec<(Pubkey, AccountSharedData)>>: Filtered accounts
Cleaning and Maintenance
Clean up old account data that is no longer needed.Removes:
- Zero-lamport accounts that are not in the roots
- Old versions of accounts that have been updated
- Accounts in slots that have been purged
max_clean_root: Optional maximum root slot to clean up to
Shrink all account storage to reclaim space.Parameters:
is_startup: Whether this is being called during startuplast_full_snapshot_slot: Last full snapshot slot for ancient append vec handling
Flush the accounts cache to persistent storage.Parameters:
should_flush: Whether to force a flushsnapshot_slot: Optional snapshot slot for coordination
Snapshot Support
Mark a slot as a root (finalized) slot.Parameters:
slot: The slot to mark as root
AccountsAddRootTiming: Timing metrics for the operation
Get the accounts delta hash for a slot.Parameters:
slot: The slot number
AccountsHash: The delta hash for that slot
calculate_accounts_hash
fn(&self, slot: Slot, config: &CalcAccountsHashConfig) -> Result<(AccountsHash, u64), AccountsHashVerificationError>
Calculate the accounts hash for a slot.Parameters:
slot: The slot to hashconfig: Configuration for hash calculation
Result<(AccountsHash, u64), Error>: The hash and total lamports, or error
AccountsIndex
The accounts index provides fast lookups and secondary indexing.Structure
Methods
upsert
fn(&self, slot: Slot, pubkey: &Pubkey, account_info: AccountInfo, reclaims: &mut SlotList<T>, account_shared_data: &AccountSharedData) -> UpsertReclaim
Insert or update an account in the index.Parameters:
slot: Current slotpubkey: Account public keyaccount_info: Account metadatareclaims: Mutable list to collect reclaimable slotsaccount_shared_data: The account data
UpsertReclaim: Information about reclaimed entries
Storage Types
AccountsFile
Represents a single account storage file (append vec). Key Methods:append_accounts: Append accounts to the fileget_stored_account: Retrieve a stored account by offsetaccounts: Iterate over all accounts in the file
AccountStorageEntry
A reference-counted storage entry. Fields:slot: The slot this storage belongs toid: Unique storage IDaccounts: The underlying accounts file
Example Usage
Constants
WRITE_CACHE_LIMIT_BYTES_DEFAULT: 15 GB default cache sizeDEFAULT_FILE_SIZE: 4 MB default file sizeSCAN_SLOT_PAR_ITER_THRESHOLD: 4000 accounts for parallel iteration
See Also
- Runtime API - High-level bank interface
- Validator Configuration - Storage configuration and tuning
- Operations - Production deployment guide