Home / Articles / Web Development
Web Development

Website Cache Is Not Free: How to Use It Without Displaying Stale Data

Cache can make a website feel much faster, but it can also display outdated information when its update rules are unclear. Here’s how to understand, design, and safely clear cache in web applications.

Cache Website Tidak Gratis: Cara Memakainya Tanpa Menampilkan Data Basi

Cache is often seen as a simple button to speed up a website: store previously generated results, then reuse them when there’s a subsequent request. The problem is, cache is not just about speed. It also determines how fresh the information is that users see.

Product prices can change, stock can run out, user profiles can be updated, and articles can be edited. If old results are still stored, the website may feel fast, but users receive incorrect information. Therefore, cache should be treated as part of application design, not a last-minute patch after the website feels slow.

What exactly does cache do?

Cache is temporary storage for data that is likely to be reused. When a page or query is processed for the first time, the result is stored. Subsequent requests can retrieve that result without repeating the entire process.

In PHP and MySQL applications, for example, a list of articles typically needs to perform several steps:

  • receive parameters from the URL;
  • run a query to the database;
  • process the query results;
  • build HTML to send to the browser.

If the list of articles does not change every second, the results can be stored for several minutes. The next visitor does not need to make the database work from scratch.

However, cache does not make the process free. Applications still require storage space, expiration rules, deletion mechanisms, and ways to handle data that is no longer relevant.

Three types of cache that are often mixed up

Browser cache

Browsers can store files such as CSS, JavaScript, and images on the user's device. This reduces repeated downloads when users open other pages on the same website.

The advantage is that server load and network usage decrease. The risk is that users may still see old files after you make changes. Techniques like cache busting—for example, adding a version to the name or URL of the file—can help.

<link rel="stylesheet" href="/css/app.css?v=2026-01">

The version is not a solution for everything, but it is useful when the browser needs to be informed that a file has changed.

Application cache

Application cache is on the server side. Query results, API data, or snippets of pages can be stored using Redis, Memcached, or even temporary files for small applications.

This type of cache is suitable for data that is read multiple times but rarely changes. Examples include category lists, website configurations, or popular articles.

Cache in content delivery networks

CDNs can store certain assets and pages closer to users. This helps reduce network distance but adds a layer that must be considered when content changes.

If a page is already stored in the CDN, clearing the cache on the application server alone does not necessarily make users see the new version immediately.

TTL is not the only strategy

TTL or time to live is the duration a cache is considered valid. For example, the cache for the list of articles is valid for 300 seconds.

$cacheKey = 'articles:latest';
$ttl = 300;

TTL is easy to implement, but it has drawbacks. If an editor fixes an error in an article one minute after the cache is created, users may still see the old version for the next four minutes.

Therefore, TTL should be combined with invalidation, which is the process of deleting or updating the cache when the source data changes.

A simple example:

  1. a new article is published;
  2. the article data is stored in MySQL;
  3. the cache for the list of articles is cleared;
  4. the next request regenerates the cache with the latest data.

This approach is usually more accurate than just waiting for the TTL to expire.

Choose a strategy based on the nature of the data

Not all data requires the same level of freshness. This is often a decision more important than choosing a caching tool.

  • Data that rarely changes: categories, country lists, or public configurations can have a long TTL.
  • Data that changes periodically: articles, documentation pages, and catalogs can use a medium TTL with invalidation after updates.
  • Highly sensitive data: balances, payment statuses, and user permissions should not rely on old cache without very clear rules.
  • Personal data: ensure that cache cannot be mixed between users.

Serious errors occur when cache keys do not distinguish user contexts. For example, a user profile page is first stored with the key profile, then the second user receives the same result. For personal data, keys should include relevant identities or parameters while ensuring access is still checked on the server.

Use understandable cache keys

A cache key is a unique name for the stored data. Keys that are too generic can easily cause collisions, while keys that are too random make deletion difficult.

Clearer examples:

article:list:page:1:category:php
article:detail:482
user:dashboard:731

If the list of articles changes, you can delete all keys related to the article list. If one article is edited, the cache for that article's details and the affected list cache need to be considered separately.

For larger applications, using version namespaces also helps:

v2:article:detail:482

When the data structure changes, the namespace can be incremented without having to guess all the old keys that are still stored.

Do not cache errors unintentionally

One of the hardest bugs to find is when error responses are also cached. For example, if the database has issues and the application generates a blank page. If that result goes into the cache, users still see the broken page even though the database has recovered.

In general, cache should only contain valid results that are worth sharing. Responses like temporary errors, incomplete user data, or failed query results should not be treated the same as normal results.

Also, pay attention to empty responses. A product list that is currently empty may indeed be correct, but it could also happen due to a wrong filter or an unfinished synchronization process. Caching for empty results needs to have clear rules and often a shorter TTL.

Add ways to observe cache conditions

Unobservable cache will make debugging difficult. At the very least, the application needs to provide internal information about:

  • how many requests received a cache hit;
  • how many had to regenerate data or experienced a cache miss;
  • when a cache was created and when it expires;
  • which keys are associated with a specific update process;
  • how long the cache regeneration process takes.

This information does not need to be displayed to users. Structured logs and internal monitoring panels are sufficient to help answer questions like: “Why hasn’t the newly edited article appeared?” or “Why did database usage spike after the cache was cleared?”

What you can do now

  1. Note the pages or queries that are frequently read and expensive to process.
  2. Determine how long that data can be considered valid.
  3. Create cache keys that contain important parameters.
  4. Decide when the cache should be cleared after data changes.
  5. Ensure personal data is not mixed between users.
  6. Test conditions for empty cache, expired cache, and cache containing old data.
  7. Add logs so that issues can be traced without guessing.

A good cache is not one that stores as much data as possible. A good cache is one that helps applications work more efficiently without sacrificing information accuracy. Speed is indeed important, but users find it harder to forgive a fast website that displays outdated prices, statuses, or content.

– Rio Yotto @rioyotto