Cloudflare DevelopersCD
Cloudflare Developers13mo ago
12 replies
iLearn

How Cloudflare KV Billing Works for .list()

Following is my Question and Answer(given by ChatGPT)
Please confirm / update the Answer given by ChatGPT. Thanks.

QUESTION :
In the following Cloudflare Worker Code to List by Prefix .. how will the billing happen ? .. billing only for the Keys which are read and correspond to prefix or billing for complete scan of the KV namespace ?


export default {
async fetch(request, env, ctx) {
const value = await env.NAMESPACE.list({ prefix: "user:1:" });
return new Response(value.keys);
},
};

ANSWER by CHATGPT :
In Cloudflare Workers, billing for KV operations depends on the specific API calls and the data retrieved. Here's how the billing works for your provided code:

1. KV List API Billing

- The KV.list() operation is billed based on the number of keys returned that match the prefix filter, not the total number of keys in the namespace.
- The filtering by prefix is handled internally by Cloudflare, so the operation scans only the relevant part of the namespace that matches the specified prefix.

2. Breakdown of Costs

- List Operation: Each call to list() counts as a single read operation, regardless of the number of keys returned.
- Key Retrieval: You are billed for each key returned in the value.keys array. If the list() operation retrieves 10 keys, it will count as 10 read operations.

3. Example Billing Scenario

- Assume NAMESPACE.list({ prefix: "user:1:" }) returns 50 keys.
- You are billed for:
- 1 list operation (base cost for calling the list() method).
- 50 read operations (one for each key returned in the result).

4. What Is NOT Charged

- You are not billed for scanning or evaluating keys that do not match the prefix. The filtering happens on Cloudflare's side, and only matching keys are returned and billed.

This means you are billed based on the keys matching your prefix and retrieved by the list() operation, not for scanning the entire namespace.
Was this page helpful?