Command: EXISTS


Overview

The EXISTS command is used to verify the existence of one or more keys in the database. It returns the count of keys that exist from the list provided.

Command Name

EXISTS

Description

Checks whether each provided key exists in the current database. The result is the number of keys that are found.

Syntax

  EXISTS <key> [<key> ...]
  
  • <key> : A string key to check for existence. You can provide multiple keys separated by space.

Permissions

  • Read-only command.

  • Requires no special privileges.

  • Any authenticated client can use this command.

Input Examples
Example 1: Single key check
  localhost:9219> EXISTS key1
  
Example 2: Multiple keys check
  localhost:9219> EXISTS key1 key2 key3
  
Output Examples
Example 1:
  Ok 1
  

(Indicates that one of the keys exists.)

Example 2:
  Ok 2
  

(Indicates that two of the provided keys exist.)


Behavior on Error

Missing argument:

  localhost:9219> EXISTS
  

Response:

  Error: InvalidKeyError: Key must be provided
  
  • If no key is supplied, the command returns an InvalidKeyError .

  • No partial processing is done—error occurs before any checking.


Use Cases

Use Case 1: Pre-check before GET/SET

Before performing an expensive GET or UPDATE , a user may want to confirm whether a key exists:

  EXISTS user:12345
  
Use Case 2: Bulk existence check

For cache population or eviction logic, checking which of a set of keys exist:

  EXISTS session:abc123 session:def456 session:xyz789
  
Use Case 3: Key health monitoring

Used by monitoring tools to check the presence of important heartbeat or sentinel keys.