Back to course
    article~22 min readCaching & Performance

    Browser & HTTP Caching

    Use built-in web caching layers before reaching for infrastructure.

    Browser & HTTP Caching — Beginner Friendly System Design Guide

    Why Some Websites Load Instantly

    Have you noticed:

    • YouTube opens faster second time
    • Instagram images load quickly after first visit
    • Amazon logo appears instantly

    Why?

    Because of:

    Caching
    

    Caching is one of the biggest reasons modern apps feel fast.

    Without caching:

    • every request hits server
    • every image downloads again
    • every CSS file reloads
    • databases overload

    Internet would feel much slower.


    What Is Caching?

    Caching means:

    Storing frequently used data temporarily for faster access.

    Instead of:

    • fetching data repeatedly

    system reuses:

    • previously stored copy

    Real-Life Analogy

    Imagine going to kitchen every time you need water.

    Slow and inefficient.

    Instead:

    • keep water bottle nearby

    Faster access.

    Caching works similarly.


    What Is Browser Caching?

    Browser caching stores:

    • images
    • CSS
    • JavaScript
    • fonts
    • API responses

    inside user's browser.


    Real Example — Amazon Logo

    When you open Amazon first time:

    • browser downloads logo

    Next visit:

    • browser reuses stored logo
    • no need to download again

    Much faster.


    Browser Cache Flow

    User Opens Website
            │
            ▼
    Browser Requests Image
            │
            ▼
    Server Sends Image
            │
            ▼
    Browser Stores In Cache
            │
            ▼
    Next Visit
            │
            ▼
    Browser Uses Cached Copy
    

    Why Browser Caching Matters

    Without browser cache:

    • every visit downloads everything again

    This causes:

    • slower websites
    • higher bandwidth usage
    • more server load

    Real Example — Swiggy

    Swiggy app contains:

    • logos
    • icons
    • fonts
    • restaurant images

    Many of these rarely change.

    Browser/app caches them locally.

    This improves:

    • speed
    • user experience
    • bandwidth efficiency

    What Is HTTP Caching?

    HTTP caching uses special headers to tell browser:

    "How long should this data be cached?"
    

    These headers are part of:

    • HTTP response

    Example HTTP Response

    Cache-Control: max-age=3600
    

    Meaning:

    Cache this for 1 hour
    

    Browser Decision Flow

    User Requests Resource
            │
            ▼
    Check Browser Cache
       │               │
    Found            Not Found
       │                │
       ▼                ▼
    Use Cached      Fetch From Server
    Copy
    

    Common HTTP Cache Headers

    HeaderPurpose
    Cache-ControlDefines caching rules
    ExpiresExpiry time
    ETagDetect content changes
    Last-ModifiedResource update timestamp

    What Is Cache-Control?

    Most important caching header.


    Example

    Cache-Control: max-age=86400
    

    Means:

    • browser can cache file for 24 hours

    Real Example — YouTube

    Static assets like:

    • JS bundles
    • logos
    • CSS

    may use:

    1 month cache duration
    

    because they rarely change.


    What Is ETag?

    ETag helps browser check:

    "Has file changed?"
    

    without downloading full file again.


    ETag Flow

    Browser Requests File
            │
            ▼
    Server Sends ETag
            │
            ▼
    Browser Stores ETag
            │
            ▼
    Next Request Sent With ETag
            │
            ▼
    Server Checks Changes
       │                 │
    Changed           Same
       │                 │
       ▼                 ▼
    Send New File    Return 304
    

    What Is 304 Not Modified?

    If file unchanged:

    server responds:

    304 Not Modified
    

    Meaning:

    • browser should use cached copy

    Very efficient.


    Real Example — Instagram

    When scrolling Instagram:

    • many UI assets already cached

    Only:

    • new content
    • new images

    need downloading.

    This makes app feel smooth.


    Types Of Browser Cache

    TypeExample
    Memory CacheTemporary fast storage
    Disk CacheStored on device disk
    Service Worker CacheOffline web apps

    What Is CDN Caching?

    Beyond browser cache:

    large companies use:

    CDN
    

    CDN Means

    Content Delivery Network
    

    Distributed servers globally.


    Real Example — Hotstar IPL Streaming

    Suppose Jaipur user watches IPL.

    Without CDN:

    • video fetched from USA server

    Very slow.

    With CDN:

    • nearby Indian server serves content

    Much faster.


    CDN Cache Flow

    User Requests Video
            │
            ▼
    Nearest CDN Server
       │               │
    Cached            Not Cached
       │                │
       ▼                ▼
    Fast Delivery    Fetch From Origin
    

    Why Caching Improves Performance

    Caching reduces:

    • server load
    • database queries
    • bandwidth usage
    • latency

    Example

    Without cache:

    Every User Request
            │
            ▼
    Application Server
            │
            ▼
    Database Query
    

    Huge load.


    With Cache

    User Request
          │
          ▼
    Cache Layer
      │            │
    Hit           Miss
     │              │
     ▼              ▼
    Fast         Database Query
    Response
    

    What Is Cache Hit?

    Data found in cache.

    Very fast.


    What Is Cache Miss?

    Data not found in cache.

    System fetches from:

    • server
    • database
    • origin

    Why Cache Hits Are Important

    Higher cache hit rate means:

    • lower latency
    • better scalability
    • lower infrastructure cost

    Real Example — Flipkart Big Billion Day

    Millions search products simultaneously.

    Caching helps:

    • reduce database load
    • improve response time
    • survive traffic spikes

    What Should Be Cached?

    Good candidates:

    • static assets
    • product pages
    • popular searches
    • public APIs

    What Should NOT Be Cached?

    Avoid caching:

    • sensitive user data
    • highly dynamic data
    • payment confirmations

    unless carefully controlled.


    Cache Expiry

    Cached data eventually becomes outdated.

    Need:

    • expiration rules

    Example

    Cache-Control: max-age=300
    

    Means:

    • cache valid for 5 minutes

    What Is Cache Invalidation?

    One of hardest problems in system design.

    Suppose:

    • product price changes
    • cache still shows old price

    Wrong data shown.

    Need cache invalidation.


    Cache Invalidation Flow

    Database Updated
           │
           ▼
    Invalidate Cache
           │
           ▼
    Next Request Fetches Fresh Data
    

    Real Example — Blinkit Inventory

    Inventory changes rapidly.

    Suppose:

    • milk packet becomes out of stock

    Cache must update quickly.

    Otherwise:

    • users may order unavailable item

    Common Cache Strategies

    StrategyMeaning
    TTLCache expires automatically
    Write-throughUpdate cache immediately
    Lazy loadingCache only when needed
    Cache-asideApp manages cache manually

    What Is Cache-Aside?

    Very common strategy.


    Cache-Aside Flow

    Application Receives Request
            │
            ▼
    Check Cache
       │               │
    Hit              Miss
       │                │
       ▼                ▼
    Return          Query Database
    Cached Data         │
                        ▼
                  Store In Cache
    

    Real Example — Swiggy Restaurant List

    Popular restaurant lists may be cached in:

    • Redis
    • CDN
    • browser

    instead of hitting database repeatedly.


    Browser Cache vs CDN Cache vs Redis

    Cache TypeLocationExample
    Browser CacheUser deviceImages/CSS
    CDN CacheEdge serversVideos/static assets
    Redis CacheBackend memoryAPI/database caching

    Common Beginner Misconceptions


    "Caching Means Data Always Fresh"

    No.

    Caching introduces:

    • stale data risk

    "Cache Solves Everything"

    No.

    Poor caching strategy may:

    • increase bugs
    • create inconsistency

    "Only Big Companies Need Caching"

    Even small apps benefit significantly.


    Real Internet-Scale Architecture

    Users
      │
      ▼
    Browser Cache
      │
      ▼
    CDN Cache
      │
      ▼
    Load Balancer
      │
      ▼
    Application Server
      │
      ▼
    Redis Cache
      │
      ▼
    Database
    

    Final Mental Model

    Cache =
    Temporary fast storage
    used to avoid repeated expensive operations
    

    Caching improves:

    • speed
    • scalability
    • user experience

    One-Line Interview Definition

    Browser and HTTP caching improve application performance by storing reusable resources closer to users, reducing server load, bandwidth usage, and latency through controlled cache policies and validation mechanisms.

    Module context

    From browser cache to Redis clusters and CDNs. Caching strategies that turn slow apps into fast ones, with real-world invalidation patterns.

    Mark this lesson complete when you finish reviewing it.