Config Design Overview
This section gives you an overview of how the router handles configuration merging and provides guidance on how you can structure your configurations effectively.Configuration Merging
Available since Router 0.221.0
- Using the
CONFIG_PATH
environment variable or in your.env
file:
- Using the
config
flag when starting the router:
Environment Variables
The router supports two types of environment variable usage:- Direct environment variable settings
- Environment variable interpolation in YAML configurations
Environment Variable Precedence
Environment variables have the lowest precedence in the configuration hierarchy:- They are loaded first
- YAML configurations are loaded after environment variables and will override any matching values
/updatedPath
because the YAML configuration overrides the environment variable. Even if the YAML configuration value is empty, as long as it’s specified the environment variable will be overridden, for example
- A base
.env
file for your common settings - An override environment file using
OVERRIDE_ENV
for environment-specific settings
OVERRIDE_ENV
file will take precedence over the base .env
file (if you have one).
Not all configuration values have corresponding environment variables. If you need to configure a value via environment variables but it’s not currently supported, do not hesitate to reach out to us.
Environment Variable Interpolation
We also allow the interpolation / expanding of environment variables in YAML’s by using the following syntaxWhen using environment variable interpolation, make sure the environment variables are set before the router starts. If an environment variable is not set, the interpolation will result in no value.
Understanding Environment Variable Interpolation
Environment variable interpolation provides a way to use environment variables as the source of truth in your configuration. This is particularly useful for:- Path configurations that might change between environments
- Sensitive values that should be managed through environment variables
- Values that need to be dynamically set during deployment
READINESS_CHECK_PATH
, you can:
- Use it directly in your YAML through interpolation (
${READINESS_CHECK_PATH}
) - Change its value through environment variables without modifying the YAML
- Maintain consistency across different environments
How Merging Works
Before we merge your configurations, we validate each YAML file against our configuration schema. This ensures everything is valid before we start combining them. Here’s how the merging process works:- The last configuration in your list has the highest priority
- If a key exists in multiple files, the last one wins
- New keys get added if they don’t exist yet
- After merging, we validate everything again to make sure all rules are followed
listen_addr
from dev.yaml overrode the one from base.yaml, while poll_interval
carried forward since it wasn’t overridden.
If we add another file after dev.yaml:
Important Note: When dealing with lists in your YAML configurations, the entire list gets replaced rather than merged. This is especially important to remember when your list elements have a key
attribute.
Here’s an example using a simple array
base.yaml
Simple scalar values can also be represented as the following in YAML, they are technically the same as the above syntax.
watch_interval
cannot be present when watch
is false
. While each individual configuration file is valid, the merged result becomes invalid because watch_interval
from the base config remains even though watch
is set to false
in the dev config.
To handle this scenario, you have two options:
- Recommended Approach: Set
watch: false
as the default in your base configuration and explicitly enable it where needed:
- Alternative Approach: Create separate configurations for different watch settings:
Hot Config Reloading
When you enable hot config reloading, we’ll watch all your configuration files for changes. Whenever any configuration changes, we’ll reprocess everything and rebuild your final merged configuration.The router does not support reloading of .env files at this moment, and any .env change will require you to restart the router.
Designing Configurations
Before configuration merging, you had to use one big configuration file, which could get messy and hard to maintain. Now you can split your configurations into smaller, more manageable pieces. Here are some ways you can structure your configurations:Environment-Based Structure
This is a common approach where you split your configs by environment:Feature-Based Structure
You can also organize your configs by feature:Note: When using this approach, be careful not to accidentally duplicate attributes across different files. For example, if you define a rate limiting attribute in bothrate_limit.yaml
andtelemetry.yaml
, the one intelemetry.yaml
will override the other.