Learnitweb

Redis: Working with Simple Key-Value Pairs

1. Introduction

In this lecture, we’ll explore the most fundamental concept in Redis — storing and retrieving key-value pairs.
Redis is a key-value data store at its core, and everything you store in Redis is associated with a key that uniquely identifies the data.

Redis supports several advanced data structures such as lists, sets, hashes, and sorted sets, but in this session, we’ll start with the simplest data type — strings.


2. Understanding Keys and Values in Redis

In Redis, a key is like a variable name in programming languages such as Java or Python, and a value is the data stored in that variable.

Unlike strongly typed languages (where variables must have specific types like int, float, or boolean), Redis treats everything as strings.
Even if you store a number, Redis will store it as a string internally.

Key characteristics:

  • A key can be any binary sequence, meaning it can contain letters, numbers, symbols, or even spaces (with quotes).
  • Redis can hold over 4 billion keys.
  • Each key can have a maximum size of 512 MB.
  • Keys are case-sensitive and must match exactly when retrieved.

3. The SET and GET Commands

The two most basic Redis commands are:

  • SET key value – Stores a value with the given key.
  • GET key – Retrieves the value stored for that key.

Let’s try these out using the Redis CLI (redis-cli).

Example 1: Basic String Storage

SET a Hello

Output:

OK

This means Redis successfully created a key named a and stored the value Hello.

Now, retrieve it using:

GET a

Output:

"Hello"

The GET command fetches the value stored for key a.


4. Accessing Nonexistent Keys

If you try to access a key that doesn’t exist, Redis will return (nil):

GET c

Output:

(nil)

This is similar to getting a null value in programming — it simply means the key has not been created.


5. Storing Numeric Values

Although Redis stores everything as a string, you can still store numeric data:

SET number 123
GET number

Output:

"123"

Redis doesn’t complain whether you store a string or a number — it treats both as strings.
You can even overwrite the same key with different data types:

SET number 100.25
GET number

Output:

"100.25"

Redis simply updates the key’s value.


6. Using Longer Keys and Values

Redis keys and values can be arbitrary strings — including long ones.

SET myKey "This is a long string value stored in Redis."
GET myKey

Output:

"This is a long string value stored in Redis."

If your key or value contains spaces, you must wrap it in double quotes; otherwise, Redis will misinterpret it as multiple parameters.

Example (incorrect):

SET some key some value

Redis will treat "some" as the command argument for the key and throw a syntax error.

Correct usage:

SET "some key" "some value"

Output:

OK

Now you can retrieve it with:

GET "some key"

Output:

"some value"

7. Keys Are Case-Sensitive and Exact

Redis keys are case-sensitive, and the key name must match exactly.

Example:

SET Key1 "First"
GET key1

Output:

(nil)

Even though Key1 and key1 look similar, Redis treats them as two completely different keys.


8. Using Different Data-Like Values

You can store any kind of value (string representation):

SET a "100"
SET b "true"
SET c "false"
SET d "null"
GET b
GET d

Redis will simply store and return these as strings:

"true"
"null"

There are no special types for boolean or null — everything is stored as plain text.


9. Recommended Key Naming Conventions

Even though Redis doesn’t have the concept of tables or collections like SQL or MongoDB, developers usually follow certain naming conventions to organize data logically.

For example, suppose you have a table named user with columns id and name:

id | name
-----------
1  | John
2  | Alice

If you were designing an API for this, it might expose endpoints like:

/user/1/name → John
/user/2/name → Alice

You can follow a similar naming pattern in Redis using colons (:) to separate logical segments.

Example:

SET user:1:name John
SET user:2:name Alice

Now, retrieving them:

GET user:1:name
GET user:2:name

Output:

"John"
"Alice"

This pattern (object:id:field) is widely used because:

  • It keeps your keyspace organized.
  • It’s easy to read and understand.
  • It mirrors RESTful API structures or database schemas.

10. Important Notes on Key Structure

  • Colons (:) are just part of the key name — Redis doesn’t interpret them specially.
  • This structure is purely a naming convention for human readability.
  • You can choose any separator (for example, user|1|name), but be consistent throughout your application.

Example of inconsistent naming (not recommended):

SET user:1:name John
SET users.1.name Mike

This can cause confusion when maintaining large datasets.

The best practice is to stick to one consistent naming style, typically using colons.


11. Summary

Let’s recap what we learned in this session:

  1. Redis stores all data as key-value pairs, and everything is stored as a string.
  2. The basic commands are:
    • SET key value to store data
    • GET key to retrieve data
  3. Redis keys are case-sensitive and must match exactly.
  4. If a key doesn’t exist, GET returns (nil).
  5. You can store any text or numeric data, but it’s always treated as a string.
  6. Use quotes if keys or values contain spaces.
  7. Follow naming conventions like object:id:field for readability and consistency.