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 greetingOutput
"Hello, Redis!"
Common Pitfalls
Common mistakes when using GET include:
- Trying to
GETa key that does not exist, which returnsnull. - Using
GETon 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 valueOutput
(nil)
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Quick Reference
| Command | Description | Example |
|---|---|---|
| GET key | Retrieve the string value of a key | GET greeting |
| SET key value | Set a string value for a key | SET greeting "Hello" |
| DEL key | Delete a key | DEL 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.