Efficient API Usage
It is good engineering practise to reduce the frequency of API calls made from your applications to Flagsmith. There are a number of reasons for this:
- Reducing network activity means your applications use less client resources like mobile device battery, CPU and network.
- You don't need to reflect state updates in your application as often.
- It saves you money! Whether you are self hosting or using our SaaS API, reducing API call volume costs you less.
Client Side
Getting Flags Once Per Session
The most common, most efficient workflow we have found with people using Flagsmith on the client side (browsers, mobile apps etc) is the following:
- The user opens the app for the first time, as an anonymous user.
- The application loads, using default flag values as defined in code.
- The application makes a request for the flags for the environment (not the Identity as the user is still unknown at this stage).
- The user logs into the application. A request is then made for the identity flags (along with any traits for that identity).
- This data is then cached locally on the device and used for the duration of the user's session.
- When the user reopens the app (for example, the following day) the cached values in the previous step are used. The application then re-requests the identity flags (in case any flags have changed in the meantime) and caches the data.
Setting Traits Efficiently
Every time a trait as set via the SDK, they will make a request to the Flagsmith API with the trait data and receive an updated set of flags.
In order to reduce these calls, we recommend setting the full complement of traits in a single SDK call. There's more info around achieving this in our Javascript FAQ.
Real Time Flag Updates
In our experience, most applications do not benefit a great deal from real time flag updates. In addition, and especially with client-side flags, thought needs to be given to ensuring features/UI widgets don't appear/disappear in real time due to flag changes.
That being said, there are use-cases for real time flags. Using our real-time flags service negates the need to poll the API from the client SDK, which can significantly reduce API usage.