What is caching?
Simply put, a cache is a storage location. On websites, caching is used to avoid repeating time-consuming tasks. The first time a task (e.g., building a menu) is requested, the result is stored in the cache. The next time, the task is not executed again; instead, the cached result is served directly to the visitor.
How and what can we cache?
At several levels:
- query cache
- key/value cache
- reverse proxy
Query cache (database)
All databases have built-in query caching. This allows frequently executed SQL query results to be served directly from memory. It’s important to regularly tune your database server to make the most of this, and it also helps to write your queries in a way that maximizes cache efficiency.
However, query caching is not a magic solution: the database server can struggle to determine which queries are suitable candidates for caching and how long their results should remain in the cache.
Key/value cache (PHP)
We can also avoid repeatedly querying the database. Using a key/value cache such as Memcache or Redis, the developer decides which objects to cache and for how long.
For example, a website with a large navigation structure is usually stored in a tree structure in the database, which requires multiple SQL queries to retrieve completely. Even if query caching helps, multiple database requests are still needed.
With key/value caching, the results of these SQL queries can be stored in the server's RAM for hours, or until the site structure changes.
Full page caching
This type of caching goes even further. Instead of storing just parts of a site, the full HTML code is cached in memory. After all, the same page is often served to hundreds or thousands of users.
There are several ways to implement full page caching. In Drupal, this functionality is built-in. WordPress offers plugins to handle this for you.
An even better approach is to serve visitors before they reach your web server, using a reverse proxy, Varnish being the most well-known. This article explains what Varnish is, why it matters for you, and what to watch out for.
How does Varnish work?
Here’s an illustration of how a standard website works today.









