ๆƒฏๆ€ง่šๅˆ ้ซ˜ๆ•ˆ่ฟฝ่ธชๅ’Œ้˜…่ฏปไฝ ๆ„Ÿๅ…ด่ถฃ็š„ๅšๅฎขใ€ๆ–ฐ้—ปใ€็ง‘ๆŠ€่ต„่ฎฏ
้˜…่ฏปๅŽŸๆ–‡ ๅœจๆƒฏๆ€ง่šๅˆไธญๆ‰“ๅผ€

ๆŽจ่่ฎข้˜…ๆบ

F
Fortinet All Blogs
WordPressๅคงๅญฆ
WordPressๅคงๅญฆ
The Cloudflare Blog
ไบ‘้ฃŽ็š„ BLOG
ไบ‘้ฃŽ็š„ BLOG
ๅš
ๅšๅฎขๅ›ญ - Franky
D
Docker
ๅฐไผ—่ฝฏไปถ
ๅฐไผ—่ฝฏไปถ
้˜ฎไธ€ๅณฐ็š„็ฝ‘็ปœๆ—ฅๅฟ—
้˜ฎไธ€ๅณฐ็š„็ฝ‘็ปœๆ—ฅๅฟ—
H
Hackread โ€“ Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
U
Unit 42
M
MIT News - Artificial intelligence
B
Blog
GbyAI
GbyAI
C
Check Point Blog
P
Proofpoint News Feed
ๅš
ๅšๅฎขๅ›ญ - ๅธๅพ’ๆญฃ็พŽ
Hugging Face - Blog
Hugging Face - Blog
้›ทๅณฐ็ฝ‘
้›ทๅณฐ็ฝ‘
ITไน‹ๅฎถ
ITไน‹ๅฎถ
Google DeepMind News
Google DeepMind News
V
V2EX
Stack Overflow Blog
Stack Overflow Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Donโ€™t Fail โ€” They Drift Spilling beans for how i learn for exam๐Ÿ˜"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" โ€” What Actually Happened Comfy Cloudโ€™s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions โ€” here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components โ€” Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cรณmo construรญ un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 ๐Ÿš€ I Built an Ethical Hacking Scanner Tool โ€“ Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points โ€” Here's What I Found About How Markets Really Move EcoTrack AI โ€” Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด ๐—ฆ๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐—ถ๐—ฒ๐˜€ ๐—˜๐˜…๐—ฝ๐—น๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฑ (Backend & Frontend Developers)
Kiran ยท 2026-05-27 ยท via DEV Community

An interviewer asked: "What caching strategy does your app use?"
The candidate said: "We use Redis."
Interviewer: "That's a tool. I asked for a strategy."
Silence. Interview over. ๐Ÿ˜ถ

Here's every caching strategy broken down ๐Ÿ‘‡


๐Ÿง  ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด?

๐Ÿ‘‰ Storing data temporarily so future requests are served faster
๐Ÿ‘‰ Avoids hitting the original source โ€” DB, API, server โ€” every time
๐Ÿ‘‰ Wrong strategy = stale data, crashes, data loss

Without cache: User โ†’ Server โ†’ Database (slow ๐Ÿข)
With cache:    User โ†’ Cache (fast โšก)
                        โ†“ miss only
                      Database

Enter fullscreen mode Exit fullscreen mode

โœ” Reduces latency
โœ” Reduces database load
โœ” Scales better under traffic


โšก 1๏ธโƒฃ ๐—–๐—ฎ๐—ฐ๐—ต๐—ฒ-๐—”๐˜€๐—ถ๐—ฑ๐—ฒ (๐—Ÿ๐—ฎ๐˜‡๐˜† ๐—Ÿ๐—ผ๐—ฎ๐—ฑ๐—ถ๐—ป๐—ด)

๐Ÿ‘‰ App checks cache first. Miss โ†’ fetch DB โ†’ store in cache.

async function getUser(id) {
  // Step 1 โ€” check cache
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);

  // Step 2 โ€” cache miss, hit DB
  const user = await db.findUser(id);

  // Step 3 โ€” store in cache for next time
  await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 3600);
  return user;
}

Enter fullscreen mode Exit fullscreen mode

โœ” Only caches what's actually requested
โœ” Cache failure doesn't break the app
โŒ First request always slow โ€” cold cache
โŒ Risk of stale data between TTL cycles


๐ŸŽฏ 2๏ธโƒฃ ๐—ช๐—ฟ๐—ถ๐˜๐—ฒ-๐—ง๐—ต๐—ฟ๐—ผ๐˜‚๐—ด๐—ต

๐Ÿ‘‰ Every write goes to cache AND database simultaneously

async function updateUser(id, data) {
  // Write to DB and cache together
  await db.updateUser(id, data);
  await redis.set(`user:${id}`, JSON.stringify(data), 'EX', 3600);
}

Enter fullscreen mode Exit fullscreen mode

โœ” Cache always in sync with DB
โœ” No stale reads after writes
โŒ Write latency increases โ€” two writes every time
โŒ Cache fills with data that may never be read

๐Ÿ‘‰ Best for: Read-heavy apps where freshness is critical


๐Ÿš€ 3๏ธโƒฃ ๐—ช๐—ฟ๐—ถ๐˜๐—ฒ-๐—•๐—ฒ๐—ต๐—ถ๐—ป๐—ฑ (๐—ช๐—ฟ๐—ถ๐˜๐—ฒ-๐—•๐—ฎ๐—ฐ๐—ธ)

๐Ÿ‘‰ Write to cache instantly. Sync to DB asynchronously later.

User writes โ†’ Cache โœ… (instant response)
                 โ†“ async worker (batched every 5s)
              Database ๐Ÿ• (eventually consistent)

Enter fullscreen mode Exit fullscreen mode

async function updateScore(userId, score) {
  // Instant write to cache
  await redis.set(`score:${userId}`, score);

  // Add to queue โ€” worker syncs to DB later
  await queue.add('syncScore', { userId, score });
}

Enter fullscreen mode Exit fullscreen mode

โœ” Blazing fast write performance
โœ” Reduces DB write load โ€” batch updates
โŒ Risk of data loss if cache crashes before sync
โŒ Complex to implement correctly

๐Ÿ‘‰ Best for: Leaderboards, analytics, gaming, counters


๐Ÿ”„ 4๏ธโƒฃ ๐—ฅ๐—ฒ๐—ฎ๐—ฑ-๐—ง๐—ต๐—ฟ๐—ผ๐˜‚๐—ด๐—ต

๐Ÿ‘‰ App only talks to cache. Cache fetches from DB on miss.

App โ†’ Cache โ†’ (hit)  โ†’ App โœ…
App โ†’ Cache โ†’ (miss) โ†’ DB โ†’ Cache populates โ†’ App โœ…

Enter fullscreen mode Exit fullscreen mode

// App never touches DB directly
const user = await cacheProvider.get(`user:${id}`);
// Cache provider handles DB fetch internally on miss

Enter fullscreen mode Exit fullscreen mode

โœ” App logic stays clean โ€” zero cache handling code
โœ” Cache always populated after first request
โŒ Cache provider must support read-through natively
โŒ First request latency still exists

๐Ÿ‘‰ Best for: Managed caches โ€” AWS ElastiCache, DAX


โฐ 5๏ธโƒฃ ๐—ฅ๐—ฒ๐—ณ๐—ฟ๐—ฒ๐˜€๐—ต-๐—”๐—ต๐—ฒ๐—ฎ๐—ฑ

๐Ÿ‘‰ Cache proactively refreshes data before TTL expires

TTL = 60s
At 45s โ†’ background job pre-fetches fresh data
At 60s โ†’ cache already has new data โœ… zero miss latency

Enter fullscreen mode Exit fullscreen mode

async function getWithRefreshAhead(key, fetchFn, ttl = 60) {
  const cached = await redis.get(key);
  const ttlRemaining = await redis.ttl(key);

  // Refresh when 75% of TTL has passed
  if (ttlRemaining < ttl * 0.25) {
    fetchFn().then(data =>
      redis.set(key, JSON.stringify(data), 'EX', ttl)
    );
  }

  return cached ? JSON.parse(cached) : fetchFn();
}

Enter fullscreen mode Exit fullscreen mode

โœ” No latency spikes on cache expiry
โœ” Always serving warm data
โŒ May refresh data that's never requested โ€” wasted compute

๐Ÿ‘‰ Best for: Homepages, dashboards, trending feeds


๐Ÿ”‘ 6๏ธโƒฃ ๐—–๐—ฎ๐—ฐ๐—ต๐—ฒ ๐—œ๐—ป๐˜ƒ๐—ฎ๐—น๐—ถ๐—ฑ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ฆ๐˜๐—ฟ๐—ฎ๐˜๐—ฒ๐—ด๐—ถ๐—ฒ๐˜€

๐Ÿ‘‰ Knowing WHEN to clear cache is as important as caching itself

// TTL โ€” expire after fixed time
await redis.set('user:1', data, 'EX', 3600); // expires in 1hr

// Event-based โ€” clear on data change
async function updateUser(id, data) {
  await db.updateUser(id, data);
  await redis.del(`user:${id}`); // invalidate immediately
}

// Cache versioning โ€” bump version on deploy
const key = `user:${id}:v2`; // old v1 cache naturally expires

Enter fullscreen mode Exit fullscreen mode

โœ” TTL โ€” simple, automatic
โœ” Event-based โ€” precise, immediate
โœ” Versioning โ€” safe for deployments


๐Ÿšจ ๐—–๐—ฎ๐—ฐ๐—ต๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ๐˜€ ๐—ฌ๐—ผ๐˜‚ ๐— ๐˜‚๐˜€๐˜ ๐—ž๐—ป๐—ผ๐˜„

// Cache Stampede โ€” TTL expires, 1000 users hit DB together
// Fix: mutex lock โ€” only one request rebuilds cache
const lock = await redis.set('lock:user:1', 1, 'NX', 'EX', 5);
if (lock) { /* fetch DB and repopulate */ }
else { /* wait and retry */ }

// Cache Penetration โ€” requests for non-existent data bypass cache
// Fix: cache null values too
await redis.set(`user:${id}`, 'NULL', 'EX', 60);

// Cache Avalanche โ€” all keys expire at same time
// Fix: add random jitter to TTL
const ttl = 3600 + Math.floor(Math.random() * 300);

Enter fullscreen mode Exit fullscreen mode


โš›๏ธ ๐—™๐—ฟ๐—ผ๐—ป๐˜๐—ฒ๐—ป๐—ฑ ๐—–๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ด (๐—ฅ๐—ฒ๐—ฎ๐—ฐ๐˜)

// React Query โ€” cache-aside in the frontend
const { data } = useQuery({
  queryKey: ['user', id],
  queryFn: () => fetch(`/api/user/${id}`),
  staleTime: 5 * 60 * 1000,   // fresh for 5 mins
  cacheTime: 10 * 60 * 1000,  // keep in memory for 10 mins
});

Enter fullscreen mode Exit fullscreen mode

โœ” staleTime โ€” how long data is considered fresh
โœ” cacheTime โ€” how long unused data stays in memory
โœ” React Query implements cache-aside pattern automatically


๐Ÿšจ ๐—–๐—ผ๐—บ๐—บ๐—ผ๐—ป ๐— ๐—ถ๐˜€๐˜๐—ฎ๐—ธ๐—ฒ๐˜€

โŒ Caching without a TTL โ€” stale data lives forever
โŒ Not handling cache miss gracefully โ€” app crashes
โŒ Caching user-specific data globally โ€” data leaks
โŒ Using Write-Behind without a reliable queue
โŒ Ignoring cache stampede on high-traffic TTL expiry


๐Ÿ’ก ๐—ฆ๐—ฒ๐—ป๐—ถ๐—ผ๐—ฟ-๐—Ÿ๐—ฒ๐˜ƒ๐—ฒ๐—น ๐—œ๐—ป๐˜€๐—ถ๐—ด๐—ต๐˜

There are only two hard problems in computer science: cache invalidation and naming things.
Choosing the wrong strategy doesn't slow your app โ€” it silently corrupts it.


๐ŸŽฏ ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„ ๐—ข๐—ป๐—ฒ-๐—Ÿ๐—ถ๐—ป๐—ฒ๐—ฟ

Caching strategy is a tradeoff between consistency, latency, and complexity โ€” Cache-Aside for flexibility, Write-Through for consistency, Write-Behind for write performance, Read-Through for clean app logic, and Refresh-Ahead for zero miss latency โ€” with the right choice always depending on your read/write ratio, consistency requirements, and failure tolerance.


#SystemDesign #Backend #Caching #Redis #WebDevelopment #InterviewPrep #SoftwareEngineering #PerformanceOptimization #EngineeringMindset