How Kinsta Uses Cloudflare Workers To Improve Cache Hit Rates by 56%
انتشار: شهریور 01، 1402
بروزرسانی: 31 خرداد 1404

How Kinsta Uses Cloudflare Workers To Improve Cache Hit Rates by 56%

Thanks to our cache key customization, we realized we could cache the Workers KV data with Cache API, saving on reading operations and possibly lowering the latency by avoiding multiple Workers KV GET requests per visitor. Since the cached response is now based on the request’s URL combined with KV data, we no longer need to worry about caching stale content.

Chart showing process flow when caching Workers KV data.
The process flow with caching of Workers KV data included.
Chart showing TTFB responses for various caching scenarios.
Average time to first byte in various caching scenarios.

Get started with a free trial of our Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.

Paulo Paracatu

Since we always read the domain’s Workers KV data, we realized we could route requests and customize the cache key, appending things like the domain’s ID and features that could affect the asset, like Polish. Today, our cache key is heavily customized to quickly reflect every client’s change in our panel or API. By modifying the cache key using Workers KV’s data, nobody needs to worry about clearing the cache anymore. As soon as Workers KV propagates the changes, the cache key also changes, and we request and cache a fresh asset.

However, unlike many applications, we can’t cache Workers KV for extended periods. Kinsta’s customers are constantly trying new features, changing Polish and Auto Minify settings, sometimes excluding pages or extensions from being cached, and they want to see their changes in production as soon as possible.

Of course, you need to check the URL for existing parameters to determine which connector to use — ? or & — and ensure you are using a unique identifier.

const handleKVCache = async (event, myCustomDomain) => {
  // Try to get KV from cache first
  const cache = caches.default;
  let site_data = await cache.match( `https://${myCustomDomain}/some-string-ID-kv-data/` );

  // Valid KV cache match
  if (site_data && site_data.status === 200) {
    // ... modify your cached data if necessary, then return it
    return site_data;
  }

  // Invalid cache (expired, miss, etc), get data from KV namespace
  site_data = await KV_NAMESPACE.get(myCustomDomain.toLowerCase());
  
  // Cache valid KV responses with Cache API
  if (site_data) {
    let kvResponse = new Response(JSON.stringify(site_data), {status: 200});
    kvResponse.headers.set("Cache-Control", "public, s-maxage=30");
    event.waitUntil(cache.put(`https://${myCustomDomain}/some-string-ID-kv-data/`, kvResponse));
  }
  
  return site_data;
};

It’s pretty simple to implement your own Workers KV caching logic. For example:

Get all your applications, databases, and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 260 PoPs worldwide

Workers KV operations are affordable, but the numbers can pile up when you trigger billions of reading operations daily.

That’s when we decided to microcache our Workers KV data — caching dynamic or constantly-changed content for a very short period of time, usually less than 60 seconds.

// Assign KV values to variables
const { customBackend } = kvdata.kinstaConf;

// Override the backend
cf.resolveOverride = customBackend;

Paulo is a seasoned DevOps Engineer at Kinsta with a solid web hosting and optimization background. Equipped with Bash and JavaScript expertise, he uses Cloudflare Workers to continually improve user experiences in hosting.



In early 2023, we doubled down on Cloudflare cache wrangling, making caches more responsive to client-side configuration changes while also shifting the heavy lifting behind broadcasting feature updates away from our admins on the backend and into Cloudflare Workers. A key result was a dramatic increase in the share of customer data successfully cached, increasing 56.3% between October 2022 and March 2023.

Data showing increase in percentage of successful cache hits over time.
Optimization via Cloudflare Workers became a stronger focus in January of 2023.

Cloudflare Workers and Workers KV allow us to programmatically customize every request and response with minimal effort and lower latency. We no longer need to deploy changes to hundreds of thousands of containers when we want to implement new features; we can replicate or implement the feature with Workers and deploy it everywhere with a few commands and clicks, saving us days of work and maintenance.

Request Routing With Workers KV and Workers

Then, you can use this new cache key to save the response with Cache API or Fetch — or both.