-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
Context
Part of load balancer architecture work (see PR #228 for full analysis).
Risk: Medium | Reward: Medium | Code Changes: Required
Architecture Change
Uses config service for guild lookups → Deterministic sharding
Load Balancer → HTTP Service (stateless, 2-10 pods)
↓
Calculate: hash(guildId) % numPods
↓
Gateway Pod N (via headless service DNS)
Implementation
Deterministic Routing
// app/helpers/guildRouting.ts
import crypto from 'crypto';
const NUM_GATEWAY_PODS = parseInt(process.env.NUM_GATEWAY_PODS || '3', 10);
export function getGatewayPodForGuild(guildId: string): number {
// Consistent hash - same guild always routes to same pod
const hash = crypto.createHash('md5').update(guildId).digest();
const num = hash.readUInt32BE(0);
return num % NUM_GATEWAY_PODS;
}
export function getGatewayUrl(guildId: string): string {
const podOrdinal = getGatewayPodForGuild(guildId);
// k8s headless service DNS
return `http://gateway-${podOrdinal}.gateway-internal:3000`;
}Request Routing
// In HTTP service handlers
import { getGatewayUrl } from './guildRouting';
async function routeToGateway(guildId: string, path: string, options: RequestInit) {
const gatewayUrl = getGatewayUrl(guildId);
return fetch(`${gatewayUrl}${path}`, options);
}Service Discovery
Uses Kubernetes headless service DNS:
- Gateway pods:
gateway-0.gateway-internal,gateway-1.gateway-internal, etc. - No external config service needed
Tasks
- Implement
getGatewayPodForGuild()deterministic hash function - Add
NUM_GATEWAY_PODSenvironment variable - Implement guild routing in HTTP handlers
- Handle cases where target pod is unavailable (retry/failover)
- Deploy
cluster/proposed/http-service.yaml(remove CONFIG_SERVICE_URL) - Configure HPA for auto-scaling (2-10 pods)
- Test request routing in staging
- Load test with multiple HTTP pods
Dependencies
- Split monolith into smaller gateway/http services #232 (SERVICE_MODE support)
References
- PR Architecture analysis: Load balancing with SQLite constraint #228 (architecture analysis)
- Closed: Build config service for guild-to-pod assignments #233 (config service - no longer needed)
Metadata
Metadata
Assignees
Labels
No labels