Redis Basic Commands and Key Operations Tutorial
This article provides a concise tutorial on essential Redis commands, covering basic operations, database selection, key management, expiration settings, and practical examples demonstrated through the Redis CLI, including how to list keys with patterns, check existence, and clear databases.
The article begins with a brief invitation to follow the “DevOps Architecture Practice” account for daily technical updates.
1. Basic Redis Commands 127.0.0.1:6379> ping PONG 127.0.0.1:6379> DBSIZE (integer) 0 127.0.0.1:6379> SELECT 5 # Switch to database 5 (databases are indexed from 0 to 15)
OK 127.0.0.1:6379[5]> DBSIZE (integer) 0 127.0.0.1:6379[5]> set k1 v2 OK 127.0.0.1:6379[5]> set k2 v1 OK 127.0.0.1:6379[5]> DBSIZE (integer) 2
Delete all data in the current database: FLUSHDB 127.0.0.1:6379[5]> FLUSHDB OK 127.0.0.1:6379[5]> DBSIZE (integer) 0
2. Redis Key Operations
A. Syntax: keys pattern
Purpose: Find all keys matching the given pattern; the pattern can use wildcards.
* matches zero or more characters (e.g., keys * lists all keys – not recommended in production).
? matches a single character (e.g., keys he?lo matches hello and heolo). 127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> set k2 v3 OK 127.0.0.1:6379> keys * 1) "k2"
2) "k1" 127.0.0.1:6379> set hello hello OK 127.0.0.1:6379> set heolo heolo OK 127.0.0.1:6379> keys he?lo 1) "heolo"
2) "hello"
B. exists
Check whether a key exists. 127.0.0.1:6379> EXISTS hello (integer) 1 127.0.0.1:6379> EXISTS helol (integer) 0 127.0.0.1:6379> EXISTS heolo (integer) 1
C. expire
Set a key’s time‑to‑live (TTL) in seconds; returns 1 if the timeout was set, 0 otherwise.
D. ttl
Returns the remaining TTL of a key in seconds.
-1: key has no expiration (never expires).
-2: key does not exist. 127.0.0.1:6379> ttl k1 # The key currently has no expiration
(integer) -1 127.0.0.1:6379> EXPIRE k1 20 # Set k1 to expire after 20 seconds
(integer) 1 127.0.0.1:6379> ttl k1 # 15 seconds remaining
(integer) 15 127.0.0.1:6379> ttl k1 # 11 seconds remaining
(integer) 11 127.0.0.1:6379> ttl k1 # 2 seconds remaining
(integer) 2 127.0.0.1:6379> ttl k1 # After expiration, the key is removed
(integer) -2 127.0.0.1:6379> keys * # Verify remaining keys
1) "heolo"
2) "k2"
3) "hello"
----------------------end---------------------
Recommended Reading:
Redis Advantages and Remote Client Connections
Redis Data Backup and Restoration
Setting Up Redis Sentinel
Redis Sentinel and Master‑Slave Replication
Detailed Summary of Redis Configuration Files
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
