{"id":31793539,"url":"https://github.com/eiflow3/fastapi-redis-docker-rate-limiting","last_synced_at":"2025-10-10T18:19:40.097Z","repository":{"id":316892729,"uuid":"1065202629","full_name":"eiflow3/fastapi-redis-docker-rate-limiting","owner":"eiflow3","description":"API endpoints rate limiting using redis and fastapi","archived":false,"fork":false,"pushed_at":"2025-09-27T11:18:27.000Z","size":890,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-27T12:24:56.381Z","etag":null,"topics":["docker","fastapi","redis"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eiflow3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-27T08:53:39.000Z","updated_at":"2025-09-27T11:18:30.000Z","dependencies_parsed_at":"2025-09-27T12:24:58.032Z","dependency_job_id":"4a4fcaf6-3dbf-4623-9fb8-b9061fcadbc1","html_url":"https://github.com/eiflow3/fastapi-redis-docker-rate-limiting","commit_stats":null,"previous_names":["eiflow3/fastapi-redis-docker-rate-limiting"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/eiflow3/fastapi-redis-docker-rate-limiting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eiflow3%2Ffastapi-redis-docker-rate-limiting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eiflow3%2Ffastapi-redis-docker-rate-limiting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eiflow3%2Ffastapi-redis-docker-rate-limiting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eiflow3%2Ffastapi-redis-docker-rate-limiting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eiflow3","download_url":"https://codeload.github.com/eiflow3/fastapi-redis-docker-rate-limiting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eiflow3%2Ffastapi-redis-docker-rate-limiting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004913,"owners_count":26083802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","fastapi","redis"],"created_at":"2025-10-10T18:19:36.646Z","updated_at":"2025-10-10T18:19:40.076Z","avatar_url":"https://github.com/eiflow3.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI Redis Docker Rate Limiting\n\nA robust FastAPI application implementing configurable rate limiting using Redis, containerized with Docker for easy deployment and scalability.\n\n## 🚀 Features\n\n- **FastAPI Framework**: High-performance, modern Python web framework\n- **Redis-based Rate Limiting**: Distributed rate limiting with Redis backend\n- **Configurable Rate Limits**: Environment-based configuration for requests and time windows\n- **Docker Containerization**: Easy deployment with Docker and Docker Compose\n- **Middleware Implementation**: Custom rate limiting middleware for per-store, per-endpoint limits\n- **Agnostic Path Extraction**: Intelligently extracts feature/endpoint paths, ignoring service versions and prefixes\n- **Health Check Endpoint**: Built-in health monitoring with rate limit bypass option\n- **Cleanup Endpoint**: Administrative endpoint for Redis data management\n- **Comprehensive Logging**: Built-in logging for monitoring and debugging\n- **Error Handling**: Fail-open strategy for Redis unavailability\n\n## 📋 Rate Limiting Specifications\n\nThe application implements configurable rate limiting based on:\n\n- **Store ID**: Identified via `store_id` query parameter\n- **Endpoint Path**: Rate limits are applied per feature/endpoint (ignoring service version, name, and access prefixes)\n- **Configurable Limits**: Set via environment variables `RATE_LIMIT` and `WINDOW`\n- **Redis Key Format**: `{store_id}_{feature/endpoint}`\n\n### Default Configuration\n\n- **Max Requests**: 5 per window (configurable via `RATE_LIMIT`)\n- **Time Window**: 1 minute (configurable via `WINDOW` in minutes)\n- **Bypass Paths**: `/health` (when strict mode is disabled)\n\nWhen rate limit is exceeded, returns HTTP 429 with appropriate headers:\n\n- `X-Rate-Limit-Remaining`: Remaining requests in current window\n- `X-Rate-Limit-Reset`: Timestamp when the limit resets\n\n## 🛠️ Installation \u0026 Setup\n\n### Prerequisites\n\n- Docker and Docker Compose\n- Git\n\n### Clone the Repository\n\n```bash\ngit clone https://github.com/eiflow3/fastapi-redis-docker-rate-limiting.git\ncd fastapi-redis-docker-rate-limiting\n```\n\n### Environment Variables\n\nConfigure the following environment variables (optional, defaults provided):\n\n- `RATE_LIMIT`: Maximum requests per window (default: 5)\n- `WINDOW`: Time window in minutes (default: 1)\n- `REDIS_HOST`: Redis host (default: redis)\n- `REDIS_PORT`: Redis port (default: 6379)\n- `REDIS_DB`: Redis database number (default: 0)\n\n### Run with Docker Compose\n\n```bash\n# Build and start the services\ndocker-compose up --build\n\n# Run in background\ndocker-compose up -d --build\n```\n\nThe API will be available at `http://localhost:8000`\n\n## 📖 API Endpoints\n\n### GET /v1/service/feature/one\n\nFeature endpoint for service v1.\n\n**Parameters:**\n\n- `store_id` (query, required): Store identifier for rate limiting\n\n**Response:**\n\n```json\n{\n  \"feature\": \"feature-1\",\n  \"store_id\": \"123456\"\n}\n```\n\n### GET /v2/service/feature/two\n\nFeature endpoint for service v2.\n\n**Parameters:**\n\n- `store_id` (query, required): Store identifier for rate limiting\n\n**Response:**\n\n```json\n{\n  \"feature\": \"feature-2\",\n  \"store_id\": \"123456\"\n}\n```\n\n### GET /health\n\nHealth check endpoint. Bypasses rate limiting by default.\n\n**Parameters:**\n\n- `store_id` (query, required): Store identifier\n\n**Response:**\n\n```json\n{\n  \"status\": \"ok\",\n  \"store_id\": \"123456\"\n}\n```\n\n### DELETE /cleanup\n\nAdministrative endpoint to flush Redis database.\n\n**Response:**\n\n```json\n{\n  \"message\": \"Redis database flushed successfully\"\n}\n```\n\n## 🔧 Development\n\n### Local Development (without Docker)\n\n1. Install dependencies:\n\n```bash\npip install -r requirements.txt\n```\n\n2. Start Redis server (locally or via Docker):\n\n```bash\ndocker run -d -p 6379:6379 redis:7\n```\n\n3. Run the application:\n\n```bash\nuvicorn main:app --reload\n```\n\n### Testing Rate Limiting\n\nTest the rate limiting by making multiple requests to an endpoint:\n\n```bash\n# This should work (first few requests)\ncurl \"http://localhost:8000/v1/service/feature/one?store_id=123456\"\n\n# After exceeding the limit, you'll get 429:\n# HTTP 429 Too Many Requests\n# Headers: X-Rate-Limit-Remaining: 0, X-Rate-Limit-Reset: \u003ctimestamp\u003e\n```\n\n## 🏗️ Architecture\n\n- **main.py**: FastAPI application with Redis connection management and route definitions\n- **middlewares/rate_limit.py**: Custom middleware implementing rate limiting logic with configurable parameters\n- **Dockerfile**: Container configuration for the API service\n- **docker-compose.yml**: Multi-service setup with API and Redis\n- **enhancement.md**: Detailed enhancement plan and assessment\n- **rate-limit-specs.md**: Comprehensive rate limiting specifications\n- **middleware_docs.md**: Documentation explaining middleware implementation\n\n## 📁 Project Structure\n\n```\n.\n├── main.py                    # FastAPI application\n├── middlewares/\n│   └── rate_limit.py          # Rate limiting middleware\n├── requirements.txt           # Python dependencies\n├── Dockerfile                # Docker image configuration\n├── docker-compose.yml        # Docker services orchestration\n├── rate-limit-specs.md       # Rate limiting specifications\n├── middleware_docs.md        # Middleware documentation\n├── guidelines.md             # Project guidelines\n├── .github/\n│   └── instructions/\n│       └── code-creation.md.instructions.md  # Code creation guidelines\n└── README.md                # This file\n```\n\n## 🤝 Contributing\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## 📄 License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## 📞 Support\n\nIf you have any questions or issues, please open an issue on GitHub.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feiflow3%2Ffastapi-redis-docker-rate-limiting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feiflow3%2Ffastapi-redis-docker-rate-limiting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feiflow3%2Ffastapi-redis-docker-rate-limiting/lists"}