Skip to content

Instantly share code, notes, and snippets.

@supercaracal
Last active June 10, 2023 22:20
Show Gist options
  • Save supercaracal/31f8dfeb52aca0be693290c797f1988d to your computer and use it in GitHub Desktop.
Save supercaracal/31f8dfeb52aca0be693290c797f1988d to your computer and use it in GitHub Desktop.
A naive example for Redis migration from a standalone server to a cluster
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'redis-cluster-client'
end
src = RedisClient.config(url: ENV.fetch('REDIS_URL')).new_client
dest = RedisClient.cluster(nodes: ENV.fetch('REDIS_CLUSTER_URL')).new_client
node = dest.instance_variable_get(:@router).instance_variable_get(:@node)
src.scan do |key|
slot = RedisClient::Cluster::KeySlotConverter.convert(key)
node_key = node.find_node_key_of_primary(slot)
host, port = RedisClient::Cluster::NodeKey.split(node_key)
src.blocking_call(10, 'MIGRATE', host, port, key, 0, 7, 'COPY', 'REPLACE')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment