DeployU
Interviews / Databases / What is the difference between `DEL` and `UNLINK` in Redis?

What is the difference between `DEL` and `UNLINK` in Redis?

conceptual Core Concepts Interactive Quiz Code Examples

The Scenario

You are a backend engineer at a social media company. You are writing a new service that needs to delete a large number of keys from Redis.

You are not sure whether to use the DEL command or the UNLINK command.

The Challenge

Explain the difference between the DEL and UNLINK commands in Redis. What are the pros and cons of each approach, and which one would you choose for this use case?

Wrong Approach

A junior engineer might think that they are interchangeable. They might not be aware of the performance implications of choosing one over the other.

Right Approach

A senior engineer would be able to provide a detailed explanation of the differences between `DEL` and `UNLINK`. They would also be able to explain the trade-offs between each approach and would have a clear recommendation for which one to use in this use case.

Step 1: Understand the Key Differences

FeatureDELUNLINK
BlockingBlocks the server while the keys are being deleted.Does not block the server. The keys are deleted in the background.
PerformanceCan cause performance issues if you are deleting a large number of keys.More performant than DEL for deleting a large number of keys.
Use CasesWhen you need to delete a small number of keys.When you need to delete a large number of keys.

Step 2: Choose the Right Tool for the Job

For our use case, we should use the UNLINK command. This is because we are deleting a large number of keys, and we do not want to block the server.

Step 3: Code Examples

Here are some code examples that show the difference between the two approaches:

DEL:

DEL mykey1 mykey2 mykey3

UNLINK:

UNLINK mykey1 mykey2 mykey3

Practice Question

You need to delete a single key from Redis. Which of the following would be the most appropriate?