Mastering Redis: Essential Commands and Practical Examples
This guide introduces Redis as an advanced key‑value store, explains how to start the server and use its CLI tools, and provides detailed command references and demos for strings, lists, sets, sorted sets, hashes, transactions, replication, and pub/sub.
Introduction
Redis is an open‑source, in‑memory key‑value store that supports five primary data structures: strings, hashes, lists, sets, and sorted sets. Each structure provides a rich set of commands for manipulation and querying.
Server and Client Utilities
redis-server – Starts the Redis server.
Start with a specific configuration file: redis-server redis.conf Start on a custom port: redis-server --port 6379 redis-cli – Interactive command‑line client.
Connect via Unix socket: redis-cli -s /tmp/redis.sock Connect using default TCP: redis-cli redis-benchmark – Performance testing tool.
Run with default parameters: redis-benchmark Custom test with 1 000 000 requests and CSV output: redis-benchmark -n 1000000 --csv redis-check-dump – Verify and repair RDB dump files. redis-check-dump dump.rdb redis-check-aof – Verify and repair AOF log files. redis-check-aof appendonly.aof redis-sentinel – High‑availability and cluster management utility.
General Administrative Commands
AUTH– Authenticate a client when the server requires a password. PING – Test connectivity; returns PONG. CONFIG GET * – Retrieve all configuration parameters. CONFIG SET <name> <value> – Change a configuration parameter at runtime. INFO – Show server statistics and configuration. EXISTS <key> – Test if a key exists. TTL <key> – Get remaining time‑to‑live of a key. EXPIRE <key> <seconds> – Set expiration for a key. PERSIST <key> – Remove expiration from a key. DEL <key> [<key> ...] – Delete one or more keys. SELECT <db-index> – Switch to a different logical database (0‑15 by default). MOVE <key> <db-index> – Move a key to another database. TYPE <key> – Return the data type of a key. DBSIZE – Return the number of keys in the selected database. KEYS * – List all keys (use with caution on large datasets). FLUSHDB – Delete all keys in the current database. FLUSHALL – Delete all keys in all databases. SAVE – Synchronous snapshot of the dataset to disk. BGSAVE – Asynchronous snapshot (does not block the server). EVAL <script> <numkeys> [key ...] [arg ...] – Execute a Lua script.
Data‑type Commands
String
Common string operations include SET, GET, INCR, DECR, INCRBY, DECRBY, STRLEN, APPEND, SETNX, and GETRANGE.
127.0.0.1:6379> GET number
"20"
127.0.0.1:6379> INCR number
(integer) 21
127.0.0.1:6379> DECR number
(integer) 20
127.0.0.1:6379> INCRBY number 5
(integer) 25
127.0.0.1:6379> APPEND number hello
(integer) 7
127.0.0.1:6379> STRLEN number
(integer) 7
127.0.0.1:6379> SETNX number hello
(integer) 0
127.0.0.1:6379> GETRANGE number 0 5
"25hell"List
List commands include LPUSH, RPUSH, LRANGE, LPOP, LLEN, and LSET.
127.0.0.1:6379> LPUSH mylist apple orange pear
(integer) 3
127.0.0.1:6379> LLEN mylist
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "pear"
2) "orange"
3) "apple"
127.0.0.1:6379> RPUSH mylist banana
(integer) 4
127.0.0.1:6379> LPOP mylist
"pear"
127.0.0.1:6379> LSET mylist 0 four
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "four"
2) "apple"
3) "banana"Set
Set commands include SADD, SMEMBERS, SISMEMBER, SUNION, SDIFF, SINTER, SCARD, and SREM.
127.0.0.1:6379> SADD myset one two three
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> SDIFF myset myset1
1) "three"
2) "one"
127.0.0.1:6379> SINTER myset myset1
1) "two"
127.0.0.1:6379> SUNION myset myset1
1) "two"
2) "three"
3) "mysql"
4) "one"
5) "redis"
127.0.0.1:6379> SCARD myset
(integer) 3
127.0.0.1:6379> SREM myset two
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "three"
2) "one"Sorted Set
Sorted‑set commands include ZADD, ZRANGE, Z SCORE, ZCOUNT, ZREM, ZRANK, and ZINCRBY.
127.0.0.1:6379> ZADD sset 1 one 2 two 3 three
(integer) 3
127.0.0.1:6379> ZRANGE sset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1:6379> ZRANK sset one
(integer) 0
127.0.0.1:6379> ZINCRBY sset 2 two
"4"
127.0.0.1:6379> ZREM sset three
(integer) 1
127.0.0.1:6379> ZRANGE sset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "4"Hash
Hash commands include HSET, HGET, HMSET, HMGET, HINCRBY, HEXISTS, HLEN, HKEYS, HDEL, and HGETALL.
127.0.0.1:6379> HSET user name jack
(integer) 1
127.0.0.1:6379> HGET user name
"jack"
127.0.0.1:6379> HMSET user1 name bob sex man age 11
OK
127.0.0.1:6379> HMGET user1 name sex age
1) "bob"
2) "man"
3) "11"
127.0.0.1:6379> HEXISTS user1 score
(integer) 0
127.0.0.1:6379> HLEN user1
(integer) 3
127.0.0.1:6379> HINCRBY user1 age 5
(integer) 16
127.0.0.1:6379> HDEL user1 sex
(integer) 1
127.0.0.1:6379> HGETALL user1
1) "name"
2) "bob"
3) "age"
4) "16"Transactions
Redis supports optimistic transactions. Use MULTI to start, EXEC to execute, DISCARD to abort, and WATCH to monitor keys for changes.
127.0.0.1:6379> WATCH "mykey"
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> SET mykey "value"
QUEUED
127.0.0.1:6379> GET mykey
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) "value"Replication
Replication commands allow a Redis instance to act as a replica of another server. INFO REPLICATION – Show replication status. SLAVEOF <host> <port> – Configure the current instance as a replica of the specified master. SYNC – Force a full synchronization with the master (used internally).
Publish/Subscribe
Redis provides a lightweight messaging system. SUBSCRIBE <channel> [<channel> ...] – Listen for messages on one or more channels. PUBLISH <channel> <message> – Send a message to a channel.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
