inference_gateway

Building an LLM Inference Gateway: Queues, Limits & Timeouts

A lightweight inference gateway built with FastAPI and vLLM to explore production-oriented LLM serving concepts, including concurrency control, bounded queues, overload handling, upstream timeouts, request logging, and metrics.

This project focuses on understanding how a gateway manages incoming requests when inference capacity is limited, and how its behavior can be measured through controlled load tests.

๐ŸŽฏ Project Goals

๐Ÿ—๏ธ Architecture


                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

                   โ”‚   Load Test Client  โ”‚

                   โ”‚    load_test.py     โ”‚

                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

                              โ”‚

                              โ–ผ

                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

                   โ”‚   FastAPI Gateway   โ”‚

                   โ”‚                     โ”‚

                   โ”‚  Admission Control  โ”‚

                   โ”‚  Concurrency Limit  โ”‚

                   โ”‚  Bounded Queue      โ”‚

                   โ”‚  Timeout Handling   โ”‚

                   โ”‚  Request Logging    โ”‚

                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

                              โ”‚

                              โ–ผ

                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”

                   โ”‚     vLLM Server     โ”‚

                   โ”‚   LLM Inference     โ”‚

                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The gateway sits between the load-test client and vLLM. It regulates access to the inference server and records the outcome of each request.

๐Ÿ› ๏ธ Tech Stack

๐Ÿ“‚ Project Structure


production_vllm/

โ”œโ”€โ”€ gateway.py       # FastAPI inference gateway

โ”œโ”€โ”€ load_test.py     # Concurrent request load tester

โ”œโ”€โ”€ results/         # Experiment logs in JSONL format

โ””โ”€โ”€ README.md        # Project documentation

โš™๏ธ Gateway Features

1. Concurrency Control

Uses an asynchronous semaphore to limit the number of requests actively processed by the gateway.


MAX_ACTIVE=1

This setting allows one request at a time to occupy an active gateway slot.

2. Bounded Queue

Limits how many additional requests can wait for an active slot.


MAX_QUEUE=4

Requests arriving when the active capacity and waiting queue are full are rejected rather than admitted without limit.

3. Queue Deadline

Limits how long a request can wait to acquire an active slot.


QUEUE_DEADLINE=5

Requests that exceed this waiting deadline receive an HTTP 429 response.

4. Upstream Timeout

Limits how long the gateway waits for a response from vLLM.


UPSTREAM_TIMEOUT=30

An upstream timeout is handled as an HTTP 504 response.

5. Metrics and Logging

The gateway exposes a /metrics endpoint with counters and current-state values, including:

Request events are also written to JSONL logs for later analysis.

๐Ÿ–ฅ๏ธ Running Experiments with Four Terminals

I used four terminals in my WSL Ubuntu environment to separate the gateway, inference server, load generator, and monitoring tasks.

Terminal Responsibility Example

|โ€”|โ€”|โ€”|

Terminal 1 Run the vLLM inference server Start the model-serving process
Terminal 2 Run the FastAPI gateway uvicorn gateway:app --port 9000
Terminal 3 Generate concurrent requests python load_test.py 20
Terminal 4 Inspect metrics and experiment logs curl -s http://localhost:9000/metrics

This separation makes it easier to observe how the gateway behaves while inference requests are being processed.

Note: The exact commands and environment variables depend on the model configuration and the experiment being run.

๐Ÿš€ Running the Gateway

1. Activate the Python environment


source venv/bin/activate

2. Start the vLLM server

Start vLLM using your chosen model and its appropriate serving configuration. The gateway expects the OpenAI-compatible API to be available at:


http://localhost:8000

If vLLM is running on a different address, configure VLLM_URL accordingly.

3. Start the gateway


uvicorn gateway:app --port 9000

4. Run a load test

In another terminal:


python load_test.py 20

This sends 20 simultaneous requests to the gateway.

5. Inspect metrics


curl -s http://localhost:9000/metrics

6. Inspect the logs


cat results/requests.jsonl

The log filename can be changed using the LOG_PATH environment variable.

๐Ÿงช Experiments Completed

Experiment 1: Concurrency and Admission Control

Explored how the gateway limits active requests and handles requests that exceed its admission capacity.

Experiment 2: Queue Capacity vs. Overload

Kept the active-request limit fixed while changing the queue capacity.

Queue Capacity Requests Sent Successful Rejected

|โ€”:|โ€”:|โ€”:|โ€”:|

1 20 2 18
4 20 5 15
9 20 10 10

In these individual runs, larger queues allowed more requests to be accepted, but successful-request latency increased as the queue grew.

These results are observations from small experiments, not statistically conclusive performance benchmarks.

Experiment 3: Queue Deadline

Tested a one-second queue deadline with one active slot and a queue capacity of nine.

In the latest run:

This demonstrated the difference between rejecting a request immediately due to capacity and rejecting it after excessive queue waiting.

Experiment 4: Upstream Timeout Handling

Configured an intentionally short upstream timeout:


UPSTREAM_TIMEOUT=0.001

Five simultaneous requests returned HTTP 504, and the gateway recorded five upstream timeouts.

This was a fault-injection test to validate timeout handling, not a realistic production configuration.

Experiment 5: Recovery After Timeout Configuration Change

Restored the upstream timeout to 30 seconds and restarted the gateway.

The subsequent request returned HTTP 200 with an observed latency of approximately 0.91 seconds.

This confirmed that the gateway worked after restarting with the restored configuration. It did not independently establish recovery within the same running process.

๐Ÿ“Š Key Learnings

๐Ÿ”ญ Future Work

The following features are planned but have not yet been implemented or validated:

โš ๏ธ Current Limitations

๐Ÿ‘ค About This Project

This is a hands-on learning project focused on the engineering problems surrounding LLM inference serving: request admission, bounded resources, overload behavior, timeouts, observability, and performance evaluation.

The goal is to develop a practical understanding of inference infrastructure through implementation, experimentation, and measurements.