A production-ready smart load balancer built on Vercel Edge with AI/heuristics-based bot detection and comprehensive Sentry instrumentation.
- π Edge-first architecture - Runs on Vercel Edge for global low-latency
- π‘οΈ Bot Guard - Heuristics + optional AI classifier for bot detection
- βοΈ Smart Load Balancing - Multiple strategies: weighted RR, latency-aware, health-aware, sticky
- π Sentry Instrumentation - Errors, traces, metrics, and dashboards
- π Rate Limiting - Per-IP, session, endpoint, or composite keys
- π Admin Dashboard - Real-time metrics and policy management
- π₯ Health Monitoring - Automatic backend health checks
- Node.js 18+
- pnpm
- Vercel account
- Sentry account
- Vercel KV instance
- PostgreSQL database (Vercel Postgres, Neon, or Supabase)
# Clone the repository
git clone <your-repo-url>
cd guard
# Install dependencies
pnpm install
# Copy environment variables
cp .env.example .env.local
# Initialize database
pnpm db:init
# Start development server
pnpm devCopy .env.example to .env.local and configure:
# Sentry
SENTRY_DSN=your-sentry-dsn
NEXT_PUBLIC_SENTRY_DSN=your-sentry-dsn
# Vercel KV
KV_REST_API_URL=your-kv-url
KV_REST_API_TOKEN=your-kv-token
# PostgreSQL
DATABASE_URL=your-database-url
# Security
ADMIN_API_KEY=your-secure-api-key
IP_HASH_SALT=random-salt-for-ip-hashing
CHALLENGE_SECRET=secret-for-challenge-tokensβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Edge Middleware β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Request β Features β Rate Limit β Bot Guard β LB β Proxy β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββΌββββββββββββββββ
βΌ βΌ βΌ
βββββββββββ βββββββββββ βββββββββββ
βBackend 1β βBackend 2β βBackend Nβ
βββββββββββ βββββββββββ βββββββββββ
- Feature Extraction - Extract IP (hashed), UA, headers, geo, etc.
- Rate Limiting - Check against configured limits
- Bot Detection - Score request using heuristics/AI
- Backend Selection - Choose backend based on strategy
- Proxy - Forward request and return response
Access the dashboard at /dashboard:
- Overview - Traffic stats, decision distribution
- Bot Guard - Score distributions, detection reasons
- Backends - Health status, latency metrics
- Policies - View and manage configuration
All admin endpoints require Authorization: Bearer YOUR_API_KEY header.
| Endpoint | Method | Description |
|---|---|---|
/api/admin/config |
GET | Get active config |
/api/admin/config?list=true |
GET | List all config versions |
/api/admin/config |
POST | Create/update config |
/api/admin/config |
PUT | Activate a draft config |
/api/admin/config?version=x |
DELETE | Delete a draft config |
/api/admin/backends |
GET | Get backend health status |
| Endpoint | Method | Description |
|---|---|---|
/api/health |
GET | Health check endpoint |
/api/challenge/verify |
POST | Verify challenge response |
| Strategy | Description |
|---|---|
weighted-round-robin |
Distribute by weight in rotation |
latency-aware |
Prefer backends with lower P95 latency |
health-aware |
Avoid unhealthy backends |
sticky |
Route same client to same backend |
random |
Random selection |
The heuristics engine evaluates:
- Missing/empty User-Agent
- Known bot patterns
- Missing Accept headers
- Few headers (< 5)
- Missing Accept-Language
- High request frequency
- Unusual HTTP methods
- Deep paths without referer
edge.balancer GET /api/users
edge.feature_extractedge.rate_limitedge.bot_heuristicsedge.route_selectedge.proxy
requests_total{decision,route,backend}bot_score{route}backend_latency_ms{backend,route}backend_health{backend}
See sentry/alerts.json for preconfigured alert rules.
# Run unit tests
pnpm test
# Run tests with coverage
pnpm test:coverage
# Run load test
pnpm load-test
# Run load test with bot simulation
pnpm load-test -- --bot# Install Vercel CLI
pnpm add -g vercel
# Deploy
vercel
# Set environment variables
vercel env add SENTRY_DSN
# ... repeat for all variables- Initialize the database: Run
pnpm db:initwith production DATABASE_URL - Create initial config via Admin API
- Verify cron job is running (check
/api/cron/health) - Verify Sentry is receiving events
{
"version": "1.0.0",
"status": "active",
"backends": [
{
"id": "primary",
"name": "Primary API",
"url": "https://api.example.com",
"weight": 80,
"healthEndpoint": "/health"
}
],
"policies": [
{
"id": "api-policy",
"name": "API Routes",
"pathPattern": "/api/**",
"strategy": "latency-aware",
"backendIds": ["primary"],
"rateLimit": {
"enabled": true,
"windowMs": 60000,
"maxRequests": 100
},
"botGuard": {
"enabled": true,
"thresholds": { "low": 0.3, "medium": 0.6, "high": 0.85 },
"actions": { "low": "allow", "medium": "challenge", "high": "block" }
}
}
]
}MIT