Need Help with a worker code to apply rate limiting using CF objects such as IP lists etc. Should be able to work behind the NAT of a Common IP ( Single ) IP as well.
Thanks for reaching out on the Cloudflare Community!
Unfortunately, because Cloudflare Workers aren’t guaranteed to have persistent memory between requests, they aren’t typically reliable enough for use cases which require counting, such as rate limiting.
However, Cloudflare does have a full-featured Rate Limiting product. You can purchase or configure Rate Limiting from the Firewall → Tools section of the Cloudflare dashboard.
You can read more about Cloudflare Rate Limiting here:
Once you configure your Rate Limiting rule, it will be applied to all requests coming in to the URL pattern(s) you define.
However, you can Bypass the Rate Limiting feature by creating a Firewall Rule and using the Bypass action. This allows you to only apply the Rate Limiting to requests matching your Firewall Rule expression, which could include things like IP Lists.
Here’s an example of a Firewall Rule I created to ensure only my IP List is rate limited against:
Since you mentioned NAT users - Cloudflare Rate Limiting does have the option to work around NATs for individual users behind a private network. However, this feature is available for Enterprise customers only - contact us for more info on Enterprise from here.
Finally, if you want to use Workers exclusively, we are developing a feature called Durable Objects.
Once released, Durable Objects will allow for more persistence between Worker instances and might help for cases where you require persistent data between requests.
At time of writing, this feature is currently in a closed beta, but you can read more info and request an invite here: