Feature flags allow developers to enable or disable application functionality without deploying new code. They are commonly used for gradual rollouts, A/B testing, beta features, maintenance switches, and controlled releases.
For example, an application might introduce a new checkout experience. Instead of enabling it for every user immediately, developers can activate it for a small group, observe the results, and gradually increase the rollout.
This approach becomes more complicated when an ASP.NET Core application runs on multiple instances.
A production application may have several instances running behind a load balancer. Each instance handles incoming requests independently, but all instances are expected to follow the same feature configuration.
If one instance enables a feature while another still uses the old configuration, users may receive different behavior depending on which instance handles their requests.
This can cause inconsistent API responses, unexpected user experiences, and difficult production issues.
In this article, we will explore why feature flags become inconsistent across multiple ASP.NET Core instances and how to manage them using centralized configuration, distributed caching, and integration testing.
Understanding Feature Flag Inconsistency
Consider an ASP.NET Core application running on three instances:
Application Instance A
Application Instance B
Application Instance C
The application uses a feature flag named NewCheckout to control a new checkout workflow.
Initially, the feature is disabled on all three instances.
Later, an administrator enables the feature. If each instance reads its configuration independently, the instances may not receive the update at the same time.
For example:
Instance | NewCheckout |
|---|
Instance A | Enabled |
Instance B | Disabled |
Instance C | Enabled |
A user may receive the new checkout experience when a request reaches Instance A but see the old experience when another request reaches Instance B.
This problem becomes more visible when an application uses load balancing, autoscaling, container orchestration, or rolling deployments.
The main issue is that feature configuration is being treated as local application state even though the application operates as a distributed system.
Why Local Feature Flag Configuration Can Cause Problems
A simple implementation might store feature flags in appsettings.json.
{
"FeatureFlags": {
"NewCheckout": true,
"NewDashboard": false
}
}
The application can read these values through IConfiguration.
var newCheckoutEnabled =
builder.Configuration.GetValue<bool>(
"FeatureFlags:NewCheckout");
This works for a small application running on one instance.
However, when the application runs across multiple servers or containers, each instance may have its own configuration file or environment variables.
Changing the configuration on one server does not automatically update the others.
Restarting every instance after each feature flag change is also inconvenient. It can introduce deployment overhead and may affect application availability.
For production systems, feature flag configuration should have a centralized source of truth.
Using Microsoft Feature Management
Microsoft Feature Management provides a feature flag framework for .NET applications.
It allows developers to define feature flags, check whether a feature is enabled, and control feature availability through configuration.
Install the package:
dotnet add package Microsoft.FeatureManagement.AspNetCore
Add feature flags to appsettings.json:
{
"FeatureManagement": {
"NewCheckout": true,
"NewDashboard": false
}
}
Register feature management in Program.cs:
using Microsoft.FeatureManagement;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFeatureManagement();
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();
Now inject IFeatureManager into an application service or controller.
using Microsoft.FeatureManagement;
public class CheckoutService
{
private readonly IFeatureManager _featureManager;
public CheckoutService(IFeatureManager featureManager)
{
_featureManager = featureManager;
}
public async Task<string> GetCheckoutExperienceAsync()
{
var enabled = await _featureManager.IsEnabledAsync(
"NewCheckout");
return enabled
? "New checkout experience"
: "Existing checkout experience";
}
}
The application can now check whether the feature is enabled without directly accessing configuration values.
However, using Microsoft Feature Management does not automatically guarantee that every application instance receives configuration changes at exactly the same time. That depends on how the configuration source is managed and refreshed.
Moving Feature Flags to Centralized Configuration
A better approach is to keep feature flag settings in a shared configuration source.
For applications hosted on Azure, Azure App Configuration provides centralized configuration management and feature flag support.
Multiple ASP.NET Core instances can read from the same configuration store rather than maintaining separate feature flag files.
The general architecture looks like this:
Azure App Configuration
|
+------------+------------+
| | |
Instance A Instance B Instance C
| | |
Feature Feature Feature
Manager Manager Manager
Each instance reads feature configuration from the same central source.
This reduces configuration differences between instances and makes feature flag management easier.
Install the required packages:
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
dotnet add package Microsoft.FeatureManagement.AspNetCore
Register Azure App Configuration in Program.cs:
using Microsoft.Azure.AppConfiguration.AspNetCore;
using Microsoft.FeatureManagement;
var builder = WebApplication.CreateBuilder(args);
var connectionString =
builder.Configuration["AppConfiguration:ConnectionString"];
builder.Configuration.AddAzureAppConfiguration(options =>
{
options.Connect(connectionString)
.Select("FeatureManagement:*")
.ConfigureRefresh(refresh =>
{
refresh.Register(
"FeatureManagement:Sentinel",
refreshAll: true);
refresh.SetCacheExpiration(
TimeSpan.FromSeconds(30));
});
});
builder.Services.AddAzureAppConfiguration();
builder.Services.AddFeatureManagement();
var app = builder.Build();
app.UseAzureAppConfiguration();
app.MapControllers();
app.Run();
The example uses a sentinel configuration key to trigger refresh.
When the sentinel value changes in Azure App Configuration, the application can refresh the selected configuration values.
The cache expiration controls how frequently an instance checks whether its configuration needs to be refreshed. It does not mean every instance updates at exactly the same instant.
In a production environment, the connection string should be stored securely, such as in an appropriate secret store or managed identity configuration, rather than committed to source control.
Understanding Configuration Refresh Across Instances
Centralized configuration provides a shared source of truth, but each application instance still maintains its own local configuration state.
For example, assume the refresh interval is 30 seconds.
An administrator enables NewCheckout at 10:00:00.
Instance A may detect the change at 10:00:05, while Instance B may detect it at 10:00:12. Instance C may not refresh until 10:00:25.
During this period, users can still experience different behavior.
This is an important distinction: centralized configuration prevents long-term configuration differences, but periodic refresh does not provide instantaneous synchronization.
If the application requires stronger consistency, developers should consider how feature decisions are made.
For example, a feature flag can be evaluated at a centralized service or gateway, or the application can use a distributed configuration version to coordinate activation.
The correct approach depends on the business requirement. A marketing banner may tolerate a short delay, while a feature controlling financial transactions may require stricter coordination.
Using a Distributed Cache for Shared Feature Configuration
Another approach is to store feature configuration in a distributed cache such as Redis.
Instead of keeping independently managed feature values inside each instance, applications can retrieve the current configuration from a shared cache.
For example, define a simple feature configuration model:
public class FeatureConfiguration
{
public bool NewCheckout { get; set; }
public bool NewDashboard { get; set; }
public long Version { get; set; }
}
A shared configuration service can read the configuration from Redis.
public interface IFeatureConfigurationService
{
Task<FeatureConfiguration> GetConfigurationAsync(
CancellationToken cancellationToken);
}
The implementation can use IDistributedCache:
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;
public class FeatureConfigurationService
: IFeatureConfigurationService
{
private readonly IDistributedCache _cache;
public FeatureConfigurationService(
IDistributedCache cache)
{
_cache = cache;
}
public async Task<FeatureConfiguration>
GetConfigurationAsync(
CancellationToken cancellationToken)
{
var json = await _cache.GetStringAsync(
"application-feature-flags",
cancellationToken);
if (string.IsNullOrWhiteSpace(json))
{
return new FeatureConfiguration();
}
return JsonSerializer.Deserialize<FeatureConfiguration>(
json) ?? new FeatureConfiguration();
}
}
Register the distributed cache using Redis:
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration =
builder.Configuration.GetConnectionString("Redis");
options.InstanceName = "MyApplication:";
});
builder.Services.AddScoped<
IFeatureConfigurationService,
FeatureConfigurationService>();
Every application instance can now read the same configuration entry.
However, a distributed cache alone does not provide feature flag management. The application still needs a reliable process for updating values, validating configuration, handling cache failures, and preventing stale updates.
It is also important to consider how the application behaves when Redis becomes unavailable. A fallback policy should be defined rather than allowing every feature check to fail unexpectedly.
For more advanced systems, use a centralized feature management platform or configuration service instead of building a complete feature management system around a cache.
Testing Feature Flag Consistency
Feature flag behavior should be tested across multiple application instances, not only within one test host.
A useful integration test creates two application instances that read the same configuration source.
The test can enable a feature, update the shared configuration, and verify that both instances eventually observe the change.
For a simpler test, begin by testing the feature configuration service.
[Fact]
public async Task GetConfiguration_ShouldReturnSharedFeatureValues()
{
var expected = new FeatureConfiguration
{
NewCheckout = true,
NewDashboard = false,
Version = 2
};
var json = JsonSerializer.Serialize(expected);
await distributedCache.SetStringAsync(
"application-feature-flags",
json);
var result = await featureConfigurationService
.GetConfigurationAsync(
CancellationToken.None);
Assert.True(result.NewCheckout);
Assert.False(result.NewDashboard);
Assert.Equal(2, result.Version);
}
This test verifies that the service reads the expected configuration from the distributed cache.
For a multi-instance test, create two service providers using the same Redis instance or shared configuration backend. Retrieve the feature values through both providers after updating the shared configuration.
The test should verify both the expected configuration and the refresh behavior.
Avoid relying on a fixed short delay without accounting for the configured refresh interval. Poll for the expected version with a reasonable timeout instead.
Common Mistakes When Managing Feature Flags
One common mistake is assuming that a shared configuration source provides immediate updates. Each application instance may refresh at a different time, depending on its refresh policy.
Another mistake is storing feature flags in local memory without a refresh strategy. This can leave instances using outdated values for longer than expected.
Developers should also avoid using feature flags as a replacement for authorization. A feature flag controls whether functionality is available, but it should not determine whether a user has permission to perform a protected operation.
Another important consideration is configuration versioning. When multiple administrators or automated processes update feature flags, version information helps identify which configuration an instance is using.
Finally, feature flags should have an ownership and cleanup process. Temporary release flags that remain in the application indefinitely increase complexity and make future changes harder to understand.
Best Practices for Production Applications
Use a centralized source of truth for feature configuration. This reduces the possibility of different servers maintaining unrelated feature values.
Choose a refresh interval based on the business requirement. Not every feature needs immediate updates, and excessively frequent refresh checks can create unnecessary load.
For important changes, use configuration versioning and monitor which version each instance has loaded.
Define a fallback strategy for configuration service failures. Critical features should not become unpredictable simply because a configuration provider is temporarily unavailable.
Test feature flag behavior with multiple application instances and include scenarios involving delayed refresh, unavailable configuration services, and conflicting updates.
Most importantly, decide which features can tolerate eventual consistency and which require stronger coordination. This decision should be based on the impact of inconsistent behavior rather than applying the same strategy to every feature.
Summary
Feature flags make it easier to release functionality gradually and control application behavior without deploying new code. However, managing them across multiple ASP.NET Core instances introduces a consistency challenge.
Local configuration files and independently maintained values can cause different instances to use different feature settings. Centralized configuration services such as Azure App Configuration provide a shared source of truth, while distributed caching can support shared configuration access.
Developers should also understand that periodic configuration refresh does not guarantee that every instance updates at exactly the same moment.
By using centralized configuration, defining refresh policies, tracking configuration versions, and testing multiple application instances, teams can make feature flag behavior more predictable in distributed ASP.NET Core applications.
Join the conversation! Your thoughts help the community grow.