Engineering

How HotKV handles key eviction under memory pressure

Key eviction on a 2 GiB HotKV instance. We measured LFU, LRU and random against the ideal hit rate, then tested scans, a live resize and TTL policies.

Key eviction drawn as a full memory bar with the hottest keys kept inside it and cold keys leaving through a gate.

Key eviction decides which keys a server throws away when its memory is full. Get it right and a cache keeps serving the data people ask for. Get it wrong and the hit rate falls, the database behind the cache takes the load, or writes start to fail. We measured how HotKV behaves once the data no longer fits, using a workload that acts like a real cache and a yardstick that needs no other product.

In short. On a 2 GiB HotKV instance, allkeys-lfu reached 94.9% of the ideal hit rate and allkeys-lru 94.4%, with no failed commands. LFU lost less when one-off scans polluted the cache. Halving maxmemory on a live 16 GiB instance took 8 seconds and a 7-second latency spike. The volatile policies never touched keys without a TTL. The instance image we tested ships with noeviction, so choose a policy.

Why key eviction matters under memory pressure

A cache has a memory limit, set with maxmemory. When the data reaches that limit, the server must free memory before it can accept another write. The eviction policy picks the victims. A good policy removes keys nobody will ask for again and keeps the ones that earn their space.

Three things are worth judging. One is whether the policy keeps the right keys, which shows up as the hit rate. Another is whether bad traffic can wreck it, such as a scan that touches millions of keys once. Last is whether eviction stays out of the way, meaning the memory limit holds, latency stays flat and no command fails.

This article reports one set of measurements on HotKV. It is not a comparison with other servers. Capacity per gigabyte is a separate question and we left it out on purpose. What matters here is how well eviction does its job.

The key eviction policies HotKV supports

HotKV accepts the policy names that Redis users already know. Redis documents what each name means, and the same names work with CONFIG SET maxmemory-policy on HotKV. The policies fall into three groups.

Policies that can evict any key

With allkeys-lru, HotKV evicts the key that was used longest ago. With allkeys-lfu it evicts the key used least often, and allkeys-random removes a random key and serves as a baseline. Use these when every key is cache data and any of it can be rebuilt.

Policies that evict only keys with a TTL

The volatile-lru, volatile-lfu, volatile-ttl and volatile-random policies only consider keys that have an expiry. Keys without a TTL are never evicted. This suits a server that holds both cache entries and data you must keep, such as counters or session settings.

No eviction

With noeviction, HotKV evicts nothing. Reads keep working and writes that need more memory fail with an -OOM error. Use it when losing a key is worse than failing a write.

You can set the limit and the policy at runtime, or in the [memory] section of hotkv.toml.

CONFIG SET maxmemory 1192mb

CONFIG SET maxmemory-policy allkeys-lfu

How we tested key eviction

Every run used the same setup, so the policies are comparable with each other.

  • Server. HotKV 0.3.0 on an AWS t4g.small in eu-north-1 (2 vCPU, 2 GiB), the size of the Starter plan. maxmemory was 1,192 MiB.
  • Client. A c7g.xlarge in the same availability zone, with 64 connections.
  • Workload. A read-through cache. Each request reads a key chosen from a Zipf distribution with exponent 1.01 over 8 million keys. On a miss the client writes a 1 KiB value, as a real cache client would.
  • Fit. The instance held about 825,000 keys, roughly 10% of the key space.
  • Procedure. 90 seconds of warm-up, then 90 seconds measured. Snapshots and append-only persistence were off. Each configuration ran once.

For the scan runs, half of the connections read a burst of keys that are never reused. The burst lasted 10 seconds and repeated every 30 seconds. Each scan run read between 556,000 and 590,000 such keys.

To judge quality without a rival we computed an ideal hit rate. If you could know every key's popularity in advance, the best fixed cache would hold the hottest keys. Its hit rate is the sum of those keys' probabilities. For the roughly 825,000 keys the instance held, that ideal is 87.2%. No online policy can know popularity in advance, so none reaches it.

Hit rate against the ideal

All three policies landed within 8% of the ideal, and the order matched what the policies promise.

PolicyPlain ZipfShare of idealWith scan burstsShare of ideal
allkeys-lfu82.7%94.9%81.1%93.1%
allkeys-lru82.3%94.4%80.0%91.8%
allkeys-random80.6%92.4%78.0%89.5%
Bar chart of hit rate for allkeys-lfu, allkeys-lru and allkeys-random with and without scan bursts, next to a dashed line at the ideal hit rate of 87.2 percent.
Hit rate by policy on the 1,192 MiB instance. The axis starts at 60%.

On plain traffic LFU and LRU are half a point apart, which is about as far as a single run can be trusted. Random trails by two points. Frequency helps here because a Zipf workload has a stable set of hot keys, and counting uses is a direct way to find them.

Operation latency stayed flat. The p50 was about 0.76 ms and the p99 between 1.3 and 1.45 ms in every run, including the GETs and the SETs that refill misses. No command failed in any of the six runs.

Scans, resizes and tail latency

Two situations hurt caches more than steady traffic does. One is a scan that fills the cache with keys nobody wants. The other is a change in the memory budget.

Scan bursts

A scan reads many keys once. A policy that treats each new key as valuable lets the scan push out the hot set. LFU lost 1.6 points of hit rate to the bursts, LRU lost 2.3 and random lost 2.6. That fits the idea that a key used once rarely builds up a high count, though we did not instrument the policy to prove the cause. The measured result is narrow. It covers one scan shape on one instance, so read it as a direction, not a guarantee.

Shrinking a live instance

On an m7g.xlarge (4 vCPU, 16 GiB) holding about 4.4 million keys, we lowered maxmemory from 6,144 MiB to 3,072 MiB while the same read-through traffic ran. This used a 15 million key space and the same Zipf exponent.

CONFIG SET maxmemory 3072mb

Within 8 seconds used_memory was back under the new limit, for both LRU and LFU. The worst one-second p99 rose to about 22 ms, against 0.7 to 0.8 ms before. It stayed above 2 ms for 7 seconds and was back to normal after that. No command failed. The server evicted about 6.0 million keys under LRU and 5.8 million under LFU between the resize and the end of the run, which includes keys the traffic re-inserted. The hit rate settled from about 91.8% to roughly 84% to 86%, which is what half the cache costs on this workload.

TTL policies keep permanent keys

We also checked that the volatile policies do what their names say. At a maxmemory of 400 MiB we stored 100,000 permanent keys, 100,000 keys with a 60-second TTL and 100,000 keys with a 3,600-second TTL. Then we wrote 2 million keys with a 1,800-second TTL, about 2.8 GB of data.

In all four volatile policies, all 100,000 permanent keys survived the flood. The policies differ in what they remove first. Under volatile-ttl all 100,000 keys with the longest TTL were still there afterwards, while the shorter-lived keys had been evicted. Under volatile-lru none of those 3,600-second keys remained, which fits LRU removing the older keys first.

The last case matters in production. We then wrote only permanent keys until memory was full. The volatile policies evicted nothing and rejected writes with -OOM command not allowed when used memory > 'maxmemory'. In the same situation allkeys-lru kept accepting writes by evicting keys, including permanent ones. If you mix cache and durable data on one instance, a volatile policy protects the durable part, and it turns a full instance into write errors you can alert on.

Settings to check before production

The tested instance image ships with maxmemory-policy set to noeviction and maxmemory at 65% of RAM. Out of the box, a full instance refuses writes instead of evicting. If you want a cache, set allkeys-lfu or allkeys-lru. Choose LFU when a stable hot set dominates and LRU when recent use is the better signal.

Leave headroom for the process itself. On the 6 GB instance used_memory peaked about 1.6 MB above the limit. On the 2 GiB instance the process resident size reached about 6% above maxmemory, so a limit near total RAM invites trouble.

Watch the counters that tell you eviction is working. These fields come from INFO.

INFO stats

INFO memory

Track evicted_keys for the eviction rate, keyspace_hits and keyspace_misses for the hit rate, and used_memory against maxmemory for the margin. The hit rate is hits divided by hits plus misses. A rising evicted_keys with a falling hit rate means the instance is too small for the working set. The Starter plan is the size we tested. Our latency measurements cover speed under normal load.

What these results do not cover

These numbers describe one workload on one build, and the limits are worth stating plainly.

  • One Zipf shape (exponent 1.01), one value size (1 KiB) and one scan pattern.
  • One run per configuration and one instance, with no replicas, no snapshots and no append-only persistence.
  • LRU, LFU and random only. We did not test hit rates for the volatile policies, mixed value sizes or very large values.
  • The ideal hit rate assumes known, fixed popularity. A workload whose hot keys change quickly can behave differently, and recency policies often do better there.

Frequently asked questions

What is key eviction?

Key eviction is the removal of keys when a server reaches its memory limit. The server picks keys according to a policy such as LRU, LFU or random, frees their memory and then accepts the new write. It lets a cache run at full memory without failing.

Which eviction policy should I use?

For a pure cache, start with allkeys-lfu if a stable set of keys gets most of the traffic, or allkeys-lru if recent use predicts the next request. In our test LFU kept the highest hit rate and lost the least during scans. Use a volatile policy when the instance also stores keys that must not be evicted.

What happens when memory is full and the policy is noeviction?

HotKV evicts nothing. Reads continue, and any write that needs more memory fails with an -OOM command not allowed when used memory > 'maxmemory' error. The instance image we tested ships with this policy, so a cache needs a different one set.

Does key eviction slow HotKV down?

In our test it did not move the tail much. With the instance at its limit and evicting, operation p50 was about 0.76 ms and p99 between 1.3 and 1.45 ms, with no failed commands. That includes network time inside one availability zone. A large live reduction of maxmemory raised the p99 to about 22 ms for 7 seconds.

Can I change maxmemory or the policy on a running instance?

Yes. CONFIG SET maxmemory and CONFIG SET maxmemory-policy both work on a live instance, and we used them throughout these tests. Lowering the limit makes the server evict until it is under the new value. In our test that took 8 seconds for a drop from 6 GiB to 3 GiB.

Do volatile policies evict keys that have no TTL?

No. In our test all 100,000 permanent keys survived a flood of expiring keys under every volatile policy. If no key with a TTL is left to evict, the server rejects writes with an -OOM error instead of removing a permanent key.

Found this useful?

Share the article

Continue the conversation

Comments

Questions about the method? A different result on your hardware? Share what you found. Comments are reviewed before they appear.

Leave a comment

Your display name and comment become public after approval. Your email address is used only for the updates above and is never shown. Cloudflare Turnstile checks for spam once you start writing. Privacy details.