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:
- The filtering by
- Key Retrieval: You are billed for each key returned in the
- You are billed for:
- 1 list operation (base cost for calling the
- 50 read operations (one for each key returned in the result).
This means you are billed based on the keys matching your prefix and retrieved by the
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
- TheKV.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 tolist() 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
- AssumeNAMESPACE.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 theprefix. 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.