Latency Cloudflare Workers + Workers KV
Hello guys. Just doing a few tests and the latency is around 110-200ms after a cold start. I'm pinging the host with 3ms roundtrip. The code in index.ts is
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const { pathname } = new URL(request.url);
let value = await env.test.get("test");
if (pathname === "/api/kv") {
if (value === null) {
await env.test.put("test", "1");
}
else {
await env.test.put("test", (parseInt(value) + 1).toString()).then(() => {
value = value === null ? "0" : (parseInt(value) + 1).toString();
}).catch((r) => {
return new Response('fail to update');
});
}
return new Response(value);
}
return new Response('');
},
Is this latency in the normal range?
1 Reply
KV does not perform like an in-memory datastore, such as Redis. Accessing KV values, even when locally cached, has significantly more latency than reading a value from memory within a Worker script.