vtsfly
vtsfly4mo ago

Using Deno.KV outside deno deploy environment, limit problem

Using Deno.KV with kubernetes(k8s) outside deno deploy environment is giving me rateLimit, too many requests error after only 200-300 requests in 2-3 hours. When l was using deno deploy it never happened. l am using Server Sent Events, together with kv.watch to detect changes and update the other clients. Its a real time application. Here is how l am using kv with SSE:
export const handler: Handlers = {
async GET(_req) {
const kv = await Deno.openKv(`https://api.deno.com/databases/${DATABASE_ID}/connect`);

return new Response(
new ReadableStream({
async start(controller) {
for await (
const [{ value: message }] of kv.watch([["maps", "couve", "teste", "1"]])
) {
controller.enqueue({
data: message,
id: Date.now(),
event: "message",
});
}
},
cancel() {
console.log("cancel");
},
}).pipeThrough(new ServerSentEventStream()),
{
headers: {
"Content-Type": "text/event-stream",
},
},
);
},
};
export const handler: Handlers = {
async GET(_req) {
const kv = await Deno.openKv(`https://api.deno.com/databases/${DATABASE_ID}/connect`);

return new Response(
new ReadableStream({
async start(controller) {
for await (
const [{ value: message }] of kv.watch([["maps", "couve", "teste", "1"]])
) {
controller.enqueue({
data: message,
id: Date.now(),
event: "message",
});
}
},
cancel() {
console.log("cancel");
},
}).pipeThrough(new ServerSentEventStream()),
{
headers: {
"Content-Type": "text/event-stream",
},
},
);
},
};
l checked Deno.kv limits and they are well above my usage. After 1-2 days with the error, kv comes back working again. l dont understand why is this happening if the read and write monthly limits are not being reached.
No description
1 Reply
Deno AI Helper
Deno AI Helper4mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you provided, I can offer some general advice, but please note that this advice is not based on the Deno documentation, and I advise you to double-check the accuracy of the advice given. In your code, you are using kv.watch within an infinite loop, which could potentially lead to a high number of requests to the Deno.KV service. If the kv.watch method is implemented in a way that it polls the server at a high frequency, this could be the reason why you are hitting rate limits. One thing to consider is whether the kv.watch method is designed to be used in a long-polling fashion or if it's expected to be called less frequently. If it's the latter, you might need to implement some form of back-off strategy or limit the frequency of your requests to avoid hitting rate limits. Additionally, ensure that your cancel function in the ReadableStream is properly closing any open connections or stopping the watch operation to prevent unnecessary requests when the client disconnects. Since this is not based on the official documentation, I recommend reaching out to the Deno community or checking the source code of the Deno.KV service if it's available to you, to understand the expected usage patterns and rate limits when not running within the Deno Deploy environment.