7.9 — Reloadable Configuration and Its Limits¶
← Roadmap · Section: 7. Configuration and Options Prev: 7.8 Options validation · Next: 7.10 Custom providers
Markers: ⚠️ · Confidence: 🔴 · Last reviewed: —
TL;DR¶
reloadOnChange makes a configuration provider re-read its source when it changes, without
restarting the application.
- It works for file-based providers. Environment variables never reload — they are read once at process start.
- ⚠️ A reload updates
IConfiguration, but not everything that consumed it. Anything captured at startup keeps its old value. - In most deployments — immutable containers, environment-variable config — nothing ever reloads, so this machinery buys nothing.
Definitions¶
| Term | What it is | What it means |
|---|---|---|
reloadOnChange |
a parameter on AddJsonFile |
Watch the file and re-read it when it changes. |
| File watcher | an OS mechanism | Notifies the process when a file is modified. What reloadOnChange uses. |
IConfigurationRoot.Reload() |
a method | Forces every provider to re-read its source. |
| Change token | an IChangeToken |
The notification primitive. Fires once, then must be re-registered. |
IOptionsMonitor<T> |
an interface | The only options interface that sees changes in a singleton. See 7.6. |
OnChange |
a method | Callback fired when configuration reloads. |
| ConfigMap | a Kubernetes object | Configuration mounted into a pod as files. Can update in place. |
| Captured value | a stale reference | A setting read once at startup and stored, so a reload never reaches it. |
| Duplicate notification | a file-watcher quirk | One save often produces two change events. |
The concept¶
What reload actually does¶
Enable it on a file provider:
builder.Configuration.AddJsonFile("appsettings.json",
optional: false,
reloadOnChange: true); // ← the watcher
Edit the file, and the provider re-reads it. IConfiguration now returns the new values.
That is genuinely all it does — update the configuration object. What it does not do is push those values into everything that already read them.
A picture: updating the noticeboard in the staff room. The notice is now correct. Everyone who read it yesterday and wrote the number on their hand still has the old one.
⚠️ The limit that matters¶
This is the point of the note. A reload reaches:
| Consumer | Sees the change? |
|---|---|
IConfiguration read directly |
✅ Yes |
IOptionsMonitor<T> |
✅ Yes |
IOptionsSnapshot<T> |
✅ Yes — on the next request |
IOptions<T> |
❌ No — captured at startup |
A value read in Program.cs |
❌ No — that was a one-off read |
A configured DbContext connection string |
❌ No — set when the context was configured |
| Kestrel endpoints and ports | ❌ No — bound at startup |
| Registered services | ❌ No — the container is built once |
Most of an application falls in the "no" column. So "we can change configuration without a
restart" is only true for the small part of it that goes through IOptionsMonitor or
IOptionsSnapshot.
That is worth being precise about in an interview, because the naive answer — "yes, it reloads automatically" — is wrong for most of the settings people care about.
How it works¶
The simple version — what reloads¶
File providers, with the flag on:
The defaults from CreateBuilder already enable this for appsettings.json and the
environment-specific file. So file reload is on unless you built the configuration manually.
Environment variables do not reload. Ever.
A process reads its environment when it starts. Changing a variable afterwards has no effect on a running process — that is an operating system behaviour, not an ASP.NET Core limitation. Changing an environment variable requires restarting the process.
⚠️ This matters because most deployments configure through environment variables. In a container, in Kubernetes, in Azure App Service — the settings arrive as environment variables, so nothing reloads at all and the entire options-monitoring apparatus is inert.
Key Vault reloads only on a configured interval (7.4):
Consuming a change¶
The only two options interfaces that see it:
// Scoped service — fresh at the start of each request
public class PricingService(IOptionsSnapshot<PricingOptions> options)
{
private readonly PricingOptions _options = options.Value;
}
// Singleton — read the current value each time
public class RateLimiter(IOptionsMonitor<RateLimitOptions> monitor)
{
public bool IsAllowed(string client)
{
var options = monitor.CurrentValue; // capture ONCE per operation
// ...
}
}
And reacting to a change:
public class CacheManager : IDisposable
{
private readonly IDisposable? _subscription;
public CacheManager(IOptionsMonitor<CacheOptions> monitor)
{
_subscription = monitor.OnChange(options => Resize(options.MaxSizeMb));
}
public void Dispose() => _subscription?.Dispose(); // ⚠️ not optional
}
⚠️ Dispose the subscription. The monitor holds a reference to your callback, and through it to your object, for the application's lifetime otherwise — a memory leak (3.5).
⚠️ OnChange often fires twice for one save. Editors write files in two steps — truncate,
then write — and the watcher reports both. So the callback must be idempotent and cheap.
Rebuilding an expensive cache directly inside it means doing that twice per edit.
A common mitigation is debouncing:
private DateTimeOffset _lastHandled = DateTimeOffset.MinValue;
_subscription = monitor.OnChange(options =>
{
var now = DateTimeOffset.UtcNow;
if (now - _lastHandled < TimeSpan.FromSeconds(2)) return; // ignore the duplicate
_lastHandled = now;
Resize(options.MaxSizeMb);
});
How it actually works — where reload is genuinely useful¶
Kubernetes ConfigMaps mounted as files. This is the main real case:
volumeMounts:
- name: config
mountPath: /app/config
volumes:
- name: config
configMap:
name: app-settings
builder.Configuration.AddJsonFile("/app/config/appsettings.json",
optional: true, reloadOnChange: true);
Updating the ConfigMap updates the mounted file, and the application picks it up without a restart.
⚠️ Two caveats. ConfigMap updates propagate to the pod on a kubelet sync interval, often
around a minute — so it is not instant. And ConfigMaps mounted via subPath do not update at
all, which is a well-known trap; mount the whole directory instead.
Feature flags are the other genuine case, and there is a purpose-built system for them — 7.11.
The deeper detail — why restarting is often better¶
⚠️ Live reload has real hazards:
Partial application. Half your settings update and half do not, so the application runs in a state that never existed in any config file. Debugging that is unpleasant.
Mid-request changes. With IOptionsMonitor, a value can change between two lines of the same
method (7.6). One request sees an inconsistent pair of settings.
Validation is bypassed. ValidateOnStart runs at startup
(7.8). A reload can introduce invalid values that never get
checked — you can reload your way into a configuration the application would have refused to
start with.
No rollback. A bad reload takes effect immediately, everywhere, with no deployment gate.
Against that, a rolling restart applies configuration atomically, runs validation, and rolls back automatically if the new pods fail their health checks (24.6).
The honest position: prefer a restart for configuration changes. Reserve live reload for things genuinely designed to change at runtime — feature flags, log levels — where the blast radius is small and the value is high.
Log levels are the best example, because turning on debug logging to diagnose a live incident without restarting the affected service is exactly when you need it.
Code¶
A realistic setup that reloads only what should reload:
var builder = WebApplication.CreateBuilder(args);
// Defaults already watch appsettings.json and appsettings.{Env}.json.
// A separately mounted file for things that SHOULD change at runtime
builder.Configuration.AddJsonFile("/app/config/runtime.json",
optional: true, reloadOnChange: true);
// Fixed at startup — IOptions, so a reload deliberately does not affect it
builder.Services
.AddOptions<DatabaseOptions>()
.Bind(builder.Configuration.GetSection("Database"))
.ValidateDataAnnotations()
.ValidateOnStart();
// Designed to change — IOptionsMonitor consumers
builder.Services
.AddOptions<RateLimitOptions>()
.Bind(builder.Configuration.GetSection("RateLimit"))
.ValidateDataAnnotations()
.ValidateOnStart();
var app = builder.Build();
// ✓ Reads the current value, safely
public class RateLimitService(
IOptionsMonitor<RateLimitOptions> monitor,
ILogger<RateLimitService> logger) : IDisposable
{
private readonly IDisposable? _subscription;
private DateTimeOffset _lastChange = DateTimeOffset.MinValue;
public RateLimitService(/* ... */)
{
_subscription = monitor.OnChange(options =>
{
// Debounce the duplicate notification
var now = DateTimeOffset.UtcNow;
if (now - _lastChange < TimeSpan.FromSeconds(2)) return;
_lastChange = now;
logger.LogInformation(
"Rate limit configuration reloaded: {Limit}/min", options.RequestsPerMinute);
});
}
public bool IsAllowed(string clientId)
{
var options = monitor.CurrentValue; // once per operation, not per line
// ...
}
public void Dispose() => _subscription?.Dispose();
}
Four things right in that class: IOptionsMonitor because it is a singleton, CurrentValue
captured once per operation, the change notification debounced, and the subscription disposed.
Interview questions¶
Q: How does configuration reloading work?
File-based providers can watch their source and re-read it when it changes, which is enabled with
reloadOnChange: true — and the defaults already turn it on for appsettings.json. When the
file changes, IConfiguration returns the new values. The important limitation is that a reload
updates the configuration object but does not push values into anything that already consumed
them, so most of an application does not see it.
Q: What does and does not see a reload?
IConfiguration read directly sees it, as do IOptionsMonitor<T> and IOptionsSnapshot<T>. What
does not: IOptions<T>, because it captured the value at startup; anything read once in
Program.cs; a DbContext connection string, which was fixed when the context was configured;
Kestrel's endpoints, bound at startup; and the service registrations themselves, since the
container is built once. So the claim "we can change configuration without a restart" only holds
for the narrow part of the app going through monitor or snapshot.
Q: Do environment variables reload?
No, and this matters because most deployments use them. A process reads its environment when it starts, and changing a variable afterwards has no effect on a running process — that is an operating system behaviour, not a framework limitation. So in a container or a Kubernetes pod where configuration arrives as environment variables, nothing reloads at all and the whole options-monitoring apparatus is inert. It becomes useful with a ConfigMap mounted as files, or a Key Vault provider with a reload interval.
Q: Would you rely on live reload in production?
Sparingly. There are real hazards: a partial reload leaves the application in a state that never
existed in any config file, IOptionsMonitor values can change between two lines of the same
method so one request sees an inconsistent pair, and ValidateOnStart does not run on reload —
so you can reload your way into a configuration the application would have refused to start with.
A rolling restart applies changes atomically, runs validation, and rolls back automatically if
health checks fail. I would reserve live reload for things designed to change at runtime, like
log levels during an incident or feature flags, where the blast radius is small.
Traps ⚠️¶
- Environment variables never reload. Which means most deployments never reload at all.
IOptions<T>does not see changes. It captured the value at startup.- A
DbContextconnection string, Kestrel endpoints and DI registrations are fixed at startup. OnChangefires twice for one save. Debounce, and keep the callback cheap.OnChangesubscriptions leak unless disposed.ValidateOnStartdoes not run on reload. Invalid values can arrive unchecked.- Kubernetes ConfigMaps mounted with
subPathdo not update. Mount the directory. - ConfigMap updates take up to a kubelet sync interval, often around a minute.
CurrentValuecan change mid-method. Capture it once per operation.
Related¶
- 7.6 The options pattern — which interface sees changes
- 7.1 The configuration system — the providers
- 7.4 Secret providers — Key Vault reload intervals
- 7.8 Options validation — why reload bypasses it
- 7.11 Feature flags — the right tool for runtime toggles
- 3.5 Memory leaks — undisposed subscriptions
- 24.6 Graceful shutdown — why restarting is safe