A production application rarely changes in just one way. Developers introduce new features, modify existing workflows, integrate third-party services, and update business rules. However, deploying a new version does not always mean every user should immediately receive those changes.
Consider an e-commerce application running on Google Cloud Run. The development team has completed a new checkout experience, but the product team wants to release it to only 10% of users initially. If the checkout process causes unexpected errors, the team should be able to disable the new experience without rebuilding or redeploying the application.
This is where feature flags become useful.
Feature flags allow developers to control application behavior independently of the deployment process. Instead of maintaining separate code branches or deploying different application versions for every release decision, developers can use configuration to decide which features are active.
When combined with Cloud Run, feature flags provide a practical approach to gradual releases, controlled rollouts, experiments, and emergency feature shutdowns.
Understanding Feature Flags
A feature flag is a configuration value that determines whether a particular feature or behavior should be enabled.
For example, an application might contain the following features:
- New checkout experience.
- AI-powered product recommendations.
- Updated dashboard.
- Additional payment provider.
- New customer registration workflow.
Without feature flags, enabling or disabling these features may require code changes and a new deployment.
With feature flags, the application checks a configuration value before executing the relevant functionality.
Consider a simple C# example:
if (featureFlags.EnableNewCheckout)
{
return await ProcessNewCheckoutAsync(request);
}
return await ProcessLegacyCheckoutAsync(request);The application contains both implementations, but the configuration determines which one executes.
The important distinction is that deploying code and enabling a feature become two separate operations.
A new feature can be deployed but remain disabled until the team is ready to activate it.
Why Feature Flags Are Useful in Cloud Run
Cloud Run makes it straightforward to deploy containerized applications without managing servers. It supports revisions, traffic allocation, automatic scaling, and configuration through environment variables and other runtime settings.
Cloud Run traffic splitting can direct a percentage of requests to different revisions. This is useful for testing a new application version alongside an existing version.
However, revision-based traffic splitting and feature flags solve different problems.
Suppose an application contains three new features:
- New checkout.
- Updated dashboard.
- AI recommendations.
If all three features are included in a new Cloud Run revision, traffic splitting sends requests to that revision as a whole. It does not independently control each feature.
A feature flag allows the application to make those decisions separately.
For example:
Cloud Run Application
|
v
Feature Flag Configuration
|
+---- New Checkout: Enabled
|
+---- Updated Dashboard: Disabled
|
+---- AI Recommendations: EnabledThis gives product teams more control over individual capabilities without creating a separate deployment for every combination of features.
Feature flags can also help with:
- Gradual feature releases.
- Internal testing before public release.
- Customer-specific functionality.
- A/B testing.
- Emergency feature shutdowns.
- Separating deployment decisions from release decisions.
Using Environment Variables for Simple Feature Flags
The simplest way to introduce feature flags in a Cloud Run application is through environment variables.
This approach works well when the application has only a few flags and configuration changes do not need to happen frequently.
Consider an ASP.NET Core application.
Add the following configuration to appsettings.json:
{
"FeatureFlags": {
"EnableNewCheckout": false,
"EnableAiRecommendations": true
}
}Create a configuration model:
public class FeatureFlags
{
public bool EnableNewCheckout { get; set; }
public bool EnableAiRecommendations { get; set; }
}Register the configuration in Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<FeatureFlags>(
builder.Configuration.GetSection("FeatureFlags"));
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();The application can now access the feature settings through dependency injection.
For example:
using Microsoft.Extensions.Options;
[ApiController]
[Route("api/checkout")]
public class CheckoutController : ControllerBase
{
private readonly FeatureFlags _features;
public CheckoutController(
IOptions<FeatureFlags> options)
{
_features = options.Value;
}
[HttpPost]
public IActionResult ProcessCheckout()
{
if (_features.EnableNewCheckout)
{
return Ok("New checkout is enabled");
}
return Ok("Legacy checkout is active");
}
}When deploying the application to Cloud Run, override the configuration using environment variables.
gcloud run deploy ecommerce-api \
--image=us-docker.pkg.dev/my-project/apps/ecommerce-api:latest \
--region=us-central1 \
--set-env-vars="FeatureFlags__EnableNewCheckout=false,FeatureFlags__EnableAiRecommendations=true"The double underscore maps to the configuration hierarchy used by ASP.NET Core.
For example, FeatureFlags__EnableNewCheckout maps to FeatureFlags:EnableNewCheckout.
This allows the same container image to run with different feature settings.
However, environment variables are part of the Cloud Run revision configuration. Changing them creates a new revision. This means environment variables are suitable for deployment-specific configuration, but they are not ideal when a product team needs to enable or disable a feature immediately without creating another revision.
Managing Feature Flags With Cloud Run and Firestore
When feature flags need to change independently of deployments, a centralized configuration store is a better option.
Firestore can store feature flag definitions and allow an application to retrieve them at runtime.
Consider the following document in a Firestore collection named FeatureFlags:
{
"EnableNewCheckout": true,
"EnableAiRecommendations": false,
"EnableNewDashboard": true
}The application reads this document and uses its values to decide which features are active.
A simple C# service can represent the configuration:
public class FeatureFlagSettings
{
public bool EnableNewCheckout { get; set; }
public bool EnableAiRecommendations { get; set; }
public bool EnableNewDashboard { get; set; }
}The following service reads the configuration from Firestore:
using Google.Cloud.Firestore;
public class FeatureFlagService
{
private readonly FirestoreDb _firestore;
public FeatureFlagService(FirestoreDb firestore)
{
_firestore = firestore;
}
public async Task<FeatureFlagSettings> GetFlagsAsync()
{
var document = _firestore
.Collection("FeatureFlags")
.Document("Application");
var snapshot = await document.GetSnapshotAsync();
if (!snapshot.Exists)
{
return new FeatureFlagSettings();
}
return snapshot.ConvertTo<FeatureFlagSettings>();
}
}Register the Firestore client and service:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(
_ => FirestoreDb.Create("my-project"));
builder.Services.AddScoped<FeatureFlagService>();
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();The application can retrieve the latest configuration when it needs to evaluate a feature.
For example:
[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
private readonly FeatureFlagService _flags;
public ProductsController(FeatureFlagService flags)
{
_flags = flags;
}
[HttpGet("recommendations")]
public async Task<IActionResult> GetRecommendations()
{
var flags = await _flags.GetFlagsAsync();
if (!flags.EnableAiRecommendations)
{
return Ok(new
{
Message = "Recommendations are currently unavailable"
});
}
return Ok(new
{
Message = "AI recommendations are enabled"
});
}
}
Join the conversation! Your thoughts help the community grow.