Lightweight Redis Client
Ruby Makefile
Clone or download
Latest commit 1a08a7c May 12, 2015


Lightweight Redis Client


Lightweight Redis Client inspired by redigo, a Redis client library for golang.


# Accepts a Redis URL and defaults to "redis://".
redis =

# Processes the command and returns the response."SET", "foo", "bar")

assert_equal "bar","GET", "foo")

# Pipelining is implemented by buffering commands,
# then calling Redic#commit
redis.queue("SET", "foo", "bar")
redis.queue("GET", "foo")

assert_equal ["OK", "bar"], redis.commit

You can provide the password and the database to be selected. The format for Redis URLs is redis://user:pass@host:port/db. As Redis only needs a password for authentication, the user can be omitted:

# Connect to localhost:6380 using "bar" as password and use the
# database 2. Both AUTH and SELECT commands are issued after
# connecting. The user part of the URL is not provided.
redis ="redis://:bar@localhost:6380/2")

It is also possible to configure a timeout for the connection. The default timeout is 10 seconds.

# Timeout expressed in microseconds.
redis =, 2_000_000)
redis.timeout == 2_000_000 #=> true

A client can be re-configured, forcing the next connection to be established with the new details:

redis ="redis://localhost:6379")

Here's one final example using both a Redis URL and a timeout:

# It's recommended to store the REDIS_URL as an environment
# variable. Use `fetch` to retrieve values that must be present,
# as it raises an error if the value is not found.


Both the initializer and the configure method accept a URL and a timeout.

In order to close the connection, call quit:

redis ="redis://localhost:6379")

With that command, "QUIT" is sent to Redis and the socket is closed.

Differences with redis-rb

Redic uses hiredis for the connection and for parsing the replies. There are no alternative connection drivers. Unlike redis-rb it doesn't define all the Redis commands, and instead it acts as a transport layer. The lock provided is smaller than that of redis-rb, as it only wraps the writing and reading from the connection. So even if both clients are thread-safe by default, the peformance of a smaller lock is marginally better.


When a client enters a subscribed mode, further reads to retrieve the messages are not thread safe. It is very important to take this into account and to create a different client if you need to send different operations while a client is subscribed to a channel.

# Example of pub/sub usage.
c1 =
c2 =

# After this command, the client is no longer thread safe."SUBSCRIBE", "foo")

# That's why we need to publish from a different connection."PUBLISH", "foo")

# Note that this operation is not thread safe.
assert_equal ["message", "foo", "value1"],

You can wrap thread unsafe operations in a mutex:

redis =

mutex =

mutex.synchronize do"MONITOR")

  # Display every command sent to Redis.
  loop do


You can install it using rubygems.

$ gem install redic