CacheKit Docs

High-performance cache policies and supporting data structures.

View the Project on GitHub OxidizeLabs/cachekit

Random Eviction

Feature: policy-random

Goal

Evict a uniformly random resident entry when capacity is reached.

Core Idea

Select victims randomly with equal probability:

Random eviction provides a baseline for comparing smarter policies.

Core Data Structures

In cachekit, src/policy/random.rs uses:

Operations

get(key)

insert(key, value)

Random Eviction (O(1))

  1. Generate random index: i = rand() % len
  2. Get victim key: victim = keys[i]
  3. Swap keys[i] with keys[last]
  4. Update swapped key’s index in HashMap
  5. Pop last element
  6. Remove victim from HashMap

Example:

keys = [A, B, C, D]
Random picks index 1 (B)
Swap B with D: [A, D, C, B]
Update D's index to 1
Pop: [A, D, C]
Remove B from HashMap

Complexity & Overhead

When to Use

Use Random when:

Avoid Random when:

Performance Characteristics

Random provides worst-case performance bounds:

Any policy with access pattern awareness should beat Random on workloads with locality.

Comparison with Other Policies

Policy Tracks Access Overhead Hit Rate (with locality)
Random No Minimal Baseline (worst)
FIFO Insertion Low Better
LRU Recency Medium Much better
LFU Frequency Medium Much better

Use Cases

  1. Benchmarking
    • Baseline for policy comparisons
    • “Any smarter policy should beat random”
  2. Cache Infrastructure Testing
    • Test eviction logic without policy complexity
    • Verify correctness independently of policy
  3. Random Access Workloads (rare)
    • When access patterns are truly uniform
    • No temporal or frequency skew
  4. Minimal Overhead Required
    • Embedded systems
    • Performance-critical paths where tracking is expensive

Implementation Notes

The O(1) eviction uses swap-remove:

Alternative (simpler but O(n)):

cachekit uses the O(1) approach for best performance.

References