Performancebeginner

What is Caching?

Reuse a computed result instead of doing the same work again

Reviewed by Abhijith N M · 24 October 2026 · Editorial standards

The Simple VersionNo jargon

A cache stores a result keyed by the inputs that produced it. On the next request, you look up the key and skip the expensive work. The hard parts are invalidation and staleness: when the underlying data changes, the cached copy must be updated or discarded. A TTL is the simple approach, but it trades freshness for load. A cache is not a source of truth. If losing the cache breaks correctness, it is not a cache.

What is Caching? — Visual Breakdown

#1
Browser Cache
Stores assets locally (images, CSS, JS)
→
#2
CDN Cache
Caches responses at edge servers worldwide
→
#3
Application Cache
In-memory cache in your app server
→
#4
Database Cache
DB's internal query result cache
→
LAYER 1

Browser Cache

Stores assets locally (images, CSS, JS)

Payload / Concrete Example
Cache-Control: max-age=31536000
Why It Matters in Production
Caching is usually the cheapest way to cut latency and database load, and the easiest way to create a consistency bug. Start with the read path you can measure, set a TTL you can defend, and add explicit invalidation for writes that matter. Use cache-aside for general reads, write-through when freshness is critical, and never cache per-user data under a shared key. Watch hit rate, eviction rate and stale-read rate. A 99% hit rate with a 30-minute stale window may still be the wrong trade.
Implementation & Syntax
🐍Python
import redis
import json

r = redis.Redis(host="localhost", port=6379, decode_responses=True)

def get_user(user_id: str):
    cache_key = f"user:{user_id}"
    
    # Check cache first
    cached = r.get(cache_key)
    if cached:
        return json.loads(cached)  # Cache HIT — instant!
    
    # Cache MISS — query database
    user = db.query(User).filter(User.id == user_id).first()
    
    # Store in cache for 1 hour
    r.setex(cache_key, 3600, json.dumps(user.to_dict()))
    return user

Quick Concept Check

Test your understanding in 15 seconds

What is the biggest challenge with caching?

Related Concepts

Complementary topics

Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.