Skip to content

whoismehfooz/Scalable-API-With-Redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Scalable API with Redis

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. ⚑


✨ Features

βœ… 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


πŸ› οΈ Tech Stack

Technology Purpose
FastAPI API Framework
SQLAlchemy ORM
SQLite Database
Redis (Valkey) Caching & Rate Limiting
Pydantic Validation

πŸ“‚ Project Structure

src/
β”œβ”€β”€ cache/
β”‚   β”œβ”€β”€ redis_client.py
β”‚
β”œβ”€β”€ middleware/
β”‚   β”œβ”€β”€ rate_limit.py
β”‚
β”œβ”€β”€ products/
β”‚   β”œβ”€β”€ controllers.py
β”‚   β”œβ”€β”€ models.py
β”‚   β”œβ”€β”€ routers.py
β”‚   └── schemas.py
β”‚
└── utils/
    β”œβ”€β”€ db.py
    └── settings.py

⚑ Redis Caching Flow

First Request

Client
   ↓
Redis ❌ (Cache Miss)
   ↓
Database
   ↓
Redis Store
   ↓
Response

Second Request

Client
   ↓
Redis βœ… (Cache Hit)
   ↓
Response

No database query needed. 🎯


πŸ”₯ Cache Invalidation

Whenever a new product is created:

POST /products
      ↓
Database Updated
      ↓
Redis Cache Deleted
      ↓
Fresh Data Generated

This ensures users never receive stale data.


πŸ›‘οΈ Rate Limiting

The API tracks requests using Redis counters.

Example

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."
}

🌐 Available Endpoints

Create Product

POST /products

Example:

{
  "name": "Keyboard",
  "price": 1200
}

Get All Products

GET /products

Get Product By ID

GET /products/{product_id}

βš™οΈ Installation

Clone Repository

git clone <repository-url>
cd Scalable-API-With-Redis

Create Virtual Environment

python -m venv venv

Activate Environment

source venv/bin/activate

Install Dependencies

pip install -r requirements.txt

Start Redis / Valkey

sudo systemctl start valkey

Run Application

fastapi dev main.py

🧠 Key Concepts Learned

  • 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

πŸš€ Future Improvements

  • Product Update Endpoint
  • Product Delete Endpoint
  • Cache Invalidation for Update/Delete Operations
  • Authentication & Authorization
  • PostgreSQL Integration
  • Docker Support
  • Background Tasks
  • Distributed Rate Limiting

🎯 Project Goal

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.


⭐ If you found this project useful, consider giving it a star.

About

πŸ‘‰ FastAPI project demonstrating Redis caching, TTL-based cache invalidation, and rate limiting using custom middleware.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages