Configuration Files

Configuration files keep structured application settings outside source code so deployments can vary behavior without code changes. A Python service should parse and validate settings deliberately, define precedence among sources, and protect sensitive values.

Configuration files store settings separately from application code. This lets an application use different values in development, testing, and production without changing its implementation. Common formats include INI, TOML, YAML, and JSON; format choice affects parsing and features, not the need to validate the result.

A Python service commonly loads configuration during startup, then passes the resulting settings to the parts of the program that need them. Startup loading makes missing or malformed settings easier to detect before handling requests. Python's standard library includes parsers such as `configparser` for INI and `json` for JSON; other formats may require third-party packages.

Parsed values are not automatically safe or meaningful just because parsing succeeded. Validate required keys, types, ranges, and relationships—for example, ensure a port is an integer in an allowed range. Supply documented defaults only when appropriate, and fail clearly when a required setting is absent or invalid.

Applications may merge settings from multiple sources, such as defaults, a config file, and environment variables. Define and document precedence so operators know which value wins. Avoid logging secrets, restrict access to sensitive files, and treat configuration changes as operational changes that may require a restart or explicit reload.

Key points

Interview questions

What is a configuration file, and why might a backend service use one?

Configuration files keep changeable settings outside the program's source code. This allows the same application build to run with different settings in different environments. The application typically reads them during startup and validates the resulting values before serving requests.

How would you load and validate configuration in a Python service?

I would first choose a format supported by the project and load the file with a suitable parser, such as `configparser` for INI. I would check that required sections and keys exist, convert values to the types the application expects, and validate constraints such as numeric ranges. If a required value is invalid, I would report the setting clearly and stop startup rather than silently continuing with unsafe behavior.

How should a service combine defaults, a configuration file, and environment variables?

I would define the source order explicitly—for example, built-in defaults first, then a file, then environment overrides. The application should use one consistent merge policy and make the effective values understandable to operators without exposing secrets. Tests should cover both individual sources and conflicts so that precedence does not change accidentally.

Is it safe to store passwords or API keys in a configuration file?

A configuration file is a storage mechanism, not a security boundary. If it contains credentials, access to the file and its backups must be restricted, and secrets should not be printed in logs or error messages. Depending on the deployment, a dedicated secret-management mechanism may be more appropriate, while ordinary non-sensitive settings can remain in a config file.

When should a service reload configuration, and how can it do so safely?

For many services, loading once at startup is simpler and gives each request a consistent settings view. If live reload is required, it needs an explicit design: parse and validate a new complete configuration, then replace the old settings atomically so requests do not observe a partial update. The service should also define what happens when reloading fails, commonly retaining the last valid configuration and emitting an operational error.