1. Introduction
In this section, we’ll understand the most fundamental concept in Redis — storing and retrieving key-value pairs.
At its core, Redis is a key-value data store, meaning that every piece of data you store in Redis is associated with a key that uniquely identifies it. This is similar to how variables in programming languages store values under specific names.
While Redis supports a variety of advanced data structures such as lists, sets, hashes, and sorted sets, we’ll begin with the most basic and essential one — strings.
2. Understanding Keys and Values in Redis
In Redis, a key acts like a variable name, and a value is the data associated with that key.
If you’ve worked with languages like Java or Python, think of a Redis key as a variable, and its value as the content stored in that variable.
However, Redis is not a strongly typed system. In traditional programming languages, variables are typed — for example, integers, floats, or booleans. In Redis, everything is stored as a string, regardless of its apparent type.
Key Characteristics of Redis Keys
- Keys can be any binary sequence — they can include letters, digits, special characters, or even spaces (when enclosed in quotes).
- Redis can hold over 4 billion keys in a single database.
- Each key can be as large as 512 MB.
- Keys are case-sensitive and must match exactly when retrieved.
3. The SET
and GET
Commands
The two most fundamental Redis commands are:
SET key value
— stores a value under the given key.GET key
— retrieves the value associated with that key.
Let’s see how these work using the Redis CLI (redis-cli
).
Example 1: Basic String Storage
SET a Hello
Output:
OK
Redis responds with OK
to indicate that it successfully created a key named a
and stored the value Hello
.
Now, retrieve it with:
GET a
Output:
"Hello"
The GET
command fetches the value associated with the key a
.
4. Accessing Nonexistent Keys
If you try to fetch a key that doesn’t exist, Redis will return (nil)
:
GET c
Output:
(nil)
This behavior is similar to receiving a null
value in programming — it means the key has not been defined or stored yet.
5. Storing Numeric Values
Even though Redis stores all data as strings internally, you can still store numbers as values.
SET number 123 GET number
Output:
"123"
Redis doesn’t enforce type restrictions. You can overwrite the same key with a different kind of value anytime.
SET number 100.25 GET number
Output:
"100.25"
Redis simply updates the value, still treating it as a string.
6. Using Longer Keys and Values
Keys and values in Redis can be long and descriptive. You can use spaces inside them if you wrap them in quotes.
SET myKey "This is a long string value stored in Redis." GET myKey
Output:
"This is a long string value stored in Redis."
If you omit quotes for keys or values that contain spaces, Redis will interpret each space-separated word as a separate argument, leading to a syntax error.
Incorrect Example:
SET some key some value
Redis interprets "some"
as the key and "key"
as part of another argument, resulting in an error.
Correct Example:
SET "some key" "some value"
Output:
OK
Retrieve the value with:
GET "some key"
Output:
"some value"
7. Keys Are Case-Sensitive and Must Match Exactly
Redis keys are case-sensitive, meaning even a slight difference in capitalization makes them different keys.
SET Key1 "First" GET key1
Output:
(nil)
Although Key1
and key1
look similar, Redis treats them as separate keys.
8. Using Different Data-Like Values
You can store any value type, but Redis will always treat it as a string.
SET a "100" SET b "true" SET c "false" SET d "null" GET b GET d
Output:
"true" "null"
There are no dedicated data types for boolean or null in Redis — these are just plain text strings.
9. Recommended Key Naming Conventions
Even though Redis is an in-memory data store and doesn’t have tables or collections, developers often organize keys logically by following naming conventions.
Let’s say you have a user table like this:
id | name |
---|---|
1 | John |
2 | Alice |
In an API design, you might expose endpoints like:
/user/1/name → John /user/2/name → Alice
You can use a similar logical pattern in Redis using colons (:) as separators.
SET user:1:name John SET user:2:name Alice
Now retrieve them:
GET user:1:name GET user:2:name
Output:
"John" "Alice"
Why this pattern is useful:
- It keeps your Redis keyspace well-organized.
- It’s easy to read and reason about.
- It mirrors RESTful API or database naming conventions.
10. Important Notes on Key Structure
Colons (:
) in Redis have no special meaning — they are treated as regular characters in the key name. Their use is purely for readability and structure.
You can use any separator such as |
or .
, but it’s important to remain consistent.
Example of inconsistency (not recommended):
SET user:1:name John SET users.1.name Mike
Although both will work, inconsistent key patterns can cause confusion and make your dataset harder to maintain.
Best Practice:
Use a consistent and meaningful naming convention such as:
object:id:field
For example:
product:101:name employee:7:department order:5001:status
11. Summary
Here’s a quick recap of what you’ve learned:
- Redis stores everything as key-value pairs, and every value is a string internally.
- The basic commands are:
SET key value
→ Store dataGET key
→ Retrieve data
- Keys are case-sensitive and must match exactly.
- If you request a key that doesn’t exist, Redis returns
(nil)
. - Always use quotes when your key or value contains spaces.
- Follow consistent naming conventions like
object:id:field
for organized and readable data storage.