Types of Caching
The Layers Between a Visitor and the Database
Types of caching are the separate layers where a copy of content can be stored between a visitor and the database that holds a site's data. Each layer keeps different things, sits in a different place and speeds up a different part of the request.
A single request often passes through several of these layers. The browser may already hold a copy of a logo. A content delivery network may hold the images and stylesheets. A reverse proxy may keep whole pages. Inside the application, the results of database queries may be stored, and the PHP engine may keep compiled code in memory.
Not every site uses every layer. A static brochure site may rely mainly on browser and CDN caching, while a busy online shop depends on server-side layers and careful rules for what must never be cached.
Caches Outside the Application
Browser caching stores files on the visitor's own device. Once a stylesheet or photo has been downloaded, the browser can reuse it on later visits without asking the server again. The rules behind this are explained in the browser caching article.
CDN caching stores copies on edge servers spread across the world. Common files are delivered from a nearby location, which reduces both the distance travelled and the number of requests that reach the origin.
Reverse proxy caching sits on the server side, in front of the web server. It can hold complete responses and return them before the application runs, which lowers the load on the application itself.
Caches Inside the Application Stack
Page caching stores the complete HTML of a page so the application does not need to run again for each visit. It works best for pages that look the same for everyone.
Object caching stores the results of expensive work, such as database query results or computed values, often in memory. It helps dynamic pages, where a full-page copy cannot be reused.
Opcode caching, such as OPcache for PHP, keeps compiled script code in memory so the engine does not recompile the same files on every request. The general idea behind all these layers is covered in the web caching overview.
Frequently asked questions
Which type of caching should a website use?
Most sites use several types together rather than choosing one. Browser and CDN caching benefit almost any site that serves images, stylesheets and scripts. Page or object caching helps sites with repeated content or heavy database work. The right mix depends on how often content changes and whether it differs between visitors.
Why do cached copies sometimes show old content?
Each cache layer keeps its own copy with its own lifetime. A visitor may therefore see an older version stored in their browser or on a CDN edge server, even after the source has changed. Waiting for the lifetime to end, or clearing the relevant cache layer, brings the newest version through.