Trouble Implementing Rate Limiting in FastAPI with Redis - 429 Too Many Requests scenarios
I've looked through the documentation and I'm still confused about I'm sure I'm missing something obvious here, but I'm trying to implement rate limiting in my FastAPI application using Redis, but I keep running into a `429 Too Many Requests` behavior, even when I haven't hit the defined limits. I'm utilizing the `redis-py` library for Redis interactions and `fastapi-limiter` for rate limiting. Here's a simplified version of my implementation: ```python from fastapi import FastAPI, Depends, HTTPException from fastapi_limiter import FastAPILimiter from fastapi_limiter.depends import RateLimiter import aioredis app = FastAPI() async def init_redis(): redis = await aioredis.from_url("redis://localhost:6379") await FastAPILimiter.init(redis) @app.on_event("startup") async def startup(): await init_redis() @app.get("/items/", dependencies=[Depends(RateLimiter(times=5, seconds=60))]) async def read_items(): return {"message": "You can access this endpoint!"} ``` I have Redis running on my local machine, and I confirmed that it's accessible. However, when I make requests to the `/items/` endpoint, I often receive a `429 Too Many Requests` response after just a few attempts, even if I wait longer than 60 seconds. I've checked if multiple instances of the FastAPI app are running, but it's just one instance. Additionally, I've tried adjusting the `times` parameter in the rate limiter, but the scenario continues. When debugging, I noticed that the Redis keys for rate limiting do get created, but they seem to expire unexpectedly. Has anyone encountered something similar or can point out what I'm doing wrong? I want to ensure that the rate limiting works correctly without blocking legitimate requests. I'm using FastAPI version 0.68.0 and redis-py version 4.1.0. I'm working on a mobile app that needs to handle this. Hoping someone can shed some light on this.