Fast, pipelined, resilient Redis client for Elixir.
Redix is a Redis client written in pure Elixir with focus on speed, correctness, and resiliency (that is, being able to automatically reconnect to Redis in case of network errors).
This README refers to the master
branch of Redix, not the latest released version on Hex. Make sure to checkt the documentation for the version you're using.
Add the :redix
dependency to your mix.exs
file:
defp deps() do
[{:redix, ">= 0.0.0"}]
end
Then, run mix deps.get
in your shell to fetch the new dependency.
Redix is simple: it doesn't wrap Redis commands with Elixir functions. It only provides functions to send any Redis command to the Redis server. A Redis command is expressed as a list of strings making up the command and its arguments.
Connections are started via start_link/0,1,2
:
{:ok, conn} = Redix.start_link(host: "example.com", port: 5000)
{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix)
Commands can be sent using Redix.command/2,3
:
Redix.command(conn, ["SET", "mykey", "foo"])
#=> {:ok, "OK"}
Redix.command(conn, ["GET", "mykey"])
#=> {:ok, "foo"}
Pipelines are just lists of commands sent all at once to Redis for which Redis replies with a list of responses. They can be used in Redix via Redix.pipeline/2,3
:
Redix.pipeline(conn, [["INCR", "foo"], ["INCR", "foo"], ["INCRBY", "foo", "2"]])
#=> {:ok, [1, 2, 4]}
Redix.command/2,3
and Redix.pipeline/2,3
always return {:ok, result}
or {:error, reason}
. If you want to access the result directly and raise in case there's an error, bang! variants are provided:
Redix.command!(conn, ["PING"])
#=> "PONG"
Redix.pipeline!(conn, [["SET", "mykey", "foo"], ["GET", "mykey"]])
#=> ["OK", "foo"]
Redix is resilient against network errors. For example, if the connection to Redis drops, Redix will automatically try to reconnect periodically at a given "backoff" interval. Look at the documentation for the Redix
module and at the "Reconnections" page in the documentation for more information on the available options and on the exact reconnection behaviour.
Redix doesn't support the Pub/Sub features of Redis. For that, there's redix_pubsub
.
Redix is low-level, but it's still built to handle most things thrown at it. For many applications, you can avoid pooling with little to no impact on performance. Read the "Real world usage" page in the documentation for more information on this and pooling strategies that work better with Redix.
To run the Redix test suite you will have to have Redis running locally. Redix requires a somewhat complex setup for running tests (because it needs a few instances running, for pub/sub and sentinel). For this reason, in this repository you'll find a docker-compose.yml
file so that you can use Docker and docker-compose to spin up all the necessary Redis instaces with just one command. Make sure you have Docker installed and then just run:
docker-compose up
Now, you're ready to run tests with the $ mix test
command.
Redix is released under the MIT license. See the license file.