Skip to main content
This tutorial shows how to build a load balancing worker using FastAPI and deploy it as a Serverless endpoint on Runpod.

Requirements

Before you begin you’ll need:
  • A Runpod account.
  • Basic familiarity with Python and REST APIs.
  • Docker installed on your local machine.

Step 1: Create a basic FastAPI application

You can download a preconfigured repository containing the completed code for this tutorial on GitHub.
First, let’s create a simple FastAPI application that will serve as our API. Create a file named app.py:
This simple application defines the following endpoints:
  • A health check endpoint at /ping
  • A text generation endpoint at /generate
  • A statistics endpoint at /stats

Step 2: Create a Dockerfile

Now, let’s create a Dockerfile to package our application:
You’ll also need to create a requirements.txt file:

Step 3: Build and push the Docker image

Build and push your Docker image to a container registry:

Step 4: Deploy to Runpod

Now, let’s deploy our application to a Serverless endpoint:
  1. Go to the Serverless page in the Runpod console.
  2. Click New Endpoint
  3. Click Import from Docker Registry.
  4. In the Container Image field, enter your Docker image URL:
    Then click Next.
  5. Give your endpoint a name.
  6. Under Endpoint Type, select Load Balancer.
  7. Under GPU Configuration, select at least one GPU type (16 GB or 24 GB GPUs are fine for this example).
  8. Leave all other settings at their defaults.
  9. Click Deploy Endpoint.

Step 5: Access your custom API

Once your endpoint is created, you can access your custom APIs at:
For example, the load balancing worker we defined in step 1 exposes these endpoints:
  • Health check: https://ENDPOINT_ID.api.runpod.ai/ping
  • Generate text: https://ENDPOINT_ID.api.runpod.ai/generate
  • Get request count: https://ENDPOINT_ID.api.runpod.ai/stats
Try running one or more of these commands, replacing ENDPOINT_ID and RUNPOD_API_KEY with your actual endpoint ID and API key:
After sending a request, your workers will take some time to initialize. You can track their progress by checking the logs in the Workers tab of your endpoint page.
If you see: {"error":"no workers available"}% after running the request, this means your workers did not initialize in time to process it. If you try running the request again, this will usually resolve the issue.For production applications, implement a health check with retries before sending requests. See Handling cold start errors for a complete code example.
Congratulations! You’ve successfully deployed and tested a load balancing endpoint. If you want to use a real model, you can follow the vLLM worker tutorial.

(Optional) Advanced endpoint definitions

For a more complex API, you can define multiple endpoints and organize them logically. Here’s an example of how to structure a more complex API:

(Optional) WebSocket support

Load balancing endpoints also support WebSocket connections. This section shows how to add a WebSocket endpoint to your worker and connect to it from a client.
You can clone the worker-lb-websocket repository for a complete working example, including scaling tests.

Add a WebSocket endpoint

WebSocket endpoints in FastAPI use the @app.websocket() decorator. Add the following to your app.py:
app.py

Connect from a client

When connecting to a WebSocket endpoint on a load balancing worker, you must set the open_timeout parameter to allow time for workers to scale up. The default timeout of 5 seconds is usually not enough.
client.py
If you don’t set open_timeout, connections will fail with a timeout error when workers need to scale up from zero. A value of 60 seconds works for most use cases.

Update requirements.txt

Add the websockets library to your client’s dependencies:

Troubleshooting

Here are some common issues and methods for troubleshooting:
  • No workers available: If your request returns {"error":"no workers available"}%, this means means your workers did not initialize in time to process the request. Running the request again will usually fix this issue.
  • Worker unhealthy: Check your health endpoint implementation and ensure it’s returning proper status codes.
  • API not accessible: If your request returns {"error":"not allowed for QB API"}, verify that your endpoint type is set to “Load Balancer”.
  • Port issues: Make sure the environment variable for PORT matches what your application is using, and that the PORT_HEALTH variable is set to a different port.
  • Model errors: Check your model’s requirements and whether it’s compatible with your GPU.
  • WebSocket timeout: If WebSocket connections fail with timeout errors, increase the open_timeout parameter in your client code to allow workers time to scale up. See (Optional) WebSocket support for details.

Next steps

Now that you’ve learned how to build a basic load balancing worker, you can try implementing a real model with vLLM.