esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Web Server Caching: Serve Saved Responses Before the App Runs

A reverse proxy keeps finished pages and answers repeat requests itself, so the app and database behind it do less work.

In short

Web server caching is when a reverse proxy keeps finished pages and answers repeat requests itself, so the app and database behind it do less work.

12 min readFree

What You Will Learn

Web server caching means that a web server, or a reverse proxy in front of it, keeps a finished response and hands copies of it to later visitors. The app code and the database behind it run less often. Pages load faster, and the servers carry less load. In this lesson you will learn how the cache works, how it decides how long to keep a copy, which pages are safe to store, and how a news site might use it.

Read the key words first. Each one appears again in the lesson.

Key words

Web server
Software that answers requests from browsers over HTTP, the protocol browsers and servers use to exchange requests and responses.
Reverse proxy
A server that stands in front of other servers, receives requests for them, and decides how to answer. Nginx and Varnish are common examples.
Cache
A stored copy of something, kept close to where it is needed, so the next request can be answered faster.
Origin or app
The real application and its database, which sit behind the cache and build the response when no copy exists.
Cache hit
A request answered from a stored copy.
Cache miss
A request with no usable stored copy, so it must go on to the app.
Cache key
The details of a request, such as the address, that decide whether two requests may share one stored copy.
Header
A label of extra information sent with a request or a response, such as how long the response may be kept.
Cookie
A small piece of data a site stores in the browser and sends back with each request, often to recognize a logged-in user.
TTL (time to live)
How long a stored copy may be used before it expires.
Purge
Removing a stored copy on purpose, before its time to live has ended.
Personalized response
A page whose content depends on who asks, such as an account page or a shopping cart.

How Does the Cache Sit in Front?

Think of the cache as a shelf next to the kitchen. A customer who orders a dish already on the shelf gets it at once. Only a new order reaches the cooks. In web terms, the reverse proxy sits between the client, such as a browser, and the app. Every request reaches the proxy first.

Nginx and Varnish both play this role. Varnish describes itself as an HTTP accelerator and a caching reverse proxy. Nginx can save responses to a disk cache and use them to answer clients without contacting the app each time.

The basic cycle has two outcomes. A hit is answered from memory or disk. A miss is passed to the app, and the answer is stored for next time.

What Are Hits and Misses?

  1. 1

    Request arrives

    A browser asks for a page. The proxy receives the request before the app does.

  2. 2

    Look for a copy

    The proxy builds a cache key from the request and checks whether a stored response matches it.

  3. 3

    Hit: answer from the store

    If a usable copy exists, the proxy returns it. The app and the database are not touched.

  4. 4

    Miss: ask the app

    If no copy exists, the proxy forwards the request to the app, receives the full response, and passes it back.

  5. 5

    Store for next time

    The proxy keeps a copy for a set time, so the next matching request becomes a hit.

Varnish works in this same way. It answers from the cache when it can and fetches from the backend when it cannot. Its documentation says a cached answer can arrive in microseconds, far faster than a typical backend server.

How Are Requests Matched to Stored Copies?

A cache must know which stored copy fits which request. It does this with the cache key. Nginx's documentation says that by default it caches responses to GET and HEAD requests, and it uses the request string as the key. Other setups build a longer key from the host, the path, and chosen headers or cookies.

  • Address and query string

    Two addresses that differ, even in a query string after a question mark, usually need separate copies.

  • Chosen headers and cookies

    A key can include a cookie. Nginx's documentation gives an example key that adds a cookie named user, so each user gets a separate copy.

  • The Vary header

    Vary tells the cache that a response depends on another request header. Varnish keeps a separate copy for each language listed in the Accept-Language header.

Tip

Vary needs care. Varnish's documentation lists varying on User-Agent as a pitfall, because browsers send so many different values that the cache would split into many copies and rarely reuse one.

How Do You Control Freshness?

Freshness means how long a stored copy may still be used. The web server decides this. It reads hints the app sends in headers, and it also follows its own settings. The main hints are listed below.

  • Cache-Control

    A response header that carries short instructions for caches. The public directive lets any cache store the response, and max-age sets how many seconds it stays fresh. Varnish treats a response sent as public with max-age=3600 as cacheable for one hour.

  • Expires

    An older header that gives a fixed end time for a copy.

  • ETag and Last-Modified

    These are validators. A cache can ask the app whether its copy is still current. If it is, the app can answer with a short not-changed reply instead of the full page.

The web server also chooses rules of its own. Nginx keeps responses until the cache is full, which is why a lifetime should always be set on purpose. The sub-topic below explains the two main ways to remove an old copy.

Should You Use Time to Live or Purge?

The simplest rule is a TTL. When the time ends, the copy expires, and the next request is a miss. A TTL is easy to set, but it is slow to correct. If a page changes one minute after it was cached, visitors may see the old version until the timer ends.

A purge removes a copy on purpose. Varnish offers purges of single objects and a ban function that removes many objects matched by an expression.

Tip

Use a short TTL for pages that change often. Purge a page when you know it has changed, so readers do not wait for the timer.

The sample below uses plain numbers for time instead of a real clock, so its output is always the same. Each number is the second when a request arrives. An article gets a 60 second lifetime, and the account path is never stored.

Python
cache = {}
app_calls = 0

def app(path):
    global app_calls
    app_calls += 1
    return 'rendered ' + path

def proxy(path, now):
    if path.startswith('/account'):
        return 'BYPASS ' + app(path)
    entry = cache.get(path)
    if entry is not None and now < entry[1]:
        return 'HIT ' + entry[0]
    body = app(path)
    cache[path] = (body, now + 60)
    return 'MISS ' + body

print(proxy('/article/1', 0))
print(proxy('/article/1', 30))
print(proxy('/article/1', 61))
print(proxy('/account', 62))
print(proxy('/account', 63))
print('app calls:', app_calls)

Output

MISS rendered /article/1
HIT rendered /article/1
MISS rendered /article/1
BYPASS rendered /account
BYPASS rendered /account
app calls: 4

Read the output line by line. The first request is a miss, so the app runs. The request at second 30 is a hit. At second 61 the 60 second lifetime has ended, so the app runs again. The account requests always reach the app, which is why the app call count reaches 4.

What Is Safe to Cache

A stored copy is served to whoever asks next. So a response is safe to share only when it is the same for everyone who requests that address. Varnish's documentation says it will not store a response from the backend that sets a cookie by default, and it skips the cache when the client sends a cookie.

  • Good candidates

    Static files such as images, stylesheets, and scripts, plus public pages, such as a news article that every reader sees in the same form.

  • Risky pages

    Personal pages such as a logged-in dashboard, a shopping cart, or account settings. A copy built for one user could reach another user.

  • Pages that vary

    Pages that change by language, device, or region. These can be cached safely only when the cache key or the Vary header covers every difference.

MDN explains the danger in plain terms. A personal response that lacks the private directive may be stored by a shared cache and reused for many users, which can leak personal information. The private directive limits storage to a single user's browser.

Keeping Private Pages Out

  1. 1

    Mark the response

    The app sends Cache-Control with the private directive on every response built for one user.

  2. 2

    Bypass on login

    The proxy skips the cache when a request carries a session cookie or an Authorization header.

  3. 3

    Exclude by path

    Turn caching off for paths such as account or cart pages. This is simple to set up and easy to check.

  4. 4

    Test with two accounts

    Request the page while logged in as two different users, and confirm that neither sees the other's data.

Tip

A caching mistake on a personal page is quiet. Nobody sees an error, so test logged-in paths before you go live.

Weighing Speed Against Freshness

Caching buys speed and lower load. The price is stale content. A reader may see an old version until the copy expires or is purged. For a news article, a minute of age is often acceptable. For a bank balance or a stock price, it usually is not. Choose each lifetime by asking how much harm an old copy could cause.

Worked Example: A News Article

Here is an imaginary news site. Its articles are the same for every reader, and its account page is personal. The team places Nginx in front of the app and sets rules by path. The configuration below is a simplified sketch, not a full production setup.

Text
proxy_cache_path /var/cache/nginx/news levels=1:2 keys_zone=news_cache:10m max_size=1g;

server {
    listen 80;
    server_name news.example.com;

    location /articles/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_cache news_cache;
        proxy_cache_valid 200 60s;
    }

    location /account/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_cache off;
    }
}
  1. 1

    Render once

    A reader requests an article. Nginx has no copy, so it forwards the request. The app renders the page, and Nginx stores it for 60 seconds.

  2. 2

    Serve from memory

    Other readers who ask for the same article within that minute get the stored page. The app does not run for them.

  3. 3

    Refresh after expiry

    When the 60 seconds end, the next request is a miss. The app renders a fresh copy, and the cycle starts again.

  4. 4

    Bypass for the account

    A request to the account path skips the cache. The app runs on every visit, so the page shows the right person's details.

Notice where each decision comes from. The article rule depends on the page being the same for everyone. The account rule is a path exclusion. The app should also send Cache-Control headers, so the proxy and the browser follow the same plan.

Check Your Understanding

Try these two short scenarios. Each one needs the ideas above, not just memory.

Quick check

An online store caches every web server response for 10 minutes. Shoppers now see another shopper's name and cart on their pages. What went wrong, and what change keeps product pages cached while making cart pages safe?

Quick check

A shop changes a product price at 9:00. The cached product page has a one-hour TTL. At 9:05, most visitors still see the old price. What is the most direct fix for future price changes?

Common Mistakes

  • Keying only by address

    If the cache key ignores who is asking, personal pages can be shared. Add login rules before you turn caching on.

  • Setting no lifetime

    Nginx keeps responses until the cache is full, so an unlimited lifetime can hide changes for a long time. Set a lifetime for every cached path.

  • Forgetting to purge

    A page that changes without a purge stays old until its timer ends. Build purging into the publishing step.

Key Takeaways

A web server or reverse proxy can store finished responses and answer repeat requests without running the app. A hit is fast and light. A miss goes to the app, and the answer is stored for next time. The cache key, the Cache-Control rules, the Vary header, and purges decide what is stored and for how long. Public pages and static files are good candidates. Personal pages need a bypass or a private marking, or they can leak to other users.

Tip

When you are unsure about a page, leave it uncached. Measure the speed gain first, then turn caching on for the pages that are the same for everyone.

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.