Skip to main content

Saga Configuration

Saga can be configured via environment variables, configuration files, or default values. Understanding the configuration precedence and options is essential for proper deployment.

Configuration Precedence

Configuration values follow this order (highest to lowest precedence):

  1. Environment variables (override everything)
  2. Config file (~/.saga/config.toml or custom path)
  3. Default values (lowest precedence)

Configuration Methods

Environment variables provide the highest precedence and are ideal for containerized deployments.

export REDIS_URL=redis://localhost:6379
export PORT=8030
export HOST=0.0.0.0
cargo run

Advantages:

  • ✅ Highest precedence
  • ✅ Easy to override in different environments
  • ✅ Works well with Docker/Kubernetes
  • ✅ No file management needed

Environment Variables Reference

Required Variables

VariableDescriptionExampleNotes
REDIS_URLRedis connection URLredis://localhost:6379Required - No default value
Missing REDIS_URL

If REDIS_URL is not set, Saga will fail to start. Always ensure this variable is configured.

Optional Variables

VariableDescriptionDefaultExampleNotes
HOSTHTTP server host0.0.0.00.0.0.0Bind to all interfaces
PORTHTTP server port80308030Must be available
SERVICE_NAMEService namesagasagaUsed in health checks
HEARTBEAT_INTERVALHeartbeat interval (seconds)3030How often to refresh TTL
REGISTRATION_TTLRegistration TTL (seconds)6060Service expiration time
TTL vs Heartbeat Interval

Set HEARTBEAT_INTERVAL to approximately half of REGISTRATION_TTL to ensure reliable service availability:

  • TTL: 60 seconds
  • Heartbeat: 30 seconds (50% of TTL)
  • Safety margin: 30 seconds buffer for network delays

Configuration File Format

Saga uses TOML format for configuration files. The default location is ~/.saga/config.toml.

Example Config File

Create ~/.saga/config.toml:

# Redis Configuration
redis_url = "redis://localhost:6379"

# Server Configuration
host = "0.0.0.0"
port = 8030

# Service Configuration
service_name = "saga"

# Registration Settings
heartbeat_interval = 30 # seconds
registration_ttl = 60 # seconds

Custom Config File Path

Specify a custom config file using the --config flag:

cargo run -- --config /path/to/custom/config.toml

Or set via environment:

export SAGA_CONFIG=/path/to/custom/config.toml
cargo run

Redis Configuration

Local Development

# Using Docker Compose
REDIS_URL=redis://redis:6379

# Using standalone Docker
REDIS_URL=redis://localhost:6379

Redis Connection String Format

redis://[username][:password]@host[:port][/database]

Examples:

# Basic connection
redis://localhost:6379

# With password
redis://:mypassword@localhost:6379

# With username and password
redis://user:pass@localhost:6379

# With database selection
redis://localhost:6379/0

# Full example
redis://user:pass@redis.example.com:6379/0
Redis Authentication

If your Redis instance requires authentication, include credentials in the URL:

REDIS_URL=redis://:yourpassword@localhost:6379

For username + password:

REDIS_URL=redis://username:password@localhost:6379

Production Configuration

For production environments, use these recommended values:

# Redis (use Redis Cluster or Sentinel for HA)
REDIS_URL=redis://redis-cluster:6379

# Server
HOST=0.0.0.0
PORT=8030

# Service
SERVICE_NAME=saga

# Registration (longer TTL for production)
HEARTBEAT_INTERVAL=30
REGISTRATION_TTL=120 # 2 minutes for production

High Availability Setup

For high availability configurations:

# Redis Sentinel configuration
REDIS_URL=redis-sentinel://sentinel1:26379,sentinel2:26379,sentinel3:26379/mymaster

Benefits:

  • ✅ Automatic failover
  • ✅ High availability
  • ✅ No single point of failure
Production TTL Settings

For production, increase REGISTRATION_TTL to account for:

  • Network partitions
  • Temporary service restarts
  • Load balancer health checks

Recommended:

  • TTL: 120 seconds (2 minutes)
  • Heartbeat: 30 seconds
  • Safety margin: 90 seconds buffer

Configuration Examples

Development Environment

# .env file for development
REDIS_URL=redis://localhost:6379
PORT=8030
HOST=0.0.0.0
REGISTRATION_TTL=60
HEARTBEAT_INTERVAL=30

Usage:

# Load from .env file
source .env
cargo run

# Or use dotenvy
dotenvy::dotenv().ok();

Production Environment

# Production environment variables
export REDIS_URL=redis://redis-cluster:6379
export PORT=8030
export HOST=0.0.0.0
export REGISTRATION_TTL=120
export HEARTBEAT_INTERVAL=30
export SERVICE_NAME=saga

Docker Compose

services:
saga:
image: saga:latest
environment:
- REDIS_URL=redis://redis:6379
- PORT=8030
- HOST=0.0.0.0
- REGISTRATION_TTL=120
- HEARTBEAT_INTERVAL=30
ports:
- "8030:8030"
depends_on:
- redis

Kubernetes ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
name: saga-config
data:
HOST: "0.0.0.0"
PORT: "8030"
SERVICE_NAME: "saga"
HEARTBEAT_INTERVAL: "30"
REGISTRATION_TTL: "120"
---
apiVersion: v1
kind: Secret
metadata:
name: saga-secrets
data:
REDIS_URL: <base64-encoded-redis-url>

Configuration Validation

Saga validates all configuration values on startup:

Validation Rules

VariableValidationError Message
REDIS_URLMust be valid Redis URL"Invalid Redis URL format"
PORTMust be 1-65535"Port must be between 1 and 65535"
HEARTBEAT_INTERVALMust be > 0"Heartbeat interval must be greater than 0"
REGISTRATION_TTLMust be > 0"Registration TTL must be greater than 0"

Validation Example

# Invalid port (too high)
export PORT=99999
cargo run
# Error: Port must be between 1 and 65535

# Invalid TTL (zero)
export REGISTRATION_TTL=0
cargo run
# Error: Registration TTL must be greater than 0

Troubleshooting Configuration

Redis Connection Issues

# Test Redis connection
redis-cli -u $REDIS_URL ping

# Should return: PONG

Port Already in Use

# Find process using port
lsof -i :8030

# Kill the process
kill -9 <PID>

# Or use different port
export PORT=8031
cargo run

Configuration Not Loading

# Check config file exists
ls -la ~/.saga/config.toml

# Validate TOML syntax
toml-cli validate ~/.saga/config.toml

# Use verbose logging
RUST_LOG=saga=debug cargo run

Best Practices

Configuration Best Practices
  1. Use environment variables in production - More secure and flexible
  2. Use config files for development - Easier to manage locally
  3. Never commit secrets - Use environment variables or secrets management
  4. Validate before deployment - Test configuration in staging first
  5. Document your settings - Keep a record of production configuration

Next Steps