Skip to content

dwickyfp/flask-redis-cache-performance

Repository files navigation

Flask Redis API Cache Performance Test

A comprehensive Flask API system with Redis caching and PostgreSQL database performance comparison.

System Overview

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

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   K6 Load Test  │───▢│   Flask API     │───▢│   PostgreSQL    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚   (Gunicorn)    β”‚    β”‚   Database      β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Redis Cache   β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Features

  • 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

Prerequisites

  • Python 3.8+
  • Docker and Docker Compose
  • Node.js (for K6 testing)

Installation & Setup

1. Install Dependencies

pip install -r requirements.txt

2. Start Docker Services

Start PostgreSQL and Redis using Docker Compose:

docker-compose up -d

This will start:

  • PostgreSQL database on port 5432
  • Redis server on port 6379

3. Initialize Database

Run the setup script to create tables and populate with sample data:

python setup_db.py

This 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

4. Start Flask Application

Start Flask application with Gunicorn:

gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 --timeout 120 app:app

API Endpoints

User Management

  • GET /api/users - Get all users (with pagination)
  • GET /api/users/<id> - Get user by ID
  • POST /api/users - Create new user
  • PUT /api/users/<id> - Update user
  • DELETE /api/users/<id> - Delete user

Product Catalog

  • GET /api/products - Get all products (with pagination)
  • GET /api/products/<id> - Get product by ID
  • GET /api/products/category/<category> - Get products by category
  • POST /api/products - Create new product
  • PUT /api/products/<id> - Update product
  • DELETE /api/products/<id> - Delete product

Order System

  • GET /api/orders - Get all orders
  • GET /api/orders/<id> - Get order by ID
  • GET /api/orders/user/<user_id> - Get orders by user
  • POST /api/orders - Create new order
  • PUT /api/orders/<id> - Update order status

Performance Testing

  • GET /api/performance/users - Test user queries with/without cache
  • GET /api/performance/products - Test product queries with/without cache
  • GET /api/performance/orders - Test order queries with/without cache
  • GET /api/performance/clear-cache - Clear all cache
  • GET /api/performance/stats - Get cache statistics

Performance Testing with K6

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

Expected Performance Improvements

With 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

Monitoring & Metrics

The system provides built-in performance monitoring:

  • Response time comparison (cached vs uncached)
  • Database query count
  • Cache hit/miss ratios
  • Memory usage statistics

Project Structure

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

Running the System

  1. Start Docker Services: docker-compose up -d
  2. Initialize Database: python setup_db.py
  3. Start Flask API: gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 app:app
  4. Run Tests: k6 run tests/load_test.js

Cache Strategy

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

Performance Tuning

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:app

Testing Scenarios

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

About

πŸš€ Flask API with Redis caching performance demonstration - PostgreSQL + Redis + K6 load testing with up to 95% query performance improvement

Topics

Resources

Stars

Watchers

Forks