Skip to main content
The Exchange platform is configured using environment variables. This guide documents all available configuration options.

Environment file setup

The platform uses .env files for configuration. Two example files are provided:
  • Project root - .env.example for local development
  • Docker directory - docker/.env.example for containerized deployment

Local development

cp .env.example .env

Docker deployment

cd docker
cp .env.example .env

Configuration categories

Database configuration

PostgreSQL database connection settings.
PG__USER
string
required
PostgreSQL username for authentication.Default: root
PG__USER=root
PG__PASSWORD
string
required
PostgreSQL password for authentication.Default: root
Use a strong password in production!
PG__PASSWORD=your-secure-password
PG__HOST
string
required
PostgreSQL server hostname.Local development: localhostDocker deployment: exchange-postgres
PG__HOST=localhost
PG__PORT
number
required
PostgreSQL server port.Default: 5000 (local) or 5432 (Docker)
PG__PORT=5000
PG__DBNAME
string
required
PostgreSQL database name.Default: exchange-db
PG__DBNAME=exchange-db
PG__POOL_MAX_SIZE
number
default:"16"
Maximum number of connections in the database connection pool.Adjust based on expected load and server capacity.
PG__POOL_MAX_SIZE=16
DATABASE_URL
string
required
Complete PostgreSQL connection string used by SQLx.Format: postgres://user:password@host:port/database
DATABASE_URL=postgres://${PG__USER}:${PG__PASSWORD}@${PG__HOST}:${PG__PORT}/${PG__DBNAME}
This variable uses variable substitution from the other PG__* variables.

Docker-specific database variables

These variables are used in Docker Compose for PostgreSQL container setup:
POSTGRES_HOST
string
PostgreSQL container hostname.
POSTGRES_HOST=exchange-postgres
POSTGRES_DB
string
PostgreSQL database name (Docker container).
POSTGRES_DB=exchange-db
POSTGRES_USER
string
PostgreSQL username (Docker container).
POSTGRES_USER=root
POSTGRES_PASSWORD
string
PostgreSQL password (Docker container).
POSTGRES_PASSWORD=root
POSTGRES_PORT
number
PostgreSQL internal port in Docker.
POSTGRES_PORT=5432

Redis configuration

Redis is used for pub/sub messaging and caching.
REDIS_URL
string
required
Complete Redis connection URL.Local development: redis://localhost:6380Docker deployment: redis://exchange-redis:6379
REDIS_URL=redis://exchange-redis:6379

Service configuration

SERVER_ADDR
string
default:"0.0.0.0:8080"
Router (REST API) server bind address and port.Format: host:port
SERVER_ADDR=0.0.0.0:8080
WS_STREAM_URL
string
default:"0.0.0.0:4000"
WebSocket streaming service bind address and port.Format: host:port
WS_STREAM_URL=0.0.0.0:4000

Build-time configuration

SQLX_OFFLINE
boolean
default:"true"
Controls SQLx compile-time query verification.Values:
  • true - Skip compile-time query verification (faster builds)
  • false - Verify queries against database at compile time
SQLX_OFFLINE=true
In Dockerfiles, this is set to true to enable offline builds without database access.
RUST_LOG
string
default:"info"
Rust logging level.Values: trace, debug, info, warn, error
RUST_LOG=info

Configuration examples

Local development

.env
# Database
PG__USER=root
PG__PASSWORD=root
PG__HOST=localhost
PG__PORT=5000
PG__DBNAME=exchange-db
PG__POOL_MAX_SIZE=16

DATABASE_URL=postgres://${PG__USER}:${PG__PASSWORD}@${PG__HOST}:${PG__PORT}/${PG__DBNAME}

# Redis
REDIS_URL=redis://localhost:6380

# Services
SERVER_ADDR=0.0.0.0:8080
WS_STREAM_URL=0.0.0.0:4000

# Logging
RUST_LOG=debug

Docker deployment

docker/.env
# PostgreSQL Docker container
POSTGRES_HOST=exchange-postgres
POSTGRES_DB=exchange-db
POSTGRES_USER=root
POSTGRES_PASSWORD=root
POSTGRES_PORT=5432

# Service configuration
SERVER_ADDR=0.0.0.0:8080
WS_STREAM_URL=0.0.0.0:4000

# Redis
REDIS_URL=redis://exchange-redis:6379

# Database connection for services
PG__USER=root
PG__PASSWORD=root
PG__HOST=exchange-postgres
PG__PORT=5432
PG__DBNAME=exchange-db
PG__POOL_MAX_SIZE=16

DATABASE_URL=postgres://${PG__USER}:${PG__PASSWORD}@${PG__HOST}:${PG__PORT}/${PG__DBNAME}

Production deployment

.env
# Database - use strong credentials!
PG__USER=exchange_prod_user
PG__PASSWORD=<STRONG_RANDOM_PASSWORD>
PG__HOST=production-db-host
PG__PORT=5432
PG__DBNAME=exchange_production
PG__POOL_MAX_SIZE=32

DATABASE_URL=postgres://${PG__USER}:${PG__PASSWORD}@${PG__HOST}:${PG__PORT}/${PG__DBNAME}

# Redis - consider Redis cluster for HA
REDIS_URL=redis://production-redis-host:6379

# Services
SERVER_ADDR=0.0.0.0:8080
WS_STREAM_URL=0.0.0.0:4000

# Logging - use warn or error in production
RUST_LOG=warn

Environment validation

The Exchange platform uses the confik crate for configuration management with automatic validation. If required environment variables are missing, services will fail to start with clear error messages.

Security considerations

Follow these security best practices:
  • Never commit .env files to version control
  • Use strong passwords in production (minimum 20 characters)
  • Rotate credentials regularly
  • Use secrets management tools (AWS Secrets Manager, HashiCorp Vault, etc.)
  • Restrict database access to application servers only
  • Enable SSL/TLS for database and Redis connections in production

SQLx offline mode

The platform uses SQLx’s offline mode for Docker builds:

Preparing the query cache

If you modify database queries, regenerate the query cache:
SQLX_OFFLINE=false cargo sqlx prepare --workspace
This creates/updates files in the .sqlx directory.

Building with offline mode

SQLX_OFFLINE=true cargo build
Or set it in your .env:
SQLX_OFFLINE=true

PostgreSQL tuning

Max connections

In Docker Compose, PostgreSQL is configured with:
command: -c 'max_connections=800'
Adjust based on:
  • Number of service instances
  • PG__POOL_MAX_SIZE setting
  • Expected concurrent load
Formula: max_connections >= (number_of_services × pool_size) + 10

Shared memory

shm_size: 1gb
Increase for better performance with large datasets.

Redis tuning

Persistence settings

command: redis-server --save 20 1 --loglevel warning
Explanation:
  • --save 20 1 - Save to disk if at least 1 key changed in 20 seconds
  • --loglevel warning - Reduce log verbosity
For high-write workloads, consider:
command: redis-server --save 60 1000 --loglevel warning

Troubleshooting

Database connection fails

Verify your configuration:
# Check if variables are set
echo $DATABASE_URL

# Test connection
psql $DATABASE_URL -c "SELECT 1;"

Redis connection fails

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

Environment variables not loading

Ensure you’re loading the .env file:
# The project uses dotenvy to load .env automatically
# If using docker-compose, ensure env_file is specified
docker compose config  # Verify env vars are loaded

Next steps

Build docs developers (and LLMs) love