Modern, full-featured Redis client for Elixir.
This module provides the top-level API for single-connection usage. For other deployment modes and features, see:
Redis.Connection- single connection with full optionsRedis.Connection.Pool- connection poolingRedis.Cluster- cluster-aware client with slot routingRedis.Sentinel- sentinel-aware client with failoverRedis.PubSub- pub/sub subscriptionsRedis.PhoenixPubSub- Phoenix.PubSub adapterRedis.Cache- client-side caching with ETSRedis.Consumer- streams consumer group GenServerRedis.JSON- high-level JSON document APIRedis.Search- high-level search API (Meilisearch-inspired)Redis.PlugSession- Plug session storeRedis.Resilience- composed retry, circuit breaker, bulkheadRedis.Script- Lua script execution with SHA cachingRedis.Commands- 21 command builder modules
Quick Start
{:ok, conn} = Redis.start_link()
{:ok, "OK"} = Redis.command(conn, ["SET", "key", "value"])
{:ok, "value"} = Redis.command(conn, ["GET", "key"])Pipelining
{:ok, results} = Redis.pipeline(conn, [
["SET", "a", "1"],
["SET", "b", "2"],
["MGET", "a", "b"]
])Transactions
{:ok, [1, 2, 3]} = Redis.transaction(conn, [
["INCR", "counter"],
["INCR", "counter"],
["INCR", "counter"]
])Optimistic Locking
Redis.watch_transaction(conn, ["balance"], fn conn ->
{:ok, bal} = Redis.command(conn, ["GET", "balance"])
new_bal = String.to_integer(bal) + 100
[["SET", "balance", to_string(new_bal)]]
end)
Summary
Functions
Returns a child spec for supervision trees.
Sends a single command.
Sends a single command, raises on error.
Sends a command without waiting for a reply (CLIENT REPLY OFF/ON).
Sends multiple commands without waiting for replies.
Sends multiple commands in a single pipeline.
Starts a connection. Accepts a keyword list or a Redis URI string.
Executes commands in a MULTI/EXEC transaction.
Executes a WATCH-based optimistic locking transaction.
Types
@type conn() :: GenServer.server()
Functions
Returns a child spec for supervision trees.
Sends a single command.
Sends a single command, raises on error.
Sends a command without waiting for a reply (CLIENT REPLY OFF/ON).
Sends multiple commands without waiting for replies.
Sends multiple commands in a single pipeline.
@spec start_link(keyword() | String.t()) :: GenServer.on_start()
Starts a connection. Accepts a keyword list or a Redis URI string.
Executes commands in a MULTI/EXEC transaction.
@spec watch_transaction( conn(), [String.t()], (conn() -> [[String.t()]] | {:abort, term()}), keyword() ) :: {:ok, [term()]} | {:error, term()}
Executes a WATCH-based optimistic locking transaction.
Watches the given keys, calls fun to read values and build commands,
then executes in MULTI/EXEC. Retries automatically on conflict.
Redis.watch_transaction(conn, ["balance"], fn conn ->
{:ok, bal} = Redis.command(conn, ["GET", "balance"])
new_bal = String.to_integer(bal) + 100
[["SET", "balance", to_string(new_bal)]]
end)