A high-performance, multi-tenant API Gateway with distributed caching, rate limiting, and advanced routing capabilities.
The backend consists of 6 core components:
- Control Plane Service - Configuration and tenant management
- Authentication Layer - Clerk integration and identity management
- API Gateway - Request routing and traffic management
- Load Balancer - Intelligent traffic distribution
- Distributed Cache - Redis-compatible caching layer
- Observability - Metrics, traces, and logs
- Language: Go 1.21+
- Database: PostgreSQL 15
- Cache: Redis 7
- Authentication: Clerk
- Observability: OpenTelemetry
- Containerization: Docker & Docker Compose
- Docker & Docker Compose
- Go 1.21+ (for local development)
- Clerk account with API keys
- Copy the environment template:
cp .env.example .env- Update
.envwith your Clerk credentials:
CLERK_SECRET_KEY=your_clerk_secret_key
CLERK_PUBLISHABLE_KEY=your_clerk_publishable_key# Build and start all services
docker-compose up --build
# Run in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Stop all services
docker-compose down# Install dependencies
go mod download
# Run database migrations
make migrate-up
# Seed initial data
make seed
# Run control plane
make run-control-plane
# Run gateway (in another terminal)
make run-gatewayCreate Tenant
curl -X POST http://localhost:8080/api/v1/tenants \
-H "Authorization: Bearer <clerk_token>" \
-H "Content-Type: application/json" \
-d '{
"name": "acme-corp",
"subdomain": "acme",
"clerk_org_id": "org_xxx"
}'Get Tenant
curl -X GET http://localhost:8080/api/v1/tenants/:id \
-H "Authorization: Bearer <clerk_token>"Add Origin
curl -X POST http://localhost:8080/api/v1/origins \
-H "Authorization: Bearer <clerk_token>" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "tenant_uuid",
"name": "api-backend",
"url": "https://api.example.com",
"health_check_path": "/health",
"timeout_seconds": 30
}'Create Route
curl -X POST http://localhost:8080/api/v1/routes \
-H "Authorization: Bearer <clerk_token>" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "tenant_uuid",
"path_pattern": "/api/users/*",
"origin_id": "origin_uuid",
"auth_mode": "jwt_required",
"rate_limit": {
"requests_per_second": 100,
"burst": 200
},
"cache_policy": {
"enabled": true,
"ttl_seconds": 300,
"cache_key_pattern": "path+query"
}
}'Generate API Key
curl -X POST http://localhost:8080/api/v1/api-keys \
-H "Authorization: Bearer <clerk_token>" \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "tenant_uuid",
"name": "production-key",
"scopes": ["read", "write"],
"expires_at": "2025-12-31T23:59:59Z"
}'Make Request Through Gateway
# JWT Authentication
curl -X GET https://acme.vantageedge.dev/api/users/123 \
-H "Authorization: Bearer <clerk_jwt_token>"
# API Key Authentication
curl -X GET https://acme.vantageedge.dev/api/users/123 \
-H "X-API-Key: <generated_api_key>"
# Public Route (no auth)
curl -X GET https://acme.vantageedge.dev/api/public/statusvantageedge-backend/
├── cmd/
│ ├── control-plane/ # Control plane service entry point
│ ├── gateway/ # API gateway entry point
│ └── migrator/ # Database migration tool
├── internal/
│ ├── auth/ # Authentication & authorization
│ │ ├── clerk/ # Clerk integration
│ │ ├── jwt/ # JWT validation
│ │ └── apikey/ # API key management
│ ├── controlplane/ # Control plane business logic
│ │ ├── handlers/ # HTTP handlers
│ │ ├── grpc/ # gRPC service
│ │ └── service/ # Business logic
│ ├── gateway/ # Gateway core
│ │ ├── router/ # Request routing
│ │ ├── middleware/ # Middleware chain
│ │ └── proxy/ # Reverse proxy
│ ├── loadbalancer/ # Load balancing algorithms
│ │ ├── roundrobin/
│ │ ├── leastconn/
│ │ └── consistenthash/
│ ├── cache/ # Distributed cache
│ │ ├── redis/ # Redis implementation
│ │ └── memory/ # In-memory fallback
│ ├── ratelimit/ # Rate limiting
│ │ ├── tokenbucket/
│ │ └── slidingwindow/
│ ├── models/ # Domain models
│ ├── repository/ # Data access layer
│ └── observability/ # Metrics, traces, logs
├── migrations/ # Database migrations
├── pkg/ # Shared packages
│ ├── config/
│ ├── database/
│ ├── logger/
│ └── telemetry/
├── api/
│ ├── proto/ # gRPC definitions
│ └── openapi/ # OpenAPI specs
├── scripts/
│ ├── seed.sql # Sample data
│ └── test-requests.sh # Example requests
├── docker/
│ ├── control-plane.Dockerfile
│ ├── gateway.Dockerfile
│ └── nginx.conf
├── docker-compose.yml
├── Makefile
├── go.mod
├── go.sum
└── README.md
- Subdomain-based tenant routing (
https://<tenant>.vantageedge.dev) - Isolated configurations per tenant
- Clerk organization mapping
- Clerk JWT validation
- API key authentication
- Service-to-service authentication
- OAuth token support
- Token bucket algorithm
- Sliding window counters
- Per-tenant, per-route, and per-user limits
- Configurable burst capacity
- Distributed Redis cache
- Tenant-aware namespacing
- Configurable TTL and eviction policies
- Cache key patterns (path, query, headers)
- Selective cache bypass rules
- Round robin distribution
- Least connections
- Consistent hashing
- Health checking
- Circuit breaking
- OpenTelemetry traces
- Structured JSON logging
- Prometheus metrics:
- Request latency histograms
- Cache hit/miss ratios
- Rate limit actions
- Error rates by status code
- Active connections
id(UUID, PK)name(String)subdomain(String, Unique)clerk_org_id(String, Unique)created_at,updated_at
id(UUID, PK)clerk_user_id(String, Unique)tenant_id(UUID, FK)email(String)role(Enum)created_at,updated_at
id(UUID, PK)tenant_id(UUID, FK)name(String)url(String)health_check_path(String)timeout_seconds(Integer)created_at,updated_at
id(UUID, PK)tenant_id(UUID, FK)origin_id(UUID, FK)path_pattern(String)auth_mode(Enum: public, jwt_required, apikey_required)priority(Integer)rate_limit_config(JSONB)cache_policy(JSONB)created_at,updated_at
id(UUID, PK)tenant_id(UUID, FK)key_hash(String, Unique)name(String)scopes(JSONB)expires_at(Timestamp)created_at,updated_at
# Run all tests
make test
# Run with coverage
make test-coverage
# Run integration tests
make test-integration
# Load testing
make load-test-
Database
- Use managed PostgreSQL (AWS RDS, Google Cloud SQL)
- Enable connection pooling
- Regular backups
-
Cache
- Use managed Redis (AWS ElastiCache, Redis Cloud)
- Enable persistence for critical cache data
- Set up replication for HA
-
Secrets
- Use secret management (AWS Secrets Manager, HashiCorp Vault)
- Rotate Clerk API keys regularly
- Secure API key hashing with strong algorithms
-
Monitoring
- Set up alerting for error rates, latency spikes
- Monitor cache hit ratios
- Track rate limit violations
-
Scaling
- Gateway and control plane can scale independently
- Use horizontal pod autoscaling based on CPU/memory
- Consider multi-region deployment for global users
The system comes with preconfigured demo data:
Tenant: demo-company Subdomain: demo Origin: https://jsonplaceholder.typicode.com
# 1. Public route (no auth)
curl https://demo.vantageedge.dev/api/public/posts
# 2. Protected route with JWT
curl https://demo.vantageedge.dev/api/users/1 \
-H "Authorization: Bearer <your_clerk_jwt>"
# 3. API key protected route
curl https://demo.vantageedge.dev/api/admin/users \
-H "X-API-Key: demo_key_abc123"
# 4. Demonstrate caching (first call: MISS, second: HIT)
curl -v https://demo.vantageedge.dev/api/posts/1
curl -v https://demo.vantageedge.dev/api/posts/1
# 5. Trigger rate limit
for i in {1..150}; do
curl https://demo.vantageedge.dev/api/users/1
doneDatabase Connection Failed
- Check PostgreSQL is running:
docker-compose ps - Verify connection string in
.env - Check network connectivity
Clerk Token Validation Failed
- Ensure
CLERK_SECRET_KEYis correct - Verify token is not expired
- Check token format:
Bearer <token>
Cache Not Working
- Verify Redis is running
- Check cache policy configuration
- Ensure route has caching enabled
Rate Limit Not Applied
- Check rate limit configuration in route
- Verify tenant identification is working
- Check Redis connectivity for state storage
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
MIT License - See LICENSE file for details
For issues and questions:
- GitHub Issues: [Create an issue]
- Documentation: [Wiki]
- Email: support@vantageedge.dev