Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NeuroGate 🧠⚑

A Distributed, Fault-Tolerant Inference Gateway for LLMs built with Go, gRPC, Kubernetes, and Terraform.

Architecture Go Version Kubernetes Terraform

🎯 Overview

NeuroGate is not a chatbotβ€”it's the platform that serves the chatbot. It provides:

  • Load Balancing: Round-robin distribution across multiple LLM workers
  • Fault Tolerance: Circuit breaker pattern to handle failing workers gracefully
  • Observability: Prometheus metrics + Grafana dashboards for tokens/second, latency, etc.
  • Cloud-Native: Kubernetes deployment with Terraform IaC

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              CLIENTS                                  β”‚
β”‚                     curl / SDK / Web Application                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚ HTTP (REST)
                                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         GATEWAY (Load Balancer)                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ API Auth   β”‚  β”‚ Round Robin LB  β”‚  β”‚ Circuit Breaker (3 fails)β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚ gRPC
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β–Ό                  β–Ό                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Worker 1     β”‚  β”‚    Worker 2     β”‚  β”‚    Worker 3     β”‚
β”‚  (gRPC Server)  β”‚  β”‚  (gRPC Server)  β”‚  β”‚  (gRPC Server)  β”‚
β”‚  :50051/:9090   β”‚  β”‚  :50052/:9092   β”‚  β”‚  :50053/:9093   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                    β”‚                    β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚ HTTP
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚     Ollama      β”‚
                    β”‚  (LLM Engine)   β”‚
                    β”‚    :11434       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

neurogate/
β”œβ”€β”€ api/proto/              # gRPC Protocol Buffer definitions
β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ gateway/            # Load Balancer REST API
β”‚   └── worker/             # gRPC Worker connecting to Ollama
β”œβ”€β”€ deploy/
β”‚   β”œβ”€β”€ k8s/                # Kubernetes YAML manifests
β”‚   β”œβ”€β”€ terraform/          # Terraform IaC for K8s resources
β”‚   └── prometheus/         # Prometheus configuration
β”œβ”€β”€ pkg/
β”‚   β”œβ”€β”€ circuitbreaker/     # Circuit Breaker pattern implementation
β”‚   β”œβ”€β”€ health/             # Health checking utilities
β”‚   β”œβ”€β”€ logger/             # Structured logging with slog
β”‚   β”œβ”€β”€ metrics/            # Prometheus instrumentation
β”‚   └── ollama/             # Ollama API client
β”œβ”€β”€ Dockerfile.gateway      # Multi-stage build for Gateway
β”œβ”€β”€ Dockerfile.worker       # Multi-stage build for Worker
β”œβ”€β”€ docker-compose.yaml     # Local development with Docker
β”œβ”€β”€ Makefile                # Build automation
└── go.mod                  # Go dependencies

πŸš€ Quick Start

Prerequisites

  • Go 1.22+
  • Docker and Docker Compose
  • Ollama (running locally with a model like llama3.2)
  • Kind (optional, for Kubernetes)
  • Terraform (optional, for IaC)

1. Start Ollama

# Install Ollama (if not already)
brew install ollama

# Start Ollama and pull a model
ollama serve &
ollama pull llama3.2

2. Run Locally (No Docker)

# Terminal 1: Start a worker
make run-worker

# Terminal 2: Start the gateway
make run-gateway

# Terminal 3: Test it!
make demo

3. Run with Docker Compose

# Build and start all services
docker-compose up --build

# Test the gateway
curl -X POST http://localhost:8080/prompt \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer neurogate-secret-key-1" \
  -d '{"query": "Explain quantum computing in simple terms"}'

4. Deploy to Kubernetes (Kind)

# Build Docker images
make docker

# Create Kind cluster and deploy
make kind-create
make deploy

# Check status
make status

# Test it
make demo-k8s

πŸ“‘ API Reference

POST /prompt

Generate text from the LLM.

Request:

curl -X POST http://localhost:8080/prompt \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer neurogate-secret-key-1" \
  -d '{
    "query": "Why is the sky blue?",
    "model": "llama3.2",
    "max_tokens": 500,
    "temperature": 0.7
  }'

Response:

{
  "request_id": "req-1704567890123456789",
  "response": "The sky appears blue due to a phenomenon called Rayleigh scattering...",
  "model": "llama3.2",
  "tokens": 156,
  "latency_ms": 2340,
  "worker_id": "worker-0"
}

GET /health

Check gateway health status.

GET /workers

List all workers and their status including circuit breaker state.

πŸ“Š Observability

Prometheus Metrics

Metric Type Description
neurogate_gateway_requests_total Counter Total HTTP requests
neurogate_gateway_request_duration_seconds Histogram Request latency
neurogate_gateway_circuit_breaker_state Gauge CB state per worker
neurogate_worker_inference_duration_seconds Histogram LLM inference time
neurogate_worker_tokens_generated_total Counter Tokens generated
neurogate_worker_tokens_per_second Gauge Current TPS

Grafana Dashboards

Access Grafana at http://localhost:3000 (admin/neurogate) with pre-configured dashboards showing:

  • Requests per second
  • P95 latency
  • Tokens per second
  • Circuit breaker states

πŸ”§ Configuration

Environment Variables

Gateway:

Variable Default Description
HTTP_PORT 8080 HTTP listen port
METRICS_PORT 9091 Prometheus metrics port
WORKER_ADDRESSES localhost:50051 Comma-separated worker addresses
API_KEYS (none) Comma-separated valid API keys
LOG_LEVEL info Log level (debug, info, warn, error)

Worker:

Variable Default Description
GRPC_PORT 50051 gRPC listen port
METRICS_PORT 9090 Prometheus metrics port
OLLAMA_URL http://localhost:11434 Ollama API URL
LOG_LEVEL info Log level

πŸ›‘οΈ Fault Tolerance

Circuit Breaker

The gateway implements a circuit breaker for each worker:

  • Closed (normal): Requests flow through
  • Open (tripped): After 3 consecutive failures, traffic stops for 30 seconds
  • Half-Open (testing): One request allowed through to test recovery
   [CLOSED] ──3 failures──> [OPEN] ──30s timeout──> [HALF-OPEN]
       β–²                                                  β”‚
       └────────────successβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
                                                          β”‚
       └────────────failureβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ§ͺ Make Commands

make help           # Show all available commands

# Development
make build          # Build all binaries
make test           # Run tests
make lint           # Run linters
make clean          # Clean build artifacts

# Docker
make docker         # Build all Docker images
make docker-push    # Push images to registry

# Kubernetes
make kind-create    # Create Kind cluster
make deploy         # Deploy to K8s
make undeploy       # Remove from K8s
make status         # Show deployment status

# Terraform
make tf-init        # Initialize Terraform
make tf-apply       # Apply Terraform changes

# Demo
make demo           # Test local gateway
make demo-k8s       # Test Kind gateway

πŸ† Tech Stack Highlights

Component Technology Enterprise Value
Language Go 1.22+ Cloud-native standard
Communication gRPC + Protobuf High-performance internal comms
Orchestration Kubernetes (Kind) Industry-standard container orchestration
IaC Terraform Infrastructure as code
AI Engine Ollama Local LLM inference
Observability Prometheus + Grafana Industry-standard monitoring
Patterns Circuit Breaker Resilience engineering

πŸ“œ License

MIT License - see LICENSE for details.


Built with ❀️ for learning distributed systems and cloud-native development.

About

A Distributed, Fault-Tolerant Inference Gateway for Large Language Models.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages