GithubHelp home page GithubHelp logo

mohammedamarnah / redis-pool Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 29 KB

A simple Redis dynamic-sized connection pool.

Home Page: https://rubygems.org/gems/redis_dynamic/

License: MIT License

Ruby 100.00%
redis ruby connection-pool redis-client rails

redis-pool's Introduction

redis-pool Build Status

A simple redis dynamic-sized connection pool. There are several implementations of connection pools in ruby, but none of them supports dynamic sizes. Any connection pool would open a static amount of connections and would never kill them even if they are idle and not being used. What if you need a large number of connections in very rare cases, but normally will not use more than a couple of ones? This pool is a great choice!

Why redis-pool?

Imagine that you have an application that in very rare cases (let's say X minutes-hours per day) gets a huge amount of traffic that needs a high number of connections, but all the other times you would need only a couple of ones open. Having a big amount of connections open (while the connections are idle) is a memory consuming operations.

I took inspiration from where I work: we have N servers, each server instantiates M puma instances (ruby on rails server) and each instance fires up K redis connections on boot-up. This makes the total open connections N x M x K redis connections! Imagine adding one instance? or one server!

This gem was implemented heavily based on two main implementations:

1- mperham's connection_pool

2- rails' ActiveRecord Connection Pool

The former provides an efficient connection pool implementation that creates connections lazily (only upon actual need of that connection), but keeps the connections alive once they're opened. The latter provides a connection pool implementation (specific for rails' database interfaces) that supports a dynamic size of the connection pool. This gem is a combination of the perks of the two, combines the lazy evaluation with idle-connection killing support.

Here's the difference between normal connection-pools and redis-pool shown in a simple graph: difference

Installation

Install the gem using the gem install command or add it to your Gemfile and run bundle install

# bundler install: add this to your Gemfile
gem 'redis_dynamic'

# manual install
gem install redis_dynamic

Usage

Easily create a pool:

pool = RedisPool.new()
# You can set the maximum size of the pool
pool = RedisPool.new(max_size: 10)

You can set several options for the pool. i.e: connection_timeout: raises a TimeoutError after connection_timeout of trying to get hold of a connection. reaping_frequency: checks every reaping_frequency for any idle connections to kill. idle_timeout: kills any connection that has been idle for idle_timeout.

Use the connections using with_conn:

pool = RedisPool.new(max_size: 10, connection_timeout: 5, idle_timeout: 300)
pool.with_conn { |conn|
    conn.get('hello')
}

You can override the default connection_timeout using with_conn(timeout):

pool = RedisPool.new()
pool.with_conn(5) { |conn|
    conn.get('hello')
}

Pool Stats

You can also get stats about all connections that are currently alive in the pool. Stats include the id of each connection, when it was created alive_since and when it was last used at last_used_at.

pool = RedisPool.new()
pool.with_conn { |conn| conn.get('hello') }
pool.stats # {:available_to_create=>4, :total_available=>5, :connections_stats=>[{:id=>0, :alive_since=>2021-01-25 13:41:12.749529574 UTC, :last_used_at=>2021-01-25 13:41:12.749532585 UTC}]}

Redis Configuration

You can specify custom redis configuration when initializing the pool.

pool = RedisPool.new(redis_config: {host: 'localhost', port: 5000, db: 10})

redis-pool's People

Contributors

mohammedamarnah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redis-pool's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.