Bird
Raised Fist0
Redisquery~10 mins

EXISTS to check key existence in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - EXISTS to check key existence
Start
↓
Check if key exists
↓
Return 1
↓
Return 0
↓
End
The EXISTS command checks if a key is present in the database and returns 1 if yes, 0 if no.
Execution Sample
Redis
EXISTS mykey
EXISTS otherkey
Check if 'mykey' and 'otherkey' exist in Redis, returning 1 or 0.
Execution Table
StepCommandKey CheckedExists?Returned Value
1EXISTS mykeymykeyYes1
2EXISTS otherkeyotherkeyNo0
💡 All commands executed; EXISTS returns 1 if key exists, else 0.
Variable Tracker
KeyInitial StateAfter Step 1After Step 2
mykeyunknownexistsexists
otherkeyunknownunknowndoes not exist
Key Moments - 2 Insights
Why does EXISTS return 0 for 'otherkey'?
Because 'otherkey' does not exist in the database at step 2, as shown in the execution_table row 2.
Can EXISTS return values other than 0 or 1?
No, for a single key EXISTS returns only 1 if the key exists or 0 if it does not, as shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does EXISTS return for 'mykey' at step 1?
A0
B1
CError
Dnull
💡 Hint
Check the 'Returned Value' column for step 1 in the execution_table.
At which step does EXISTS find that the key does not exist?
AStep 1
BBoth steps
CStep 2
DNeither step
💡 Hint
Look at the 'Exists?' column in the execution_table.
If 'mykey' was deleted before step 1, what would EXISTS return?
A0
BError
C1
DDepends on Redis version
💡 Hint
EXISTS returns 0 if the key does not exist, as shown in the execution_table for 'otherkey'.
Concept Snapshot
EXISTS key
- Checks if a key exists in Redis
- Returns 1 if key exists
- Returns 0 if key does not exist
- Simple way to test key presence
Full Transcript
The EXISTS command in Redis checks if a given key is present in the database. When you run EXISTS with a key name, Redis returns 1 if the key exists and 0 if it does not. For example, if 'mykey' exists, EXISTS mykey returns 1. If 'otherkey' does not exist, EXISTS otherkey returns 0. This command helps you quickly check key presence without retrieving the key's value.

Practice

(1/5)
1. What does the Redis command EXISTS key do?
easy
A. Checks if the specified key exists and returns 1 if yes, 0 if no
B. Deletes the specified key from the database
C. Returns the value stored at the specified key
D. Sets a new key with a default value

Solution

  1. Step 1: Understand the purpose of EXISTS

    The EXISTS command is used to check if a key is present in the Redis database.
  2. Step 2: Know the return values

    It returns 1 if the key exists, and 0 if it does not.
  3. Final Answer:

    Checks if the specified key exists and returns 1 if yes, 0 if no -> Option A
  4. Quick Check:

    EXISTS returns 1 or 0 for key presence [OK]
Hint: EXISTS returns 1 if key is found, else 0 [OK]
Common Mistakes:
  • Thinking EXISTS deletes keys
  • Assuming EXISTS returns the key's value
  • Confusing EXISTS with SET command
2. Which of the following is the correct syntax to check if a key named user:1 exists in Redis?
easy
A. EXIST user:1
B. EXISTS user:1
C. CHECK user:1
D. HASKEY user:1

Solution

  1. Step 1: Identify the correct Redis command

    The correct command to check key existence is EXISTS.
  2. Step 2: Verify the syntax

    The syntax is EXISTS keyname, so EXISTS user:1 is correct.
  3. Final Answer:

    EXISTS user:1 -> Option B
  4. Quick Check:

    EXISTS keyname is correct syntax [OK]
Hint: EXISTS is the exact command to check keys [OK]
Common Mistakes:
  • Using non-existent commands like EXIST or HASKEY
  • Misspelling EXISTS
  • Adding extra keywords after EXISTS
3. Given the following Redis keys: user:1 and user:2 exist, but user:3 does not. What is the result of EXISTS user:1 user:3 user:2?
medium
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Understand EXISTS with multiple keys

    When given multiple keys, EXISTS returns the count of keys that exist.
  2. Step 2: Count existing keys in the list

    Keys user:1 and user:2 exist, but user:3 does not, so count is 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    EXISTS counts existing keys = 2 [OK]
Hint: EXISTS with multiple keys returns count of existing keys [OK]
Common Mistakes:
  • Assuming EXISTS returns 1 if any key exists
  • Thinking it returns all keys found
  • Confusing count with boolean result
4. You run the command EXISTS without any key arguments in Redis. What happens?
medium
A. Returns 0 because no keys were specified
B. Deletes all keys in the database
C. Returns an error due to missing key argument
D. Returns 1 by default

Solution

  1. Step 1: Check command requirements

    The EXISTS command requires at least one key argument to check.
  2. Step 2: Understand Redis behavior on missing arguments

    If no keys are provided, Redis returns a syntax error indicating missing arguments.
  3. Final Answer:

    Returns an error due to missing key argument -> Option C
  4. Quick Check:

    EXISTS needs keys, else error [OK]
Hint: EXISTS must have keys; no keys causes error [OK]
Common Mistakes:
  • Assuming it returns 0 with no keys
  • Thinking it returns 1 by default
  • Believing it deletes keys without arguments
5. You want to check if any of the keys session:1, session:2, or session:3 exist, and if none exist, set session:1 with a value. Which Redis command sequence correctly uses EXISTS to do this safely?
hard
A. EXISTS session:1 session:2 session:3\nIF 1 THEN SET session:1 'active' END
B. IF EXISTS session:1 session:2 session:3 == 0 THEN SET session:1 'active' END
C. EXISTS session:1 session:2 session:3\nIF EXISTS == 0 THEN SET session:1 'active' END
D. EXISTS session:1 session:2 session:3\nIF 0 THEN SET session:1 'active' END

Solution

  1. Step 1: Use EXISTS to check multiple keys

    EXISTS returns the count of existing keys among the given list.
  2. Step 2: Use conditional logic to set key if none exist

    If EXISTS returns 0, it means none of the keys exist, so we can safely set session:1.
  3. Step 3: Choose the correct syntax for conditional check

    EXISTS session:1 session:2 session:3\nIF 0 THEN SET session:1 'active' END shows the correct sequence: run EXISTS, then if result is 0, set the key.
  4. Final Answer:

    EXISTS session:1 session:2 session:3\nIF 0 THEN SET session:1 'active' END -> Option D
  5. Quick Check:

    EXISTS count 0 means no keys, then set key [OK]
Hint: Check EXISTS count == 0 before setting key [OK]
Common Mistakes:
  • Using incorrect conditional syntax
  • Not checking all keys at once
  • Setting key without checking existence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard