Skip to content

Commit 849bffd

Browse files
supercaracalbyroot
authored andcommitted
[ci skip] Add about cluster support to readme
1 parent c5c91fc commit 849bffd

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,46 @@ but a few so that if one is down the client will try the next one. The client
9595
is able to remember the last Sentinel that was able to reply correctly and will
9696
use it for the next requests.
9797

98+
## Cluster support
99+
100+
`redis-rb` supports [clustering](https://redis.io/topics/cluster-spec).
101+
102+
```ruby
103+
# Nodes can be passed to the client as an array of connection URLs.
104+
nodes = (7000..7005).map { |port| "redis://127.0.0.1:#{port}" }
105+
redis = Redis.new(cluster: nodes)
106+
107+
# You can also specify the options as a Hash. The options are the same as for a single server connection.
108+
(7000..7005).map { |port| { host: '127.0.0.1', port: port } }
109+
```
110+
111+
You can also specify only a subset of the nodes, and the client will discover the missing ones using the [CLUSTER NODES](https://redis.io/commands/cluster-nodes) command.
112+
113+
```ruby
114+
Redis.new(cluster: %w[redis://127.0.0.1:7000])
115+
```
116+
117+
If you want [the connection to be able to read from any replica](https://redis.io/commands/readonly), you must pass the `replica: true`. Note that this connection won't be usable to write keys.
118+
119+
```ruby
120+
Redis.new(cluster: nodes, replica: true)
121+
```
122+
123+
The calling code is responsible for [avoiding cross slot commands](https://redis.io/topics/cluster-spec#keys-distribution-model).
124+
125+
```ruby
126+
redis = Redis.new(cluster: %w[redis://127.0.0.1:7000])
127+
128+
redis.mget('key1', 'key2')
129+
#=> Redis::CommandError (CROSSSLOT Keys in request don't hash to the same slot)
130+
131+
redis.mget('{key}1', '{key}2')
132+
#=> [nil, nil]
133+
```
134+
135+
* The client automatically reconnects after a failover occurred, but the caller is responsible for handling errors while it is happening.
136+
* The client supports `MOVED` and `ASK` redirections transparently.
137+
98138
## Storing objects
99139

100140
Redis only stores strings as values. If you want to store an object, you

0 commit comments

Comments
 (0)