Advertise here — become a partner
Advertise here — become a partner
Programming / Tech

Caching strategy for performance

You are a performance engineer expert in caching. Help me design the cache strategy: the system is [DESCRIBE: what it serves, the data — what reads a lot and changes little vs. what changes all the time, current volume/latency], stack [STACK + infra], and the pain is [PAIN: database getting hammered by repeated queries, slow endpoint, third-party API cost, heavy page]. Deliver: the layered where-to-cache map — from cheapest to deepest (the HTTP cache that many ignore: Cache-Control/ETag letting browser and CDN work for free — the right directives per resource type; the CDN for static and nearly-static; application cache — Redis/Memcached or local memory — for expensive queries, sessions, external API results; database query cache and materialized views when it fits — with the recommendation of where I start given my bottleneck), the design decisions that define success (what to cache — the rule: expensive to generate × read frequently × tolerates being slightly stale; well-designed key — deterministic, with version/namespace to invalidate en masse; honest TTL per data type — the question 'how many seconds of stale data does the business tolerate here?' answered per item), access patterns with code example in my stack (cache-aside — the pattern of 90% of cases: read from cache, miss fetches from source and populates, with the stampede care — the stampede of simultaneous misses at expiry: lock/single-flight or TTL with jitter; write-through/behind when write demands it), invalidation — the famous problem treated with respect (explicit invalidation on write event — data changed, key dies; version in key when explicit is impractical; and the humility of short TTL as a safety net always), classic pitfalls neutralized (caching per-user data without user in key — the classic leak; caching error/empty by accident; local memory in multiple instances serving different versions — when that's ok and when it needs Redis; the cache that hides performance bug that should be fixed in the query), measurement that separates faith from fact (hit rate per cache, latency before/after, the minimum dashboard), and if you paste code/queries: concrete application with before/after. Objective: responses already calculated never calculated again — and no user seeing the neighbor's data.
Advertise here — become a partner Advertise here — become a partner