Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › AI caching

How to Set Redis Eviction Policies and TTLs for AI Caches

By Freedom251 Team · · 5 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set a Redis memory ceiling with maxmemory, choose a maxmemory-policy that fits the cache’s access pattern, and assign TTLs to entries that should become stale or be reclaimed. These settings address different problems: eviction responds to memory pressure, while TTL controls how long an individual entry remains eligible to serve. For an AI semantic cache, also isolate lookup by context such as tenant and model version; neither eviction nor expiration can make an unsafe similarity match correct.

Set a memory ceiling and eviction policy

In Redis Open Source, maxmemory sets the threshold at which the configured eviction behavior is enforced. You can configure it in redis.conf at startup or change it at runtime with CONFIG SET:

As an Amazon Associate I earn from qualifying purchases.

maxmemory 100mb
CONFIG SET maxmemory 100mb

The 100 MB value is an example, not a recommended size. Set a limit appropriate to the instance and workload. Then choose a policy with maxmemory-policy. Redis’s policy reference describes allkeys-lru as a common fit when a small subset of keys is expected to be accessed much more often than the rest: Redis key eviction documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose among policies by considering which keys can be removed and what signal should guide the choice:

Policy Keys eligible Retention or failure behavior When it may fit
allkeys-lru All keys Removes least recently used candidates; Redis LRU is approximate, using sampling rather than exact ordering. A skewed workload where a relatively small hot set is accessed far more than other keys.
allkeys-lfu All keys Favors keeping frequently used keys. When repeated access frequency is a useful signal for which cached entries to retain.
allkeys-random All keys Chooses keys randomly. When accesses are expected to be roughly uniform.
volatile-lru Keys with an expiration Removes least recently used candidates among expiring keys. With no keys carrying TTLs, it behaves like noeviction. When expiring cache entries should be eligible but non-expiring keys should be protected.
volatile-lfu Keys with an expiration Favors retaining frequently used keys among expiring keys. With no keys carrying TTLs, it behaves like noeviction. When frequency should guide removal among TTL-bearing entries, while non-expiring keys remain ineligible.
volatile-random Keys with an expiration Chooses expiring keys randomly. With no keys carrying TTLs, it behaves like noeviction. When only expiring keys should be candidates and their relative access patterns do not need to guide selection.
volatile-ttl Keys with an expiration Considers expiring keys with the shortest remaining TTL first. With no keys carrying TTLs, it behaves like noeviction. Only when the TTLs themselves encode which entries are preferable to remove sooner.
noeviction No keys are evicted Commands that would add data can fail at the memory limit. When rejecting writes is preferable to discarding existing keys.
allkeys-lrm / volatile-lrm All keys / keys with an expiration Prioritize least recently modified keys; modification timestamps update on writes rather than reads. Available in Redis 8.6 and later; verify the deployed version before selecting either policy.

For a mixed-use instance, a volatile-* policy can keep non-expiring keys out of eviction candidates only if the cache entries actually receive TTLs. That creates a dependency: an entry written without an expiration is not eligible under those policies. Separating disposable cache data from persistent workloads, when feasible, reduces this policy coupling.

Redis’s eviction threshold is not a reason to allocate every byte of physical RAM to maxmemory. Replication and AOF buffers need headroom; INFO memory exposes mem_not_counted_for_evict to help estimate memory used by such buffers. Consult the Redis eviction documentation for the deployment-specific details.

Set TTLs according to answer freshness

Redis expiration applies per key. Assign a TTL when storing an answer if it can become stale or should be reclaimed after a bounded period. There is no universal TTL duration for AI caches: base it on the freshness and invalidation needs of the underlying data and on how long an answer remains valid for its intended context.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

RedisVL’s SemanticCache API guide documents a default TTL in seconds, per-store TTL options, and an expire method for setting or refreshing an entry. Its TTL guide says ttl=None means entries persist indefinitely; configured TTL is applied on store, and a cache hit refreshes the TTL as a sliding window. A missing default and per-entry TTL means the API operation does not add expiration. Confirm these behaviors against the RedisVL version you deploy.

Expiration should not be the only way to correct an answer that has become invalid. Use an explicit invalidation strategy where necessary, and check after rollout whether important entries repeatedly expire before reuse. Redis notes that a high expired-key count may indicate TTLs are too short or applied to the wrong keys.

Protect semantic-cache correctness

A semantic cache may reuse a response for a prompt that is similar rather than identical. A cache hit is useful only when the old response still fits the new request’s context. Treat context boundaries as hard filters, not as details to leave to similarity scoring.

  • Scope lookups by tenant so one customer’s cached response cannot be served to another.
  • Include locale and model version where they affect the answer.
  • Include relevant safety flags or other context that changes whether a response is appropriate.
  • Tune the similarity threshold with answer-quality checks: a loose threshold risks wrong answers, while a strict threshold reduces hits.

TTL helps limit how long a stored response remains available, but it does not make a bad match safe during that interval. Evaluate reuse quality as well as cache efficiency.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Monitor behavior and tune from workload evidence

Use Redis’s operational counters together rather than judging the policy by one metric. INFO stats reports hits, misses, evictions, and expirations; INFO memory reports dataset memory and memory not counted for eviction.

Signal Where to inspect What it can indicate
keyspace_hits and keyspace_misses INFO stats Whether lookups are finding entries; interpret together with workload and cache correctness.
evicted_keys INFO stats How often Redis has removed keys under memory pressure. Low hit rates alongside high evictions can suggest the policy is displacing useful entries.
expired_keys INFO stats How many keys expired. A high count can point to TTLs that are too short or attached to the wrong keys.
used_memory_dataset and mem_not_counted_for_evict INFO memory Dataset memory and buffer use that helps explain the gap between the eviction threshold and total memory needs.
Rejected writes or command statistics Inspect command statistics and rejected-command signals for the deployment With noeviction, rejected writes signal that the configured bound is affecting ingestion. Volatile policies can also leave writes without eligible expiring keys unable to proceed.

Use these observations to adjust the memory bound, policy, or TTL assignment. Benchmark against your own prompt mix, data freshness requirements, and quality criteria: no hit-rate improvement, latency reduction, or cost saving can be assumed for a workload based on the policy name alone.

Check the Redis product and version first

The configuration examples above describe Redis Open Source. Redis Cloud and Redis Software have their own configuration surfaces and defaults, so do not assume that the same runtime command or default applies unchanged to a managed deployment. Policy availability also depends on version: the LRM variants are documented for Redis 8.6 and later. Check the product documentation and deployed version before applying a setting.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

AI cachingRedisRedis evictionsemantic cacheTTL

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.