Cloudflare Workers as an API endpoint

Hi all!

I apologize in advance if my question is something simple but I’m new to this CF Worker concept and also very green to Javascript.

I’m building an API to query data that doesn’t change very often, for this I’m using a CF Worker for the API endpoint and Cloudflare KV to store the data being queried – all of this is already implemented and is working perfectly, however, what I require to do next is to count requests by an API key to control API usage and this is where I’m unsure on how to proceed, do I use KV’s again to count requests per API key? do I make a POST request to a separate endpoint to log the request data (and do the count there)? My primary concern is that I don’t want the request to be “blocked” by doing this logging, anyone have any thoughts on this? Not sure if I’m over complicating this.

Thank you!

  • Ever Daniel

The issue with using KV as a counter is that it’s eventually consistent and can only be written to once a second. If you wanted to go ahead and log with an external endpoint, you’d do your logging requests within a waitUntil that doesn’t block your response to the client.

It’ll keep the request alive until the promise passed to waitUntil has resolved.

As @KianNH mentioned above KV is eventually consistent. What you can do is to use strongly consistent features like durable objects or MQTT which is described in this article in a very similar scenario.

Hi guys!

thank you very much for the quick response – so it sounds like I either need to use waitUntil to log requests to an external endpoint or use durable objects, some additional comments on this if you guys don’t mind:

  1. lets say I log requests to an external endpoint, I will then have this external endpoint count requests and then update the api key (which is stored in KV) with additional metadata (say every 5 minutes?) – does this sound OK? my goal here is to check if the API key is within their allowed usage – I don’t mind if they go above their allowed limit for some time as long as I eventually let them know they’re over their allowed usage (X number of requests per hour)

  2. if I use durable objects, do I still get the speed I get with KV? my concern here is if using durable objects would make the Worker response slower?

Thanks again!