Databases 15 min read

Redis Concepts and Command Reference

This article introduces Redis as a high‑performance, in‑memory key‑value database, explains its core features such as persistence, pub/sub, data structures, and atomic operations, and provides a comprehensive list of commands for keys, strings, hashes, lists, sets, and sorted sets.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Redis Concepts and Command Reference

Concepts

Redis (Remote DIctionary Server) is an open‑source, high‑performance key‑value database built on the BSD license. It runs single‑threaded, executing all operations sequentially to avoid context switches and race conditions, and uses non‑blocking I/O with epoll for high I/O efficiency.

Official sites: Chinese , English .

Default 16 databases (0‑15), default port 6379, default password "merz".

Redis Features

In‑memory storage and persistence : Asynchronously writes data to disk without affecting reads, and reloads on restart.

Pub/Sub messaging system

Timers and counters : Supports key expiration.

Rich data structures : Supports String , list , set , zset , hash , etc.

Data backup : Master‑slave replication.

High performance : Read ~110000 ops/s, write ~81000 ops/s.

Atomicity : All operations are atomic; multi‑command transactions use MULTI and EXEC .

Previously we introduced Redis installation; see the linked article for details.

Command Reference

Key Commands

select db : Switch to database db (numeric).

dbsize : Show the number of keys in the current database.

flushdb : Delete all keys in the current database.

flushall : Delete all keys in all databases.

keys * : List all keys.

exists key : Test whether key exists.

move key db : Move key to database db .

expire key seconds : Set expiration time for key .

ttl key : Show remaining time‑to‑live for key .

type key : Show the data type of key .

del key : Delete key .

Command execution returns 1 for success, 0 for failure.

String Commands

SET key value : Set the value of key .

GET key : Get the value of key .

DEL key : Delete key .

APPEND key value : Append value to an existing string key .

STRLEN key : Return the length of the string stored at key .

GETRANGE key start end : Return a substring of the string value.

SETRANGE key offset value : Overwrite part of the string at offset .

SETEX key seconds value : Set key with an expiration.

SETNX key value : Set key only if it does not exist.

MSET key value [key value ...] : Set multiple keys at once.

MGET key1 [key2 ...] : Get values of multiple keys.

MSETNX key value [key value ...] : Set multiple keys only if none exist.

GETSET key value : Set new value and return the old one.

INCR key : Increment numeric key by 1.

DECR key : Decrement numeric key by 1.

INCRBY key increment : Increment by a given amount.

DECRBY key decrement : Decrement by a given amount.

Hash Commands

HSET key field value : Set field in hash key .

HGET key field : Get the value of field in hash key .

HMSET key field1 value1 [field2 value2 ...] : Set multiple fields.

HMGET key field1 [field2 ...] : Get values of multiple fields.

HGETALL key : Get all fields and values.

HDEL key field1 [field2 ...] : Delete one or more fields.

HLEN key : Get the number of fields.

HEXISTS key field : Test if a field exists.

HKEYS key : Get all field names.

HVALS key : Get all field values.

HINCRBY key field increment : Increment integer field.

HINCRBYFLOAT key field increment : Increment floating‑point field.

HSETNX key field value : Set field only if it does not exist.

List Commands

LPUSH key value [value ...] : Push one or more values to the head.

RPUSH key value [value ...] : Push one or more values to the tail.

LRANGE key start stop : Get a range of elements.

LPOP key : Pop the first element.

RPOP key : Pop the last element.

LINDEX key index : Get element by index (negative indices allowed).

LLEN key : Get list length.

LREM key count value : Remove count occurrences of value .

LTRIM key start stop : Trim list to the specified range (inclusive).

RPOPLPUSH source destination : Pop from tail of source and push to head of destination .

LSET key index value : Set element at index .

LINSERT key BEFORE|AFTER pivot value : Insert value before or after pivot .

List Performance

If the key does not exist, a new list is created.

If the key exists, new elements are appended.

When all elements are removed, the key disappears.

Head and tail operations are O(1); operations in the middle are slower.

Set Commands

SADD key member [member ...] : Add one or more members.

SMEMBERS key : Return all members.

SISMEMBER key member : Test membership.

SCARD key : Get the number of members.

SREM key member [member ...] : Remove members.

SRANDMEMBER key [count] : Return random member(s).

SPOP key : Remove and return a random member.

SMOVE source destination member : Move a member from one set to another.

Mathematical Set Operations

SDIFF key1 [key2 ...] : Difference of sets.

SINTER key1 [key2 ...] : Intersection of sets.

SUNION key1 [key2 ...] : Union of sets.

Sorted Set (ZSet) Commands

ZADD key score1 member1 [score2 member2 ...] : Add or update members with scores.

ZRANGE key start stop [WITHSCORES] : Return members by index range.

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] : Return members by score range.

ZREM key member [member ...] : Remove members.

ZCARD key : Get number of members.

ZCOUNT key min max : Count members with scores in range.

ZRANK key member : Get rank (ascending).

ZSCORE key member : Get the score of a member.

Reverse Operations

ZREVRANK key member : Rank in descending order.

ZREVRANGE key start stop [WITHSCORES] : Return members in descending order by index.

ZREVRANGEBYSCORE key max min [WITHSCORES] : Return members in descending order by score.

Recommended reading: Spring Boot integration with Kafka, distributed configuration center selection (Apollo), and why MySQL uses REPEATABLE READ as the default isolation level.

DatabaseRedisData StructuresNoSQLcommandsKey-Value Store
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.