Introduction
In this lab, you will explore basic data management techniques in Redis. You'll begin by learning how to increment and decrement numerical values using the atomic INCR and DECR commands, suitable for counters and rate limiters. You'll connect to the Redis server using redis-cli, set initial values, and then increment and decrement them, verifying the results with the GET command.
Furthermore, you will learn how to retrieve all keys stored in Redis using the KEYS command.
Increment and Decrement Values with INCR/DECR
In this step, you will learn how to increment and decrement numerical values stored in Redis using the INCR and DECR commands. These commands are atomic, meaning they are guaranteed to execute without interference from other clients, making them suitable for tasks like counters and rate limiters.
First, let's connect to the Redis server using the redis-cli command in the terminal:
redis-cli
Now, let's set a key named mycounter to an initial value of 10. This creates a key-value pair in the Redis database. The key is mycounter, and the value is 10.
SET mycounter 10
You should see the following output, confirming that the key has been set successfully:
OK
The INCR command increments the value of a key by 1. Let's increment mycounter:
INCR mycounter
The output will be the incremented value:
(integer) 11
To verify the value, you can use the GET command:
GET mycounter
The output should be:
"11"
The DECR command decrements the value of a key by 1. Let's decrement mycounter:
DECR mycounter
The output will be the decremented value:
(integer) 10
Again, verify the value with GET:
GET mycounter
The output should be:
"10"
If a key does not exist, INCR and DECR treat it as if it contains the value 0. Let's try incrementing a non-existent key called newcounter:
INCR newcounter
The output will be:
(integer) 1
Now, check the value of newcounter:
GET newcounter
The output should be:
"1"
Similarly, decrementing a non-existent key will treat it as 0 and decrement it to -1.
DECR anothercounter
The output will be:
(integer) -1
GET anothercounter
The output should be:
"-1"
Finally, exit the redis-cli:
exit
It's important to exit redis-cli after completing the commands so that the command history is properly logged.
Retrieve All Keys with KEYS
In this step, you will learn how to retrieve all keys stored in Redis using the KEYS command. While KEYS is useful for development and debugging, it's generally not recommended for production environments with large datasets because it can block the server while it iterates through all the keys.
First, connect to the Redis server using redis-cli:
redis-cli
In the previous step, you created several keys: mycounter, newcounter, and anothercounter. Let's add a few more keys to make the example more interesting.
SET user:1000:name "John"
SET user:1000:age 30
SET user:1001:name "Jane"
Now, use the KEYS command with the * pattern to retrieve all keys in the database:
KEYS *
The output will be a list of all keys:
1) "anothercounter"
2) "user:1000:age"
3) "user:1001:name"
4) "mycounter"
5) "newcounter"
6) "user:1000:name"
The order of the keys may vary.
You can also use patterns to retrieve keys that match a specific pattern. For example, to retrieve all keys that start with user:, use the following command:
KEYS user:*
The output will be:
1) "user:1000:age"
2) "user:1001:name"
3) "user:1000:name"
Another example, to retrieve all keys that contain counter, use the following command:
KEYS *counter*
The output will be:
1) "anothercounter"
2) "mycounter"
3) "newcounter"
Remember that using KEYS * on a large database can impact performance. For production environments, consider using SCAN instead, which iterates through the keyspace in a non-blocking manner.
Finally, exit the redis-cli:
exit
Check Data Type with TYPE
In this step, you will learn how to check the data type of a key stored in Redis using the TYPE command. Redis supports various data types, including strings, lists, sets, sorted sets, and hashes. Understanding the data type of a key is crucial for performing appropriate operations on it.
First, connect to the Redis server using redis-cli:
redis-cli
In the previous steps, you created several keys with different values. Let's check their data types.
First, let's check the data type of the mycounter key:
TYPE mycounter
The output will be:
string
This indicates that mycounter is stored as a string, even though it contains a numerical value. Redis automatically converts numerical values to strings when using SET.
Next, let's check the data type of the user:1000:name key:
TYPE user:1000:name
The output will be:
string
This also indicates that user:1000:name is stored as a string.
Now, let's check the data type of a key that doesn't exist, such as nonexistentkey:
TYPE nonexistentkey
The output will be:
none
This indicates that the key does not exist in the database.
To further illustrate data types, let's create a list:
LPUSH mylist "item1"
LPUSH mylist "item2"
Now, check the data type of mylist:
TYPE mylist
The output will be:
list
This confirms that mylist is stored as a list.
Similarly, you can create other data types like sets, sorted sets, and hashes and use the TYPE command to verify their types. For example:
SADD myset "member1"
SADD myset "member2"
TYPE myset
The output will be:
set
ZADD mysortedset 1 "element1"
ZADD mysortedset 2 "element2"
TYPE mysortedset
The output will be:
zset
HSET myhash field1 "value1"
HSET myhash field2 "value2"
TYPE myhash
The output will be:
hash
Finally, exit the redis-cli:
exit
Clear Data with FLUSHDB
In this step, you will learn how to clear all data from the currently selected Redis database using the FLUSHDB command. This command is useful for resetting the database during development or testing. Use this command with caution in production environments, as it will permanently delete all data in the database.
First, connect to the Redis server using redis-cli:
redis-cli
Before flushing the database, let's verify that there are keys present. Use the KEYS * command:
KEYS *
You should see a list of keys that you created in the previous steps, such as mycounter, newcounter, user:1000:name, and mylist.
Now, execute the FLUSHDB command:
FLUSHDB
The output will be:
OK
This indicates that the database has been successfully flushed.
To verify that the database is now empty, use the KEYS * command again:
KEYS *
The output will be an empty list:
(empty array)
This confirms that all keys have been removed from the database.
It's important to understand that FLUSHDB only clears the currently selected database. Redis supports multiple databases (numbered 0 to 15 by default). If you want to clear all databases, you can use the FLUSHALL command. However, we will not use FLUSHALL in this lab to avoid unintended data loss.
Finally, exit the redis-cli:
exit
Summary
In this lab, you learned fundamental data management techniques in Redis. Specifically, you practiced incrementing and decrementing numerical values using the atomic INCR and DECR commands, which are useful for implementing counters and rate limiters. You also learned that if a key doesn't exist, these commands treat it as if it contains the value 0.
Furthermore, you were introduced to the KEYS command for retrieving all keys stored in Redis. While useful for development and debugging, it's generally not recommended for production environments with large datasets. Finally, you learned how to check data types and clear the database.


