• Stars
    star
    1,062
  • Rank 43,462 (Top 0.9 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast, pipelined, resilient Redis driver for Elixir. ๐Ÿ›

Redix

hex.pm badge Documentation badge CI Coverage Status

Fast, pipelined, resilient Redis client for Elixir.

dalle

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 main branch of Redix, not the latest released version on Hex. Make sure to check the documentation for the version you're using.

Features

  • Idiomatic interface for sending commands to Redis
  • Pipelining
  • Resiliency (automatic reconnections)
  • Pub/Sub
  • SSL
  • Redis Sentinel

Installation

Add the :redix dependency to your mix.exs file. If you plan on connecting to a Redis server over SSL you may want to add the optional :castore dependency as well:

defp deps do
  [
    {:redix, "~> 1.1"},
    {:castore, ">= 0.0.0"}
  ]
end

Then, run mix deps.get in your shell to fetch the new dependencies.

Usage

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"]

Resiliency

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.

Redis Sentinel

Redix supports Redis Sentinel out of the box. You can specify a list of sentinels to connect to when starting a Redix (or Redix.PubSub) connection. Every time that connection will need to connect to a Redis server (the first time or after a disconnection), it will try to connect to one of the sentinels in order to ask that sentinel for the current primary or a replica.

sentinels = ["redis://sent1.example.com:26379", "redis://sent2.example.com:26379"]
{:ok, primary} = Redix.start_link(sentinel: [sentinels: sentinels, group: "main"])
Terminology

Redix doesn't support the use of the terms "master" and "slave" that are usually used with Redis Sentinel. I don't think those are good terms to use, period. Instead, Redix uses the terms "primary" and "replica". If you're interested in the discussions around this, this issue in the Redis repository might be interesting to you.

Pub/Sub

A Redix.PubSub process can be started via Redix.PubSub.start_link/2:

{:ok, pubsub} = Redix.PubSub.start_link()

Most communication with the Redix.PubSub process happens via Elixir messages (that simulate a Pub/Sub interaction with the pub/sub server).

{:ok, pubsub} = Redix.PubSub.start_link()

Redix.PubSub.subscribe(pubsub, "my_channel", self())
#=> {:ok, ref}

Confirmation of subscriptions is delivered as an Elixir message:

receive do
  {:redix_pubsub, ^pubsub, ^ref, :subscribed, %{channel: "my_channel"}} -> :ok
end

If someone publishes a message on a channel we're subscribed to:

receive do
  {:redix_pubsub, ^pubsub, ^ref, :message, %{channel: "my_channel", payload: "hello"}} ->
    IO.puts("Received a message!")
end

Using Redix in the Real Worldโ„ข

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.

Contributing

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 instances 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.

License

Redix is released under the MIT license. See the license file.

More Repositories

1

vim-gotham

Code never sleeps in Gotham City. ๐ŸŒƒ
Vim Script
1,215
star
2

stream_data

Data generation and property-based testing for Elixir. ๐Ÿ”ฎ
Elixir
843
star
3

corsica

Elixir library for dealing with CORS requests. ๐Ÿ–
Elixir
513
star
4

xandra

Fast, simple, and robust Cassandra/ScyllaDB driver for Elixir.
Elixir
392
star
5

gotham-contrib

Ports of the Gotham colorscheme for common editors and terminal emulators ๐ŸŒƒ
Shell
242
star
6

guide_async_processing_in_elixir

A Livebook-based guide to async processing in Elixir. ๐Ÿƒ
127
star
7

vim-lengthmatters

Highlight the flooding part of an overly long line ๐Ÿ“
Vim Script
82
star
8

short_maps

โš ๏ธRetiredโš ๏ธ library that provided a ~m sigil for ES6-like map destructuring in Elixir.
Elixir
76
star
9

vim-textobj-xmlattr

A vim text object for XML/HTML attributes.
Vim Script
74
star
10

saul

Data validation and conformation library for Elixir.
Elixir
67
star
11

redix_pubsub

Deprecated Redis Pub/Sub client for Elixir. Now built-in into Redix.
Elixir
64
star
12

protohackers_in_elixir

Code for a video series I'm making on solving protohackers.com challenges in Elixir. ๐Ÿ“บ
Elixir
55
star
13

plug_heartbeat

A plug for responding to heartbeat requests.
Elixir
36
star
14

nimble_lz4

LZ4 compression for Elixir using Rust NIFs. ๐Ÿ—œ
Elixir
25
star
15

vim-textobj-erb

A Vim text object for ERB blocks.
Vim Script
20
star
16

dotfiles

My dotfiles ๐Ÿฅ‘
Shell
19
star
17

from_zero_to_hero_with_elixir

Elixir 101 training. Done by @ericmj and me.
Elixir
19
star
18

convertat

An Elixir library for converting from and to arbitrary bases.
Elixir
17
star
19

workshop-parallel-computation-with-elixir

Elixir
16
star
20

emacs.d

My Emacs config. โš™๏ธ (buuuuut I don't use Emacs anymore)
Emacs Lisp
15
star
21

training_otp_in_elixir

"OTP in Elixir" training done at Elixir Club Kyiv in ~2020. ๐Ÿ‘ฉโ€๐Ÿซ
Elixir
12
star
22

testing_aws_in_elixir

Support code for the blog post "Testing AWS in Elixir" ๐Ÿ•โ€๐Ÿฆบ
Elixir
12
star
23

small_ints

varint and ZigZag encoding/decoding for Erlang
Erlang
11
star
24

ecto_unix_timestamp

Ecto type for datetimes stored and cast as Unix timestamps. ๐Ÿ•ฐ๏ธ
Elixir
11
star
25

bases

A Ruby gem to convert from and to any base.
Ruby
10
star
26

concurrent_data_processing_in_elixir

Material for the "Concurrent Data Processing in Elixir" training.
10
star
27

connected-web-with-elixir-and-phoenix

Code for my Elixir/Phoenix workshop (currently at FunctionalConf India 2019).
Elixir
5
star
28

whatyouhide.github.io

My personal website. ๐Ÿ‘จโ€๐Ÿ’ป
SCSS
4
star
29

.vim

Vim configuration. Left to oblivion. โšฐ๏ธ
Vim Script
3
star
30

.atom

Atom configuration โš›๏ธ (buuut I don't use Atom anymore)
CSS
2
star
31

rack-domain

Rack middleware for dispatching to Rack apps based on the request domain.
Ruby
2
star
32

vim-tmux-syntax

Vim syntax for tmux configuration files.
Vim Script
2
star
33

advent_of_code_2022

Solutions to Advent of Code 2022, in Rust. ๐Ÿฆ€
Rust
1
star
34

intro-to-git-ingegneria

Git talk @ Univaq.
CSS
1
star
35

whatyouhide

Special repository for my GitHub homepage. ๐Ÿ›‹๏ธ
1
star