Skip to content

Configuration management and multiple environments

Introduction

Things that vary by environment, such as database addresses, keys, and switches, are not hardcoded but read from outside at runtime.

Why use it

  • One build, many places: the same build artifact can run in development, test, and production.
  • Safer: passwords and tokens stay out of the repository (see Secrets management).
  • Change config without changing code: adjust timeouts and feature switches without republishing.

This is also a core principle of the Twelve-factor app: store config in the environment.

How to do it

  1. Separate environments: at least development (dev), test (staging), and production (prod), each with its own database and secrets. The test environment must never connect to production data.
  2. Layered overrides: safe defaults in code → non-sensitive differences in config files → environment-specific values in environment variables → sensitive values in a secrets manager. Later sources override earlier ones.
  3. Validate on startup: if a required setting is missing or malformed, make the service fail to start, instead of failing halfway through a request.
  4. Strong typing: read config into typed objects (an integer is an integer); do not scatter string keys everywhere.
  5. Keep secrets out of the repo: add .env to .gitignore; keep only a .env.example sample in the repository.

Component comparison

Environment variables + .env files

Built into the frameworkOpsLow

Put passwords, addresses, and other settings in environment variables: written in a .env file for local development and supplied by the server or deployment platform in production.

Best forSmall projects with few settings, and containerized deployments.

Avoid whenThere are many settings with complex hierarchy, or configuration must be changed dynamically at runtime.

Pros

Simple and direct, supported by every language and platform

Code and configuration are separate, so the same code runs in different environments

Works naturally with Docker and all kinds of deployment platforms

Cons

With many settings it gets messy, lacking structure and type checking

.env files contain passwords, which leak if accidentally committed to the code repository

Changing configuration usually requires restarting the program to take effect

Per-environment configuration files (appsettings.{Environment}.json)

Built into the frameworkOpsLow

The built-in .NET approach: one common configuration file plus one each for "development, testing, production", layered automatically according to the current environment.

Best forProjects using ASP.NET Core where configuration differs noticeably between environments.

Avoid whenThe project is not C#.

Pros

Built into .NET, nothing extra to install

Configuration is hierarchical and can be bound to strongly typed classes

Environment variables can also override values, which makes adjusting passwords in production convenient

Cons

Passwords must not be written directly in files committed to the repository, so separate secrets management is needed

Multi-environment files are easy to forget to update or let drift out of sync

Only applicable to C# / .NET projects

pydantic-settings

Self-hosted open sourceOpen sourceOpsLow

A Python configuration library: define configuration as a typed class, which automatically reads from environment variables or .env and checks the format.

Best forPython projects, especially services using FastAPI.

Avoid whenThe project is not Python, or has very little configuration and reading environment variables directly is enough.

Pros

Missing or malformed configuration is detected at startup, so problems surface early

Configuration is typed, giving hints when used in code

Supports both environment variables and .env files

Cons

Requires installing an extra dependency and writing a settings class

People unfamiliar with pydantic need to learn its basics first

Only applicable to Python projects

Official docs ↗

Configuration center (Nacos / Apollo)

Self-hosted open sourceOpen sourceOpsHigh

Store configuration centrally in one service that programs pull from at startup, and changes can take effect without restarting.

Best forMid-sized and large teams with many services whose configuration changes frequently.

Avoid whenSmall projects with only one or two services.

Pros

Configuration for many services is viewed and changed in one place

It supports live updates while running, and keeps a change history

Permissions can be split by environment and by application

Cons

This service must stay highly available, or a failure blocks startup

Client maturity differs by language, so you have to check it yourself

On a small project the maintenance cost outweighs the benefit

Official docs ↗

Cloud provider configuration service

Cloud-managedPay-as-you-goOpsLow

A managed configuration and secrets service from a cloud provider: configuration lives in the cloud and programs read it according to their permissions.

Best forProjects whose services are already on the same cloud and that have higher requirements for password safekeeping.

Avoid whenYou do not want cloud lock-in, or configuration is minimal and environment variables are enough.

Pros

No need to build or maintain anything yourself

Usually has access control and access logs, suitable for storing sensitive information such as passwords

Easy to connect with servers and deployment platforms on the same cloud

Cons

Tied to the cloud provider, so reading code must change when switching clouds

May bill by call count or number of configuration entries

Local development requires network access or a separate mock, which is less convenient than .env

Validate environment variables with zod

Open-source libraryOpen sourceOpsLow

Parse and validate environment variables with zod at startup; missing or malformed values make startup fail immediately.

Best forConfiguration management in any TypeScript backend.

Avoid whenThere is already a unified configuration center that injects typed configuration.

Pros

Configuration errors surface at startup instead of blowing up at runtime

Produces a typed configuration object

Shares one library with request validation

Cons

You must write the loading and layering logic yourself

Provides no configuration center capability

Needs discipline to avoid reading process.env directly everywhere

Official docs ↗

figment / environment variables

Open-source libraryOpen sourceOpsLow

Bind environment variables and config files to a typed config with figment or envy, and fail startup when values are missing or malformed.

Best forConfiguration management in any Rust project.

Avoid whenA configuration center already injects typed config.

Pros

Missing or malformed config fails at startup

You get a typed config struct

Environment variables can override file values

Cons

You have to be clear about source precedence

Sensitive values still need secrets management

No configuration-center feature

Official docs ↗

Bind env vars to a struct

Open-source libraryOpen sourceOpsLow

Parse environment variables into a typed config struct using struct tags, validating required fields at startup.

Best forConfiguration management in any Go backend.

Avoid whenThere is already a unified configuration center.

Pros

Light dependency and simple to write

Startup fails immediately if a required field is missing

Configuration is centralized in a single struct

Cons

Provides no configuration center capability

Tag syntax for nested configuration needs conventions

Needs discipline to avoid calling os.Getenv directly everywhere

Official docs ↗

Checklist

  • Development, test, and production are isolated and do not share databases or secrets
  • Config is not hardcoded; it is injected per environment
  • Secrets are not in the Git repository; .env is ignored
  • Required config is validated at startup; missing values refuse to start
  • Config is read into strongly typed objects
  • Logs never print sensitive configuration