Optimization
Memoization
By default, Flipper will preload and memoize all features to ensure one adapter call per request. This means no matter how many times you check features, Flipper will only make one network request to Postgres, MySQL, Redis, Mongo or whatever adapter you are using for the length of the request.
Preloading
Flipper will preload all features before each request by default, which is recommended if:
- you have a limited number of features (< 100?)
- most of your requests depend on most of your features
- you have limited gate data combined across all features (< 1k enabled gates, like individual actors, across all features)
The reason for these recommendations is that more features and gates means more data being preloaded for every request. The general rule is: leave preloading on until performance is negatively affected at which point it has served its purpose and should be turned off. You can track performance by instrumenting flipper usage to your favorite metrics system.
If you need to customize preloading, it's very easy:
# config/initializers/flipper.rb
Rails.application.configure do
# Load specific features that are used on most requests
config.flipper.preload = [:stats, :search, :some_feature]
# Or completely disable preloading
config.flipper.preload = false
end
Features that are not preloaded are still memoized, ensuring one adapter call per feature during a request.
Skip memoization
Prevent preloading and memoization on specific requests by setting memoize
to a proc that evaluates to false.
# config/initializers/flipper.rb
Rails.application.configure do
config.flipper.memoize = ->(request) { !request.path.start_with?("/assets") }
end
Disable memoization
To disable memoization entirely:
Rails.application.configure do
config.flipper.memoize = false
end
Advanced (non-Rails usage)
Memoization is implemented as a Rack middleware, which can be used manually in any Ruby app:
use Flipper::Middleware::Memoizer,
preload: true,
unless: ->(request) { request.path.start_with?("/assets") }
Also Note: If you need to customize the instance of Flipper used by the memoizer, you can pass the instance to SetupEnv
:
use Flipper::Middleware::SetupEnv, -> { Flipper.new(...) }
use Flipper::Middleware::Memoizer
Cache Adapters
Cache adapters allow you to cache adapter calls for longer than a single request and should be used alongside the memoization middleware to add another caching layer.
Dalli
Dalli is a high performance pure Ruby client for accessing memcached servers.
https://github.com/petergoldstein/dalli
Example using the Dalli cache adapter with the Memory adapter and a TTL of 600 seconds:
Flipper.configure do |config|
config.adapter do
dalli = Dalli::Client.new('localhost:11211')
adapter = Flipper::Adapters::Memory.new
Flipper::Adapters::Dalli.new(adapter, dalli, 600)
end
end
RedisCache
Applications using Redis via the redis-rb client can take advantage of the RedisCache adapter.
Initialize RedisCache
with a flipper adapter, a Redis client instance, and an optional TTL in seconds. TTL defaults to 3600 seconds.
Example using the RedisCache adapter with the Memory adapter and a TTL of 4800 seconds:
require 'flipper/adapters/redis_cache'
Flipper.configure do |config|
config.adapter do
redis = Redis.new(url: ENV['REDIS_URL'])
memory_adapter = Flipper::Adapters::Memory.new
Flipper::Adapters::RedisCache.new(memory_adapter, redis, 4800)
end
end
ActiveSupportCacheStore
Rails applications can cache Flipper calls in any ActiveSupport::Cache::Store implementation.
Add this line to your application's Gemfile:
gem 'flipper-active_support_cache_store'
And then execute:
$ bundle
Or install it yourself with:
$ gem install flipper-active_support_cache_store
Example using the ActiveSupportCacheStore adapter with ActiveSupport's MemoryStore, Flipper's Memory adapter, and a TTL of 5 minutes.
require 'active_support/cache'
require 'flipper/adapters/active_support_cache_store'
Flipper.configure do |config|
config.adapter do
Flipper::Adapters::ActiveSupportCacheStore.new(
Flipper::Adapters::Memory.new,
ActiveSupport::Cache::MemoryStore.new # Or Rails.cache,
expires_in: 5.minutes
)
end
end
Setting expires_in
is optional and will set an expiration time on Flipper cache keys. If specified, all flipper keys will use this expires_in
over the expires_in
passed to your ActiveSupport cache constructor.