Skip to content
BeoHosting
BeoHosting
Technical

What Is Caching and the Types of Caching

BeoHosting Team··10 min read read
What Is Caching and the Types of Caching

What caching is and why it matters

Caching is a technique of storing a copy of data in a location from which it can be retrieved faster instead of regenerating or fetching it from the original source every time. Picture caching as making a photocopy of a document you often use instead of going to the archive for the original every time. In the context of websites, caching dramatically speeds up page loading, reduces server load, and improves user experience. Without caching, every page request would require PHP code processing, database queries, and HTML generation from scratch.

Caching works on the principle of temporary storage of expensive operation results. When a user first visits a page, the server fully generates it and saves the result in the cache. Every subsequent user gets the cached version delivered in a fraction of the time. A typical WordPress dynamic page without cache loads in 2 to 5 seconds while a cached version of the same page can be delivered in 50 to 200 milliseconds, a 10 to 100 times improvement.

Browser caching

How browser cache works

Browser caching is the simplest and closest form of caching that happens directly on the user's device. When you visit a site, the browser downloads HTML, CSS, JavaScript, images, and fonts from the server. The browser cache stores these files locally so on your next visit it doesn't have to download them again from the server. This is why on the second visit a page loads much faster than the first time.

HTTP cache headers

The server controls browser caching via HTTP headers that tell the browser how long to keep certain files. The Cache-Control header is the most important and can contain directives like max-age defining how many seconds the browser should keep the file, no-cache telling the browser it must check with the server before using the cache, no-store completely forbidding caching, and public or private determining whether intermediaries like CDNs may cache the content.

The Expires header is an older way of setting cache that defines the exact date and time when the cache expires. The ETag header is a file version identifier the browser sends to the server to check whether the file has changed. If it hasn't changed, the server returns a 304 Not Modified response without content, saving bandwidth. The Last-Modified header works similarly but uses the last modified date instead of an identifier. In practice, use Cache-Control with max-age for static resources and ETag for dynamic content.

Practical configuration

For optimal browser caching, set long max-age values for static resources that rarely change. CSS and JavaScript files with versioned names like style.v2.css can have a max-age of one year because when the content changes, you change the filename. Images can have a max-age from 30 days to one year depending on how often you change them. HTML pages should have a short max-age or no-cache because content changes more often. In Apache use the mod_expires directive in the htaccess file and in Nginx the expires directive in the configuration.

Server caching

Opcode caching

PHP is an interpreted language meaning PHP code is compiled to machine code every time a page is loaded. Opcode caching eliminates this repetition by storing compiled machine code in memory. OPcache is a built-in PHP mechanism for opcode caching that comes with PHP 5.5 and newer. When OPcache is active, PHP code is compiled only once and every subsequent request uses the previously compiled code from memory. This can speed up PHP applications by 2 to 5 times without any code changes.

Full page caching

Full page caching or page cache stores the complete HTML output of a page so PHP code doesn't execute at all for cached requests. This is the most effective form of caching for WordPress and similar CMS platforms because it eliminates all PHP processing and database queries. The server simply delivers a static HTML file from cache. Nginx FastCGI cache and Apache mod_cache are server-level solutions while WordPress plugins like WP Super Cache and W3 Total Cache implement page cache at the application level.

Server cache configuration

On Nginx, the fastcgi_cache directive defines a cache zone in memory, fastcgi_cache_valid sets how long the cache is valid for different HTTP status codes, and fastcgi_cache_key defines the key by which the cache is identified, usually a combination of request method, host, and URI. It's important to properly configure exceptions so POST requests, pages with cookies for logged-in users, admin pages, and pages with query parameters aren't cached. At BeoHosting our servers use an optimized Nginx configuration with FastCGI cache for maximum WordPress site performance.

Object caching

What object cache is

Object caching stores database query results and other expensive operations in a fast in-memory database instead of executing these queries over again on every page load. WordPress has a built-in object cache mechanism but by default it only keeps data during a single request. With a persistent object cache solution like Redis or Memcached, cached objects stay in memory between requests, which dramatically reduces the number of database queries.

Redis as object cache

Redis is an in-memory database that stores data in RAM with optional persistent disk storage. For WordPress, the Redis Object Cache plugin connects the WordPress object cache API with a Redis server. A typical WordPress site runs 50 to 200 database queries per page. With a Redis object cache, most of these queries are eliminated because results are read from memory which is hundreds of times faster than disk. Redis supports various data structures including strings, hashes, lists, sets, and sorted sets, making it extremely flexible.

Memcached alternative

Memcached is another popular object caching solution that's simpler than Redis but sufficient for most WordPress sites. Memcached stores data exclusively in memory without persistent storage, meaning all cached data is lost on server restart. The advantage of Memcached is lower memory consumption per key and very stable performance under heavy load. For most sites, the choice between Redis and Memcached won't make a noticeable performance difference.

Page caching for CMS

WordPress caching plugins

The WordPress ecosystem offers many plugins for page caching with various features and complexity. WP Super Cache is the most popular free plugin that generates static HTML files and serves them directly without PHP processing. W3 Total Cache is a more comprehensive solution that combines page cache, browser cache, object cache, and CDN integration but is more complex to configure. WP Rocket is a premium plugin offering an excellent balance between performance and simplicity with automatic configuration and advanced optimizations.

Cache invalidation

The biggest challenge of caching is invalidation - how and when to delete outdated cache. If you publish a new blog post but the cache shows the old version of the homepage, visitors won't see the new content. Good cache plugins automatically delete relevant cache files when content changes. For example, when you publish a new post, you should delete the homepage cache, the cache of the category page the post belongs to, date archive pages, and the sitemap. Manual full cache flushing should be avoided because it causes a temporary performance drop while the cache rebuilds.

CDN caching

How a CDN works

A Content Delivery Network or CDN is a network of servers distributed worldwide that stores copies of your site and delivers content from the server geographically closest to the visitor. When a visitor from Tokyo accesses your site hosted in a US data center, without a CDN the request travels across the ocean adding 200 to 300 milliseconds of latency. With a CDN, content is delivered from the CDN server in Tokyo or a nearby region with latency of only 10 to 30 milliseconds.

Popular CDN services

The Cloudflare CDN service is the most popular CDN with a free plan that includes CDN, DDoS protection, SSL certificate, and basic optimizations. Cloudflare has over 300 data centers worldwide including regional locations like Vienna and Bucharest. BunnyCDN is a budget-friendly alternative with pay-per-use and excellent performance. AWS CloudFront, Google Cloud CDN, and Azure CDN are enterprise solutions integrated with cloud platforms. For WordPress sites, Cloudflare is the simplest to set up with a dedicated WordPress plugin that automatically clears the cache when content is published.

Edge caching

Modern CDNs offer edge computing capabilities where application logic runs on CDN servers instead of the origin server. Cloudflare Workers, Vercel Edge Functions, and AWS Lambda@Edge enable running code at edge locations for content personalization, A/B testing, redirects, and authentication without communicating with the origin server. Edge caching combined with edge computing can deliver fully personalized pages at CDN speed, which is a revolutionary change in web performance.

Caching strategies

Cache-aside pattern

Cache-aside or lazy loading is the most common caching pattern where the application first checks the cache for the requested data. If data exists in the cache, that's called a cache hit and it's returned immediately. If it doesn't exist, that's a cache miss and the application fetches the data from the database, stores it in the cache, and then returns it to the user. This pattern is simple to implement and works well for read-heavy applications where data is read much more often than it changes.

Write-through and write-behind

The write-through pattern writes data simultaneously to the cache and database, guaranteeing the cache is always up to date but increasing write latency. The write-behind or write-back pattern writes data first to the cache and asynchronously writes it to the database, reducing write latency but creating a risk of data loss if the server crashes before data is written to the database. For most web applications, cache-aside with smart invalidation is the best choice because it balances performance and consistency.

Conclusion

Caching is a fundamental technique for site performance that works at multiple levels from browser to CDN. Browser cache eliminates unnecessary downloads of static resources, server cache speeds up dynamic page generation, object cache reduces database load, page cache eliminates PHP processing, and CDN cache brings content closer to users worldwide. The combination of these techniques can speed up your site by 10 to 100 times and significantly improve user experience. At BeoHosting we offer performance-optimized hosting with OPcache, Redis object cache, and CDN integration support. Check your site's speed.

BeoHosting Team

10+ years of experience — Web hosting and infrastructure specialists

  • Web Hosting
  • WordPress Hosting
  • VPS
  • Dedicated Serveri
  • Domeni
  • SSL
  • cPanel
  • LiteSpeed
  • Linux administracija
  • DNS

Last updated: