Databases 22 min read

Redis Data Types and Commands: A Comprehensive Guide

This article provides a thorough overview of Redis, covering its eight core data types, essential commands, practical usage examples, and advanced features such as geospatial indexes, hyperloglog, and bitmap operations, making it a valuable reference for developers and interview preparation.

Architecture Digest
Architecture Digest
Architecture Digest
Redis Data Types and Commands: A Comprehensive Guide

Redis's Eight Data Types

The official Redis command reference can be found at http://www.redis.cn/commands.html . Below is a concise review of each data type with common commands and usage scenarios.

1. String (字符串)

Basic key‑value storage. Supports operations such as SET, GET, APPEND, INCR, DECR, GETRANGE, SETRANGE, SETEX, and SETNX. Strings can also hold numeric values for counters.

127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> incr views
(integer) 1
127.0.0.1:6379> setex key3 30 "hello"
OK

2. List (列表)

Ordered collections that can act as stacks, queues, or blocking queues. Most commands start with LPUSH, RPUSH, LPOP, RPOP, LRANGE, LINDEX, LLEN, LREM, LSET, LINsert, etc.

127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> lpop list
"one"

3. Set (集合)

Unordered collections of unique elements. Common commands include SADD, SMEMBERS, SISMEMBER, SCARD, SREM, SDIFF, SINTER, SUNION, and SMOVE.

127.0.0.1:6379> sadd myset "hello"
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
127.0.0.1:6379> sdiff key1 key2
1) "b"
2) "a"

4. Hash (哈希)

Maps stored under a single key, ideal for objects that change frequently. Use HSET, HGET, HMSET, HMGET, HGETALL, HDEL, HINCRBY, HSETNX, etc.

127.0.0.1:6379> hset myhash field "xxx"
(integer) 1
127.0.0.1:6379> hget myhash field
"xxx"
127.0.0.1:6379> hincrby myhash counter 1
(integer) 1

5. Sorted Set (有序集合, Zset)

Elements are ordered by a floating‑point score. Commands include ZADD, ZRANGE, ZREVRANGE, ZRANGEBYSCORE, ZREM, ZCARD, ZCOUNT, etc.

127.0.0.1:6379> zadd salary 2500 xiaohong
(integer) 1
127.0.0.1:6379> zrange salary 0 -1
1) "xiaohong"
2) "xiaoming"
127.0.0.1:6379> zrem salary xiaohong
(integer) 1

6. Geospatial

Introduced in Redis 3.2, geospatial indexes are stored as sorted sets. Commands such as GEOADD, GEOPOS, GEODIST, GEORADIUS, and GEORADIUSBYMEMBER enable location‑based queries.

127.0.0.1:6379> geoadd china:city 116.40 39.90 beijing
(integer) 1
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
127.0.0.1:6379> geodist china:city beijing shanghai km
"1067.3788"

7. HyperLogLog

A probabilistic data structure for cardinality estimation, using only ~12KB of memory. Commands: PFADD, PFCOUNT, PFMERGE.

127.0.0.1:6379> pfadd mykey a b c d e f g h i j
(integer) 1
127.0.0.1:6379> pfcount mykey
(integer) 10

8. Bitmap (位图)

Bitmaps store bits (0/1) and are useful for tracking flags such as daily check‑ins. Commands include SETBIT, GETBIT, BITCOUNT.

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> bitcount sign
(integer) 4

These examples illustrate how Redis can be used for caching, counting, ranking, geolocation, and many other scenarios, making it a versatile tool for backend development and interview preparation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CachedatabaseData TypesNoSQLcommands
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

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.