Secondly, we're porting our most popular and arguably most useful SDK to Python - the Upstash Rate Limiting SDK. This SDK has become an essential component of applications requiring application-level rate limiting. Rate limiting is becoming an increasingly significant requirement, particularly for applications that consume AI APIs. These APIs are expensive in terms of both cost and resources. Thus, limiting users on an API key or IP basis is critical.
Here's what you can do with the rate-limiting SDK:
- Limit user activity by the number of requests within a certain time period.
- Set an identifier to limit a user in a unique way (e.g., user-id, API key, IP address).
- Choose from different strategies, including fixed window, sliding window, and token bucket.
- Block (or wait) the request until it is allowed to process.
- Set different limits for different plans.
- Monitor usage with an analytics dashboard
Before using the SDKs, make sure you set the following environment variables:
You can find those variables in Upstash console.
from upstash_redis import Redis redis = Redis.from_env() redis.set("foo", "bar") print("Set value:", redis.get("foo"))
Or, you can use upstash-redis in async context as well!
from upstash_redis.asyncio import Redis from asyncio import run async def main(): redis = Redis.from_env() await redis.set("foo", "bar") print("Set value in async context:", await redis.get("foo")) run(main())
You can have a look at GitHub to see different client options.
from upstash_ratelimit import Ratelimit, FixedWindow from upstash_redis import Redis from time import sleep ratelimit = Ratelimit( redis=Redis.from_env(), limiter=FixedWindow(max_requests=2, window=10), prefix="upstash-ratelimit", ) identifier = "api" response = ratelimit.limit(identifier) if not response.allowed: print("Unable to process at this time") else: sleep(1) # do_expensive_calculation() print("Here you go!")
Or,you can use it in async context as well!
from upstash_ratelimit.asyncio import Ratelimit, FixedWindow from upstash_redis.asyncio import Redis from asyncio import run, sleep ratelimit = Ratelimit( redis=Redis.from_env(), limiter=FixedWindow(max_requests=2, window=10), prefix="upstash-ratelimit", ) async def main(): identifier = "api" response = await ratelimit.limit(identifier) if not response.allowed: print("Unable to process at this time") else: await sleep(1) # do_expensive_calculation() print("Here you go!") run(main())
upstash-ratelimit provides 3 different algorithms to use for rate-limiting, each having its pros and cons. You can read more about the differences in the README.
We also provide two methods for enforcing limits:
limit(), which returns an object containing
allowedfield and some additional metadata about an
block_until_ready(), which allows waiting for a request to pass in a certain
You can also get some metadata about an identifier's state:
get_remaining(): Returns the number of requests left for the given identifier.
get_reset(): Returns the UNIX timestamp in seconds when the remaining requests will be reset or replenished.
If you encounter any issues or have any suggestions, you can also file an issue on GitHub