When to Use Cloudflare Workers KV for Rate Limiting: A Practical Decision Guide
Use Workers KV for rate limiting when eventual consistency is acceptable and counters fit within 25 MiB. This guide walks through a concrete KV‑based limiter, trade‑offs, and a checklist to decide if KV meets your traffic and consistency needs.
21 Apr 2026, 04:16 UTC

Problem: You Need a Fast, Global Rate Limiter
Imagine you run a public API that must protect itself from abuse. You want to count requests per client IP and block if the count exceeds a threshold. You need the counter to be available at every edge location, so latency is minimal, and you want the logic to scale automatically without a dedicated database.
Cloudflare Workers KV offers a globally distributed key‑value store that can serve this purpose. But the key question is: When is KV the right tool for rate limiting, and when should you look elsewhere?
Takeaway
Use Workers KV for rate limiting when you can tolerate eventual consistency, your counters fit within the 25 MiB per‑key limit, and you can design the logic around TTLs to avoid stale reads. If you need strict, immediate consistency or counters that grow beyond the per‑key quota, consider a dedicated database or a third‑party rate‑limiting service.
Understanding KV’s Strengths and Limits
- Global, edge‑proxied storage: Reads are served from the nearest data center.
- Writes are eventually consistent; a read right after a write may return stale data.
- Per‑key size limit: 25 MiB; free tier total storage: 10 MiB per namespace.
- TTL support: keys can expire automatically, which is handy for rate‑limit windows.
- Request quota: free tier allows ~10 000 KV reads/writes per day; higher plans raise the limit.
When Eventual Consistency is Acceptable
Rate limiting is typically tolerant of a one‑to‑two‑second window of stale counts. If a user’s counter is off by one or two, the impact is negligible. The key is to design the logic so that a stale read does not cause a false positive block.
When It’s Not
If you’re implementing a financial transaction counter or a critical audit trail, eventual consistency is a problem. In those cases, a relational database or a dedicated rate‑limiting service that guarantees atomic increments is preferable.
Concrete Example: IP‑Based Rate Limiting with KV
Below is a minimal Worker that limits each IP to 100 requests per minute. The logic uses a TTL of 60 seconds on the counter key.
// wrangler.toml
name = "rate-limiter"
type = "javascript"
account_id = <YOUR_ACCOUNT_ID>
workers_dev = true
[vars]
RATE_LIMIT = 100
WINDOW_SECONDS = 60
[env.production]
# Bind the KV namespace named "RATE_LIMITS"
kv_namespaces = [
{ binding = "RATE_LIMITS", id = <NAMESPACE_ID> }
]
// index.js
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const ip = request.headers.get("CF-Connecting-IP")
const key = `ip:${ip}`
const limit = Number(RATE_LIMIT)
const window = Number(WINDOW_SECONDS)
// Atomically increment the counter using a transaction
const current = await RATE_LIMITS.get(key)
const count = current ? parseInt(current, 10) : 0
const newCount = count + 1
// Write back with TTL
await RATE_LIMITS.put(key, String(newCount), { expirationTtl: window })
if (newCount > limit) {
return new Response("Too many requests", { status: 429 })
}
return new Response("OK")
}
Key points:
- We bind the KV namespace in
wrangler.tomland redeploy. - The
expirationTtloption sets the key to expire afterWINDOW_SECONDS, automatically resetting the counter. - Because writes are eventually consistent, a user might see a count that is one or two behind the real count. This is acceptable for rate limiting.
- The counter never exceeds the 25 MiB per‑key limit because it stores a small integer.
Trade‑Offs and Practical Checks
Cost vs. Performance
KV reads and writes are inexpensive, but the free tier caps you at 10 000 operations per day. If your API sees millions of requests, you’ll need a paid plan or a different storage backend.
Cold Start Overhead
Each Worker cold start adds a few milliseconds of latency. If you hit the rate limit on every request, you may experience a measurable delay. Consider caching the counter in memory for the first few requests in a session.
Verifying Consistency
To confirm that KV behaves as expected:
- Run
wrangler kv:key put testKey testValue --binding RATE_LIMITSfrom the CLI. - Immediately run
wrangler kv:key get testKey --binding RATE_LIMITSand confirm the value. - Check the Cloudflare dashboard under Workers > KV to see the key’s TTL and value.
- Monitor the
KVErrorlogs in the Worker console; a 429 status code indicates the rate limit was hit.
When to Look Elsewhere
- If you need strict atomicity (e.g., financial transactions), KV’s eventual consistency is a blocker.
- When the counter’s size might exceed 25 MiB (e.g., storing a full request log), KV is unsuitable.
- If your traffic volume exceeds the quota of your plan, consider a dedicated rate‑limiting service or a managed database with higher write throughput.
Actionable Checklist
- Define the rate‑limit window and threshold.
- Confirm the counter fits within 25 MiB and that your plan’s quota covers your traffic.
- Bind the KV namespace in
wrangler.tomland redeploy. - Test the logic locally with
wrangler devand verify TTL expiration. - Deploy to production and monitor KV usage in the dashboard.
- If you hit quota limits or notice stale reads causing false blocks, consider a different backend.
By following this decision path, you can confidently decide whether Cloudflare Workers KV is the right fit for your rate‑limiting needs.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.