A FastAPI project that demonstrates how Redis can be used to build faster and more scalable APIs through Caching, TTL, and Rate Limiting.
Instead of hitting the database on every request, the API intelligently stores frequently accessed data inside Redis, reducing database load and improving response times. β‘
β Product Management API
β Redis Integration
β Response Caching
β Cache Hit / Cache Miss Logic
β Automatic Cache Expiration (TTL)
β Cache Invalidation
β Custom FastAPI Middleware
β Redis-Based Rate Limiting
β SQLite Database
β SQLAlchemy ORM
| Technology | Purpose |
|---|---|
| FastAPI | API Framework |
| SQLAlchemy | ORM |
| SQLite | Database |
| Redis (Valkey) | Caching & Rate Limiting |
| Pydantic | Validation |
src/
βββ cache/
β βββ redis_client.py
β
βββ middleware/
β βββ rate_limit.py
β
βββ products/
β βββ controllers.py
β βββ models.py
β βββ routers.py
β βββ schemas.py
β
βββ utils/
βββ db.py
βββ settings.py
Client
β
Redis β (Cache Miss)
β
Database
β
Redis Store
β
Response
Client
β
Redis β
(Cache Hit)
β
Response
No database query needed. π―
Whenever a new product is created:
POST /products
β
Database Updated
β
Redis Cache Deleted
β
Fresh Data Generated
This ensures users never receive stale data.
The API tracks requests using Redis counters.
Request #1 β
Request #2 β
Request #3 β
Request #4 β
Request #5 β
Request #6 β 429 Too Many Requests
If the limit is exceeded:
{
"message": "Too many requests. Please try again later."
}POST /productsExample:
{
"name": "Keyboard",
"price": 1200
}GET /productsGET /products/{product_id}git clone <repository-url>
cd Scalable-API-With-Redispython -m venv venvsource venv/bin/activatepip install -r requirements.txtsudo systemctl start valkeyfastapi dev main.py- Redis Fundamentals
- Key-Value Storage
- TTL (Time To Live)
- Response Caching
- Cache Hit & Cache Miss
- Cache Invalidation
- Middleware Architecture
- Request Interception
- Redis Counters
- Rate Limiting
- FastAPI Project Structuring
- Product Update Endpoint
- Product Delete Endpoint
- Cache Invalidation for Update/Delete Operations
- Authentication & Authorization
- PostgreSQL Integration
- Docker Support
- Background Tasks
- Distributed Rate Limiting
This project was built to understand how modern backend systems use Redis to:
β‘ Improve performance
π‘οΈ Prevent abuse
π Scale efficiently
while following clean FastAPI project architecture.