A self-hosted inference node for a quantized model. It runs as a containerised stack on one machine and is reachable over a Cloudflare tunnel rather than a cloud GPU, so there is no hyperscaler bill and no inbound port forwarding. Rate-limiting and API-key state live in Redis and PostgreSQL rather than in process memory, so both survive a worker restart.
Built as a learning exercise in self-hosted LLM serving. It runs on a single machine behind an ephemeral *.trycloudflare.com tunnel — not a stable endpoint, and not something anyone is depending on.
The infrastructure is defined by strict boundary segregation, from GPU tensor allocation up to network ingress.
The core compute layer abandons standard API wrapper logic in favor of a locally hosted, quantized model.
- The Model:
google/flan-t5-basefine-tuned on thedatabricks/dolly-15kinstruction-following dataset. - Quantization: Tensors are loaded utilizing
BitsAndBytesConfigin strict 8-bit precision (load_in_8bit=True), preventing immediate GPU VRAM fragmentation during context generation. - Adapter Integration: The base model is merged with a Low-Rank Adaptation (LoRA) via
peft, allowing task-specific instruction alignment without the computational overhead of full-parameter fine-tuning. - The ASGI Gateway: The generation logic is wrapped in a FastAPI asynchronous event loop.
Standard localized LLM nodes fracture under concurrent payloads due to in-memory dictionaries. This stack moves that state out of process memory into persistent, decoupled engines.
- Authorization (PostgreSQL): Hardcoded credentials are fundamentally insecure. API keys are validated against a persistent PostgreSQL volume utilizing
asyncpgand SQLAlchemy V2, ensuring non-blocking database I/O during the FastAPI lifespan. - Atomic Rate Limiting (Redis): Transient token buckets fail under concurrency. We enforce a localized Redis container executing asynchronous Lua pipelines (
transaction=True). This guarantees atomic evaluations of payload frequency, aggressively returning HTTP 429s to hostile actors before the requests can penetrate the GPU inference queue.
Host operating systems introduce uncontrollable port collisions. The application layer is entirely severed from the localized host environment.
- Zero-Trust Bridge: All containers operate strictly within an internal Docker bridge (
edge-network). - The Ingress Tunnel: A Cloudflare
cloudflareddaemon negotiates a direct HTTP2 TCP tunnel to the global perimeter, avoiding inbound port forwarding and local firewall ACLs. - Internal Reverse Proxy: Trans-continental payloads pierce the tunnel and are intercepted by Traefik. Traefik dynamically routes the HTTP traffic to the Uvicorn workers entirely within the isolated bridge, leaving zero ports exposed to the host machine.
A functional inference node without telemetry is an operational black box. This matrix integrates a strict observability layer to monitor the perimeter defense.
- The TSDB Scraper: Prometheus silently scrapes the Uvicorn workers every 5 seconds. The
/metricsendpoint is strictly whitelisted from the Redis token bucket to prevent a self-inflicted denial of service on the telemetry layer. - Declarative Dashboards: Grafana is provisioned via Infrastructure as Code (IaC). Dashboards are etched directly into the container state, requiring zero manual UI configuration.
To see how the stack behaves under concurrency, a 150-concurrent-user Locust swarm was run against the Cloudflare tunnel.
System state during a 150-concurrent-user synthetic load test. Redis pipelines returning HTTP 429 to requests over the bucket limit, so the inference queue only sees accepted traffic. Single machine, one run — the screenshot is the whole evidence base, and no latency percentile was recorded.
To compile and boot the isolated infrastructure:
docker-compose up -d --buildThe PostgreSQL schema will initialize, the Redis token bucket will arm, and Traefik will establish the internal DNS routing.
Extract the generated tunnel URL to route traffic to your local machine:
docker logs edge-ingress-tunnelLocate the *.trycloudflare.com URL. All external payloads must be directed to https:///generate/.
The load-testing dependencies are kept out of the build context to keep the image small. To reproduce the test locally:
Install the development dependencies on your host machine:
pip install -r requirements-dev.txtRun the load test against your active Cloudflare tunnel:
locust -f benchmarks/locustfile.py --host=https://<your-cloudflare-url>.trycloudflare.comOpen the local Grafana instance at http://localhost:3000 to watch the Redis token bucket throttling the overflow. The admin credentials come from GF_SECURITY_ADMIN_USER / GF_SECURITY_ADMIN_PASSWORD in docker-compose.yml — set your own before exposing Grafana anywhere.