A comprehensive Flask API system with Redis caching and PostgreSQL database performance comparison.
This project demonstrates the performance difference between database queries with and without Redis caching. It includes:
- Flask REST API with multiple endpoints
- PostgreSQL database with sample data
- Redis caching layer
- Performance monitoring endpoints
- K6 load testing scripts
- Gunicorn WSGI server for production-like performance
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β K6 Load Test βββββΆβ Flask API βββββΆβ PostgreSQL β
βββββββββββββββββββ β (Gunicorn) β β Database β
βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β Redis Cache β
βββββββββββββββββββ
- User Management API: CRUD operations for users
- Product Catalog API: Product management with categories
- Order System API: Order processing and history
- Caching Layer: Redis implementation for frequently accessed data
- Performance Metrics: Built-in endpoints to measure query performance
- Load Testing: K6 scripts for comprehensive performance testing
- Python 3.8+
- Docker and Docker Compose
- Node.js (for K6 testing)
pip install -r requirements.txtStart PostgreSQL and Redis using Docker Compose:
docker-compose up -dThis will start:
- PostgreSQL database on port 5432
- Redis server on port 6379
Run the setup script to create tables and populate with sample data:
python setup_db.pyThis script will:
- Test database and Redis connections
- Create all required tables
- Populate database with sample data (users, products, orders)
- Warm up the cache with frequently accessed data
Start Flask application with Gunicorn:
gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 --timeout 120 app:appGET /api/users- Get all users (with pagination)GET /api/users/<id>- Get user by IDPOST /api/users- Create new userPUT /api/users/<id>- Update userDELETE /api/users/<id>- Delete user
GET /api/products- Get all products (with pagination)GET /api/products/<id>- Get product by IDGET /api/products/category/<category>- Get products by categoryPOST /api/products- Create new productPUT /api/products/<id>- Update productDELETE /api/products/<id>- Delete product
GET /api/orders- Get all ordersGET /api/orders/<id>- Get order by IDGET /api/orders/user/<user_id>- Get orders by userPOST /api/orders- Create new orderPUT /api/orders/<id>- Update order status
GET /api/performance/users- Test user queries with/without cacheGET /api/performance/products- Test product queries with/without cacheGET /api/performance/orders- Test order queries with/without cacheGET /api/performance/clear-cache- Clear all cacheGET /api/performance/stats- Get cache statistics
Install K6:
# macOS
brew install k6
# Or download from https://k6.io/docs/getting-started/installation/Run load tests:
# Basic load test
k6 run tests/load_test.js
# Stress test
k6 run tests/stress_test.js
# Cache performance comparison
k6 run tests/cache_performance_test.jsWith Redis caching, you should see:
- Query Response Time: 80-95% reduction
- Database Load: 70-90% reduction
- API Throughput: 200-500% increase
- Concurrent Users: 300-800% increase capacity
The system provides built-in performance monitoring:
- Response time comparison (cached vs uncached)
- Database query count
- Cache hit/miss ratios
- Memory usage statistics
flask-redis-api-cache/
βββ app.py # Main Flask application
βββ models.py # Database models
βββ cache.py # Redis cache utilities
βββ config.py # Configuration settings
βββ setup_db.py # Database setup and seeding script
βββ docker-compose.yml # Docker services configuration
βββ requirements.txt # Python dependencies
βββ tests/
β βββ load_test.js # Basic load test
β βββ stress_test.js # Stress test
β βββ cache_performance_test.js # Cache comparison test
βββ README.md # This file
- Start Docker Services:
docker-compose up -d - Initialize Database:
python setup_db.py - Start Flask API:
gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 app:app - Run Tests:
k6 run tests/load_test.js
The system implements several caching strategies:
- TTL-based caching: Data expires after configured time
- Write-through caching: Updates both cache and database
- Cache-aside pattern: Application manages cache logic
- Bulk operations: Efficient handling of multiple records
Recommended Gunicorn settings for performance:
gunicorn --bind 0.0.0.0:5001 \
--workers 4 \
--threads 2 \
--timeout 120 \
--keepalive 5 \
--max-requests 1000 \
--max-requests-jitter 100 \
app:appThe K6 tests cover:
- Cold start: First requests without cache
- Warm cache: Subsequent requests with cache hits
- Cache invalidation: Performance after cache clears
- Mixed workloads: Read/write operation combinations
- Concurrent users: High load scenarios
This system provides a comprehensive platform for testing and demonstrating the performance benefits of Redis caching in a Flask API environment.