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 →
Strategy | Best For | Use Case | Predictability | Infrastructure Control |
---|---|---|---|---|
Random | Simple distribution | Basic load spreading, testing | Low | Medium |
Fastest Ping | Real-time applications | Performance-critical apps, gaming | Medium | Low |
Preferred + Fallback | Dedicated infrastructure | CDN with origin fallback, enterprise gateways | High | Maximum |
Round Robin | Load balancing | Even distribution across known gateways | High | High |
Static | Single gateway | Development, specific gateway requirements | Maximum | High |
← Swipe to see more →
How It Works
- Receive Gateway List: Accept the list of available gateways
- Generate Random Index: Create a random number within the gateway list range
- Select Gateway: Choose the gateway at the random index
- Apply Filters: Optionally filter out unhealthy or blocked gateways
- 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
Parameter | Type | Default | Description |
---|---|---|---|
seed | number | undefined | Seed for random number generator (for reproducible results) |
weightedSelection | boolean | false | Use weighted random selection |
weights | Map<string, number> | new Map() | Gateway weights for weighted selection |
logger | Logger | defaultLogger | Optional 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
- Use for Testing: Great for testing applications with unpredictable traffic patterns
- Consider Weighted Selection: Use weights to favor higher-capacity gateways
- Combine with Other Strategies: Works well as a fallback strategy
- Set Seeds for Testing: Use reproducible randomness in test environments
- 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.