vutran1710 / PyrateLimiter

⚔️Python Rate-Limiter using Leaky-Bucket Algorithm Family
https://pyratelimiter.readthedocs.io
MIT License
349 stars 36 forks source link

bucket_name vs identity in Redis bucket #63

Closed JWCook closed 2 years ago

JWCook commented 2 years ago

Here's a question about initializing Redis buckets:

https://github.com/vutran1710/PyrateLimiter/blob/e3f2fd94abf96bd2977c03445e742267f1de19c7/pyrate_limiter/bucket.py#L125-L140

It requires a bucket_name, which is used in addition to identity as the key of the Redis list used by that bucket. But isn't identity already unique for each bucket? Limiter.bucket_group has exactly one bucket instance per identity. Could bucket_name be removed, or does that have some other purpose that I'm missing?

vutran1710 commented 2 years ago

The identity of the item is something that is related to the system's existing logic (eg: user_id). The bucket_name is something that we use to categorize our data in redis, avoiding duplication with other keys and making it easier to filter/search.

I mean, thinking about the case where multiple backends / microservices are dealing with user-ids at the same time and have different limiting rates, isn't it easier to separate this service A's bucket from service B's bucket etc. We can leave such responsibility to the developer - but I think enforcing this convention will save our friends from certain problems in future.

JWCook commented 2 years ago

Ah, that makes sense. I didn't think of the case of multiple services using the same IDs. Thanks for explaining.