0.0
No commit activity in last 3 years
No release in over 3 years
Prevent conflicts of several apps using same redis database or namespace. Allows to check if db is already taken and claim ownership of the db
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 10.0
~> 4.1
~> 3.0
~> 0.16
 Project Readme

Redis::Claim

Build Status Test Coverage Maintainability

Prevent conflicts of several apps using same Redis database or namespace. Allows to check if db is already taken and claim ownership of the db.

Common use case for the gem is microservices that share single physical redis instance. In such case it is common problem to make an error in redis url, or namespace. When passing redis url to the app it is very to make mistake in redis database number.

Installation

Add this line to your application's Gemfile:

gem 'redis-claim'

Usage

Assuming you have redis you want to claim

REDIS = Redis.new(url: "redis://redis-instance.aws.com:6379/12")

On the start of your application execute following code.

Redis::Claim.verify do |config|
  # Your redis instance you want to claim
  config.redis = REDIS      
  # Application name to detect any other service that claimed db  
  config.app_name = 'my cool application'
end

An Redis::Claim::Error will be raised in case of configuration or claim error

NOTE For rails you can add this code to custom initializer redis_claim.rb in app/config/initializers

Behaviour

Redis Claim has following execution steps

  1. Executed only once on start of the application.
  2. Sets lock key if it does not exist with setnx command
  3. Reads lock key if it exists
  4. Compare existing lock key is the same as app_name

IMPORTANT Redis claim will not detect db conflict if another application/service is not using it

Configuration options

When executing Redis::Claim.verify - passed block will receive configuraiton object with following properties:

Parameter Format Required Description
redis Object true Initialized Redis. Work with any object as long as it responds to get and setnx methods
app_name String true Name of your application. Should be uniuq for every microservice.
ignore_connection_error Boolean false Do not raise exception when cannot connect to Redis. Default: false
lock_key String false Name of the redis key where lock identifier will be recorded. Default: 'redis-claim:app'

Development

  1. Install Redis
  2. Run bin/setup to install dependencies
  3. Run tests rspec
  4. Add new test
  5. Add new code
  6. Go to step 3

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/matic-insurance/redis-claim. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Redis::Claim project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.