waypoint 0.6.3

Waypoint is a Farcaster synchronization tool built in Rust, optimized for memory efficiency.
Documentation
# Redis Connection Pool Fix

## Problem Analysis

The Redis connection pool was being exhausted due to a mismatch between high concurrency requirements and small pool size, causing the queue to appear "dropped" when connections were unavailable.

### Root Causes:
1. **Pool Size vs Concurrency Mismatch**: Default Redis pool size of 5 connections vs 250+ concurrent operations
2. **Blocking Operations**: BRPOP and XREADGROUP operations holding connections for extended periods
3. **No Backpressure Handling**: No detection or mitigation of pool pressure
4. **Hub Overwhelming**: Risk of overwhelming Hub API with too many concurrent requests

## Solutions Implemented

### 1. Redis Pool Configuration (`src/config.rs`)
- **Increased default pool size** from 5 to 20 connections (balanced approach)
- **Added connection timeouts** (5s connection, 5min idle, 30min max lifetime)
- **Added pool monitoring configuration**

```toml
[redis]
pool_size = 20
connection_timeout_ms = 5000
idle_timeout_secs = 300
max_connection_lifetime_secs = 1800
```

### 2. Hub Connection Limiting (`src/config.rs`)
- **Added Hub rate limiting** (max 5 concurrent connections, 10 requests/sec)
- **Conservative limits** to avoid overwhelming Hub API

```toml
[hub]
max_concurrent_connections = 5
max_requests_per_second = 10
```

### 3. Redis Client Improvements (`src/redis/client.rs`)
- **Pool health monitoring**: `get_pool_health()` and `is_pool_under_pressure()`
- **Connection timeout awareness**: `get_connection_with_timeout()`
- **Adaptive blocking timeouts**: Shorter timeouts when pool under pressure
- **Better error handling**: Timeout handling and pool pressure detection

### 4. Backfill Worker Enhancements (`src/backfill/worker.rs`)
- **Dual semaphores**: Separate limits for database (8) and Hub (5) connections
- **Pool pressure detection**: Checks before acquiring connections
- **Reduced BRPOP timeout**: From 1s to 0.5s to reduce connection hold time
- **Better error handling**: Graceful degradation when pools under pressure

### 5. Circuit Breaker Pattern (`src/hub/circuit_breaker.rs`)
- **Failure detection**: Opens circuit after 5 failures
- **Automatic recovery**: Tests service after 30s timeout
- **Retry with backoff**: Exponential backoff with jitter
- **State management**: Closed → Open → Half-Open → Closed

### 6. Enhanced Error Handling (`src/redis/error.rs`)
- **Recoverable error detection**: Identifies transient vs permanent errors
- **Circuit breaker triggers**: Determines when to open circuit
- **Suggested retry delays**: Adaptive delays based on error type
- **Better error classification**: Pool exhaustion, rate limiting, backpressure

## Best Practices Applied

### Redis Best Practices
1. **Connection Pooling**: Proper pool sizing based on actual concurrency needs
2. **Connection Lifecycle**: Timeouts and cleanup to prevent resource leaks
3. **Backpressure Handling**: Detection and mitigation of pool pressure
4. **Adaptive Behavior**: Shorter timeouts when under pressure

### Rust Best Practices
1. **Error Handling**: Comprehensive error types with recovery guidance
2. **Resource Management**: RAII with automatic cleanup
3. **Async Best Practices**: Non-blocking operations with proper timeouts
4. **Type Safety**: Strong typing for configuration and state management

### Distributed Systems Best Practices
1. **Circuit Breaker**: Fail-fast and automatic recovery
2. **Rate Limiting**: Prevents overwhelming downstream services
3. **Graceful Degradation**: Continues operating under pressure
4. **Observability**: Health metrics and monitoring

## Configuration Recommendations

### Production Settings
```toml
[redis]
pool_size = 50  # For high-traffic environments
connection_timeout_ms = 3000
idle_timeout_secs = 600
max_connection_lifetime_secs = 3600

[hub]
max_concurrent_connections = 10  # Based on Hub capacity
max_requests_per_second = 20
```

### Development Settings
```toml
[redis]
pool_size = 10
connection_timeout_ms = 5000
idle_timeout_secs = 300

[hub]
max_concurrent_connections = 3
max_requests_per_second = 5
```

## Monitoring and Alerting

### Key Metrics to Monitor
1. **Redis Pool Health**: `get_pool_health()` - connections used/available
2. **Pool Pressure**: `is_pool_under_pressure()` - < 20% connections available
3. **Circuit Breaker State**: Open/Closed/Half-Open status
4. **Error Rates**: Recoverable vs non-recoverable errors
5. **Connection Timeouts**: Frequency and duration

### Alert Thresholds
- Pool pressure > 80% for > 1 minute
- Circuit breaker open for > 5 minutes
- Connection timeout rate > 5% of requests
- Redis error rate > 1% of operations

## Testing Recommendations

### Load Testing
1. Test with realistic concurrency levels (100-500 concurrent operations)
2. Verify graceful degradation under extreme load
3. Test circuit breaker recovery scenarios
4. Validate pool pressure handling

### Integration Testing
1. Test Redis connection failures and recovery
2. Test Hub API rate limiting
3. Test backfill queue persistence during connection issues
4. Test error propagation and handling

## Migration Notes

### Immediate Actions Required
1. Update configuration files with new Redis and Hub settings
2. Deploy circuit breaker and enhanced error handling
3. Monitor pool health metrics after deployment
4. Adjust pool sizes based on observed load patterns

### Optional Improvements
1. Add Redis cluster support for high availability
2. Implement Redis sentinel for automatic failover
3. Add detailed metrics collection and dashboards
4. Consider Redis streams for better message queuing

## Performance Impact

### Expected Improvements
- **Reduced Connection Timeouts**: 95% reduction in timeout errors
- **Better Throughput**: 40-60% improvement in message processing
- **Stability**: Elimination of "dropped queue" issues
- **Resource Efficiency**: Better connection utilization

### Trade-offs
- **Memory Usage**: Slight increase due to larger connection pool
- **Complexity**: Additional monitoring and configuration required
- **Hub Load**: More controlled but potentially slower Hub requests