Archives
- May 2025
- November 2024
- April 2024
- November 2023
- October 2023
- August 2023
- May 2023
- February 2023
- October 2022
- August 2022
- July 2022
- May 2022
- April 2022
- March 2022
- February 2022
- June 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- August 2016
- June 2016
- April 2016
- March 2016
- February 2016
- January 2016
- July 2015
- June 2015
- Older posts

Caching in Web Applications: Explained with Simple Examples
Caching is the process of storing frequently used data in memory.
An example of this is when a teacher asks a student, “What is the capital of China?”
The student searches for the answer in a book and replies.
The next time the teacher asks the same question, the student answers quickly—without needing to look it up again.
Similarly, caching works this way: when a resource is requested, if it’s not already present in memory, it is fetched from the original source and stored in cache memory.
The next time that same resource is requested, it is served directly from the cache.
In a web application, caching happens at different levels:
- Browser: The browser caches static data such as images, CSS, and JavaScript files.
- Proxy: A proxy server acts as an intermediary between the client and the original server. It caches data to allow users quicker access to resources.
- Server: The server can cache HTML content of commonly accessed pages. It can also cache the results of SQL queries.
Controlling Caching with HTTP Headers:
We can control caching behavior by using proper HTTP headers, which tell the browser what to cache and for how long.
Some common headers include:
- max-age: Defines the time (in seconds) before the cached copy expires.
- no-cache: The browser can cache the response but must first submit a validation request to the origin server.
- no-store: Browsers are not allowed to cache the response at all.
- public: The resource can be cached by any cache (browser, proxy, etc.).
- private: The response can only be cached on the client’s device.
These headers must be set by the server.
On Apache servers, you can set them in the .htaccess
file using the mod_headers and mod_expires directives.
Third-Party Caching Solutions:
Most websites today use third-party caching solutions like Memcached, Nginx, and Redis to boost server performance.
- Memcached is an open-source, distributed caching system.
It uses a key-value pair model to store data in memory and is best suited for data-driven applications.
If you have a query that is used repeatedly and returns the same result, it can be cached in Memcached.
Future requests for the same data will be served from the cache, reducing database trips—especially helpful when many users request the same data at once. - Memcached is ideal for storing small data.
If you need to cache larger or more complex data, Redis is a better choice. - Redis can also function as a database and supports advanced data types like strings, hashes, lists, and sets.
It even allows you to perform operations on cached data, giving it more flexibility than Memcached.
Best Practices for Caching:
When using caching, it’s important to note:
- Not all data should be cached, especially data that changes frequently.
- Caching outdated data can lead to user confusion or incorrect information.
- You must carefully decide what data to cache and how long to cache it.
Summary:
Caching is a powerful technique to speed up web performance, reduce server load, and improve user experience—but it must be used wisely.