November 9, 2018
Thank you for reading this post, don't forget to subscribe!As the Product Manager of Firebase Remote Config, a product that helps you modify your app without deploying a new version, I spend a lot of time talking to our customers, and one of the most common requests I hear is, “Can I get rid of the Remote Config cache and fetch new values right away?”
While we can’t get rid of the cache entirely — caching ensures that the Remote Config service stays up and running, free of charge, no matter how many millions of users you have — thanks to some new features we’ve added to Cloud Functions for Firebase, you can now ensure that your users always get fresh Remote Config values whenever they open your app!
Clearing up some caching confusion
Before we get into our solution, let me explain why the problem isn’t as bad as you think, by looking at a typical app that has a Remote Config cache time set to 4 hours. You might think that, if this app developer publishes new Remote Config values, most of their users won’t see these new values until several hours later.
Most users see the latest results
But the fact is, the vast majority of their users will see the new Remote Config results as soon as they run the app! Remember, the way the cache works is that it looks at the last time Remote Config successfully fetched data. So anybody who last opened their app more than 4 hours ago will retrieve fresh data.
To put it another way, if your user opens up your app at 7:00 pm, you push out new values at midnight, and then they open up your app again at 12:15 am, Remote Config will fetch your new values from the server. Sure, it’s only been 15 minutes since you published your values, but their cache is over 5 hours old, so Remote Config will fetch fresh values.
Nevertheless, I understand that it’s really important for many of you to push out new changes to all of your users right away. Maybe you want feature flags to be disabled quickly. Or, maybe you have time-sensitive values, like marketing campaigns or a flash sale, in which case it would be awkward for your users to still see messaging for a sale that’s no longer running.
Adding Remote Config triggers to Cloud Functions
Fortunately, we’ve built a new integration with Cloud Functions for Firebase that will make it easier for you to build custom behavior based on events that happen in Remote Config. Specifically, you can now use new triggers to write code that runs whenever your team publishes Remote Config values — whether that’s through the Firebase Console or the REST API.
There are a number of ways you can use these Remote Config triggers. One significant way is to use them to notify all of your apps as soon as a new set of values get published on Remote Config.
Implementation workflow
If you’re interested in learning how to implement this feature, here’s a high level overview of the process:
- First, you need to make sure your clients are using both Firebase Remote Config and Firebase Cloud Messaging.
- Next, create a Cloud Function that is triggered whenever somebody on your team publishes new values to Remote Config. Again, this will happen whether that’s done using the Firebase Console, or through the REST API.
- Your Cloud Function then sends a silent, data-only notification to all of your clients, using Firebase Cloud Messaging, telling them that there’s new Remote Config data ready to be read.
- When your clients receive this message, they simply record a local flag noting that the Remote Config data they have is stale and needs to be updated.
- The next time your app is brought back into the foreground, it fetches fresh values immediately from Remote Config servers (if that local flag exists), and uses default Remote Config fetching behavior otherwise.
- Once the fetch is complete, your app resets that local flag.
For more information, check out the solutions guide in our documentation. This guide contains all of the code samples you’ll need, both in your Cloud Function, and on the client.
You might be wondering why you can’t either set your Remote Config cache to 0 all the time, or have your clients go and always retrieve the new Remote Config values as soon as they receive the notification.
The answer is that Remote Config still needs to make sure the service is usable by all apps, and we’ve set up both client and server-side throttles to make sure that no single app accidentally abuses the service. By sticking with the default caching time when there’s no local flag indicating that there’s new config available, you can avoid any client-side throttles. Your app will have a faster start-up time, too, by avoiding unnecessary network calls.
By waiting until your client goes into the foreground to fetch these new values, you avoid any server-side throttles. You’ll also avoid having your clients make unnecessary network calls in the background, which means your app uses less data overall, which makes your users happier, particularly in areas of the world where data usage is expensive.
By making sure your apps are still well-behaved, you can still get the freshest possible Remote Config values while avoiding any throttles that might be applied to your app. Please consult our caching & throttling documentation for further information on this topic.
No polling needed, either!
One other advantage of using this system is that if your app in the foreground when it receives this notification, you can immediately act on this information – perhaps by fetching the new Remote Config data and prompting the user to refresh their screen. So those of you who were actively polling the Remote Config service while your app was in the foreground no longer need to resort to any of those network calls anymore.
Other uses for Cloud Functions and Remote Config
Of course, this isn’t the only use for Remote Config triggers and Cloud Functions – just one of the most requested, for sure! A number of developers also want to know when new Remote Config values have been pushed to the Firebase console, and you can use Cloud Functions to send off an email to your team or push a message in a Slack channel.
You could also use Cloud Functions to keep different projects in sync. For instance, you could use a cloud function to copy a set of Remote Config values from your production project to your development or testing project.
Give it a try!
If you’re interested in giving this new approach a try, we encourage you to read over the full documentation in the solutions guide. As always, if you have other questions about Remote Config or other feature requests, we’re happy to hear your feedback. Please reach out to us in the Firebase Talk group, or on Stack Overflow.
2018-11-09 00:00:00