Skip to content
Commitpedia
Log in Join & edit

Redis data types and commands: a quick reference

Redis stores values under keys, and each value has a type: string, hash, list, set, sorted set or stream. The essential commands for each type, for expiry, and for inspecting keys safely.

Edit history of Redis data types and commands: a quick reference: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
3 min

Redis is an in-memory data structure server. Every piece of data lives under a key (a string such as user:42), and the value has a type that decides which commands work on it. The examples use redis-cli, which connects to 127.0.0.1 port 6379 by default.

redis-cli                                   # interactive prompt
redis-cli -h cache1.example.com -p 6379 PING # one command, prints PONG
redis-cli -n 1 DBSIZE                        # use logical database 1

Command names are case-insensitive; key names are case-sensitive. A common convention is colon-separated names such as session:abc123 or product:42:views.

Strings

The basic type: a sequence of bytes. It holds text, numbers, serialized JSON or small binary blobs.

SET greeting "hello"
GET greeting                    # "hello"
SET page:home "<html>..." EX 60 # expires in 60 seconds
SET lock:job1 worker-7 NX EX 30 # only if the key does not exist yet
INCR stats:visits               # atomic counter, returns the new value
INCRBY stats:visits 10
MSET a 1 b 2
MGET a b

Hashes

A collection of field-value pairs under one key, like a small record or dictionary.

HSET user:42 name "Ada" city "Springfield" logins 0
HGET user:42 name
HGETALL user:42
HINCRBY user:42 logins 1
HDEL user:42 city

Lists

Ordered lists of strings in insertion order. Good for queues and “latest N” feeds.

LPUSH news:latest "item3" "item2"   # add to the head
RPUSH jobs "job-1" "job-2"            # add to the tail
LPOP jobs                              # take from the head
LRANGE news:latest 0 9                 # first ten items
LTRIM news:latest 0 99                 # keep only 100 items
LLEN jobs

Sets

Unordered collections of unique strings. Adding, removing and checking membership take constant time regardless of size.

SADD online:users 42 43 44
SISMEMBER online:users 42      # 1 = yes, 0 = no
SMEMBERS online:users
SCARD online:users            # count
SINTER group:a group:b        # members in both

Sorted sets

Unique strings ordered by a numeric score. The classic use is a leaderboard or a time-ordered index.

ZADD leaderboard 1200 "ada" 950 "ben" 1810 "cy"
ZINCRBY leaderboard 50 "ben"
ZRANGE leaderboard 0 2 REV WITHSCORES   # top three (REV needs Redis 6.2+)
ZSCORE leaderboard "ada"
ZRANK leaderboard "ada"

Streams

An append-only log of entries, each with an ID and field-value pairs. Use streams for event processing; consumer groups let several workers share the work.

XADD events * type "login" user "42"   # * = generate the ID
XRANGE events - + COUNT 10
XLEN events

Redis also offers bitmaps and bitfields (on top of strings), HyperLogLog for approximate unique counts, geospatial indexes, and, in recent Redis Open Source releases, JSON, probabilistic types, time series and vector sets. Check the data types page for what your version includes.

Expiry

EXPIRE session:abc123 1800   # 30 minutes from now
TTL session:abc123           # seconds left
PERSIST session:abc123       # remove the expiry
PEXPIRE session:abc123 1500  # milliseconds

TTL returns -1 when the key exists but has no expiry, and -2 when the key does not exist. A plain SET on an existing key discards its old TTL unless you add KEEPTTL.

Inspecting keys

TYPE user:42
EXISTS user:42
DBSIZE                                 # number of keys in this database
SCAN 0 MATCH user:* COUNT 100          # iterate in batches; repeat with the returned cursor
MEMORY USAGE user:42                   # approximate bytes
INFO keyspace                          # keys and expiring keys per database

Avoid KEYS * on a production server: it walks the whole keyspace in one go and blocks other clients while it runs. SCAN, or redis-cli --scan --pattern 'user:*' from the shell, does the same work in small steps.

Deleting

Deletes take effect immediately and cannot be undone. Count what a pattern matches before removing anything, for example redis-cli --scan --pattern 'tmp:*' | wc -l.

DEL user:42
UNLINK big:list               # removes the key now, frees memory in the background

FLUSHDB empties the current database and FLUSHALL empties every database on the server. They are in the @dangerous ACL category; application users should not be allowed to run them.

Official documentation

See also

  • 0237 Use Redis as a cache: TTLs, maxmemory and an eviction health report Set a memory limit and an eviction policy so Redis behaves like a cache, give keys TTLs, then run a bash report… Intermediate · How-to · Linux, macOS Script Administration & persistence · Basics 15 Oct 2026
  • 0259 Speed up a slow database query with a Redis cache: cache-aside in Python, measured and locked down Put Redis in front of a slow query in a small Python app: cache-aside reads, TTLs with jitter, invalidation on writes, an… Intermediate · Tutorial · Linux Article Administration & persistence · Basics 15 Oct 2026

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. commitpedia-admin +4,544 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?