Skip to content

Implement HTTP service with guild routing #234

@vcarl

Description

@vcarl

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 lookupsDeterministic 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_PODS environment 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

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions