Introduction
Redis is a powerful in-memory data structure store, commonly used as a database, cache, and message broker.
This is a quick reference guide containing the most commonly used Redis commands. If you are like me who often forget about commands when not using redis actively then it would be helpful.
Cheatsheet Commands
1. String
- Use: Store simple values (text, numbers, JSON, etc.)
- Commands:
SET key value GET key INCR key # increment number DECR key # decrement number APPEND key value DEL key
2. List
- Use: Ordered list of strings (like a queue or stack)
- Commands:
LPUSH key value # add to head RPUSH key value # add to tail LPOP key # remove from head RPOP key # remove from tail LRANGE key 0 -1 # get entire list
3. Set
- Use: Unordered collection of unique strings
- Commands:
SADD key value SREM key value SMEMBERS key SISMEMBER key value SUNION key1 key2 SINTER key1 key2
4. Sorted Set (ZSet)
- Use: Like a set, but each value has a score for sorting
- Commands:
ZADD key score member ZRANGE key 0 -1 # get sorted elements ZREM key member ZSCORE key member ZRANK key member
5. Hash
- Use: Store field–value pairs (like a mini JSON or dict)
- Commands:
HSET key field value HGET key field HGETALL key HDEL key field HMSET key field1 val1 field2 val2
6. Fetch All keys
KEYS * # all keys KEYS user:* # keys that start with "user:" KEYS *:profile # keys that end with ":profile" KEYS order:? # keys like order:1, order:2 (one character wildcard) KEYS session:[a-z]* # regex-style pattern (for lowercase a-z)
Scan is production safe
SCAN 0 MATCH user:* COUNT 100
🔹 7. Hyperloglog Commands
Uses hyperloglog data structure under the hood. This helps in cardinality of any key and their members.
For example - You want to count the unique number likes for social media post.
# Add a like - PFADD <key> <element> PFADD like:hll:post_123 user_456 # Get unique like count - PFCOUNT <key> PFCOUNT like:hll:post_123 # Merge HLLs (e.g. across datacenters) PFMERGE like:hll:post_123:global like:hll:post_123:dc1 like:hll:post_123:dc2
Top comments (0)