Skip to main content

Eviction and admission

Instead of pure LRU, Feuer evicts entries based on what it would cost to fetch them again. When a tier runs out of room, it samples candidate entries and evicts the lowest-scoring one.

Retention scoring​

Both memory and disk score entries from their decayed request count and size. Both tiers see every lookup, even hits served from memory, so disk keeps hot fallback objects instead of going blind to them.

You can express cost in whatever unit makes sense for your workload: latency, S3 request fees, CPU time, or bandwidth.

Fetch cost (default)​

FetchCostScorer models the cost of fetching an object again and treats cache reads as free:

fetch_cost = cost_per_request + payload_len × cost_per_byte
score = decayed_request_count × fetch_cost / max(payload_len, 1)

The defaults are cost_per_request = 0 and cost_per_byte = 1, which cancels out size and simplifies to decayed request frequency. Setting a positive cost_per_request makes small objects score higher at equal demand, since each one saves a fixed round-trip or S3 GET fee:

use std::sync::Arc;
use feuer::{Cache, FetchCostScorer};

let scorer = Arc::new(FetchCostScorer {
cost_per_request: 256_000.0,
cost_per_byte: 1.0,
});

let cache = Cache::builder()
.memory_capacity(64 << 20)
.disk_capacity(1 << 30)
.with_memory_retention_scorer(scorer.clone())
.with_disk_retention_scorer(scorer)
.open()?;

Memory and disk can use different scorers. You can also change how many candidates are sampled per eviction with .reclaim_sample_size(n) (default 64).

Cache read savings​

CacheReadSavingsScorer also accounts for what it costs to read from the cache itself:

fallback_cost = fallback_cost_per_request + payload_len × fallback_cost_per_byte
cache_read_cost = cache_read_cost_per_request + payload_len × cache_read_cost_per_byte
score = decayed_request_count × (fallback_cost − cache_read_cost) / max(payload_len, 1)

If reading from that tier costs more than going to the fallback, the score goes negative and the entry gets evicted first.

Custom scorers​

If a size formula is not enough (for example, per-object compute cost), implement RetentionScorer. Your scorer gets the ObjectKeyHash, payload length, and ObjectAccessHistories. Use cache.hash_key(&key) to key any external metadata so it matches the cache's secret SipHash key.

Scoring runs under shard locks, so keep it fast and do not do I/O or call back into the cache.

Admission filters​

You can filter what gets admitted into memory and disk independently. By default, both tiers accept everything.

let cache = feuer::Cache::builder()
.memory_capacity(64 << 20)
.disk_capacity(1 << 30)
.with_memory_admission_filter(|_key, payload_len, _history| payload_len <= 1024 * 1024)
.with_disk_admission_filter(|key, _payload_len, history| {
history.decayed_request_count(key) > 1.5
})
.open()?;
  • Each filter sees the key hash, payload size, and request history.
  • Rejecting an object from memory still lets it go to disk.
  • The memory filter also runs when promoting a disk hit into memory.
  • Rejected objects are still returned to the caller and recorded in history.
  • Filters run synchronously outside shard locks.

Access history​

Every lookup records the key once before checking memory or disk. History is split across 64 sharded maps with a single atomic request counter driving exponential decay.

  • Half-life: 262,144 requests by default (.access_count_half_life(n)).
  • Entry limit: 10,000,000 entries per cache by default (.history_entry_limit(n)). A background thread checks once a second and trims the oldest entries down to 90% of the limit. Set it to 0 to turn off history completely.
  • History survives eviction until trimmed, and resets on restart.

Environment variables​

CacheBuilder::open() picks up these environment variables as defaults. Builder methods override them. Values accept plain integers or size suffixes like 8KiB or 1.5 GiB.

VariableDefaultPurpose
FEUER_PAYLOAD_VERSION0Bump to clear old disk entries when your payload or key format changes.
FEUER_MEMORY_SHARD_COUNTmax(1, memory_capacity / 8 GiB)Number of memory shards.
FEUER_RECLAIM_SAMPLE_SIZE64Candidates sampled per eviction.
FEUER_ACCESS_COUNT_HALF_LIFE262144Requests before demand weight halves.
FEUER_HISTORY_ENTRY_LIMIT10000000Max history entries before trimming (0 disables history).