Skip to content

Environment Variables Configuration for Edge MySQL Gateway

Environment Variables Configuration for Edge MySQL Gateway

Section titled “Environment Variables Configuration for Edge MySQL Gateway”

This document outlines all environment variables used by the Edge SQL gateway and how to configure them for different environments.

VariableDescriptionDefaultExample
ENVIRONMENTDeployment environmentdevelopmentdevelopment, staging, production
LOG_LEVELLogging verbosity levelinfodebug, info, warn, error
MAX_SHARD_SIZE_GBMaximum size per shard in GB101 (dev), 50 (prod)
CACHE_TTL_MSCache Time-To-Live in milliseconds300005000 (dev), 300000 (prod)
CACHE_SWR_MSStale-While-Revalidate period in milliseconds12000010000 (dev), 1800000 (prod)
VariableDescriptionDefaultExample
DEFAULT_CACHE_TTLFallback cache TTL3000060000
DEFAULT_CACHE_SWRFallback cache SWR120000300000
SHARD_COUNTNumber of shards for routing48, 16, 32
[env.development.vars]
ENVIRONMENT = "development"
LOG_LEVEL = "debug"
MAX_SHARD_SIZE_GB = "1"
CACHE_TTL_MS = "5000"
CACHE_SWR_MS = "10000"
SHARD_COUNT = "4"

Purpose: Fast iteration, verbose logging, small limits

  • Short cache times for immediate feedback
  • Debug logging for troubleshooting
  • Small shard sizes to test capacity limits
[env.staging.vars]
ENVIRONMENT = "staging"
LOG_LEVEL = "info"
MAX_SHARD_SIZE_GB = "5"
CACHE_TTL_MS = "30000"
CACHE_SWR_MS = "120000"
SHARD_COUNT = "8"

Purpose: Production-like testing with moderate limits

  • Moderate cache times for realistic testing
  • Info-level logging for performance testing
  • Medium shard sizes for load testing
[env.production.vars]
ENVIRONMENT = "production"
LOG_LEVEL = "warn"
MAX_SHARD_SIZE_GB = "50"
CACHE_TTL_MS = "300000"
CACHE_SWR_MS = "1800000"
SHARD_COUNT = "16"

Purpose: Optimized for performance and cost

  • Long cache times for performance
  • Warn-level logging to reduce noise
  • Large shard sizes for efficiency

Secrets should be set using wrangler secret put and never stored in configuration files:

Terminal window
# JWT signing key for authentication
wrangler secret put JWT_SECRET
# Database encryption key (if using encryption at rest)
wrangler secret put DATABASE_ENCRYPTION_KEY
# Admin API key for management operations
wrangler secret put ADMIN_API_KEY
Terminal window
# External monitoring API keys
wrangler secret put DATADOG_API_KEY
wrangler secret put SENTRY_DSN
# Third-party integrations
wrangler secret put EXTERNAL_API_KEY

These are configured in wrangler.toml and available as environment bindings:

  • APP_CACHE: KVNamespace for caching query results and metadata
  • DB_EVENTS: Queue for asynchronous event processing (cache invalidation, etc.)
  • SHARD: DurableObjectNamespace for TableShard instances
  • PORTABLE_DB: D1Database for optional portable data mirror
Terminal window
cp wrangler.toml.template wrangler.toml
Terminal window
# Create main namespace
wrangler kv:namespace create "APP_CACHE"
# Create preview namespace for development
wrangler kv:namespace create "APP_CACHE" --preview

Update the IDs in wrangler.toml:

[[kv_namespaces]]
binding = "APP_CACHE"
id = "your-kv-namespace-id"
preview_id = "your-preview-namespace-id"
Terminal window
# Create main queue
wrangler queues create db-events
# Create dead letter queue
wrangler queues create db-events-dlq
Terminal window
wrangler d1 create portable-mirror

Update the database ID in wrangler.toml:

[[d1_databases]]
binding = "PORTABLE_DB"
database_name = "portable-mirror"
database_id = "your-d1-database-id"
Terminal window
# Set a simple JWT secret for development
echo "dev-jwt-secret-key-$(date +%s)" | wrangler secret put JWT_SECRET
# Set other required secrets
wrangler secret put DATABASE_ENCRYPTION_KEY
wrangler secret put ADMIN_API_KEY

The application validates environment variables on startup. Missing required variables will cause the worker to fail deployment.

  • ENVIRONMENT: Must be one of development, staging, production
  • LOG_LEVEL: Must be one of debug, info, warn, error
  • MAX_SHARD_SIZE_GB: Must be a positive integer
  • CACHE_TTL_MS: Must be a positive integer
  • CACHE_SWR_MS: Must be greater than CACHE_TTL_MS
  • SHARD_COUNT: Must be a power of 2 (for consistent hashing)

Invalid environment variables will:

  1. Log detailed error messages
  2. Prevent worker deployment
  3. Provide suggestions for fixes
Section titled “Recommended Environment Variables for Production”
[env.production.vars]
# ... other vars ...
ENABLE_METRICS = "true"
METRICS_SAMPLE_RATE = "0.1"
TRACE_SAMPLE_RATE = "0.01"
# Cache optimization
CACHE_COMPRESSION = "true"
CACHE_MAX_SIZE_KB = "1024"
# Connection pooling
MAX_CONCURRENT_REQUESTS = "100"
REQUEST_TIMEOUT_MS = "30000"
# Shard management
SHARD_REBALANCE_THRESHOLD = "0.8"
SHARD_HEALTH_CHECK_INTERVAL_MS = "60000"
  1. Cache not working: Check CACHE_TTL_MS and CACHE_SWR_MS values
  2. High latency: Increase cache TTL or reduce shard count
  3. Memory errors: Reduce MAX_SHARD_SIZE_GB
  4. Authentication failures: Verify JWT_SECRET is set

Enable debug mode for troubleshooting:

[env.development.vars]
LOG_LEVEL = "debug"
DEBUG_CACHE = "true"
DEBUG_ROUTING = "true"
DEBUG_AUTHENTICATION = "true"

This will provide detailed logs for all operations.