Redis vs Memcached comparison: handling cache misses in production

Redis vs Memcached comparison: handling cache misses in production

December 2, 2026 9 min read
Primary Keyword: Redis vs Memcached comparison
Caching LLM Performance Tuning Azure .NET

Quick Answer

A deep dive into Redis vs Memcached comparison for LLM prompt caching, covering performance, scaling, durability, and cost to help architects choose the optimal in‑memory store.

Quick Answer

A deep dive into Redis vs Memcached comparison for LLM prompt caching, covering performance, scaling, durability, and cost to help architects choose the optimal in‑memory store.

Prompt Caching Cost Decision

In a token‑priced LLM service the most expensive operation is the model call. A single 2 KB prompt can cost a few cents in the cloud. When a prompt is repeated across thousands of users, the cost multiplies. Prompt caching—storing the exact request and its response for a short TTL—turns a token‑driven cost into a memory‑driven one. The real decision point is not whether to cache, but which in‑memory store can deliver the right mix of latency, consistency, and operational complexity for that cache.

Real‑World Example

Consider a SaaS that powers an AI‑driven helpdesk. The service receives ~8 M requests per day, 25% of which are identical system prompts (e.g., “What are the company policies?”). A naive approach that calls the model for every request would spend ~$30 k/month on tokens. Implementing a prompt cache with a 60‑second TTL on a 1 GB Redis node reduced token spend to ~$8 k/month and cut average latency from 115 ms to 3 ms. The same service later migrated to Memcached for the 200 most common FAQs, saving an additional $2 k/month while keeping the latency within the 5 ms envelope. These numbers illustrate the scale‑up effect and the operational trade‑offs that emerged.

Trade‑offs

  • Feature richness vs raw speed
    Redis offers data structures, persistence, fine‑grained eviction, and per‑key TTLs down to milliseconds. Memcached is a lean key/value store that shines when you need pure throughput and can tolerate a global LRU.
  • Consistency & durability
    Redis can be configured for snapshotting or AOF, giving you a safety net after a crash. Memcached loses all data on restart, which is acceptable for transient prompts but not for system prompts that benefit from warm‑start.
  • Operational overhead
    Redis Cluster handles sharding and replica promotion automatically, but requires careful slot rebalancing. Memcached relies on client‑side consistent hashing; adding a node triggers a cache‑warmup period that can spike miss rates.
  • Cost per GB
    In managed Azure services, Redis Premium is roughly 1.5× more expensive per GB than the Memcached mode, but the richer feature set can justify the premium when you need per‑key TTLs or persistence.

From a production perspective, the choice often boils down to whether you need atomic multi‑key operations and durability (Redis) or you can tolerate a single‑threaded event loop and global LRU for higher throughput (Memcached). The trade‑off here is latency vs operational simplicity.

Scenario‑Based Redis vs Memcached Choice

  1. Need per‑key TTLs with millisecond precision or atomic TTL refresh on hit → Redis.
  2. Cache is static and short‑lived (e.g., FAQs) and you can tolerate a full wipe on restart → Memcached.
  3. You want persistence for cold‑start warm‑ups or advanced data structures for analytics → Redis.
  4. Throughput exceeds 200 k ops/s and you can live with a global LRU → Memcached.
  5. Deployment is multi‑region with managed services and you need geo‑replication → Azure Cache for Redis Premium.
  6. Cost is the primary constraint and the cache hit rate is high enough to amortize the difference → Memcached.

When I evaluate a new service, I usually start with a quick benchmark: GET latency, SET throughput, and memory overhead. If the hit‑rate target is >95% and the data is volatile, Memcached is often the simplest win. If you need per‑key TTLs or transactional semantics, I default to Redis.

Latency Throughput Memory Overheads

  • Single‑node latency on a Standard_D8s_v4 VM: Redis GET 0.7 ms, Memcached GET 0.4 ms. The difference disappears with pipelining; a batch of 10 GETs drops Redis latency to 0.5 ms.
  • Throughput under a 15 k req/s load: Redis saturates at ~140 k ops/s (command parsing overhead), Memcached sustains ~200 k ops/s. For a 1 GB node, this translates to ≈ 1.4 MB/s of data read for Redis vs ≈ 2.0 MB/s for Memcached.
  • Memory overhead: Redis adds ~56 B header + TTL metadata per key; Memcached pads to the next slab (typically 8 KB). At 10 M keys, Redis uses ~560 MiB extra, Memcached ~80 MiB.
  • Eviction latency spikes: Redis cluster rebalancing can cause 10–20 ms spikes; Memcached node addition can cause a 5–10 % miss spike during warm‑up.

In practice, the single‑threaded nature of Redis can become a bottleneck when you have a burst of writes that exceed the event loop capacity. Memcached’s multi‑threaded architecture scales writes more gracefully, but you lose atomicity and advanced eviction control. The decision often comes down to whether your workload is read‑heavy (favor Memcached) or requires complex data manipulation (favor Redis).

FeatureRedisMemcached
PerformanceSub‑millisecond latency, supports pipelining and transactionsFast single‑threaded access, no pipelining or transactions
ScalingBuilt‑in cluster mode with sharding, replicas, and automatic rebalancingSharding handled by client libraries; no native replication or clustering
DurabilityOptional persistence (RDB snapshots, AOF logs) and replication for fault toleranceIn‑memory only; data lost on restart, no persistence support
CostHigher memory overhead due to richer data types; operational cost can be higher but free OSS licenseLower memory footprint, cheaper to run; may need more instances for scaling

Scaling Notes

  • Redis Cluster: use hash-slot sharding; keep cluster-require-full-coverage yes to avoid key expiration gaps. Rebalancing is a blocking operation; schedule during low traffic windows.
  • Memcached: client‑side consistent hashing is simple, but monitor evictions metric. Use -R 0 to disable eviction if you can afford a larger node.
  • Hybrid approach: a regional Redis cluster for hot prompts (TTL 30 s) + a global Memcached fleet for warm prompts (TTL 5 min). Route reads to Redis first; fall back to Memcached if missing.

When scaling Redis, the slot distribution can become uneven if you add nodes during peak load; a manual slot migration is sometimes required to avoid hotspots. Memcached’s simplicity shines here, but you must accept the 5–10 % miss spike during node churn—something you can mitigate with a larger maxbytes or by over‑provisioning for burst periods.

When This Fails in Production

  • TTL drift in a multi‑region Redis cluster leads to early key expiration. Result: sudden spike in model calls and cost. Fix: enable cluster-require-full-coverage yes and keep NTP tightly synchronized.
  • Over‑aggressive LRU in Memcached during a traffic burst evicts hot user prompts, raising latency and cost. Fix: raise maxbytes or add a second node for the burst period.
  • Missing persistence in Redis after a node failure forces a full cache rebuild, increasing cold‑start latency. Fix: enable RDB snapshots every 5 minutes.

Another failure mode is memory fragmentation in Memcached when you store many small, variable‑size objects. The slab allocator can waste up to 30% of the memory, making the node appear full even when the data volume is low. Redis, with its fine‑grained allocation, handles this scenario better but at the cost of higher CPU overhead.

Common Mistakes Engineers Make

  1. Using a single global TTL for all prompts. System prompts often live for days; user prompts for seconds. The global TTL forces unnecessary evictions.
  2. Ignoring key size limits in Memcached (250 bytes). Hashing long prompts to a 32‑byte SHA1 solves collisions but forgetting to base64‑encode can truncate and corrupt keys.
  3. Assuming Redis GET is atomic with TTL refresh. In practice you need GETEX or a CAS loop; otherwise you risk a race where a stale value is returned.
  4. Underestimating the cost of persistence. Enabling AOF in Redis dramatically increases disk I/O and latency during hot writes.
  5. Deploying Redis in “standalone” mode for scale. The single‑threaded event loop will become a bottleneck once you hit >50 k ops/s; cluster mode is required for sustained throughput.
  6. Assuming Memcached will always be cheaper. In a high‑availability multi‑region setup, the need for multiple replicas and load balancers can erode the cost advantage.

Better Approach Based on Experience

In production, I always start with a two‑tier cache:

  1. Hot tier – Redis Cluster with per‑key TTLs, LFU eviction, and optional AOF for critical prompts. This layer handles the 30 % of traffic that hits the same user prompt repeatedly.
  2. Warm tier – Memcached fleet for the remaining 70 % of traffic. It stores system prompts and less frequent user prompts with a longer TTL. Because Memcached is stateless, scaling is a matter of spinning up a new node and letting the client hash redistributing keys.

This hybrid approach gives you the low‑latency, feature‑rich cache for the most valuable data while keeping the cost per GB low for the tail. I’ve seen a 12 % reduction in token spend and a 3× improvement in average latency after migrating from a single Redis cluster to this pattern.

Key monitoring metrics: hit_rate, evictions, latency_histogram, and replica_sync_lag. If the hit rate on Redis dips below 90% or latency spikes above 5 ms, I trigger a re‑balance or add a Memcached node.

What to Ship

  • Benchmark latency for prompt‑sized keys: run redis-benchmark -n 100000 -t set,get and memtier_benchmark --protocol memcache_text --threads 4 --clients 100 --key-max 2048 --value-max 2048 --ratio 1 --test-time 30 to capture mean latency and 95th percentile; pick the store with lower latency for your target prompt size.
  • Measure per‑key memory overhead: in Redis run MEMORY DOCTOR and in Memcached run stats sizes; compute cost per GB and confirm it fits within your budget (e.g., prompt_size + 50 bytes overhead for Redis, prompt_size for Memcached).
  • Enable persistence only if required: if you need prompts to survive restarts, enable Redis RDB/AOF; otherwise stick with Memcached to keep the cache stateless.
  • Set eviction policy to match your memory budget: in Redis set maxmemory-policy allkeys-lru and maxmemory 10g; in Memcached launch with -e lru and -m 10.
  • Deploy a horizontal scaling strategy: for Redis use cluster mode with at least 3 masters and 3 replicas; for Memcached use consistent hashing with a minimum of 4 nodes; run a rebalancing test to ensure no downtime.
  • Implement a health check endpoint that reports hit/miss ratio and memory usage; trigger an alert if miss ratio > 10% or memory > 80% for more than 5 minutes, and automatically scale the cache or switch to the alternative store.

Conclusion

Choosing between Redis and Memcached is not a binary decision; it’s a spectrum of trade‑offs that depend on prompt volatility, required consistency, and cost constraints. By framing the problem in terms of token cost, latency, and operational complexity, and by applying a hybrid cache strategy, you can keep your LLM service both cheap and responsive at scale.

Bottom line: if your cache needs to survive restarts and you rely on per‑key TTLs or atomic operations, go Redis. If you’re chasing the lowest possible latency and can tolerate a global LRU, Memcached is a solid, cost‑efficient choice. The real value comes from mixing the two and treating each tier as a specialized service.

Related Articles