Databases 38 min read

Master Redis: Installation, Commands, and Persistence Explained

An in‑depth guide covering Redis fundamentals, installation steps, daemon configuration, password setup, core commands for strings, lists, hashes, sets and sorted sets, internal encodings, and both RDB and AOF persistence mechanisms, complete with practical code examples and configuration snippets.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Redis: Installation, Commands, and Persistence Explained

Redis Overview

Redis is an open‑source, ANSI‑C written, BSD‑licensed, network‑enabled, in‑memory (with optional persistence) key‑value database that supports strings, hashes, lists, sets and sorted sets.

Installing Redis

<code>$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make</code>

Running Redis as a Daemon

Modify redis.conf

Change

daemonize no

to

daemonize yes

.

Start Redis

<code>src/redis-server redis.conf</code>
Redis start output
Redis start output

Enable Autostart

Run the installation script with default options.

<code>./utils/install_server.sh</code>
Redis service installation
Redis service installation
<code>mv /etc/init.d/redis_6379 /etc/init.d/redis</code>

Set Redis Password

<code>vim /etc/redis/6379.conf
requirepass redispass
service redis restart</code>

After reconnecting, a password is required.

Redis password prompt
Redis password prompt

Redis Commands

Global Commands

Get Keys

<code>keys pattern</code>

The

keys

command supports wildcards but may affect performance on large datasets.

<code>127.0.0.1:6379> set we "hello"
OK
127.0.0.1:6379> keys *
1) "we"
127.0.0.1:6379> set name wanger
OK
127.0.0.1:6379> keys name
1) "name"
127.0.0.1:6379> keys *
1) "name"
2) "we"</code>

Delete Keys

<code>del key1 key2 ..</code>

Example:

<code>127.0.0.1:6379> keys *
1) "qwe"
2) "asd"
3) "we"
127.0.0.1:6379> del asd qwe
(integer) 2
127.0.0.1:6379> keys *
1) "we"</code>

Check Key Existence

<code>exists key1 key2</code>

Example:

<code>127.0.0.1:6379> exists we
(integer) 1
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> set qwe 2
OK
127.0.0.1:6379> exists we qwe
(integer) 2
127.0.0.1:6379> exists we name
(integer) 1</code>

Get Total Number of Keys

<code>dbsize</code>

Example:

<code>127.0.0.1:6379> dbsize
(integer) 2
127.0.0.1:6379> keys *
1) "qwe"
2) "we"</code>

Get Key Type

<code>type key</code>

Example:

<code>127.0.0.1:6379> type we
string
127.0.0.1:6379> lpush list1 1 2 3
(integer) 3
127.0.0.1:6379> type list1
list</code>

Sort Elements

<code>sort key [BY pattern] [LIMIT offset count] [GET pattern ...] [ASC|DESC] [ALPHA] [STORE destination]</code>

Example:

<code>127.0.0.1:6379> lrange list 0 -1
1) "5"
2) "7"
3) "2"
4) "4"
5) "3"
6) "1"
sort list desc limit 0 5
1) "7"
2) "5"
3) "4"
4) "4"
5) "3"
127.0.0.1:6379> lpush list2 asd qwe zxc
(integer) 3
sort list2 desc limit 0 5 alpha
1) "zxc"
2) "qwe"
3) "asd"</code>

Flush Database

<code>flushdb  // clear current DB
flushall // clear all DBs</code>

Example:

<code>127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> get a
(nil)</code>

Move Key to Another DB

<code>move key db</code>

Example:

<code>127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> move a 2
(integer) 1
127.0.0.1:6379> select 2
OK
127.0.0.1:6379> get a
"1"</code>

String Operations

Set and Get

<code>set key value [EX seconds] [PX milliseconds] [NX|XX]
get key</code>

NX : set only if key does not exist. XX : set only if key exists.

Example:

<code>127.0.0.1:6379> set name wanger
OK
127.0.0.1:6379> get name
"wanger"
127.0.0.1:6379> setnx name wanger
(integer) 0
127.0.0.1:6379> set name wang xx
OK</code>

Batch Set/Get

<code>mset key1 value1 key2 value2 ..
mget key1 key2</code>

Example:

<code>127.0.0.1:6379> mset key1 1 key2 2
OK
127.0.0.1:6379> mget key1 key2
1) "1"
2) "2"</code>

Increment / Decrement

<code>incr key
decr key</code>

Example:

<code>127.0.0.1:6379> incr key1
(integer) 2
127.0.0.1:6379> incr key2
(integer) 3
127.0.0.1:6379> get key1
"2"
127.0.0.1:6379> get key2
"3"
127.0.0.1:6379> incr we
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr key1
(integer) 1
127.0.0.1:6379> decr key2
(integer) 2</code>

Append

<code>append key value</code>

Example:

<code>127.0.0.1:6379> append key hello
(integer) 5
127.0.0.1:6379> append key world
(integer) 10
127.0.0.1:6379> get key
"helloworld"</code>

String Length

<code>strlen key</code>

Example:

<code>127.0.0.1:6379> get key
"helloworld"
127.0.0.1:6379> strlen key
(integer) 10
127.0.0.1:6379> set name "王二"
OK
127.0.0.1:6379> strlen name
(integer) 6</code>

Set/Get Sub‑range

<code>setrange key offset value
getrange key start end</code>

Example:

<code>127.0.0.1:6379> SET key1 "Hello World"
OK
127.0.0.1:6379> setrange key1 6 "Redis"
(integer) 11
127.0.0.1:6379> get key1
"Hello Redis"
127.0.0.1:6379> getrange key1 6 12
"Redis"</code>

String Object Encoding

Redis stores strings using three internal encodings:

int : 8‑byte integer.

embstr : strings up to 39 bytes.

raw : strings longer than 39 bytes.

Example:

<code>127.0.0.1:6379> set num 123456
OK
127.0.0.1:6379> object encoding num
"int"
127.0.0.1:6379> set short qweasd
OK
127.0.0.1:6379> object encoding short
"embstr"
127.0.0.1:6379> set raw "when you love me I have lost of plot wow wow"
OK
127.0.0.1:6379> object encoding raw
"raw"</code>

List Operations

Push / Insert

<code>lpush key value1 value2 ...
rpush key value1 value2 ...
linsert key BEFORE|AFTER pivot value</code>

Example:

<code>127.0.0.1:6379> lpush names 1 2 3 4
(integer) 4
127.0.0.1:6379> lrange names 0 4
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> rpush nums 1 2 3 4
(integer) 4
127.0.0.1:6379> lrange nums 0 4
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> linsert nums BEFORE 2 5
(integer) 5
127.0.0.1:6379> lrange nums 0 5
1) "1"
2) "5"
3) "2"
4) "3"
5) "4"</code>

Pop Elements

<code>lpop key
rpop key</code>

Example:

<code>127.0.0.1:6379> lrange nums 0 5
1) "1"
2) "5"
3) "2"
4) "3"
5) "4"
127.0.0.1:6379> lpop nums
"1"
127.0.0.1:6379> rpop nums
"4"
127.0.0.1:6379> lrange nums 0 5
1) "5"
2) "2"
3) "3"</code>

Index Access

<code>lindex key index</code>

Example:

<code>127.0.0.1:6379> lrange nums 0 5
1) "5"
2) "2"
3) "3"
127.0.0.1:6379> lindex nums 2
"3"
127.0.0.1:6379> lindex nums -1
"3"</code>

Range Query

<code>lrange key start stop</code>

Example:

<code>127.0.0.1:6379> lrange nums 0 1
1) "5"
2) "2"
127.0.0.1:6379> lrange nums 0 2
1) "5"
2) "2"
3) "3"</code>

List Length

<code>llen key</code>

Example:

<code>127.0.0.1:6379> lrange nums 0 3
1) "5"
2) "2"
3) "3"
127.0.0.1:6379> llen nums
(integer) 3</code>

Remove Elements

<code>lrem key count value</code>

count > 0: remove from head; count < 0: remove from tail; count = 0: remove all.

Example:

<code>127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "2"
7) "3"
8) "4"
9) "5"
10) "5"
127.0.0.1:6379> lrem mylist 1 5
(integer) 1
127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "2"
6) "3"
7) "4"
8) "5"
9) "5"
127.0.0.1:6379> lrem mylist -2 5
(integer) 2
127.0.0.1:6379> lrange mylist 0 10
1) "1"
2) "2"
3) "3"
4) "4"
5) "2"
6) "3"
7) "4"
127.0.0.1:6379> lrem mylist 0 2
(integer) 2
127.0.0.1:6379> lrange mylist 0 11
1) "1"
2) "3"
3) "4"
4) "3"
5) "4"</code>

Set Element by Index

<code>lset key index value</code>

Example:

<code>127.0.0.1:6379> lrange mylist 0 6
1) "1"
2) "3"
3) "4"
4) "3"
5) "4"
127.0.0.1:6379> lset mylist 1 5
OK
127.0.0.1:6379> lrange mylist 0 6
1) "1"
2) "5"
3) "4"
4) "3"
5) "4"</code>

Blocking Pop

<code>blpop key1 key2 timeout
brpop key1 key2 timeout</code>

Example:

<code>127.0.0.1:6379> blpop list1 list2 0
1) "list1"
2) "8"
127.0.0.1:6379> brpop list1 list2 0
1) "list1"
2) "7"
127.0.0.1:6379> brpop list1 list2 0
1) "list2"
2) "2"
... (continues until another client pushes data)</code>

List Internal Encoding

ziplist : used when all elements are < 64 bytes and the list has < 512 items.

linkedlist : fallback when ziplist conditions are not met.

quicklist : a linked list of ziplist nodes, the default implementation in modern Redis.

Reference: 张铁蕾 http://zhangtielei.com/posts/blog-redis-quicklist.html

Hash Operations

Set / Get Field

<code>hset key field value
hget key field</code>

Example:

<code>127.0.0.1:6379> hset ha name wanger
(integer) 1
127.0.0.1:6379> hget ha name
"wanger"</code>

Batch Set / Get

<code>hmset key field1 value1 field2 value2 ...
hmget key field1 field2 ...</code>

Example:

<code>127.0.0.1:6379> hmset he name wanger sex nan
OK
127.0.0.1:6379> hmget he name sex
1) "wanger"
2) "nan"</code>

Delete Field

<code>hdel key field1 field2 ...</code>

Example:

<code>127.0.0.1:6379> hdel he name
(integer) 1
127.0.0.1:6379> hget he name
(nil)</code>

Field Count

<code>hlen key</code>

Example:

<code>127.0.0.1:6379> hmset he name wanger sex nan age 18
OK
127.0.0.1:6379> hlen he
(integer) 3</code>

Get All Fields and Values

<code>hgetall key</code>

Example:

<code>127.0.0.1:6379> hgetall he
1) "sex"
2) "nan"
3) "name"
4) "wanger"
5) "age"
6) "18"</code>

Get All Field Names

<code>hkeys key</code>

Example:

<code>127.0.0.1:6379> hkeys he
1) "sex"
2) "name"
3) "age"</code>

Field Existence

<code>hexists key field</code>

Example:

<code>127.0.0.1:6379> hexists he name
(integer) 1
127.0.0.1:6379> hexists he sex
(integer) 1</code>

Increment Field

<code>hincrby key field increment</code>

Example:

<code>127.0.0.1:6379> hincrby asd asdf 2
(integer) 3
127.0.0.1:6379> hget asd asdf
"3"
127.0.0.1:6379> hincrby asd asdf 2
(integer) 5
127.0.0.1:6379> hget asd asdf
"5"</code>

Get All Values

<code>hvals key</code>

Example:

<code>127.0.0.1:6379> hvals he
1) "nan"
2) "wanger"
3) "18"</code>

Hash Internal Encoding

ziplist : used when the hash has fewer than 512 entries and each value is under 64 bytes.

hashtable : used otherwise, offering O(1) access.

Set Operations

Add Members

<code>sadd key member1 member2 ...</code>

Example:

<code>127.0.0.1:6379> sadd set s1 s2
(integer) 0</code>

Get All Members

<code>smembers key</code>

Example:

<code>127.0.0.1:6379> smembers set
1) "s2"
2) "s1"
3) "s3"</code>

Remove Members

<code>srem key member1 member2 ...</code>

Example:

<code>127.0.0.1:6379> srem set s2
(integer) 1
127.0.0.1:6379> smembers set
1) "s1"
2) "s3"</code>

Cardinality

<code>scard key</code>

Example:

<code>127.0.0.1:6379> scard set
(integer) 2</code>

Random Member(s)

<code>srandmember key [count]</code>

Example:

<code>127.0.0.1:6379> srandmember set 1
1) "s1"</code>

Membership Test

<code>sismember key member</code>

Example:

<code>127.0.0.1:6379> sismember set s2
(integer) 0
127.0.0.1:6379> sismember set s3
(integer) 1</code>

Pop Members

<code>spop key [count]</code>

Example:

<code>127.0.0.1:6379> spop set
"s4"
127.0.0.1:6379> spop set 3
1) "s5"
2) "s1"
3) "s3"</code>

Set Union / Intersection / Difference

<code>sunion key1 key2
sinter key1 key2
sdiff key1 key2</code>

Examples:

<code>127.0.0.1:6379> sunion set1 set2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> sinter set1 set2
1) "2"
2) "3"
127.0.0.1:6379> sdiff set1 set2
1) "1"
127.0.0.1:6379> sdiff set2 set1
1) "4"
2) "5"</code>

Set Internal Encoding

intset : used when all members are integers and the set has fewer than 512 entries.

hashtable : fallback for larger or non‑integer sets.

Sorted Set Operations

Add Members

<code>zadd key [NX|XX] [CH] [INCR] score1 member1 score2 member2 ...</code>

Example:

<code>127.0.0.1:6379> zadd score xx 80 wanger 80 huazai 95 dongdong +inf a
(integer) 0
127.0.0.1:6379> zrange score 0 -1
1) "huazai"
2) "wanger"
3) "dongdong"
4) "a"</code>

Get Score

<code>zscore key member</code>

Example:

<code>127.0.0.1:6379> zscore score dongdong
"95"</code>

Rank / Reverse Rank

<code>zrank key member   // ascending rank starting at 0
zrevrank key member // descending rank</code>

Example:

<code>127.0.0.1:6379> zrank score huazai
(integer) 2
127.0.0.1:6379> zrevrank score huazai
(integer) 1</code>

Cardinality

<code>zcard key</code>

Example:

<code>127.0.0.1:6379> zcard score
(integer) 4</code>

Remove Members

<code>zrem key member1 member2 ...</code>

Example:

<code>127.0.0.1:6379> zrem score dongdong
(integer) 1
127.0.0.1:6379> zrange score 0 -1
1) "wanger"
2) "huazai"
3) "a"</code>

Increment Score

<code>zincrby key increment member</code>

Example:

<code>127.0.0.1:6379> zincrby score 5 wanger
"95"</code>

Range by Score

<code>zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]</code>

Example:

<code>127.0.0.1:6379> zrangebyscore score 80 90 WITHSCORES
1) "dongdong"
2) "85"
3) "wanger"
4) "90"</code>

Count by Score

<code>zcount key min max</code>

Example:

<code>127.0.0.1:6379> zcount score 85 95
(integer) 3</code>

Remove by Score Range

<code>zremrangebyscore key min max</code>

Example:

<code>127.0.0.1:6379> zremrangebyscore score 85 90
(integer) 2</code>

Sorted‑Set Intersection

<code>zinterstore destination numkeys key1 key2 [WEIGHTS w1 w2] [AGGREGATE sum|min|max]</code>

Example:

<code>127.0.0.1:6379> zadd user 10 wanger 20 huazai 30 dongdong
(integer) 3
127.0.0.1:6379> zadd user1 15 wanger 35 huazai
(integer) 2
127.0.0.1:6379> zinterstore userset 2 user user1
(integer) 2
127.0.0.1:6379> zrange userset 0 -1 WITHSCORES
1) "wanger"
2) "25"
3) "huazai"
4) "55"</code>

Sorted‑Set Union

<code>zunionstore destination numkeys key1 key2 [WEIGHTS w1 w2] [AGGREGATE sum|min|max]</code>

Example:

<code>127.0.0.1:6379> zunionstore user3set 2 user user1 WEIGHTS 1 0.5 AGGREGATE MAX
(integer) 3
127.0.0.1:6379> zrange user3set 0 -1 WITHSCORES
1) "wanger"
2) "10"
3) "huazai"
4) "20"
5) "dongdong"
6) "30"</code>

Sorted‑Set Internal Encoding

ziplist : used when the set has fewer than 128 elements and each element is under 64 bytes.

skiplist : used otherwise, providing O(log N) range queries.

Redis Persistence

RDB (Snapshotting)

RDB creates a point‑in‑time snapshot of the dataset and writes it to a binary file. The snapshot can be loaded on restart for fast recovery and is suitable for backups.

RDB Process

Redis forks a child process.

The child writes the dataset to a temporary RDB file.

When finished, the temporary file replaces the old RDB file.

Automatic Trigger

Configured in

redis.conf

(e.g.,

save 900 1

,

save 300 10

,

save 60 10000

).

RDB configuration
RDB configuration

Additional options:

<code>rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6379</code>

Manual Trigger

Use

SAVE

(blocks the server) or

BGSAVE

(forks a child and continues serving clients).

AOF (Append‑Only File)

AOF logs every write operation using the Redis protocol. On restart, the log is replayed to rebuild the dataset. A background rewrite compresses the log when it grows large.

AOF Write Strategies

appendfsync always

: sync on every write (slow, no data loss).

appendfsync everysec

: sync every second (default, may lose up to 1 s).

appendfsync no

: let the OS handle syncing (fast, riskier).

AOF Rewrite

When the AOF becomes large, Redis forks a child that rewrites the log by reading the in‑memory dataset and emitting the minimal set of commands. New writes are buffered and appended to the old AOF; after the rewrite finishes, the temporary file replaces the old AOF.

AOF Configuration

<code>no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-rewrite-incremental-fsync yes
aof-use-rdb-preamble yes</code>

When both RDB and AOF files exist, AOF takes precedence for recovery.

Reference: Zhang Tielei – https://zhangtielei.com/posts/blog-redis-quicklist.html

Feel free to discuss and share insights.

Community image
Community image
redisPersistenceData StructuresinstallationcommandsKey-Value Database
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.