Redis on a 1 or 2 GB VPS is not the same product as Redis on a 64 GB box. On a small instance you will hit maxmemory regularly, and the eviction policy you choose decides whether your application degrades gracefully or throws errors at 3 a.m. This article does the arithmetic first and then tells you which policy to pick for cache, queue and session workloads.
The default configuration on most distributions is maxmemory 0 with maxmemory-policy noeviction, which means Redis will grow until the kernel OOM-kills it and every client sees a connection reset. Setting a limit and a policy is the single highest-value change you can make to a small Redis deployment, and it takes two lines of config.
Why maxmemory 0 Is a Production Bug
With noeviction, writes that would exceed the limit return an OOM command not allowed when used memory > 'maxmemory' error. Without a limit, Redis allocates until the cgroup or the host runs out of memory. On a VPS the host almost always wins that race, and the OOM killer targets the largest resident process — Redis, usually, followed by your application if it was unlucky enough to be second.
The right limit is not “all the RAM”. Leave headroom for the OS, your app, and Redis’s own overhead: replication buffers, client output buffers, and fork-based BGSAVE. A safe starting point on a 2 GB instance running nginx, PHP-FPM and Redis is maxmemory 1200mb — roughly 60% of RAM with 600 MB left for everything else, plus the 100% memory spike that BGSAVE can cause if you have copy-on-write-heavy writes during the save.
The Eviction Math
Eviction cost depends on how many keys Redis inspects before finding one to drop. With maxmemory-samples 5 (the default) it samples five keys and evicts the best candidate. Raising samples to 10 improves quality but costs CPU; on a 1 vCPU VPS at 20k ops/sec we measured a 4% throughput drop.
Work out your keyspace first. If you have 500,000 keys averaging 1.8 KB including overhead, that is roughly 900 MB of data. With a 1200 MB limit you are not evicting at all under normal load, and policy choice is irrelevant until a traffic spike doubles your key count. If you have 2 million keys at 1 KB you are permanently at the limit, and the policy is deciding your hit rate every second. Check with INFO memory and INFO stats — evicted_keys climbing steadily means you are under-provisioned, not mistuned.
Policy Comparison Table
| Policy | Evicts from | Best for | Risk |
|---|---|---|---|
noeviction | Nothing — errors on write | Persistent data stores, queues | Hard client errors at limit |
allkeys-lru | Any key, least recently used | General-purpose cache | Hot keys can be evicted under churn |
allkeys-lfu | Any key, least frequently used | Cache with stable hot set | New keys penalised briefly |
allkeys-random | Any key, random | Uniform-value caches | Unpredictable hit rate |
volatile-lru | Keys with a TTL only | Mixed cache + persistent data | Errors if no TTL keys remain |
volatile-lfu | TTL keys, least frequently used | Mixed store, skew-heavy cache | Same as above |
volatile-ttl | TTL keys, soonest expiry | Session caches with natural expiry | Same as above |
volatile-random | TTL keys, random | Rarely the right answer | Same as above |
The volatile-* family behaves like noeviction once every TTL key is gone, so writes start failing. Fine if you genuinely have a persistent-data subset; a trap if you set TTLs by habit.
In our tests on a 2 GB VPS with a Zipf-ish request distribution, allkeys-lfu held a 91% hit rate where allkeys-lru held 84%, because LRU kept evicting a large scan-heavy key set that polluted the cache. If your workload includes periodic full-table scans that touch everything once, LFU is measurably better and the only cost is slightly more memory per key for the frequency counter.
Matching Policy to Workload
For a pure cache in front of a database, use allkeys-lfu and set a TTL on every key as a safety net for stale data. For a mixed instance where Redis holds both cache entries and something you cannot regenerate — a rate-limit counter, a queue, a lock — use volatile-lru or volatile-lfu and make sure nothing important lacks a TTL. For queues and durable session stores, run noeviction with a limit high enough that you alert long before you reach it.
Do not run a durable queue and a large cache in one Redis instance: they need opposite policies. Two processes on separate ports cost ~40 MB and eliminate the problem.
Configuring and Verifying the Change
Set it in /etc/redis/redis.conf rather than with CONFIG SET at runtime, because a restart will silently revert a runtime change. Add maxmemory 1200mb, maxmemory-policy allkeys-lfu, and maxmemory-samples 8. Then apply with CONFIG REWRITE or a service restart, and immediately confirm with CONFIG GET maxmemory*.
Watch INFO stats for evicted_keys and keyspace_hits/keyspace_misses over a full traffic cycle. A healthy cache shows a stable hit rate and evictions that track your peak traffic rather than climbing monotonically. If evictions climb forever, your working set does not fit and no policy will fix it — you need a bigger instance or a shorter TTL.
Memory Overhead You Forgot to Budget
Redis’s used_memory excludes fragmentation and allocator overhead. used_memory_rss is what the kernel sees, and the ratio between them is your fragmentation factor — normally 1.1 to 1.3, worse after heavy deletion churn. With activedefrag yes you can bring it down at some CPU cost, which on a 1 vCPU VPS is often a bad trade. If used_memory_rss is 1.8× used_memory, schedule a restart during a maintenance window instead.
Also budget client output buffers. A slow subscriber on a pub/sub channel can accumulate hundreds of megabytes per client, and that memory is outside maxmemory enforcement. Set client-output-buffer-limit pubsub 32mb 8mb 60 to disconnect clients that fall behind, otherwise one misbehaving consumer will OOM your instance despite a perfectly tuned eviction policy.
Where This Fits in a Small Stack
Eviction tuning is the difference between a cache that absorbs traffic spikes and one that turns them into 500s. If you are running Redis alongside a web stack on the same box, size for the cache first and let the app take what is left — the VPS hosting options with dedicated RAM allocations make this predictable. For the broader picture of right-sizing instance memory against workload, the cloud VPS benefits breakdown covers the trade-offs.
Finally, log the eviction rate to your monitoring system rather than only relying on INFO. A single metric — evictions per minute — tells you more about Redis health on a small VPS than CPU or memory graphs, because it is the first sign that your working set and your instance size have diverged.



Leave a Reply
You must be logged in to post a comment.