Top 10 Redis Commands Every Developer Should Know

Top 10 Redis Commands Every Developer Should Know
Redis, or Remote Dictionary Server, is an in-memory data structure store, widely used as a database, cache, and message broker. Its speed, flexibility, and support for various data structures make it a popular choice among developers. Here are the top 10 Redis commands every developer should be familiar with.
1. SET
The SET command sets the value of a key. This is foundational for storing simple key-value pairs in Redis.
SET key valueExample:
SET user:1000 "John Doe"This command assigns the name “John Doe” to the key user:1000. You can also set expiry times with the EX option:
SET key value EX seconds2. GET
The GET command retrieves the value of a specified key.
GET keyExample:
GET user:1000This command will return “John Doe”. If the key does not exist, Redis returns nil.
3. EXISTS
The EXISTS command checks if a specified key exists in the database.
EXISTS keyExample:
EXISTS user:1000This returns 1 if the key exists and 0 if it doesn’t, making it useful for verifying key presence before performing other operations.
4. DEL
To delete a key and its associated value, use the DEL command.
DEL keyExample:
DEL user:1000Executing this command removes the user:1000 key from the database, freeing up memory.
5. INCR and DECR
The INCR and DECR commands are essential for incrementing or decrementing the integer value of a key.
INCR key
DECR keyExample:
SET counter 10
INCR counterThis command increments counter to 11. Conversely, using DECR will decrease its value by 1.
6. LPUSH and RPUSH
For developers working with lists, LPUSH and RPUSH are vital. They insert values at the head (LPUSH) or the tail (RPUSH) of a list.
LPUSH list_name value
RPUSH list_name valueExample:
LPUSH myList "Item 1"
RPUSH myList "Item 2"This creates a list with “Item 1” at the front and “Item 2” at the back.
7. LRANGE
To retrieve elements from a list, the LRANGE command allows you to specify a start and stop index.
LRANGE list_name start stopExample:
LRANGE myList 0 -1This command fetches all elements of myList, returning a list of items.
8. HSET and HGET
Redis supports Hashes to store objects with multiple fields. HSET allows you to set a field in a hash, while HGET retrieves its value.
HSET hash_name field value
HGET hash_name fieldExample:
HSET user:1000 name "John Doe"
HGET user:1000 nameThis allows you to store structured data, quickly fetching specific attributes of an object.
9. ZADD and ZRANGE
For sorted sets, ZADD adds members with scores, while ZRANGE retrieves members in rank order.
ZADD zset_name score member
ZRANGE zset_name start stopExample:
ZADD leaderboard 100 "Player1"
ZRANGE leaderboard 0 -1This creates a sorted set for a leaderboard and retrieves players sorted by their scores.
10. KEYS
The KEYS command retrieves all keys matching a specified pattern. While not ideal for production usage due to performance concerns, it’s useful in development and testing.
KEYS patternExample:
KEYS user:*This command returns all keys that start with user:, providing developers visibility on existing data.
Best Practices for Using Redis Commands
- Avoid KEYS in Production: Use
SCANinstead ofKEYSfor better performance in large datasets. - Handle Errors Gracefully: Redis commands may fail, especially under heavy loads. Always check for and handle errors.
- Batch Commands: If possible, use Redis pipelines to batch multiple commands together, which can reduce latency.
- Expire Keys Judiciously: Use the expiration feature wisely to ensure cache or temporary data does not live longer than necessary.
Understanding these top 10 Redis commands will significantly streamline your development process and enhance your application’s performance. Mastering these commands can enable developers to leverage the full potential of Redis as a powerful data store and caching mechanism.





