RandomRoutingStrategy

Overview

The RandomRoutingStrategy selects gateways randomly from the available pool. This strategy provides simple load distribution without maintaining state or performing complex calculations, making it ideal for scenarios where unpredictability is desired or where simplicity is paramount.

Routing Strategy Comparison

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

How It Works

  1. Receive Gateway List: Accept the list of available gateways
  2. Generate Random Index: Create a random number within the gateway list range
  3. Select Gateway: Choose the gateway at the random index
  4. Apply Filters: Optionally filter out unhealthy or blocked gateways
  5. Return Selection: Return the randomly selected gateway

Configuration

Basic Usage

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

const strategy = new RandomRoutingStrategy()

With Weighted Selection

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

Parameters

ParameterTypeDefaultDescription
seednumberundefinedSeed for random number generator (for reproducible results)
weightedSelectionbooleanfalseUse weighted random selection
weightsMap<string, number>new Map()Gateway weights for weighted selection
loggerLoggerdefaultLoggerOptional logger instance

Integration with Wayfinder

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

const wayfinder = new Wayfinder({
  routingSettings: {
    strategy: new RandomRoutingStrategy(),
  },
})

Use Cases

Load Testing and Development

Perfect for testing applications with unpredictable gateway selection patterns.

const testStrategy = new RandomRoutingStrategy({
  seed: 42, // Reproducible randomness for testing
})

Simple Load Distribution

Ideal for applications that need basic load spreading without complex logic.

const loadStrategy = new RandomRoutingStrategy({
  weightedSelection: true,
  weights: new Map([
    ['https://primary-gateway.com', 3],
    ['https://secondary-gateway.com', 1],
  ]),
})

Backup Strategy

Useful as a fallback strategy when other routing methods fail.

import {
  PreferredWithFallbackRoutingStrategy,
  RandomRoutingStrategy,
} from '@ar.io/wayfinder-core'

const strategy = new PreferredWithFallbackRoutingStrategy({
  preferredGateway: 'https://my-gateway.com',
  fallbackStrategy: new RandomRoutingStrategy(),
})

Best Practices

  1. Use for Testing: Great for testing applications with unpredictable traffic patterns
  2. Consider Weighted Selection: Use weights to favor higher-capacity gateways
  3. Combine with Other Strategies: Works well as a fallback strategy
  4. Set Seeds for Testing: Use reproducible randomness in test environments
  5. Monitor Distribution: Track which gateways are selected over time

The RandomRoutingStrategy is ideal for applications that need simple, unpredictable load distribution or as a fallback strategy for more complex routing methods.

Was this page helpful?