Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mastering Feature Flags: Best Practices and Imp...

Mastering Feature Flags: Best Practices and Implementation with Firebase Remote Config

Feature flags are a powerful tool that makes building and releasing mobile apps both safer and faster. Developers and teams can merge code faster and release more often, without having to worry about breaking something. Additionally, it allows the team to target specific users and to experiment via A/B testing.

We will take a closer look at what feature flags are, how they can help us, best practices, and how to start using them in your project by leveraging the Firebase Remote Config cloud service.

Domen Lanišnik

August 31, 2024
Tweet

More Decks by Domen Lanišnik

Other Decks in Programming

Transcript

  1. Agenda • Basics • Benefits and drawbacks • Best practices

    • Advanced uses • Getting started with Firebase Remote Config • Summary
  2. What is a Feature Flag • Simple boolean variable •

    Control remotely whether a feature is enabled • No need to rebuild and re-release the app Feature Flag Turned OFF Feature Flag Turned ON
  3. What is a Feature Flag if (isFeatureEnabled("shareFeatureEnabled")) { // show

    Share button } if (isFeatureEnabled("useNewComposeScreen")) { // open new Compose based screen } else { // open old View based screen }
  4. Why Use Feature Flags • Protect against critical bugs or

    crashes • Decouple from the release process • Control which users get the feature • Avoid long-lived feature branches • Support one-time features • Run A/B experiments
  5. What to Watch Out For • Overusing feature flags (complexity)

    • Dependant feature flags • Increased testing difficulty • Stale feature flags (tech debt)
  6. Best Practices • Put new features under feature flags and

    ship early • Have a clear naming convention • Define ownership • Perform regular reviews for stale flags • Avoid dependent flags
  7. Different Types of Feature Flags • Int, String, Double, Lists,

    Json, … • Allows for different configurations • Enables experimenting with different values
  8. Kill Switches • Used for critical paths • Refreshed often

    • Disable an important feature in case of an issue • Should not be same as the main feature flag
  9. A/B Testing • Divide the user base into two groups

    • Gather analytics and metrics • Decide whether to ship the new feature
  10. How to Start Using Feature Flags • Implement your own

    feature management service • Use existing open-source libraries • Use a third-party feature management service
  11. Firebase Remote Config • Cloud service for changing behavior and

    appearance of apps • Third-party feature management service • Offers everything out-of-the-box • Update values through console or backend APIs • Android, iOS, Flutter, Web, and Backend
  12. Implementation Steps 1. Set up (new) Firebase Project 2. Integrate

    Firebase SDKs into your app 3. Configure the Remote Config object 4. Fetch and activate values 5. Read values from Remote Config backend 6. Listen for updates
  13. 2. Integrate SDK dependencies { // Import the BoM for

    the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // Add the dependencies for the Remote Config and Analytics libraries implementation("com.google.firebase:firebase-config-ktx") implementation("com.google.firebase:firebase-analytics-ktx") }
  14. 4. Initialize Remote Config class FeatureChecker { private val remoteConfig

    = FirebaseRemoteConfig.getInstance() init { val configSettings = remoteConfigSettings { minimumFetchIntervalInSeconds = 3600L } remoteConfig.setConfigSettingsAsync(configSettings) } }
  15. 6. Fetch Remote Values suspend fun fetchFeaturesConfig(): Boolean { return

    remoteConfig.fetchAndActivate().await() } • Blocking during app startup (splash screen) • Async and update immediately • Async and use new values next time
  16. 6. Read Values class ViewModel(private val featureChecker: FeatureChecker) : ViewModel()

    { private val _detailsScreenEnabled: MutableStateFlow<Boolean> = MutableStateFlow(false) val detailsScreenEnabled: StateFlow<Boolean> = _detailsScreenEnabled init { viewModelScope.launch { _detailsScreenEnabled.update { featureChecker.isFeatureEnabled("detailsScreenEnabled") } } } }
  17. 6. Using it in Compose with a ViewModel val detailScreenEnabled

    = viewModel.detailsScreenEnabled .collectAsState() .value if (detailsButtonEnabled) { IconButton( onClick = {}, ) { Icon( Icons.Default.ArrowForward, contentDescription = "Open details" ) } }
  18. 8. Observe Updates in Real-Time • Receive new values as

    soon as published • Opens HTTP connection to Remote Config backend Source: https://firebase.google.com/docs/remote-config/real-time?platform=android
  19. 8. Observe Updates in Real-Time suspend fun observeFeatureConfigChanges() = callbackFlow

    { firebaseRemoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener { override fun onUpdate(configUpdate: ConfigUpdate) { firebaseRemoteConfig.activate().addOnCompleteListener { trySend(configUpdate.updatedKeys) } } override fun onError(error: FirebaseRemoteConfigException) { close(error.cause) } }) awaitClose { } }
  20. 8. Observe Updates in Real-Time featureChecker.observeFeatureConfigChanges() .filter { it.contains("detailsScreenEnabled") }

    .map { featureChecker.isFeatureEnabled("detailsScreenEnabled") } .collectLatest { featureEnabled -> _detailsScreenEnabled.update { featureEnabled } }
  21. Additional Capabilities • A/B experiments ◦ Used with Google Analytics

    • Personalization ◦ Uses ML to deliver custom experience to each user ◦ Automatically achieve maximum conversion ◦ Can be useful for showing ads, adjusting difficulty level in a game, …
  22. Setting up an A/B Experiment • how to set up

    • how to target specific users • code examples
  23. Firebase Remote Config Limitations • Free to use for unlimited

    daily active users • Maximum 2000 parameters • Parameter keys can be up to 256 characters long • Maximum 500 conditions • Throttling (default is 12 hours) • Up to 300 total A/B experiments (max 24 running)
  24. Summary • Feature flags are a powerful tool • Make

    building and releasing mobile apps both safer and faster • Benefits outweigh the drawbacks • Stick to the best practices • Easy to get started with Firebase Remote Config