Bird
Raised Fist0
Microservicessystem_design~7 mins

Environment-based configuration in Microservices - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Problem Statement
When microservices run in different environments like development, testing, and production, hardcoding configuration values causes errors and delays. If the same configuration is used everywhere, sensitive data might leak or services might misbehave due to environment-specific differences.
Solution
Environment-based configuration separates settings by environment, loading the right values automatically when a service starts. This lets each microservice adapt to its environment without code changes, using environment variables or config files managed outside the codebase.
Architecture
Development
Environment
Microservice
Instance

This diagram shows three environments each with its own configuration store. Microservice instances in each environment load their specific configuration at startup.

Trade-offs
✓ Pros
Enables safe separation of sensitive and environment-specific data.
Allows deploying the same codebase across environments without changes.
Simplifies debugging by isolating environment differences.
Supports automation and continuous deployment pipelines.
✗ Cons
Requires managing multiple configuration sources, increasing operational complexity.
Misconfiguration risks if environment variables or config files are inconsistent.
Debugging configuration issues can be harder if environment context is unclear.
Use when deploying microservices across multiple environments with different settings, especially when sensitive data or endpoints vary. Recommended for systems with automated CI/CD pipelines and multiple deployment stages.
Avoid if the system is a single environment or very small scale (e.g., local development only) where configuration complexity outweighs benefits.
Real World Examples
Netflix
Netflix uses environment-based configuration to separate streaming service settings between staging and production, ensuring safe testing without impacting live users.
Uber
Uber manages different API endpoints and feature flags per environment using environment-based configuration to safely roll out new features.
Shopify
Shopify uses environment-based configuration to handle database credentials and third-party service keys separately for development, testing, and production.
Code Example
The before code hardcodes sensitive and environment-specific values, making it unsafe and inflexible. The after code reads configuration from environment variables, allowing different values per environment without code changes.
Microservices
### Before: Hardcoded configuration
class ServiceConfig:
    DATABASE_URL = "postgres://prod-db:5432/app"
    API_KEY = "prod-secret-key"

### After: Environment-based configuration
import os

class ServiceConfig:
    DATABASE_URL = os.getenv("DATABASE_URL", "postgres://localhost:5432/app")
    API_KEY = os.getenv("API_KEY", "default-key")

# Usage example
config = ServiceConfig()
print(f"DB URL: {config.DATABASE_URL}")
print(f"API Key: {config.API_KEY}")
OutputSuccess
Alternatives
Feature Flags
Feature flags toggle features on or off dynamically rather than changing environment-specific settings.
Use when: Choose when you want to control feature availability without redeploying or changing environment configs.
Centralized Configuration Service
Centralized services provide dynamic configuration management at runtime instead of static environment-based configs.
Use when: Choose when you need real-time config updates and centralized control across many services.
Summary
Hardcoding configuration causes errors and security risks across environments.
Environment-based configuration loads settings specific to each environment automatically.
This pattern improves safety, flexibility, and supports automated deployments.

Practice

(1/5)
1. What is the main purpose of environment-based configuration in microservices?
easy
A. To store configuration only in the database
B. To hardcode all settings inside the service code
C. To make services dependent on a single environment
D. To separate configuration settings from code for flexibility

Solution

  1. Step 1: Understand configuration separation

    Environment-based configuration means keeping settings like URLs, keys, and flags outside the code so they can change without rewriting code.
  2. Step 2: Identify the benefit in microservices

    This separation allows microservices to adapt easily to different environments (development, testing, production) without code changes.
  3. Final Answer:

    To separate configuration settings from code for flexibility -> Option D
  4. Quick Check:

    Configuration separation = Flexibility [OK]
Hint: Configuration outside code means flexibility across environments [OK]
Common Mistakes:
  • Thinking configuration must be hardcoded
  • Assuming one config fits all environments
  • Storing config only in databases
2. Which of the following is the correct way to access an environment variable named DB_HOST in a microservice using Node.js?
easy
A. process.env.DB_HOST
B. env.DB_HOST()
C. getEnv('DB_HOST')
D. System.getenv('DB_HOST')

Solution

  1. Step 1: Recall Node.js environment variable syntax

    In Node.js, environment variables are accessed via the global object process.env.
  2. Step 2: Match the correct syntax

    The correct way to get DB_HOST is process.env.DB_HOST. Other options are invalid or from other languages.
  3. Final Answer:

    process.env.DB_HOST -> Option A
  4. Quick Check:

    Node.js env var = process.env.VAR [OK]
Hint: Node.js env vars use process.env.VAR_NAME [OK]
Common Mistakes:
  • Using function calls like getEnv() which don't exist
  • Confusing syntax with other languages like Java
  • Trying to access env vars without process.env
3. Given this Python snippet in a microservice startup:
import os

env = os.getenv('ENVIRONMENT', 'development')
if env == 'production':
    db_url = os.getenv('PROD_DB_URL')
else:
    db_url = os.getenv('DEV_DB_URL')
print(db_url)

What will be printed if ENVIRONMENT is not set and DEV_DB_URL is set to "localhost:5432/dev"?
medium
A. "localhost:5432/prod"
B. None
C. "localhost:5432/dev"
D. Error: ENVIRONMENT not set

Solution

  1. Step 1: Check default environment value

    The code uses os.getenv('ENVIRONMENT', 'development'), so if ENVIRONMENT is missing, it defaults to 'development'.
  2. Step 2: Determine which DB URL is selected

    Since env is 'development', the else branch runs, setting db_url to os.getenv('DEV_DB_URL'), which is "localhost:5432/dev".
  3. Final Answer:

    "localhost:5432/dev" -> Option C
  4. Quick Check:

    Default env 'development' selects DEV_DB_URL [OK]
Hint: Default env triggers DEV_DB_URL print [OK]
Common Mistakes:
  • Assuming ENVIRONMENT must be set or error occurs
  • Confusing production and development branches
  • Expecting None if variable missing
4. A microservice fails to load its configuration from environment variables and crashes. Which is the most likely cause?
medium
A. The service code has a syntax error unrelated to config
B. Environment variables were not set before service startup
C. The service uses hardcoded values instead of env vars
D. The database is down

Solution

  1. Step 1: Understand environment variable loading

    Microservices read environment variables at startup. If variables are missing, config loading fails.
  2. Step 2: Identify cause of crash

    If env vars are not set before starting, the service cannot find needed config and may crash or error out.
  3. Final Answer:

    Environment variables were not set before service startup -> Option B
  4. Quick Check:

    Missing env vars cause config load failure [OK]
Hint: Set env vars before starting service [OK]
Common Mistakes:
  • Blaming unrelated syntax errors
  • Assuming hardcoded values cause crashes
  • Confusing database issues with config loading
5. You have a microservice deployed in three environments: development, staging, and production. You want to use environment-based configuration to manage database URLs securely and avoid code changes. Which approach is best?
hard
A. Use environment variables for DB URLs and load them at startup
B. Store all DB URLs in code and comment/uncomment per environment
C. Use a single DB URL for all environments to simplify config
D. Hardcode production DB URL and pass dev/staging URLs as query params

Solution

  1. Step 1: Evaluate configuration management options

    Hardcoding or commenting code per environment is error-prone and unsafe. Using a single DB URL ignores environment differences.
  2. Step 2: Choose secure, flexible best practice

    Using environment variables allows each environment to have its own DB URL securely without code changes, loaded at service startup.
  3. Final Answer:

    Use environment variables for DB URLs and load them at startup -> Option A
  4. Quick Check:

    Env vars for config = secure + flexible [OK]
Hint: Env vars separate config from code for all environments [OK]
Common Mistakes:
  • Hardcoding config in code for each environment
  • Using same DB URL everywhere ignoring environment needs
  • Passing sensitive info in query parameters