Data Store¶
Key-value stores and database abstractions for distributed data simulation.
Data store components for simulating storage systems.
This module provides key-value stores, caching layers, and related storage infrastructure for simulating realistic data access patterns.
Example
from happysimulator.components.datastore import KVStore, CachedStore, LRUEviction
Simple key-value store¶
store = KVStore(name="db")
Cached store with LRU eviction¶
cache = CachedStore( name="cached_db", backing_store=store, cache_capacity=1000, eviction_policy=LRUEviction(), )
CacheWarmer ¶
CacheWarmer(
name: str,
cache: Entity,
keys_to_warm: list[str] | Callable[[], list[str]],
warmup_rate: float = 100.0,
warmup_latency: float = 0.001,
)
Bases: Entity
Pre-populates cache during cold start.
Fetches specified keys from the backing store and loads them into the cache before normal traffic begins. This reduces cache misses during the initial warm-up period.
The warmer processes keys at a configurable rate to avoid overwhelming the backing store.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
progress |
float
|
Fraction of warming complete (0.0 to 1.0). |
is_complete |
bool
|
Whether warming has finished. |
Initialize the cache warmer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this warmer entity. |
required |
cache
|
Entity
|
The cache to warm (must have a get method). |
required |
keys_to_warm
|
list[str] | Callable[[], list[str]]
|
List of keys or callable that returns keys. |
required |
warmup_rate
|
float
|
Keys to warm per second. |
100.0
|
warmup_latency
|
float
|
Simulated latency per key fetch in seconds. |
0.001
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
get_keys_to_warm ¶
Get the list of keys to warm.
Resolves the keys provider if it's a callable.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of keys to warm. |
start_warming ¶
warm_keys ¶
Warm all keys.
This is a generator that yields delays while warming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
now
|
Instant
|
Current simulation time. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Delays between key fetches. |
handle_event ¶
CacheWarmerStats
dataclass
¶
CacheWarmerStats(
keys_to_warm: int = 0,
keys_warmed: int = 0,
keys_failed: int = 0,
warmup_time_seconds: float = 0.0,
)
Statistics tracked by CacheWarmer.
CachedStore ¶
CachedStore(
name: str,
backing_store: KVStore,
cache_capacity: int,
eviction_policy: CacheEvictionPolicy,
cache_read_latency: float = 0.0001,
write_through: bool = True,
)
Bases: Entity
Cache layer in front of a backing store.
Provides fast access to frequently-used data by caching it in memory. Supports various eviction policies and write strategies.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
cache_capacity |
int
|
Maximum number of cached entries. |
hit_rate |
float
|
Ratio of cache hits to total reads. |
miss_rate |
float
|
Ratio of cache misses to total reads. |
Initialize the cached store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this cache entity. |
required |
backing_store
|
KVStore
|
The underlying storage to cache. |
required |
cache_capacity
|
int
|
Maximum number of entries to cache. |
required |
eviction_policy
|
CacheEvictionPolicy
|
Policy for selecting entries to evict. |
required |
cache_read_latency
|
float
|
Latency for cache hits in seconds. |
0.0001
|
write_through
|
bool
|
If True, writes go to both cache and backing store. If False, writes only go to cache (write-back). |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
get ¶
Get a value, checking cache first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Cache or backing store latency. |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value.
With write-through, writes to both cache and backing store. With write-back, writes only to cache (must flush later).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Write latency. |
delete ¶
Delete a key from cache and backing store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Delete latency. |
Returns:
| Type | Description |
|---|---|
bool
|
True if key existed in either cache or backing store. |
invalidate ¶
Remove a key from cache only (not backing store).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to invalidate. |
required |
flush ¶
Write all dirty entries to backing store.
Only relevant for write-back mode.
Yields:
| Type | Description |
|---|---|
float
|
Write latencies. |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries flushed. |
contains_cached ¶
Check if a key is in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key is cached. |
get_cached_keys ¶
Get all cached keys.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of cached keys. |
get_dirty_keys ¶
Get keys pending writeback.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of dirty keys. |
handle_event ¶
CachedStore can handle events for cache operations.
CachedStoreStats
dataclass
¶
CachedStoreStats(
reads: int = 0,
writes: int = 0,
hits: int = 0,
misses: int = 0,
evictions: int = 0,
writebacks: int = 0,
)
Statistics tracked by CachedStore.
Database ¶
Database(
name: str,
max_connections: int = 100,
query_latency: float | Callable[[str], float] = 0.005,
connection_latency: float = 0.01,
commit_latency: float = 0.01,
rollback_latency: float = 0.005,
)
Bases: Entity
Relational database with connection pool and transactions.
Simulates a database with connection pooling, query execution, and transaction support. Query latency can be fixed or depend on the query type.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
max_connections |
int
|
Maximum concurrent connections. |
active_connections |
int
|
Currently active connections. |
available_connections |
int
|
Connections available for use. |
Initialize the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this database entity. |
required |
max_connections
|
int
|
Maximum number of concurrent connections. |
100
|
query_latency
|
float | Callable[[str], float]
|
Latency per query (fixed or function of query). |
0.005
|
connection_latency
|
float
|
Latency to acquire a connection. |
0.01
|
commit_latency
|
float
|
Latency for transaction commit. |
0.01
|
rollback_latency
|
float
|
Latency for transaction rollback. |
0.005
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
execute ¶
Execute a query using a temporary connection.
Acquires a connection, executes the query, and releases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SQL query to execute. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Connection and query latency. |
Returns:
| Type | Description |
|---|---|
Any
|
Query result. |
begin_transaction ¶
Begin a new transaction.
Acquires a connection and starts a transaction.
Yields:
| Type | Description |
|---|---|
float
|
Connection latency. |
Returns:
| Type | Description |
|---|---|
Transaction
|
The new transaction. |
create_table ¶
Create a table (for simulation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name. |
required |
get_table_names ¶
Get all table names.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of table names. |
DatabaseStats
dataclass
¶
DatabaseStats(
queries_executed: int = 0,
transactions_started: int = 0,
transactions_committed: int = 0,
transactions_rolled_back: int = 0,
connections_created: int = 0,
connections_closed: int = 0,
connection_wait_count: int = 0,
connection_wait_time_total: float = 0.0,
query_latencies: list[float] = list(),
)
Transaction ¶
Represents a database transaction.
Initialize the transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transaction_id
|
int
|
Unique transaction identifier. |
required |
database
|
Database
|
The database this transaction belongs to. |
required |
connection
|
Connection
|
The connection used by this transaction. |
required |
execute ¶
Execute a query within this transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SQL query to execute. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Query latency. |
Returns:
| Type | Description |
|---|---|
Any
|
Query result. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If transaction is not active. |
commit ¶
Commit the transaction.
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Commit latency. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If transaction is not active. |
rollback ¶
Roll back the transaction.
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Rollback latency. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If transaction is not active. |
TransactionState ¶
Bases: Enum
State of a database transaction.
CacheEvictionPolicy ¶
Bases: ABC
Abstract base class for cache eviction policies.
Eviction policies track key access patterns and decide which key to evict when the cache is full.
on_access
abstractmethod
¶
Called when a key is accessed (read or write).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The accessed key. |
required |
on_insert
abstractmethod
¶
Called when a new key is inserted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The inserted key. |
required |
on_remove
abstractmethod
¶
Called when a key is explicitly removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The removed key. |
required |
evict
abstractmethod
¶
Select a key to evict.
Returns:
| Type | Description |
|---|---|
str | None
|
The key to evict, or None if no keys to evict. |
ClockEviction ¶
Bases: CacheEvictionPolicy
Clock (Second-Chance) eviction policy.
Approximates LRU using a circular buffer with reference bits. Each key has a reference bit that is set on access.
On eviction: - Scan keys in circular order - If reference bit is set, clear it and move on (second chance) - If reference bit is clear, evict that key
This provides O(1) access updates and amortized O(1) eviction, making it very efficient for large caches.
Also known as: Second-Chance algorithm, Not Recently Used (NRU).
Initialize Clock eviction policy.
FIFOEviction ¶
Bases: CacheEvictionPolicy
First-In-First-Out eviction policy.
Evicts the key that was inserted earliest. Simple and predictable, but doesn't consider access patterns.
Initialize FIFO eviction policy.
LFUEviction ¶
Bases: CacheEvictionPolicy
Least Frequently Used eviction policy.
Evicts the key with the lowest access count. Good for workloads where popular items should stay cached.
Initialize LFU eviction policy.
LRUEviction ¶
Bases: CacheEvictionPolicy
Least Recently Used eviction policy.
Evicts the key that hasn't been accessed for the longest time. Good for workloads with temporal locality.
Initialize LRU eviction policy.
RandomEviction ¶
Bases: CacheEvictionPolicy
Random eviction policy.
Evicts a random key. Useful as a baseline for comparison.
Initialize random eviction policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
SampledLRUEviction ¶
Bases: CacheEvictionPolicy
Sampled LRU (Probabilistic LRU) eviction policy.
Instead of tracking exact LRU order for all keys, this policy: 1. Samples N random keys from the cache 2. Evicts the least recently used among the sample
This provides O(1) access updates (just update timestamp) and O(N) eviction, where N is the sample size. For large caches, this is much cheaper than true LRU's O(log n) or O(n) operations.
Used by Redis for its approximated LRU implementation.
Attributes:
| Name | Type | Description |
|---|---|---|
sample_size |
int
|
Number of keys to sample during eviction. |
Initialize Sampled LRU eviction policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_size
|
int
|
Number of keys to sample for eviction decision. Higher = more accurate but slower. Redis uses 5. |
5
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If sample_size < 1. |
SLRUEviction ¶
Bases: CacheEvictionPolicy
Segmented LRU (SLRU) eviction policy.
Divides the cache into two segments: - Probationary segment: New items enter here - Protected segment: Items that are accessed again move here
Eviction order: probationary first (LRU), then protected (LRU). This provides scan resistance - a single scan won't evict hot items.
Attributes:
| Name | Type | Description |
|---|---|---|
protected_ratio |
float
|
Fraction of capacity for protected segment (0.0-1.0). |
Initialize SLRU eviction policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
protected_ratio
|
float
|
Fraction of total capacity for protected segment. Default 0.8 means 80% protected, 20% probationary. |
0.8
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If protected_ratio not in (0, 1). |
TTLEviction ¶
Bases: CacheEvictionPolicy
Time-To-Live based eviction policy.
Evicts keys that have exceeded their TTL. If no expired keys, evicts the oldest key.
Attributes:
| Name | Type | Description |
|---|---|---|
ttl |
float
|
Time-to-live in seconds. |
Initialize TTL eviction policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ttl
|
float
|
Time-to-live in seconds. |
required |
clock_func
|
Callable[[], float] | None
|
Function returning current time. Defaults to time.time(). |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If ttl <= 0. |
TwoQueueEviction ¶
Bases: CacheEvictionPolicy
2Q (Two Queue) eviction policy.
Similar to SLRU but uses FIFO for the first queue (A1). Provides excellent scan resistance.
Structure: - A1in: FIFO queue for first-time accessed items - A1out: Ghost queue tracking recently evicted from A1in (keys only) - Am: LRU queue for items accessed more than once
On first access: item goes to A1in On access while in A1in: nothing (FIFO, no promotion yet) On access while in Am: move to MRU position in Am On access of item in A1out (ghost): add to Am (it's a valuable item) On eviction: remove from A1in first, then Am
Attributes:
| Name | Type | Description |
|---|---|---|
kin_ratio |
float
|
Fraction of cache for A1in queue (default 0.25). |
Initialize 2Q eviction policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kin_ratio
|
float
|
Fraction of total cache for A1in queue. |
0.25
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If kin_ratio not in (0, 1). |
KVStore ¶
KVStore(
name: str,
read_latency: float = 0.001,
write_latency: float = 0.005,
delete_latency: float | None = None,
capacity: int | None = None,
)
Bases: Entity
In-memory key-value store with latency simulation.
Stores key-value pairs with configurable read and write latencies. Optionally limits capacity and evicts oldest entries when full.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
read_latency |
float
|
Time in seconds for read operations. |
write_latency |
float
|
Time in seconds for write operations. |
capacity |
int | None
|
Maximum number of keys (None = unlimited). |
Initialize the key-value store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this store entity. |
required |
read_latency
|
float
|
Latency for get operations in seconds. |
0.001
|
write_latency
|
float
|
Latency for put operations in seconds. |
0.005
|
delete_latency
|
float | None
|
Latency for delete operations (defaults to write_latency). |
None
|
capacity
|
int | None
|
Maximum number of keys (None = unlimited). |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
get ¶
Get a value by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Read latency delay. |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
get_sync ¶
Get a value synchronously (no latency, for internal use).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Write latency delay. |
put_sync ¶
Store a value synchronously (no latency, for internal use).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
delete ¶
Delete a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Delete latency delay. |
Returns:
| Type | Description |
|---|---|
bool
|
True if key existed, False otherwise. |
delete_sync ¶
Delete a key synchronously (no latency, for internal use).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key existed, False otherwise. |
contains ¶
Check if a key exists (no latency).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key exists. |
keys ¶
Get all keys (no latency).
Returns:
| Type | Description |
|---|---|
list[str]
|
List of all keys. |
KVStoreStats
dataclass
¶
KVStoreStats(
reads: int = 0,
writes: int = 0,
deletes: int = 0,
hits: int = 0,
misses: int = 0,
evictions: int = 0,
)
Statistics tracked by KVStore.
MultiTierCache ¶
MultiTierCache(
name: str,
tiers: list[Entity],
backing_store: Entity,
promotion_policy: PromotionPolicy
| str = PromotionPolicy.ALWAYS,
)
Bases: Entity
Hierarchical multi-tier cache.
Checks caches from fastest (L1) to slowest (Ln), then backing store. Supports promotion of items to faster tiers on access.
Tiers should be ordered from fastest to slowest, with L1 typically being smallest/fastest and Ln being largest/slowest.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
num_tiers |
int
|
Number of cache tiers. |
hit_rate |
float
|
Overall cache hit rate across all tiers. |
Initialize the multi-tier cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this cache entity. |
required |
tiers
|
list[Entity]
|
List of cache tiers from fastest (L1) to slowest. |
required |
backing_store
|
Entity
|
The underlying storage behind all caches. |
required |
promotion_policy
|
PromotionPolicy | str
|
Policy for promoting items to higher tiers. |
ALWAYS
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no tiers provided. |
promotion_policy
property
¶
Policy for promoting items between tiers.
get ¶
Get a value, checking tiers from fastest to slowest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Cache or backing store latency. |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value in cache and backing store.
Writes to backing store and invalidates/updates all cache tiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Write latency. |
delete ¶
Delete a key from all tiers and backing store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Delete latency. |
Returns:
| Type | Description |
|---|---|
bool
|
True if key existed anywhere. |
invalidate ¶
Remove a key from all cache tiers (not backing store).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to invalidate. |
required |
get_tier_stats ¶
Get statistics for each tier.
Returns:
| Type | Description |
|---|---|
dict[int, dict]
|
Dictionary mapping tier index to tier stats. |
handle_event ¶
MultiTierCache can handle events for cache operations.
MultiTierCacheStats
dataclass
¶
MultiTierCacheStats(
reads: int = 0,
writes: int = 0,
tier_hits: dict[int, int] = dict(),
backing_store_hits: int = 0,
misses: int = 0,
promotions: int = 0,
)
Statistics tracked by MultiTierCache.
PromotionPolicy ¶
Bases: Enum
Policy for promoting items to higher cache tiers.
ConsistencyLevel ¶
Bases: Enum
Consistency level for distributed operations.
ReplicatedStore ¶
ReplicatedStore(
name: str,
replicas: list[Entity],
read_consistency: ConsistencyLevel = ConsistencyLevel.QUORUM,
write_consistency: ConsistencyLevel = ConsistencyLevel.QUORUM,
read_timeout: float = 1.0,
write_timeout: float = 2.0,
)
Bases: Entity
Distributed key-value store with replication.
Replicates data across multiple nodes and uses configurable consistency levels for read and write operations.
Quorum = floor(n/2) + 1, where n is the number of replicas.
For strong consistency, use QUORUM for both reads and writes (R + W > N guarantees seeing most recent write).
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
num_replicas |
int
|
Number of replica nodes. |
read_consistency |
ConsistencyLevel
|
Consistency level for reads. |
write_consistency |
ConsistencyLevel
|
Consistency level for writes. |
Initialize the replicated store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this store entity. |
required |
replicas
|
list[Entity]
|
List of replica KVStore nodes. |
required |
read_consistency
|
ConsistencyLevel
|
Consistency level for read operations. |
QUORUM
|
write_consistency
|
ConsistencyLevel
|
Consistency level for write operations. |
QUORUM
|
read_timeout
|
float
|
Timeout for read operations in seconds. |
1.0
|
write_timeout
|
float
|
Timeout for write operations in seconds. |
2.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient replicas for consistency level. |
get ¶
Get a value with configured read consistency.
Reads from multiple replicas in parallel and returns the value once enough responses are received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Read latency delays. |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value with configured write consistency.
Writes to multiple replicas in parallel and returns success once enough acknowledgments are received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Write latency delays. |
Returns:
| Type | Description |
|---|---|
bool
|
True if write met consistency requirement. |
delete ¶
Delete a key from all replicas.
Requires write consistency for deletion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Delete latency delays. |
Returns:
| Type | Description |
|---|---|
bool
|
True if delete met consistency requirement. |
get_replica_status ¶
Get status of all replicas.
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dictionaries with replica status. |
handle_event ¶
ReplicatedStore can handle events for store operations.
ReplicatedStoreStats
dataclass
¶
ReplicatedStoreStats(
reads: int = 0,
writes: int = 0,
read_successes: int = 0,
read_failures: int = 0,
write_successes: int = 0,
write_failures: int = 0,
replica_timeouts: int = 0,
read_latencies: list[float] = list(),
write_latencies: list[float] = list(),
)
Statistics tracked by ReplicatedStore.
ConsistentHashSharding ¶
Consistent hash sharding.
Uses consistent hashing with virtual nodes to minimize key redistribution when shards are added or removed.
Initialize consistent hash sharding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
virtual_nodes
|
int
|
Number of virtual nodes per shard. |
100
|
seed
|
int | None
|
Optional random seed for reproducibility. |
None
|
HashSharding ¶
Simple hash-based sharding.
Uses MD5 hash of the key modulo number of shards. Fast and uniform, but reshards all keys when shard count changes.
RangeSharding ¶
Range-based sharding.
Assigns key ranges to shards based on alphabetical ordering. Good for range queries, but can lead to hot spots.
Initialize range sharding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundaries
|
list[str] | None
|
Optional list of key boundaries for shards. If None, keys are distributed alphabetically. |
None
|
ShardedStore ¶
Bases: Entity
Horizontally partitioned key-value store.
Distributes data across multiple shards using a configurable sharding strategy. Each key maps to exactly one shard.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
num_shards |
int
|
Number of shard nodes. |
sharding_strategy |
ShardingStrategy
|
Strategy used for key distribution. |
Initialize the sharded store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this store entity. |
required |
shards
|
list[Entity]
|
List of shard nodes (KVStore or similar). |
required |
sharding_strategy
|
ShardingStrategy | None
|
Strategy for distributing keys. Defaults to HashSharding. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no shards provided. |
sharding_strategy
property
¶
Strategy used for key distribution.
get ¶
Get a value from the appropriate shard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Read latency delay. |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value in the appropriate shard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Write latency delay. |
delete ¶
Delete a key from the appropriate shard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Delete latency delay. |
Returns:
| Type | Description |
|---|---|
bool
|
True if key existed. |
get_shard_for_key ¶
Get the shard index for a key without accessing the shard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Shard index (0 to num_shards-1). |
get_shard_sizes ¶
Get the size of each shard.
Returns:
| Type | Description |
|---|---|
dict[int, int]
|
Dictionary mapping shard index to key count. |
get_all_keys ¶
Get all keys across all shards.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of all keys. |
scatter_gather ¶
Get multiple keys, optimizing for parallel shard access.
Groups keys by shard and fetches from each shard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
list[str]
|
List of keys to fetch. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Latency delays. |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping keys to values (missing keys omitted). |
handle_event ¶
ShardedStore can handle events for store operations.
ShardedStoreStats
dataclass
¶
ShardedStoreStats(
reads: int = 0,
writes: int = 0,
deletes: int = 0,
shard_reads: dict[int, int] = dict(),
shard_writes: dict[int, int] = dict(),
)
Statistics tracked by ShardedStore.
get_shard_distribution ¶
Get read distribution across shards as percentages.
ShardingStrategy ¶
Bases: Protocol
Protocol for sharding strategies.
get_shard
abstractmethod
¶
Determine which shard a key belongs to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to shard. |
required |
num_shards
|
int
|
Total number of shards. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Shard index (0 to num_shards-1). |
CacheEntry
dataclass
¶
SoftTTLCache ¶
SoftTTLCache(
name: str,
backing_store: KVStore,
soft_ttl: float | Duration,
hard_ttl: float | Duration,
cache_capacity: int | None = None,
cache_read_latency: float = 0.0001,
)
Bases: Entity
Cache with stale-while-revalidate semantics.
Implements the soft TTL / hard TTL pattern where entries transition through three zones:
- Fresh (age < soft_ttl): Serve immediately from cache
- Stale (soft_ttl <= age < hard_ttl): Serve stale + background refresh
- Expired (age >= hard_ttl): Block until fresh data is fetched
Features: - Request coalescing: Multiple requests during refresh share one fetch - LRU eviction when at capacity - Statistics for monitoring cache effectiveness
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Entity name for identification. |
|
stats |
SoftTTLCacheStats
|
SoftTTLCacheStats with operational metrics. |
Initialize the soft TTL cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this cache entity. |
required |
backing_store
|
KVStore
|
The underlying storage to cache. |
required |
soft_ttl
|
float | Duration
|
Time in seconds (or Duration) before entries become stale. |
required |
hard_ttl
|
float | Duration
|
Time in seconds (or Duration) before entries expire. |
required |
cache_capacity
|
int | None
|
Maximum entries (None = unlimited). |
None
|
cache_read_latency
|
float
|
Latency for cache reads in seconds. |
0.0001
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid (e.g., soft_ttl > hard_ttl). |
cache_capacity
property
¶
Maximum number of cached entries (None = unlimited).
get ¶
Get a value with soft TTL semantics.
Behavior depends on cache state: - Fresh hit: Return immediately from cache - Stale hit: Return from cache AND trigger background refresh - Hard miss: Block until fresh data is fetched
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
Yields:
| Type | Description |
|---|---|
float
|
Latency delays (cache read or backing store fetch). |
Returns:
| Type | Description |
|---|---|
Any | None
|
The value if found, None otherwise. |
put ¶
Store a value in cache and backing store.
Updates the cache timestamp and writes through to backing store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to store under. |
required |
value
|
Any
|
The value to store. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Write latency. |
invalidate ¶
Remove a key from cache only (not backing store).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to invalidate. |
required |
contains_cached ¶
Check if a key is in the cache (regardless of freshness).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if key is cached. |
is_refreshing ¶
Check if a background refresh is in progress for a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if refresh is in progress. |
get_cached_keys ¶
Get all cached keys.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of cached keys. |
handle_event ¶
Handle internal cache events (background refresh).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
Event
|
The event to process. |
required |
Yields:
| Type | Description |
|---|---|
Generator[float]
|
Processing delays. |
Returns:
| Type | Description |
|---|---|
Generator[float]
|
None (background refresh doesn't produce follow-up events). |
SoftTTLCacheStats
dataclass
¶
SoftTTLCacheStats(
reads: int = 0,
fresh_hits: int = 0,
stale_hits: int = 0,
hard_misses: int = 0,
background_refreshes: int = 0,
refresh_successes: int = 0,
coalesced_requests: int = 0,
evictions: int = 0,
)
Statistics tracked by SoftTTLCache.
Attributes:
| Name | Type | Description |
|---|---|---|
reads |
int
|
Total read operations. |
fresh_hits |
int
|
Reads served from cache within soft TTL. |
stale_hits |
int
|
Reads served from cache that triggered background refresh. |
hard_misses |
int
|
Reads requiring blocking fetch (expired or uncached). |
background_refreshes |
int
|
Background refresh operations started. |
refresh_successes |
int
|
Background refreshes that completed successfully. |
coalesced_requests |
int
|
Requests that joined an in-flight refresh. |
evictions |
int
|
Entries removed due to capacity limits. |
total_hit_rate
property
¶
Ratio of all cache hits (fresh + stale) to total reads.
WriteAround ¶
Bases: WritePolicy
Write-around policy.
Writes go directly to backing store, bypassing cache. Good for write-heavy workloads where written data isn't immediately re-read.
On write, the key is invalidated from cache if present.
Initialize write-around policy.
get_keys_to_invalidate ¶
Get keys to remove from cache.
Returns:
| Type | Description |
|---|---|
list[str]
|
Keys that should be invalidated from cache. |
WriteBack ¶
Bases: WritePolicy
Write-back (write-behind) policy.
Writes go to cache only, flushed to backing store later. Provides better performance but eventual consistency.
Flush is triggered by: - Number of dirty entries exceeding max_dirty - Time since last flush exceeding flush_interval
Attributes:
| Name | Type | Description |
|---|---|---|
flush_interval |
float
|
Maximum time between flushes in seconds. |
max_dirty |
int
|
Maximum dirty entries before forced flush. |
Initialize write-back policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flush_interval
|
float
|
Maximum seconds between flushes. |
1.0
|
max_dirty
|
int
|
Maximum dirty entries before flush. |
100
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parameters are invalid. |
WritePolicy ¶
Bases: ABC
Abstract base class for write policies.
Write policies determine how writes to a cached store propagate to the backing store.
should_write_through
abstractmethod
¶
Whether to write to backing store immediately.
Returns:
| Type | Description |
|---|---|
bool
|
True if writes should go to backing store immediately. |
on_write
abstractmethod
¶
Called when a key is written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The written key. |
required |
value
|
Any
|
The written value. |
required |
should_flush
abstractmethod
¶
Whether pending writes should be flushed.
Returns:
| Type | Description |
|---|---|
bool
|
True if flush should be triggered. |
get_keys_to_flush
abstractmethod
¶
Get keys that need to be flushed.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of keys to write to backing store. |
on_flush
abstractmethod
¶
Called after keys have been flushed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keys
|
list[str]
|
Keys that were flushed. |
required |
WriteThrough
dataclass
¶
Bases: WritePolicy
Write-through policy.
Writes go to both cache and backing store synchronously. Provides strong consistency but higher latency.