Scalability Considerations

11.1 Horizontal Scaling

ComponentScaling StrategyCapacity
HTTP RouterAutomatic (Workers)Unlimited concurrent requests
TCP ConnectionsOne DO per workload~100 connections per DO
Price CollectionSingle global DO with shardingN/A (centralized)
Health MonitorsOne DO per workload10,000+ DOs supported
D1 DatabaseSingle database, sharding if needed10GB per database
KVAutomatic replication25M keys per namespace

11.2 Handling 10,000+ Concurrent Workloads

// Workload distribution strategy
// 1. Each workload gets its own Durable Object for health monitoring
// 2. KV used for routing (globally replicated)
// 3. D1 used for persistent state (supports high read throughput)

// Example: Workload distribution across DOs
function getHealthMonitorDO(env: Env, workloadId: string): DurableObjectStub {
  // Each workload has its own DO instance
  // DOs are automatically distributed across Cloudflare's network
  const id = env.HEALTH_MONITOR.idFromName(workloadId);
  return env.HEALTH_MONITOR.get(id);
}

// Price aggregation with multiple collectors
function getPriceCollectorDO(env: Env, provider: string, region: string): DurableObjectStub {
  // Shard price collection by provider-region to distribute load
  const shardKey = `${provider}:${region}`;
  const id = env.PRICE_COLLECTOR.idFromName(shardKey);
  return env.PRICE_COLLECTOR.get(id);
}

11.3 Rate Limiting

// Rate limiting using KV with atomic operations
export async function checkRateLimit(
  env: Env,
  customerId: string,
  action: string,
  limit: number,
  windowSeconds: number
): Promise<{ allowed: boolean; remaining: number; resetAt: number }> {
  const window = Math.floor(Date.now() / (windowSeconds * 1000));
  const key = `ratelimit:${customerId}:${action}:${window}`;
  
  // Get current count
  const current = await env.ROUTING_KV.get(key, 'json') || { count: 0 };
  
  if (current.count >= limit) {
    return {
      allowed: false,
      remaining: 0,
      resetAt: (window + 1) * windowSeconds * 1000,
    };
  }
  
  // Increment count
  await env.ROUTING_KV.put(key, JSON.stringify({ count: current.count + 1 }), {
    expirationTtl: windowSeconds,
  });
  
  return {
    allowed: true,
    remaining: limit - current.count - 1,
    resetAt: (window + 1) * windowSeconds * 1000,
  };
}

// Rate limits by tier
const rateLimits = {
  free: {
    'workloads:create': { limit: 10, window: 3600 },
    'api:requests': { limit: 1000, window: 3600 },
  },
  pro: {
    'workloads:create': { limit: 100, window: 3600 },
    'api:requests': { limit: 10000, window: 3600 },
  },
  enterprise: {
    'workloads:create': { limit: 1000, window: 3600 },
    'api:requests': { limit: 100000, window: 3600 },
  },
};