// Redis error reference

BUSYKEY: Target key already exists

A command that restores or moves a key found the target name occupied: RESTORE, MIGRATE and friends refuse without REPLACE.

Error prefix
BUSYKEY
類別
Memory and persistence
錯誤訊息
BUSYKEY Target key name already exists.

它的意義

BUSYKEY comes from commands that create a key from elsewhere: RESTORE writes a serialized value, and MIGRATE moves a key between instances. Both refuse to overwrite an existing target unless you pass REPLACE, and the refusal is this error. It is a collision check, not corruption: the target key may be live data, and the decision is yours.

Pass REPLACE when the source is authoritative, such as a migration replaying state; rename the target when both keys matter. Related commands behave differently on purpose: COPY without REPLACE simply returns 0 when the target exists, so scripts ported between the two need adjusting.

常見原因

  • RESTORE was called on a key name that already exists, without REPLACE.
  • MIGRATE moved a key to an instance where the same name had already been created.
  • A migration or cache-warming job re-ran without idempotency and replayed keys that already landed.

Find the cause

EXISTS target-key
TYPE target-key
OBJECT ENCODING target-key
# What currently occupies the name you are about to write

如何修正

  1. Check the target before the operation:

    EXISTS target-key
    TYPE target-key
  2. Pass REPLACE when the source of truth is the key being moved or restored:

    MIGRATE 127.0.0.1 6380 mykey 0 5000 REPLACE
  3. When both keys are real data, rename the target first instead of overwriting it:

    RENAME target-key target-key-backup

Other ways to handle it

For bulk moves, MIGRATE with COPY leaves the source in place and REPLACE makes re-runs safe; together they turn a migration into an idempotent operation. For backup-style restore flows, write to a versioned name (mykey:v42) and switch over with RENAME.

安全地重現問題

redis-server --port 7309 --save '' &
redis-server --port 7310 --save '' &
redis-cli -p 7309 SET mykey hello
redis-cli -p 7310 SET mykey occupied
redis-cli -p 7309 MIGRATE 127.0.0.1 7310 mykey 0 5000
# (error) BUSYKEY Target key name already exists.
redis-cli -p 7309 MIGRATE 127.0.0.1 7310 mykey 0 5000 REPLACE
# OK

問題

Is BUSYKEY like a duplicate key error?

In spirit, yes: the name you tried to create is taken. The difference is which commands can raise it, RESTORE and MIGRATE, while SET overwrites silently.

Did MIGRATE lose the key when this happened?

No. MIGRATE is atomic from the source's point of view: on BUSYKEY without REPLACE, the key stays on the source untouched.

相關錯誤

More memory and persistence errors

程式碼和訊息文字來自 Redis documentation and source code。瀏覽完整的 Redis error list 或 搜尋每個資料庫錯誤.

// 錯誤更少,修復更快

Work on Redis in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for Redis and the other databases you use.

本頁文字與資料: CC BY 4.0,請註明出處 SQLly (sqlly.app)。 程式碼與 SQL 片段: MIT. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。