Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Module

COPY

Copies the value of one Redis key to another key. It can target a different logical database and optionally replace an existing destination.

How it works

COPY duplicates the value held by one key under another key. It normally creates the destination in the connection's current logical database, but DB selects another database and REPLACE permits overwriting an existing destination.

Syntax

COPY source destination [DB destination-db] [REPLACE]

Parameters

NameTypeDescription
sourcekeyKey whose value is copied.
destinationkeyKey to create with the copied value.
destination-dbintegerOptional logical database index for the destination.
REPLACEflagOptional flag that removes an existing destination before copying.

Returns

Integer reply: 1 if source was copied; 0 if source was not copied.

Key points

  • COPY creates the destination key with the value held by the source key.
  • Without DB, COPY uses the connection's current logical database for the destination.
  • DB destination-db lets the destination use a different logical database index.
  • REPLACE enables overwriting an existing destination by deleting it first.
  • A successful copy returns integer 1.
  • A failed copy returns integer 0.
  • Copying collections can scale with the number of nested items.

Common mistakes

  • Without REPLACE, an existing destination is not overwritten and the command returns 0.
  • Clustered and Active-Active deployments require both keys to share a hash slot.

Example: Copy a string value

SET dolly "sheep"
COPY dolly clone
GET clone

Creates a source string key, copies its value to a new key, then reads the copied value.

When to use it

Duplicate cached data

Create a second key containing the same cached value without changing the original.

Cross-database placement

Populate a destination key in a selected logical database.

Overwrite an existing key

Refresh a destination key by replacing its prior value.

Interview questions

QWhat does COPY do, and what does its return value mean?

COPY duplicates the value associated with source under destination. It returns 1 when the copy succeeds and 0 when the source was not copied, including when the destination already exists without REPLACE.

Follow-up: How can an existing destination be overwritten? Use the REPLACE option, which removes the destination key before copying the value.

QHow does the DB option affect COPY?

By default, the destination is created in the logical database selected by the connection. The DB destination-db option directs the destination to a specified logical database index.

Follow-up: What deployment constraint applies to DB selection in clustered environments? The source and destination keys must be in the same hash slot for clustered or Active-Active databases.

QWhat is COPY's complexity?

COPY preserves the source value while creating a separate destination key. Its worst-case complexity is O(N) for collections, where N is the number of nested items, and O(1) for strings.

Follow-up: Why does data type matter for performance? Collection values require traversal proportional to their nested contents, while strings have constant-time copying.

  • DB
  • REPLACE
  • SET
  • GET
  • DEL

Source: COPY. Checked 2026-10-16.