RoundRobinRoutingStrategy

Overview

The RoundRobinRoutingStrategy distributes requests evenly across all available gateways in a cyclical manner. Each gateway is selected in turn, ensuring fair load distribution and preventing any single gateway from being overwhelmed.

Routing Strategy Comparison

← Swipe to see more →
StrategyBest ForUse CasePredictabilityInfrastructure Control
Round RobinLoad balancingEven distribution across known gatewaysHighHigh
Fastest PingReal-time applicationsPerformance-critical apps, gamingMediumLow
Preferred + FallbackDedicated infrastructureCDN with origin fallback, enterprise gatewaysHighMaximum
RandomSimple distributionBasic load spreading, testingLowMedium
StaticSingle gatewayDevelopment, specific gateway requirementsMaximumHigh
← Swipe to see more →

How It Works

  1. Initialize Gateway List: Start with an ordered list of available gateways
  2. Track Current Position: Maintain a pointer to the current gateway in the rotation
  3. Select Next Gateway: Choose the next gateway in the sequence
  4. Cycle Through List: Return to the first gateway after reaching the end
  5. Handle Failures: Skip failed gateways and continue rotation

Configuration

Basic Usage

import { RoundRobinRoutingStrategy } from '@ar.io/wayfinder-core'

const strategy = new RoundRobinRoutingStrategy({
  gateways: [new URL('https://arweave.net'), new URL('https://permagate.io')],
})

With Weighted Rotation

const strategy = new RoundRobinRoutingStrategy({
  gateways: [
    new URL('https://high-capacity-gateway.com'),
    new URL('https://medium-capacity-gateway.com'),
    new URL('https://low-capacity-gateway.com'),
  ],
  weightedRotation: true,
  weights: new Map([
    ['https://high-capacity-gateway.com', 3],
    ['https://medium-capacity-gateway.com', 2],
    ['https://low-capacity-gateway.com', 1],
  ]),
})

Parameters

ParameterTypeDefaultDescription
gatewaysURL[]requiredArray of gateway URLs to rotate through
weightedRotationbooleanfalseUse weighted rotation
weightsMap<string, number>new Map()Gateway weights for weighted rotation
shuffleOnStartbooleanfalseRandomize initial gateway order
loggerLoggerdefaultLoggerOptional logger instance

Integration with Wayfinder

import { Wayfinder, RoundRobinRoutingStrategy } from '@ar.io/wayfinder-core'

const wayfinder = new Wayfinder({
  routingSettings: {
    strategy: new RoundRobinRoutingStrategy({
      gateways: [
        new URL('https://gateway-1.com'),
        new URL('https://gateway-2.com'),
        new URL('https://gateway-3.com'),
      ],
    }),
  },
})

Use Cases

Even Load Distribution

Perfect for distributing load evenly across multiple known gateways.

const loadBalanceStrategy = new RoundRobinRoutingStrategy({
  gateways: [
    new URL('https://gateway-1.com'),
    new URL('https://gateway-2.com'),
    new URL('https://gateway-3.com'),
    new URL('https://gateway-4.com'),
  ],
})

Capacity-Based Distribution

Ideal for gateways with different capacities using weighted rotation.

const capacityStrategy = new RoundRobinRoutingStrategy({
  gateways: [
    new URL('https://high-capacity.com'),
    new URL('https://medium-capacity.com'),
    new URL('https://low-capacity.com'),
  ],
  weightedRotation: true,
  weights: new Map([
    ['https://high-capacity.com', 5],
    ['https://medium-capacity.com', 3],
    ['https://low-capacity.com', 1],
  ]),
})

Predictable Testing

Useful for testing scenarios where you need predictable gateway selection.

const testStrategy = new RoundRobinRoutingStrategy({
  gateways: [
    new URL('https://test-gateway-1.com'),
    new URL('https://test-gateway-2.com'),
  ],
  shuffleOnStart: false, // Maintain order for predictable testing
})

Best Practices

  1. Define Gateway Order: Carefully consider the order of gateways in your list
  2. Use Weighted Rotation: Apply weights when gateways have different capacities
  3. Monitor Distribution: Track requests to ensure even distribution
  4. Consider Shuffling: Use shuffleOnStart to avoid predictable patterns
  5. Plan for Failures: Combine with health checking for production use

The RoundRobinRoutingStrategy is ideal for applications that need predictable, even distribution of requests across a known set of gateways.

Was this page helpful?