Developer
Redis Guide
Install Redis, learn core data types, and copy common Redis CLI commands.
| Type | Command | Explanation | |
|---|---|---|---|
| Connection | redis-cli | Connect to a local Redis server. | |
| Connection | redis-cli -h 127.0.0.1 -p 6379 -a password | Connect to a Redis server with host, port, and password. | |
| Server | PING | Check whether Redis is responding. | |
| String | SET user:1:name "Ada" | Set a string value. | |
| String | GET user:1:name | Read a string value. | |
| String | SET session:abc "value" EX 3600 | Set a value with expiration in seconds. | |
| String | INCR counter:visits | Increment an integer value. | |
| Key | EXISTS user:1:name | Check whether a key exists. | |
| Key | DEL user:1:name | Delete one or more keys. | |
| Key | TTL session:abc | Show remaining expiration time. | |
| Hash | HSET user:1 email ada@example.com age 36 | Set fields on a hash. | |
| Hash | HGETALL user:1 | Read all fields from a hash. | |
| List | LPUSH queue:emails job-1 | Push an item to the left of a list. | |
| List | RPOP queue:emails | Pop an item from the right of a list. | |
| Set | SADD tags:post:1 redis cache database | Add unique values to a set. | |
| Set | SMEMBERS tags:post:1 | Read all set members. | |
| Sorted Set | ZADD leaderboard 100 user:1 90 user:2 | Add scored members. | |
| Sorted Set | ZREVRANGE leaderboard 0 9 WITHSCORES | Read top scores. | |
| Pub/Sub | PUBLISH events:user.created "1" | Publish a message to a channel. | |
| Pub/Sub | SUBSCRIBE events:user.created | Subscribe to a channel. | |
| Stream | XADD orders * user_id 1 total 39.99 | Append an entry to a stream. | |
| Stream | XRANGE orders - + COUNT 10 | Read stream entries. | |
| Admin | INFO | Show server metrics and configuration summary. | |
| Admin | FLUSHDB | Delete all keys from the current database. Use carefully. |
Concepts
Common Redis data types
| Type | Use case | Examples |
|---|---|---|
| String | Cache values, counters, feature flags, sessions. | SET, GET, INCR, MGET |
| Hash | Object-like field storage. | HSET, HGET, HGETALL |
| List | Queues and ordered collections. | LPUSH, RPOP, LRANGE |
| Set | Unique unordered values. | SADD, SMEMBERS, SINTER |
| Sorted Set | Leaderboards and ranked data. | ZADD, ZRANGE, ZREVRANGE |
| Stream | Append-only event streams. | XADD, XRANGE, XREAD |
FAQ
Questions people ask
What is Redis used for?
Redis is commonly used for caching, sessions, queues, rate limiting, pub/sub, leaderboards, and fast in-memory data structures.
Is Redis a database or a cache?
Redis can be used as a cache, message broker, or primary data store depending on persistence and architecture choices.
How should I avoid deleting data accidentally?
Be careful with FLUSHDB, FLUSHALL, and broad key deletion patterns. Prefer explicit keys and use separate databases or instances for environments.
Related tools