I'm looking at a caching policy based on predictions, such as score-based eviction and prefetch. I need to evaluate if I should use a "next-k requests prediction" model or a "time-to-next-access" predictor, which can create an eviction score. There’s research in "Learning-based caching," with concepts like LeCaR and Deep Q caching. I want to design a supervised learning approach where access is treated as data points, using various features. The model will predict the future access value based on this data.Proposing an eviction strategy
For eviction, I’m thinking about evicting items that have the lowest predicted future accesses. For prefetching, I want to focus on items that have a high probability of being used soon but aren't yet in the cache. I could consider using reinforcement learning (RL) for eviction decisions, but that complicates things. Instead, I’m suggesting the "ML-pCache" algorithm for a key-value cache system. The goal is to minimize the miss rate or latency while predicting future requests for each item using a gradient boosted tree or a lightweight neural network.Detailing the design
I'm laying out the design for my caching strategy. The input features will include frequency counts, recency, and global context. For offline training, I’ll use logs with sliding windows for labeling. During online inference, I’ll score each access.
For score-based caching, the formula will be Score(i, t) = p_i,t / s_i to prioritize keeping the highest-score items and evicting the lowest until we fit within capacity. I'll also need to consider cold-start issues and incorporate online learning to handle concept drift. I'll break this into four key areas: data and model design, using predictions, reinforcement modifications, and complexity considerations.Designing a predictive caching algorithm
For the request to design a predictive caching algorithm using machine learning, I'm planning a comprehensive response, aiming for about 900-1200 words for clarity. I'll start with an introduction to the goal and overall architecture.
Next, I'll formalize the request stream and its objectives. After that, I'll outline a supervised "next access probability" predictor, leading into feature engineering and the training procedure. I'll detail the online policy for eviction and admission decisions, including pseudocode. Optionally, I might touch on a reinforcement learning alternative. Finally, I'll discuss evaluation and practical considerations, specifically defining the model for each request.Drafting a predictive caching algorithm
I’m defining the labels for predictions: y_t will indicate if a key is requested again within H steps or time. I’ll use classification for easier training, possibly logistic regression or decision trees. However, I’m thinking about using reuse distance, which directly connects to caching strategies and correlates with Bélády's optimal algorithm.
My algorithm will involve preprocessing logs to compute reuse distances, engineering features like inter-arrival times, and creating a regression model (like gradient boosted trees) to predict these distances. At runtime, predicted distances will determine cache priorities for eviction and admission decisions. Additionally, I'll address model retraining and the importance of low-latency inference while also considering edge cases for rare items. I plan to structure my response with clear headings and sections to enhance readability.Considering computational complexity
I realize I need to think about the algorithm's computational complexity. The operations will be O(log N) for cache items because of the priority queue. For model inference, I'll aim for O(F) with F being the number of features, which should be small and manageable. I also want to address cold-start scenarios where an item is unknown, planning to fallback on LRU or simply skip caching altogether. Let's get started with these foundational considerations! Below is a concrete design for a predictive caching algorithm that uses machine learning to approximate an optimal (Belady-like) policy, plus notes on how to implement and evaluate it.
1. Problem setup
Assume:
You have a cache of capacity C (bytes or number of items).
Requests arrive as a sequence: r1, r2, …, where each request rt is for an item (key) k_t with size size(k_t).
Goal: maximize hit rate (or byte hit rate), or minimize average latency.
We want to learn from historical request traces so that, at runtime, the cache predicts how likely and how soon each object will be requested again, and uses that to decide:
Whether to admit an object into the cache.
Which object(s) to evict when space is needed.
2. High-level idea
Belady’s optimal algorithm (offline) evicts the item whose next use is farthest in the future. We can’t know the future, but we can learn to predict something close:
For each access to an item, predict a measure of its future utility, e.g.:
Predicted reuse distance (number of future unique requests until this item is accessed again), or
Prob. of being accessed again within a horizon H, or
Expected number of accesses in the next H seconds.
Then:
Keep in cache items with high predicted utility.
Evict (or never admit) items with low predicted utility.
This is a two-part system:
A supervised ML model to predict future access characteristics per item.
A cache policy that uses those predictions to do admission + eviction.
3. What the ML model predicts
3.1 Target (label)
A good target that closely mimics Belady is reuse distance:
For each request at time t for item k:
Let u_t = number of distinct other items requested after time t until k is requested again.
If k is never requested again, set u_t to a large sentinel (e.g. U_max).
We then train a regressor f(x_t) ≈ u_t, where x_t are features extracted at time t.
Interpretation:
Small f(x_t) → “this item will be reused soon” → valuable to cache.
Large f(x_t) → “won’t be reused for a long time (or ever)” → a good eviction candidate.
Alternative targets (if reuse distance is hard to compute or not ideal):
p_t = P(k is requested again within next H seconds or requests) (classification).
λ_t = expected # of requests to k in next H (regression).
value_t = expected latency savings from caching k in next H (if back-end latencies differ widely).
The mechanism below works similarly for any of these; just swap the scoring function.
4. Features for the ML model
You want features that describe an item’s recency, frequency, context, and possibly semantics.
Examples (for a web/CDN key-value cache):
4.1 Per-item temporal statistics
Keep small, per-key stats in memory (or a compressed structure):
last_access_time(k).
access_count(k) in multiple windows (e.g., last 10 min, 1 hour, 24 hours).
Top = item with largest predicted reuse distance (worst to keep).
Current used capacity used_bytes.
6.2 Admission + eviction logic
Parameters:
D_cutoff: a maximum reuse distance for admission; if d̂ > D_cutoff, do not cache the item.
Optional: include size in the score, e.g. score(k) = d̂(k) * (size(k) / mean_size) or use a utility-per-byte metric.
On each request for key k:
pseudo
function on_request(k, context):
if k in cache:
// Cache hit
x = extract_features(k, context)
d_hat = model(x)
cache[k].d_hat = d_hat
adjust_heap_position(k) // O(log N)
return HIT
else:
// Cache miss
x = extract_features_for_miss(k, context)
d_hat = model(x)
// Admission control: skip objects unlikely to be reused
if d_hat > D_cutoff:
return MISS_NO_ADMIT
// Ensure enough space, evicting worst-predicted items
while used_bytes + size(k) > C:
victim = heap_extract_max_by_d_hat() // largest d_hat
evict(victim)
used_bytes -= size(victim)
insert_into_cache(k, d_hat)
insert_into_heap(k, key = d_hat)
used_bytes += size(k)
return MISS_ADMIT
Key ideas:
Prediction-driven eviction: at eviction time, pick the item with largest predicted reuse distance (least likely to be used soon).
Admission policy: don’t cache items predicted to have very low future utility—this prevents cache pollution from one-off requests.
6.3 Handling cold-start / unknown items
Some items have little or no history:
If access_count(k) is very low, your features may be weak.
Use a fallback:
Default prediction (e.g., treat as moderately bad).
Or route to a classic policy (e.g., “admit but with LRU priority”).
Simple approach: if insufficient history, temporarily use an LRU score.
7. Integrating size and objective
If you care about byte hit ratio or latency reduction, modify the score:
Let v(k) = predicted # of future hits if kept in cache over horizon H (from model).
Let size(k) = bytes of k.
Use a priority like score(k) = v(k) / size(k) (value per byte).
Then:
Evict the lowestscore(k) (instead of highest reuse distance).
Admission: only admit if score(k) is above a threshold or better than the current worst item.
This aligns the cache with maximizing “hits per byte” or “savings per byte”.
8. Practical considerations
8.1 Performance overhead
Feature extraction must be cheap:
Maintain per-item stats in simple structs or counters.
Avoid complex joins or multi-round trips for features.
Model inference should be batchable or microsecond-level:
Pre-load model in memory.
Use highly optimized libraries, possibly quantized models.
8.2 Concept drift
Access patterns change (e.g., seasonal traffic, new product launches). To handle this:
Train on relatively recent windows (e.g., last week).
Use rolling retraining and A/B test new models.
Optionally include time-based features to help model seasonal effects.
8.3 Tail latency and stability
Ensure eviction is O(log N) via heaps or indexed priority queues.
Avoid recomputing predictions more often than needed:
Typically recompute only on access (hits or misses), not in background.
9. Extensions: Prefetching & RL
9.1 Predictive prefetching
Add a second model to predict what will be requested next given the sequence of requests (session-based model):
For each request sequence in a session, train a next-item predictor (e.g., RNN/Transformer or simpler Markov model).
On a request for item k, also prefetch top-M predicted next items into cache (subject to a stricter score threshold to avoid pollution).
Combine with the reuse-distance model so you don’t prefetch low-value items.
9.2 Reinforcement learning / bandits
Instead of predicting reuse distance directly, you can:
Let an RL agent choose between a small set of candidate policies (LRU, LFU, GDSF, random) per request; reward = hits or latency savings.
Or define actions as “evict item i” and learn Q-values; this is powerful but much more complex (state = cache contents).
For many real systems, the supervised approximation of Belady described above is simpler, more stable, and easier to ship.
If you tell me your specific environment (e.g., web CDN, database buffer cache, Redis-like KV cache), I can refine this design into a more concrete feature set and implementation plan for that context.