Data Migration

Difficulty: Intermediate

Question

How do you move a key, or a whole dataset, from one Redis instance to another?

Answer

For individual keys, use MIGRATE. It serializes the key, sends it to the target instance, restores it there and, unless you pass COPY, deletes it from the source, all as one blocking operation on both sides. For a whole dataset, do not loop over MIGRATE; set the new server up as a replica (REPLICAOF) or load an RDB file, then switch clients over.

How MIGRATE works

MIGRATE host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH password | AUTH2 username password] [KEYS key ...]

Under the hood it is DUMP on the source, RESTORE on the target, then DEL on the source:

  • DUMP key returns the value in Redis's internal serialization format, including an RDB version and a CRC64 checksum.
  • RESTORE key ttl payload [REPLACE] [ABSTTL] recreates it. The TTL is carried over by MIGRATE.
  • COPY keeps the source key; REPLACE overwrites an existing key on the target (otherwise you get a BUSYKEY error).
  • KEYS k1 k2 ... (with an empty string in the key position) moves many keys in one call.
  • It replies OK, or NOKEY if none of the keys existed.

The operation is atomic in the sense that the key exists in at least one instance at any time. If it times out, the key may end up in both, so retries should use REPLACE.

Gotchas

  • It blocks both servers for the duration. A 500 MB sorted set or hash will freeze the source and the target while it transfers. Find big keys first with redis-cli --bigkeys or MEMORY USAGE.
  • Version compatibility. A DUMP payload from a newer Redis can use an RDB version the older target cannot read, so RESTORE fails. Migrate from older to newer, not the other way.
  • Redis Cluster allows only DB 0, so destination-db must be 0.
  • Authentication. AUTH2 (Redis 6+) is needed when the target uses ACL users.

Choosing a method

GoalBest tool
Move a few keys between instancesMIGRATE
Copy keys across Redis versions or to a managed serviceDUMP/RESTORE via a script, or a tool like RIOT or RedisShake
Move a whole instance with minimal downtimeREPLICAOF old-host 6379 on the new server, wait for sync, then fail over
Restore from backupCopy the RDB file into the data directory and start Redis
Rebalance a Redis Clusterredis-cli --cluster reshard or rebalance, which uses MIGRATE slot by slot

Real example: moving to a managed cluster

A common real-world task is moving a self-hosted Redis to a managed service where you cannot run REPLICAOF against it. The usual approach is a tool that reads the source via SCAN plus DUMP (or by acting as a replica) and writes RESTORE to the target, with a short dual-write or read-only window during cutover. Test the cutover on staging and verify key counts with INFO keyspace and sample checks before switching production traffic.

What MIGRATE does for one key. The source serializes, the target restores, then the source deletes, all while both are blocked.
  1. Client → Source Redis: MIGRATE target 6379 cart:42 0 5000
  2. Source Redis → Target Redis: RESTORE cart:42 ttl <DUMP payload>
  3. Target Redis → Source Redis: OK
  4. Source Redis → Client: OK (source key deleted unless COPY)

Code examples

MIGRATE single and multiple keys

# Move one key to 10.0.2.15, DB 0, 5000 ms timeout
MIGRATE 10.0.2.15 6379 cart:42 0 5000

# Copy several keys, overwrite on target, authenticate as an ACL user
MIGRATE 10.0.2.15 6379 "" 0 5000 COPY REPLACE AUTH2 migrator S3cret KEYS cart:43 cart:44 session:ab12

# The manual equivalent
DUMP cart:42
# on the target:
# RESTORE cart:42 0 "<payload from DUMP>" REPLACE

The empty string in the key position switches MIGRATE to multi-key mode with KEYS. RESTORE with ttl 0 creates the key without expiry.

Migrating a whole instance via replication

# On the NEW server
redis-cli -h new-redis REPLICAOF old-redis 6379
redis-cli -h new-redis INFO replication | grep -E 'master_link_status|master_sync_in_progress'

# When master_link_status:up and offsets match, stop writes on old,
# then promote the new server and repoint clients
redis-cli -h new-redis REPLICAOF NO ONE

Replication copies everything, including TTLs, without blocking clients for long, which is why it beats looping over MIGRATE for full moves.

Key points

  • MIGRATE = DUMP + RESTORE + DEL, blocking both instances during transfer
  • COPY keeps the source key, REPLACE overwrites the target, KEYS moves many at once
  • On timeout the key can exist in both places; retry with REPLACE
  • DUMP payloads are version-specific; migrate from older to newer Redis
  • For whole instances use replication or RDB files, not MIGRATE loops

Common follow-up questions

  • How does Redis Cluster move slots between nodes during resharding?
  • What problems do big keys cause during migration?
  • How would you migrate to a managed Redis that does not allow REPLICAOF?
  • How do you verify a migration was complete?

Interview tips

  • Explain the DUMP/RESTORE/DEL internals and that it blocks both servers
  • Separate single-key moves from whole-dataset moves; picking replication for the latter shows judgement
  • Mention big keys and version compatibility as real-world gotchas

Concepts covered

MIGRATE, DUMP and RESTORE, Resharding, Replication-based migration, Big keys

More Redis & Caching questions