Skip to content

A FastAPI-based HTTP proxy with request caching using Redis, designed to forward requests while caching responses for efficient repeated queries.

Notifications You must be signed in to change notification settings

yigitkonur/fastapi-http-proxy-with-caching

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

FastAPI HTTP Proxy with Caching

A FastAPI-based HTTP proxy that forwards requests to specified URLs, while caching responses using Redis to optimize repeated requests. This implementation includes detailed logging to track the flow of requests and caching mechanisms.

Features

  • Forward HTTP POST requests to specified URLs.
  • Cache responses to avoid redundant network calls.
  • Detailed logging for easy debugging and monitoring.
  • Configurable with Redis for caching.
  • Set up as a systemd service for automatic startup and resource monitoring.

Getting Started

Prerequisites

  • Python 3.7+
  • FastAPI
  • Redis (Upstash or local Redis instance)
  • httpx
  • uvicorn

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/fastapi-http-proxy-with-caching.git
    cd fastapi-http-proxy-with-caching
  2. Create a virtual environment and activate it:

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  3. Install the dependencies:

    pip install -r requirements.txt

Using Upstash for Redis Caching

Introduction to Upstash

Upstash is a serverless database solution that offers Redis-compatible caching with a pay-as-you-go pricing model. It is an excellent choice for applications that require scalable and cost-effective caching.

Pricing Details

Upstash's pricing model is straightforward and cost-effective, making it a great choice for caching needs:

  • Pay as You Go: $0.2 per 100K commands
  • Pro 2K: $280 per month
  • Pro 10K: $680 per month

For example, caching:

  • 1 million objects: This would typically involve around 1 million commands. At $0.2 per 100K commands, the cost would be $2.
  • 10 million requests: Assuming each request involves a caching command, this would cost $20 at the pay-as-you-go rate.

Configuration

To use Upstash as your Redis provider, follow these steps:

  1. Sign Up for Upstash:

    • Go to Upstash's website and sign up for an account.
    • Create a new Redis database and note the provided endpoint and credentials.
  2. Configure Redis URL in the Application:

    • Open main.py and set the redis_url variable with your Upstash Redis credentials:

      redis_url = "redis://:your_upstash_password@your_upstash_endpoint:your_upstash_port"
  3. Run the Application:

    • Start the FastAPI server using uvicorn:

      uvicorn main:app --host 0.0.0.0 --port 8000

Running the Application

Start the FastAPI server using uvicorn:

uvicorn main:app --host 0.0.0.0 --port 8000

Usage

Send a POST request to the /webhook-test/post-response endpoint with a url query parameter specifying the target URL.

Example:

curl -X POST "http://127.0.0.1:8000/webhook-test/post-response?url=https://example.com/api" -H "Content-Type: application/json" -d '{"key": "value"}'

Logging

The application includes detailed logging to help trace the flow of requests and responses. Logs are printed to the console.

Setting Up as a Systemd Service

To ensure that your FastAPI application runs automatically on system restart and operates as a background service, you can set it up as a systemd service on your server.

Finding the Required Paths and Information

  1. Finding the Uvicorn Path:

    Ensure that the virtual environment is activated:

    source /path/to/your/venv/bin/activate

    Then find the path to the uvicorn executable:

    which uvicorn

    This will output something like:

    /path/to/your/venv/bin/uvicorn

    Use this path in the ExecStart directive of your systemd service file.

  2. Finding Your Username:

    Your username can be found by running:

    whoami

    This will output your current user's name, which should be used in the User directive.

  3. Finding the Working Directory:

    The working directory is where your FastAPI application (e.g., main.py) is located. Use the pwd command in your project directory to find the full path:

    pwd

    This will output something like:

    /path/to/your/fastapi-app

    Use this path in the WorkingDirectory directive.

  4. Setting Up the Environment Path:

    The environment path should point to the bin directory of your virtual environment. It is typically:

    /path/to/your/venv/bin

Creating and Configuring the Systemd Service

  1. Create the Systemd Service File:

    sudo nano /etc/systemd/system/fastapi.service
  2. Add the Following Content to the Service File:

    Replace the placeholders with the actual paths and user information found in the steps above.

    [Unit]
    Description=FastAPI Service
    After=network.target
    
    [Service]
    User=your_username
    Group=www-data
    WorkingDirectory=/path/to/your/fastapi-app
    Environment="PATH=/path/to/your/venv/bin"
    ExecStart=/path/to/your/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
    
    [Install]
    WantedBy=multi-user.target
  3. Reload Systemd Daemon:

    sudo systemctl daemon-reload
  4. Enable the FastAPI Service to Start on Boot:

    sudo systemctl enable fastapi.service
  5. Start the FastAPI Service:

    sudo systemctl start fastapi.service
  6. Check the Status of the Service:

    sudo systemctl status fastapi.service

Monitoring System Resources

You can monitor the system resource usage of the FastAPI service using standard Linux tools.

  1. Check CPU and Memory Usage:

    top

    Find your service by its name or PID and monitor its resource usage.

  2. Detailed Resource Usage:

    htop

    Use htop for a more user-friendly and detailed view of system resources.

  3. View Service Logs:

    sudo journalctl -u fastapi.service -b

    This command will display detailed logs for the FastAPI service, which can help diagnose any issues.

Additional Resources

By following these steps, you can ensure that your FastAPI application runs correctly as a systemd service and can monitor its resource usage effectively. If there are any further issues, please provide detailed logs for further assistance.

About

A FastAPI-based HTTP proxy with request caching using Redis, designed to forward requests while caching responses for efficient repeated queries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages