anders
anders
CDCloudflare Developers
Created by anders on 10/2/2024 in #workers-help
Best way to count own SaaS API limits/usage
Hey, So, currently I'm providing an API through RapidAPI. They deal with billing, users, limits, etc. I'm looking of moving that to Workers instead, and I'll deal with user sign-ups, limits for requests they can make, etc. Ideally, I would like all customer requests to my workers to add a header like "X-Requests-Remaining: 1000", and decrease that every time they GET the worker, with their auth-id. And then have a hard limit, so it responds with HTTP error 403/429 when they've made 1000 requests. There are multiple ways of doing this (KV, D1, Durable Objects, Analytics+KV?), and of course I want to limit the costs, but still provide a good solution to this, and of course make the worker respond as quick as possible. Currently I'm at ~100k requests per day, so 3 million calls per month, but I expect this to increase. Using Durable Objects seems to be the best solution for exact results, but that can quickly increase the pricing too. I'm okay with somewhat close numbers, so one solution I'm looking at is using KV to store "requests_remaining" for each customer, so that is read once per worker, but let's say it randomly writes -100 to requests_remaining, at a 1% chance. Just to limit writing, both to get it cheaper, but also to respond to the call quicker. Statistically they'll get the right amount of calls (could also be like -100 at 0.09% chance or something to make sure they aren't too unlucky). Exact counting is nice, but not necessary. I'm also considering using analytics (or queues but seems more expensive?) to do the same, read the requests_remaining from KV, to make sure they aren't overusing it, log a write to analytics, and then every X minutes, go through analytics, see how many requests they made, and remove that from requests_remaining. Then the usage will be correct, and they can only overuse it for X minutes until the KV requests_remaining is 0. Customers are globally distributed, so I would prefer to have something close to the workers. I've only been playing with workers for a short while, so I'm at a loss what would be the "best" solution, and what would be "okay", but more cost effective. Thanks<3
13 replies