Skip to content

Commit 19f66a4

Browse files
authored
docs: add migration example to readme (#211)
1 parent 93deb53 commit 19f66a4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,56 @@ $ bundle exec rake test
222222

223223
You can see more information in the YAML file for GItHub actions.
224224

225+
## Migration
226+
This library might help you if you want to migrate your Redis from a standalone server to a cluster.
227+
228+
```ruby
229+
# frozen_string_literal: true
230+
231+
require 'bundler/inline'
232+
233+
gemfile do
234+
source 'https://rubygems.org'
235+
gem 'redis-cluster-client'
236+
end
237+
238+
src = RedisClient.config(
239+
host: ENV.fetch('REDIS_HOST'),
240+
port: ENV.fetch('REDIS_PORT')
241+
).new_client
242+
243+
dest = RedisClient.cluster(
244+
host: ENV.fetch('REDIS_CLUSTER_HOST'),
245+
port: ENV.fetch('REDIS_CLUSTER_PORT')
246+
).new_client
247+
248+
node = dest.instance_variable_get(:@router)
249+
.instance_variable_get(:@node)
250+
251+
src.scan do |key|
252+
slot = ::RedisClient::Cluster::KeySlotConverter.convert(key)
253+
node_key = node.find_node_key_of_primary(slot)
254+
host, port = ::RedisClient::Cluster::NodeKey.split(node_key)
255+
256+
if host.nil? || port.nil? || key.nil?
257+
print "WARN: host=#{host}, port=#{port}, key=#{key}: could not get the destination node\n"
258+
next
259+
end
260+
261+
src.blocking_call(10, 'MIGRATE', host, port, key, 0, 7, 'COPY', 'REPLACE')
262+
rescue ::RedisClient::Error => e
263+
print "ERROR: host=#{host}, port=#{port}, key=#{key}: (#{e.class}) #{e.message}\n"
264+
end
265+
```
266+
267+
The above example is too naive.
268+
It may not be enough performance in the production environment that has tons of keys.
269+
Since [MIGRATE](https://redis.io/commands/migrate/) command does locking internally, we can use [DUMP](https://redis.io/commands/dump/) and [RESTORE](https://redis.io/commands/restore/) commands instead.
270+
225271
## See also
272+
* https://redis.io/docs/reference/cluster-spec/
226273
* https://github.com/redis/redis-rb/issues/1070
227274
* https://github.com/redis/redis/issues/8948
228275
* https://github.com/antirez/redis-rb-cluster
276+
* https://www.youtube.com/@antirez
277+
* https://www.twitch.tv/thetrueantirez/

0 commit comments

Comments
 (0)