How to improve KV read performance

I’ve implemented KV in a worker on a pay-as-you-go plan. My worker will first check cache, then KV, and if its in neither it will write to both. When data gets pulled from KV I am seeing response times of 250ms. This isn’t really any better than my server can do pulling from the DB. Is this expected or am I doing it wrong?

Get KV code:

  /**
   * Retrieve response from KV
   * 
   * @link https://developers.cloudflare.com/workers/runtime-apis/kv
   * @param {string} url 
   * @returns Response|boolean
   */
  async function getKv(url) {
    const cacheKey = await sha256(url);
    const {value, metadata} = await CARAPI_KV.getWithMetadata(cacheKey);
    if (value) {
      metadata.headers['carapi-kv'] = 'true';
      response = new Response(value, {
        status: 200,
        headers: metadata.headers
      });
      return response;
    }

    return false;
  }

Where getKv is called

    let response = await getCache(url, event);
    if (!response) {
      response = await getKv(url);
      if (!response) {
        response = await fetch(request);
        if (response.status >= 300) { 
          return response;
        }
        await putKv(url, event, response);
      }
      await putCache(url, response, event);
    }

    return response;
1 Like

That’s a lot of await’d operations and the put ones don’t sound particularly relevant to the user, but they’re blocking the response.

Use waitUntil to do your writes without blocking a response.

2 Likes

You mean like this?

    let response = await getCache(url, event);
    if (!response) {
      response = await getKv(url);
      if (!response) {
        response = await fetch(request);
        if (response.status >= 300) { 
          return response;
        }
        event.waitUntil(putKv(url, event, response));
      }
      event.waitUntil(putCache(url, response, event));
    }
1 Like

And I guess another question I should ask is what kind of performance should I be expecting?

1 Like