0
0
RedisHow-ToBeginner · 3 min read

How to Use GET Command in Redis: Syntax and Examples

In Redis, use the GET command to retrieve the value stored at a specific key. The syntax is GET key, which returns the value as a string or null if the key does not exist.
📐

Syntax

The GET command in Redis retrieves the value associated with a given key.

  • GET: The command to fetch the value.
  • key: The name of the key whose value you want to get.

If the key exists, Redis returns its value as a string. If the key does not exist, it returns null.

redis
GET key
💻

Example

This example shows how to set a key and then retrieve its value using GET.

redis
SET greeting "Hello, Redis!"
GET greeting
Output
"Hello, Redis!"
⚠️

Common Pitfalls

Common mistakes when using GET include:

  • Trying to GET a key that does not exist, which returns null.
  • Using GET on keys holding non-string data types like hashes or lists, which causes an error.

Always ensure the key exists and holds a string value before using GET.

redis
GET non_existing_key
# Returns (nil)

HSET myhash field1 "value"
GET myhash
# Error: WRONGTYPE Operation against a key holding the wrong kind of value
Output
(nil) (error) WRONGTYPE Operation against a key holding the wrong kind of value
📊

Quick Reference

CommandDescriptionExample
GET keyRetrieve the string value of a keyGET greeting
SET key valueSet a string value for a keySET greeting "Hello"
DEL keyDelete a keyDEL greeting

Key Takeaways

Use GET followed by the key name to retrieve its string value in Redis.
GET returns null if the key does not exist.
GET only works on keys holding string values; using it on other types causes errors.
Always check if the key exists before using GET to avoid unexpected null results.