This project demonstrates how to integrate Redis with FastAPI to build a scalable and efficient caching system. It was created as a learning exercise to better understand Redis and its integration with FastAPI. It includes examples of caching user and item data, as well as clearing cache entries.
- FastAPI Redis Caching System
- FastAPI: A modern, high-performance web framework for building APIs with Python.
- Redis: An in-memory data structure store used as a caching layer.
- Caching: Built-in caching decorator to cache API responses.
- Docker: Containerized setup for easy deployment and development.
- GET & DELETE Operations: Examples of caching and clearing cache for user and item data.
- Docker and Docker Compose installed on your machine.
- Basic knowledge of FastAPI and Redis.
git clone https://github.com/Khailas12/Fastapi-Redis-Caching-System.git
cd Fastapi-Redis-Caching-System
Run the following command to start the FastAPI application and Redis using Docker Compose:
docker-compose up --build
This will:
- Build the Docker image for the FastAPI application.
- Start the Redis container.
- Start the FastAPI application on
http://localhost:8000
.
Once the containers are running, you can check the health of the application by visiting:
http://localhost:8000/health
You should see the response:
{"status": "healthy"}
This section tells you that after running docker-compose up --build
, you can verify that the FastAPI application is running correctly by visiting the /health
endpoint. If the application is healthy, it will return a JSON response with {"status": "healthy"}
.
The docker-compose.yml
file includes health checks for both the FastAPI application (web
service) and the Redis service. These health checks ensure that the services are running and responsive.
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:8000/health || exit 1"]
interval: 30s
timeout: 10s
retries: 3
test
: This command usescurl
to call the/health
endpoint of the FastAPI application. If the endpoint returns a non-2xx status code, the command will fail (exit 1
), indicating that the service is unhealthy.interval
: The health check runs every 30 seconds.timeout
: The command must complete within 10 seconds.retries
: If the health check fails, it will retry up to 3 times before marking the service as unhealthy.
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 30s
timeout: 10s
retries: 3
test
: This command usesredis-cli ping
to check if the Redis server is responsive. If Redis is running, it will respond withPONG
.interval
: The health check runs every 30 seconds.timeout
: The command must complete within 10 seconds.retries
: If the health check fails, it will retry up to 3 times before marking the service as unhealthy.
- The
/health
endpoint in the FastAPI application is used to verify that the service is running correctly. - The
docker-compose.yml
file includes health checks for both the FastAPI application and Redis to ensure they are operational. - The
README.md
file guides users on how to manually verify the health of the application by visiting the/health
endpoint.
These health checks are crucial for maintaining the reliability of your application, especially in a containerized environment where services need to be monitored for uptime and responsiveness.
The project uses Redis to cache API responses. The cache_response
decorator is used to cache the results of specific endpoints.
- Endpoint:
GET /redis-v2/users/{user_id}
- Cache Key:
users:user:{user_id}
- TTL: 120 seconds
When you call this endpoint, the response is cached in Redis. Subsequent calls within the TTL will return the cached data.
- Endpoint:
GET /redis-v1/items/{item_id}
- Cache Key:
item_{item_id}
- TTL: 3600 seconds
You can clear the cache for a specific user or item using the delete endpoints.
- Endpoint:
DELETE /redis-v2/users/{user_id}
- Cache Key:
users:user:{user_id}
- Endpoint:
DELETE /redis-v1/delete/{item_id}
- Cache Key:
item_{item_id}
The Redis connection pool is configured in app/config/redis.py
. This ensures that the Redis client is reused across the application, improving performance.
- GET /redis-v1/items/{item_id}: Retrieve item data (cached).
- DELETE /redis-v1/delete/{item_id}: Clear item cache.
- GET /redis-v2/users/{user_id}: Retrieve user data (cached).
- DELETE /redis-v2/users/{user_id}: Clear user cache.
Fastapi-Redis-Caching-System/
- app/
- main.py
- services/
- redis_basic/
- v1/
- router.py
- service.py
- schemas.py
- v2/
- router.py
- cache_response.py
- config/
- redis.py
- docker-compose.yml
- Dockerfile
- requirements.txt
@router.get("/users/{user_id}")
@cache_response(ttl=120, namespace="users")
async def get_user_details(user_id: int):
user = users_db.get(user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
return user
@router.delete("/users/{user_id}")
async def delete_user(user_id: int):
if user_id not in users_db:
raise HTTPException(status_code=404, detail="User not found")
cache_key = f"users:user:{user_id}"
await redis_client.delete(cache_key)
return {"message": "User cache removed successfully"}
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
- Features: Highlights the main features of the project.
- Installation: Step-by-step instructions to set up the project.
- How It Works: Explains the caching mechanism and how to use the API.
- API Endpoints: Lists the available endpoints and their functionality.
- Project Structure: Provides an overview of the project’s directory structure.
- Code Examples: Includes snippets of key functionality.
- Contributing: Encourages contributions from the community.
- License: Specifies the license for the project.
This README.md
is designed to be clear and comprehensive, making it easy for developers to understand and use your project. Let me know if you’d like to add or modify anything! 😊