Skip to content
A Ruby client library for Redis
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A Ruby client library for the Redis key-value store.

Information about Redis

Redis is a key-value store with some interesting features:

  1. It's fast.
  2. Keys are strings but values are typed. Currently Redis supports strings, lists, sets, sorted sets and hashes. Atomic operations can be done on all of these types.

See the Redis homepage for more information.

Getting started

You can connect to Redis by instantiating the Redis class:

require "redis"

redis =

This assumes Redis was started with default values listening on localhost, port 6379. If you need to connect to a remote server or a different port, try:

redis = => "", :port => 6380)

Once connected, you can start running commands against Redis:

>> redis.set "foo", "bar"
=> "OK"

>> redis.get "foo"
=> "bar"

>> redis.sadd "users", "albert"
=> true

>> redis.sadd "users", "bernard"
=> true

>> redis.sadd "users", "charles"
=> true

How many users?

>> redis.scard "users"
=> 3

Is albert a user?

>> redis.sismember "users", "albert"
=> true

Is isabel a user?

>> redis.sismember "users", "isabel"
=> false

Handle groups:

>> redis.sadd "admins", "albert"
=> true

>> redis.sadd "admins", "isabel"
=> true

Users who are also admins:

>> redis.sinter "users", "admins"
=> ["albert"]

Users who are not admins:

>> redis.sdiff "users", "admins"
=> ["bernard", "charles"]

Admins who are not users:

>> redis.sdiff "admins", "users"
=> ["isabel"]

All users and admins:

>> redis.sunion "admins", "users"
=> ["albert", "bernard", "charles", "isabel"]

Storing objects

Redis only stores strings as values. If you want to store an object inside a key, you can use a serialization/deseralization mechanism like JSON:

>> redis.set "foo", [1, 2, 3].to_json
=> OK

>> JSON.parse(redis.get("foo"))
=> [1, 2, 3]

Executing multiple commands atomically

You can use MULTI/EXEC to run arbitrary commands in an atomic fashion:

redis.multi do
  redis.set "foo", "bar"
  redis.incr "baz"

More info

Check the Redis Command Reference or check the tests to find out how to use this client.


Fork the project and send pull requests. You can also ask for help at #redis on Freenode.

Something went wrong with that request. Please try again.