Why and how to use Feature Toggles

Decouple deployment from release

Image for post
Image for post
Photo by Florian Olivo on Unsplash

What are feature toggles

Why use Feature Toggles

Implementing Feature Toggles

Application Configuration Files

Application database

Central Configuration Management Service

Coding Time

public void mainBusinessLogic() {
//some business logic here
unoptimizedMethod();
//Some more business logic here
}
private void unoptimizedMethod() {
// Some shitty code here
}
public abstract class ConfigClient  {
private static final long REFRESH_INTERVAL_MS = Duration.of(5L, ChronoUnit.MINUTES).toMillis();
private Map<String, String> configurationCache;
private ConfigurationRefresherThread configurationRefresherThread;
public ConfigClient() throws Exception {
// Load the properties one time so that we can fail app startup on any problem.
loadProperties();
// Start thread for periodic reloads after this
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(this.configurationRefresherThread, 0, REFRESH_INTERVAL_MS, TimeUnit.MILLISECONDS);
}
public Optional<String> getConfigValue(String property) {
String props = configurationCache.get(property);
return Optional.ofNullable(props);
}
private void loadProperties() {
Map<String, String> propertyMap = getProperties();
if (propertyMap != null && !propertyMap.isEmpty()) {
configurationCache.putAll(propertyMap);
}
}
// Override this method to read from were the configurations are stored
protected abstract Map<String, String> getProperties();
/**
* This thread refreshes the property cache
*/
public class ConfigurationRefresherThread implements Runnable {
@Override
public void run() {
log.debug("Refreshing configurations...");
loadProperties();
}
}
}
public void mainBusinessLogic() {
//some business logic here
if (Boolean.valueOf(configClient.getConfigValue("enable-optimized-method").orElse("false"))) {
optimizedMethod();
} else {
unoptimizedMethod();
}
//Some more business logic here
}
private void unoptimizedMethod() {
// Some shitty code here
}
private void optimizedMethod() {
// Some good code here
}

The problems

Backward compatibility

Two failure modes

Cleaning up the toggles

A shortcut to proper modelling

Written by

Code, products, platforms, books, music

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store