Performance Optimization

Building High-Performance Applications with Advanced Caching Mechanisms

By the Domain India teamPublished 10 min read
Knowledge base article
Contents (11 sections)

Caching means keeping a ready-made copy of something expensive, a page, a query result, compiled code, so the next request gets it without the work. It is the single most effective way to make a website or application faster and to let it handle more visitors on the same hardware. This guide walks through the caching layers from the browser to the database, what each one is good for, what is already switched on for you on Domain India shared hosting, and what you can set up yourself on your own VPS.

Key takeaways

Cache at several layers: browser headers for static files, a page cache for whole HTML pages, OPcache for compiled PHP, an object cache such as Redis for query results, and a CDN for visitors far from the server. On Domain India shared hosting, OPcache and compression are already on, cPanel adds an nginx proxy cache in front of Apache, and you add browser headers and a page-cache plugin yourself. Redis, Memcached and Varnish need a VPS. Every cache needs a plan for invalidation, or visitors see stale content.

1. The caching layers at a glance

LayerWhat it storesBest forWho controls it
Browser cacheImages, CSS, JavaScript, fontsRepeat visitsYour Cache-Control and Expires headers
CDNStatic files, sometimes whole pagesVisitors in many regions, traffic spikesThe CDN's settings and your headers
Page cacheFinished HTML pagesPages that look the same for every visitorA plugin, or a reverse proxy on your own server
OPcacheCompiled PHP codeEvery PHP siteThe PHP configuration
Object cacheQuery results, API responses, sessionsDatabase-heavy appsYour code, with Redis or Memcached
Database bufferTable and index data in memoryEvery queryThe database configuration

The closer a cache is to the visitor, the faster the answer and the less work reaches your server. Most sites need the first four; busy applications add the last two.

2. Browser caching: stop re-downloading the same files

A returning visitor should not download your logo and style sheet again. Tell the browser how long to keep them. On Apache hosting, add this to .htaccess in your site's folder:

apacheconf
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

Long lifetimes are safe only if the file name changes when the content does, for example style.3f9a1c.css. Build tools and most WordPress caching plugins do this for you. Keep HTML pages short-lived or uncached, so updates show up.

3. Page caching: serve finished HTML

Building a WordPress page can mean dozens of database queries and a lot of PHP. A page cache saves the finished HTML and hands it to the next visitor, skipping PHP and the database for most requests.

  • WordPress on shared hosting: use WP Super Cache or W3 Total Cache, and keep only one page-caching plugin active. Our shared hosting runs Apache, so plugins built for other web servers' caches do not apply.
  • Other PHP applications: use the framework's full-page or response cache.
  • Never cache personal pages: carts, checkouts, account pages and anything shown to a logged-in user. Good plugins exclude these by default; check the settings for WooCommerce.

4. OPcache: stop recompiling PHP

PHP turns your code into bytecode before running it. OPcache keeps that bytecode in memory so it is not recompiled on every request. On a VPS, a reasonable starting point in your PHP configuration is:

ini
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2

With validate_timestamps=0 PHP never checks for changed files, which is slightly faster, but you must reload PHP-FPM after every deploy or visitors keep running the old code. See the PHP OPcache guide for sizing and monitoring.

5. Object caching with Redis or Memcached

An object cache stores the result of expensive work, a product list, an exchange rate, a rendered menu, in memory, with an expiry time:

php
$products = Cache::remember('products.active', 300, fn () =>
    Product::where('active', true)->with('category')->get()
);   // Laravel: runs the query at most once every 5 minutes

The pattern is the same everywhere: look in the cache, and on a miss do the work, store the result with a TTL, and return it. When the data changes, delete the key (Cache::forget('products.active')).

On your own server, install Redis and give it a memory limit with an eviction policy suited to a cache:

ini
# /etc/redis/redis.conf (cache-only instance)
maxmemory 256mb
maxmemory-policy allkeys-lru
bind 127.0.0.1 -::1

allkeys-lru removes the least recently used keys when memory is full, which is right for a cache and wrong for a job queue. If the same app also uses Redis for queues, run a separate instance for them with noeviction; see Redis-based queues with BullMQ and Laravel.

6. Database caching

  • The MySQL query cache is gone. It was removed in MySQL 8.0, so tips about query_cache_size or SHOW STATUS LIKE 'Qcache%' no longer apply. Cache results in your application instead.
  • Indexes come first. A missing index on a column you filter or join on is the most common cause of a slow page. Use EXPLAIN (or EXPLAIN ANALYZE on MySQL 8.0.18 and later) to see how a query runs. See MySQL performance optimisation.
  • On your own server, size the buffer pool. innodb_buffer_pool_size decides how much table and index data InnoDB keeps in memory. On a server that runs only the database, it is commonly set to well over half the RAM; on a VPS that also runs the web server and PHP, leave room for them.

7. CDN and edge caching

A CDN keeps copies of your static files, and optionally whole pages, on servers around the world. It helps most when visitors are spread across regions or your pages carry many images. It won't speed up pages that can't be cached, such as a logged-in dashboard. Setting up Cloudflare with your Domain India site, including caching rules and SSL modes, is covered in the complete Cloudflare setup guide.

8. A reverse-proxy cache on your own server

On a VPS you can cache whole responses in the web server. With nginx and PHP-FPM:

nginx
# in the http block
fastcgi_cache_path /var/cache/nginx/fcgi levels=1:2 keys_zone=APP:100m inactive=60m max_size=1g;

# in the server block
set $skip_cache 0;
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($http_cookie ~* "wordpress_logged_in|woocommerce_items_in_cart|PHPSESSID") { set $skip_cache 1; }

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php-fpm.sock;
    fastcgi_cache APP;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_valid 200 301 10m;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    add_header X-Cache $upstream_cache_status;
}

Adjust the socket path to your PHP-FPM version. Test with nginx -t before reloading, and check the X-Cache header (HIT or MISS) with curl -I. The skip rules matter: without them, one visitor's cart or account page can be served to another. Varnish does the same job as a separate service in front of the web server.

9. Invalidation and measuring

  1. Measure before you change anything.
    Use PageSpeed Insights or WebPageTest and note the time to first byte and the total page weight.
  2. Pick TTLs by how often data changes.
    Seconds or minutes for prices and stock, hours for menus, a year for fingerprinted files.
  3. Purge on change.
    Clear the page cache when you publish, delete object-cache keys on writes, and purge the CDN after a deploy.
  4. Measure again, as a visitor.
    Test logged out, in a private window, and check that personal pages are not cached.
Seeing an old version of your page?

Add ?t= followed by anything to the address, for example ?t=123. The query string bypasses most caches, so if that version is correct, the old copy is coming from a cache, not your files.

10. What is already done for you on Domain India

On shared hostingcPanelDirectAdmin
OPcacheOnOn for every PHP version except 8.3
Compressionnginx gzips HTML, CSS, JS, JSON, XML and SVGmod_deflate on server-wide
Proxy cachenginx in front of Apache can keep 200, 301 and 302 responses for up to 120 minutesNo nginx proxy
Browser-cache headersmod_expires and mod_headers available in .htaccessSame
Redis, Memcached, APCuNot availableNot available

Measured on our servers in September 2026. There is no Brotli, and you don't need to switch compression on yourself. Because the cPanel proxy can keep a copy of a page, use the ?t= test above after a change, and see my website is slow for finding what is slow before adding caches.

When your application needs Redis, Memcached, Varnish or its own nginx cache, it has outgrown shared hosting. A VPS gives you root access to run them; it is self-managed, so you install and maintain the stack yourself.

VPS Starter
₹552.65/mo + GST
  • 1 vCPU
  • 2 GB DDR4 RAM
  • 64 GB NVMe SSD Storage
  • 2 TB Monthly Bandwidth
See plan details

Frequently asked questions

Is OPcache enabled on Domain India shared hosting?

Yes on cPanel. On DirectAdmin it is enabled on every PHP version except PHP 8.3. You don't need to install anything.

Can I use Redis or Memcached on Domain India shared hosting?

No. There is no Redis or Memcached service on shared plans, and the PHP extensions for them are not installed. Use a VPS if your application needs an object cache.

Which WordPress caching plugin should I use on Domain India shared hosting?

WP Super Cache or W3 Total Cache. Our shared hosting runs the Apache web server, so plugins built for other web servers' caches do not apply. Keep only one page-caching plugin active.

Why do I still see the old version of my page after an update?

A cache is serving a stored copy. Clear your caching plugin, purge your CDN if you use one, and test with ?t=123 added to the address. On cPanel the nginx proxy in front of Apache can keep a copy for up to 120 minutes.

Does the MySQL query cache still exist?

No. It was removed in MySQL 8.0. Use indexes, and cache results in your application with an object cache on a server where one is available.

Do I need a CDN?

Not always. A CDN helps most when visitors are spread across many regions or pages carry many images. It will not fix a slow server response on pages that can't be cached, such as checkouts or dashboards.

Ready to speed things up? Start with my website is slow to find the bottleneck, compare cPanel hosting, or look at VPS plans when you need your own Redis or proxy cache. For help, open a support ticket.

Need your own cache stack?

A self-managed VPS with root access lets you run Redis, Memcached, Varnish or an nginx page cache for your application.

See VPS plans

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
Caching for Faster Websites and Apps: A Practical Guide