Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan

When you serve a personalized homepage you have two enemies: network latency and stale data. A CDN can shave milliseconds off the round-trip, but it only knows about static assets. Redis gives you sub-millisecond reads on dynamic data, while Memcached is cheap for high-throughput cache-only workloads. The hot take? The only way to win today is to stack them in a deliberate hierarchy instead of picking a single "best" cache.
The flow looks like this:
User -> CDN (HTML shell) -> Edge Redis (user token) -> Core Memcached (recommendations) -> Origin DB
Many devs write off Memcached as "old school". The truth is its simple protocol (GET/SET) and lock-free design let it handle 2-5 million ops/sec on a single node with negligible CPU. When you pair it with Redis you get:
Imagine a SaaS product that shows a dashboard with three sections:
python# FastAPI endpoint
from fastapi import FastAPI, Request
import aioredis
import aiomcache
import json
app = FastAPI()
redis = await aioredis.from_url("redis://edge-redis.mycompany.com")
memcached = aiomcache.Client("core-memcached.mycompany.com", 11211)
@app.get("/dashboard")
async def dashboard(request: Request):
user_id = request.cookies.get("uid")
# 1. Get user settings from edge Redis (fast, consistent)
settings = await redis.get(f"user:{user_id}:settings")
# 2. Get recent reports from core Memcached (cheap bulk cache)
reports = await memcached.get(b"recent_reports")
# 3. Assemble response
return {
"settings": json.loads(settings) if settings else {},
"reports": json.loads(reports) if reports else []
}
The CDN already delivered the HTML shell, so the browser only waits for the JSON payload. The edge Redis call finishes in ~1 ms, while the Memcached call is ~2 ms even though it travels across the internal backbone. Total latency stays under 50 ms on average - fast enough to feel instantaneous.
Cache invalidation is the only thing that separates production-grade systems from academic demos. Here are three patterns that scale:
| Pattern | Where it lives | When to trigger |
|---|---|---|
| Write-through Redis | Edge Redis | On every user settings update - %%INLINECODE_0%% the new value and publish to a Redis Stream. |
| Time-bucketed Memcached | Core Memcached | Use a key prefix with a timestamp bucket, e.g., %%INLINECODE_1%%. When the bucket expires, a background job writes the next bucket. |
| CDN purge via API | CDN | After a major UI rollout, send a batch purge for %%INLINECODE_2%%. Most CDNs support async purge queues. |
The key insight: Never let a single layer be the source of truth. Let the DB be the ultimate source, but keep each cache layer authoritative for its own slice of data. This eliminates "cache stampede" because each layer can fall back to the next one without hammering the DB.
A lot of hype pushes "just use Redis for everything". The reality is that Redis' persistence and replication cost adds latency and price. In a multi-region product you'll see 10-20 ms added just to talk to the primary replica. Memcached gives you a free-performance tier for data that tolerates eventual consistency, and a CDN gives you geographic proximity for static assets. The hybrid approach wins on both cost and latency.
memcached Helm chart, set replica count based on your QPS.The next wave of "real-time personalization" will be built on this three-layer cache. If you keep betting on a single cache technology, you'll pay for latency, cost, or both. Stack them, and watch your user engagement metrics climb.