Flipper Cloud

Redis adapter

A Redis adapter for Flipper.


Add this line to your application's Gemfile:

gem 'flipper-redis'

And then execute:

$ bundle

Or install it yourself with:

$ gem install flipper-redis


In most cases, all you need to do is require the adapter. It will connect to the Redis instance specified in the REDIS_URL or FLIPPER_REDIS_URL environment vairable, or localhost by default.

require 'flipper/adapters/redis'

If you need to customize the adapter, you can add this to an initializer:

Flipper.configure do |config|
  config.adapter { Flipper::Adapters::Redis.new(Redis.new) }


## Internals

Each feature is stored in a redis hash, which means getting a feature is single query.

require 'flipper/adapters/redis'

client = Redis.new
adapter = Flipper::Adapters::Redis.new(client)
flipper = Flipper.new(adapter)

# Register a few groups.
Flipper.register(:admins) { |thing| thing.admin? }
Flipper.register(:early_access) { |thing| thing.early_access? }

# Create a user class that has flipper_id instance method.
User = Struct.new(:flipper_id)

flipper[:stats].enable_group :admins
flipper[:stats].enable_group :early_access
flipper[:stats].enable_actor User.new('25')
flipper[:stats].enable_actor User.new('90')
flipper[:stats].enable_actor User.new('180')
flipper[:stats].enable_percentage_of_time 15
flipper[:stats].enable_percentage_of_actors 45


print 'all keys: '
pp client.keys
# all keys: ["stats", "flipper_features", "search"]

print "known flipper features: "
pp client.smembers("flipper_features")
# known flipper features: ["stats", "search"]

puts 'stats keys'
pp client.hgetall('stats')
# stats keys
# {"boolean"=>"true",
#  "groups/admins"=>"1",
#  "actors/25"=>"1",
#  "percentage_of_time"=>"15",
#  "percentage_of_actors"=>"45",
#  "groups/early_access"=>"1",
#  "actors/90"=>"1",
#  "actors/180"=>"1"}

puts 'search keys'
pp client.hgetall('search')
# search keys
# {"boolean"=>"true"}

puts 'flipper get of feature'
pp adapter.get(flipper[:stats])
# flipper get of feature
# {:boolean=>"true",
#  :groups=>#<Set: {"admins", "early_access"}>,
#  :actors=>#<Set: {"25", "90", "180"}>,
#  :percentage_of_actors=>"45",
#  :percentage_of_time=>"15"}
Start flipping features with Flipper Cloud to get support, audit history, finer-grained permissions, multi-environment sync, and all of your projects in one place.

Prefer our open source Cloudless option? You can also choose from several tiers to sponsor Flipper on GitHub.