MoaazMuhmmad/My-Redis_API
Redis API Django Project
This is a Django REST framework project integrated with Redis and Celery, deployed on Hugging Face Spaces using Docker.
๐ Session-Based Cart API
A high-performance shopping cart REST API built with Django REST Framework and Redis as a fast in-memory session store. The project demonstrates real-world use of Redis as a caching/storage layer instead of a traditional database โ achieving near-instant cart operations.
๐๏ธ Architecture Overview
Client (Postman / Browser)
โ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ Django DRF โ โ REST API + Session Middleware
โ (Port 8000) โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โโโโโโดโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ Redis โ โ PostgreSQL โ
โ (Cart โ โ (Products & โ
โ Data) โ โ Categories) โ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโดโโโโโโโโโโโ
โ RedisInsight โ โ Visual Redis UI (Port 5540)
โโโโโโโโโโโโโโโโโKey Design Decision: Cart data lives in Redis (not PostgreSQL) using session-keyed hashes โ this gives O(1) read/write speed and automatic expiry when the session ends.
โจ Features
- Session-based cart โ no authentication required, cart is tied to the browser session
- Redis HSET storage โ each cart stored as
cart:{session_id}hash in Redis - Full product & category inventory API backed by PostgreSQL
- Swagger UI auto-generated via
drf-spectacular - Dockerized โ one command to run everything (Django + Redis + PostgreSQL + RedisInsight)
- Admin panel auto-seeded with superuser on first run
๐งฑ Tech Stack
๐ Project Structure
Session_Based_Cart/
โโโ app/
โ โโโ cart/ # Cart app (Redis-powered)
โ โ โโโ redis_cart.py # Redis operations (add, get)
โ โ โโโ serializers.py # Request/Response validation
โ โ โโโ views.py # API endpoints
โ โ โโโ urls.py
โ โโโ inventory/ # Products & Categories app
โ โ โโโ models.py # Category (tree) + Product models
โ โ โโโ serializers.py
โ โ โโโ views.py
โ โ โโโ urls.py
โ โโโ core/
โ โ โโโ settings.py # Redis + DB config
โ โ โโโ urls.py # Main URL routing + Swagger
โ โโโ manage.py
โโโ docker-compose.yml
โโโ Dockerfile
โโโ requirements.txt
โโโ init.sql # DB seed data๐ Quick Start (Live Demo)
Prerequisites
- Docker Desktop installed and running
1. Clone the repository
git clone https://github.com/iiMoaaz/Session_Based_Cart.git
cd Session_Based_Cart2. Start all services
docker compose up --buildThis single command will:
- Build the Django image
- Start PostgreSQL and run migrations automatically
- Start Redis
- Create an
adminsuperuser automatically - Start RedisInsight (Redis visual UI)
Wait for the log line:
django_app | Starting development server at http://0.0.0.0:8000/3. Access the services
Admin credentials: admin / admin
๐ API Endpoints
Inventory
GET /api/products/
Returns all products from PostgreSQL.
Response:
[
{
"id": 1,
"category": 2,
"name": "Wireless Headphones",
"description": "Noise-cancelling over-ear headphones",
"price": "149.99"
}
]GET /api/categories/
Returns all product categories.
Response:
[
{
"id": 1,
"name": "Electronics",
"slug": "electronics",
"is_active": true
}
]Cart (Redis-powered)
POST /api/cart/add/
Adds a product to the session cart. Creates a new session automatically if one doesn't exist.
Request Body:
{
"product_id": 1,
"name": "Wireless Headphones",
"price": 149.99,
"quantity": 2
}Response:
{
"message": "Added to cart."
}What happens in Redis:
HSET cart:{session_key} 1 '{"product_id": 1, "name": "Wireless Headphones", "price": 149.99, "quantity": 2}'GET /api/cart/get/
Returns all items in the current session's cart.
Response:
[
{
"product_id": 1,
"name": "Wireless Headphones",
"price": 149.99,
"quantity": 2
}
]๐ How Redis Storage Works
Cart data is stored in Redis as a Hash data structure:
Key: cart:{django_session_key}
Field: {product_id}
Value: JSON string of product dataExample in Redis CLI:
HGETALL cart:abc123xyz
# Output:
# 1) "1"
# 2) "{\"product_id\": 1, \"name\": \"Headphones\", \"price\": 149.99, \"quantity\": 2}"Why Redis instead of the database?
- Cart reads/writes are extremely frequent โ Redis handles millions of ops/sec
- Cart data is temporary by nature โ it doesn't need to persist long-term
- Session expiry naturally cleans up abandoned carts
๐ ๏ธ Local Development (Without Docker)
Requirements
- Python 3.11+
- Redis running locally on port 6379
- PostgreSQL running locally
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # macOS/Linux
# Install dependencies
pip install -r requirements.txt
# Update settings.py โ change Redis/DB hosts from 'redis'/'postgres' to 'localhost'
cd app
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver๐งช Testing the API Manually
Using Swagger UI (Recommended)
- Open http://localhost:8000/docs/
- Try
POST /api/cart/add/with a product payload - Try
GET /api/cart/get/to see the cart
Using curl
# Add item to cart
curl -X POST http://localhost:8000/api/cart/add/ \
-H "Content-Type: application/json" \
-c cookies.txt -b cookies.txt \
-d '{"product_id": 1, "name": "Headphones", "price": 149.99, "quantity": 1}'
# Get cart
curl http://localhost:8000/api/cart/get/ \
-c cookies.txt -b cookies.txtNote: The -c cookies.txt -b cookies.txt flags persist the session cookie across requests.Viewing Cart Data in RedisInsight
- Open http://localhost:5540
- Add connection: host
redis, port6379 - Browse keys โ look for
cart:*keys
๐ฆ Dependencies
Django==5.2
djangorestframework==3.16.0
drf-spectacular==0.28.0 # Swagger/OpenAPI auto-generation
redis==5.2.1 # Redis Python client
psycopg[binary]==3.2.7 # PostgreSQL adapter๐ Security Notes (Production Checklist)
This project is configured for development only. Before deploying to production:
- [ ] Replace
SECRET_KEYwith a secure random value (use environment variables) - [ ] Set
DEBUG = False - [ ] Configure
ALLOWED_HOSTSwith your domain - [ ] Change admin credentials from
admin/admin - [ ] Add Redis password authentication
- [ ] Use environment variables for all secrets (
.envfile +python-decouple) - [ ] Switch
runservertogunicornoruvicorn
๐จโ๐ป Author
Moaz โ @iiMoaaz
๐ License
MIT License
