Published on

Essential Redis Commands Every Developer Should Know

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Introduction

Redis is a fast, in-memory database with a simple yet powerful set of commands. This guide covers the basic commands that every developer should master to interact effectively with Redis.


1. Key Management

Commands:

SET key "value"         # Set a key with a value
GET key                 # Retrieve the value of a key
DEL key                 # Delete a key
EXISTS key              # Check if a key exists
EXPIRE key 60           # Set a time-to-live (TTL) of 60 seconds on a key
TTL key                 # Check the remaining TTL of a key

Explanation:

  • These commands are fundamental for creating, retrieving, and managing keys in Redis.
  • Use EXPIRE to manage temporary data like session tokens.

2. String Operations

Commands:

SET key "value"          # Set a key with a value
GET key                  # Retrieve the value of a key
INCR counter             # Increment the value of a key (must be a number)
DECR counter             # Decrement the value of a key (must be a number)
APPEND key "extra"       # Append a string to the existing value of a key

Use Cases:

  • Use strings for caching simple values, counters, or concatenating data.

3. List Operations

Commands:

LPUSH list "item1"      # Add an item to the beginning of the list
RPUSH list "item2"      # Add an item to the end of the list
LPOP list               # Remove and return the first element of the list
RPOP list               # Remove and return the last element of the list
LRANGE list 0 -1        # Retrieve all elements in the list

Use Cases:

  • Manage tasks in a queue (e.g., job queues, activity logs).

4. Set Operations

Commands:

SADD set "item1"         # Add an item to the set
SREM set "item1"         # Remove an item from the set
SMEMBERS set             # Retrieve all items in the set
SISMEMBER set "item1"    # Check if an item exists in the set

Use Cases:

  • Manage unique collections like tags or categories.

5. Hash Operations

Commands:

HSET hash field "value"  # Set a field in a hash
HGET hash field          # Retrieve a field's value from a hash
HDEL hash field          # Delete a field from a hash
HGETALL hash             # Retrieve all fields and values from a hash

Use Cases:

  • Store structured data like user profiles or configuration settings.

6. Sorted Set Operations

Commands:

ZADD zset 1 "item1"     # Add an item with a score to the sorted set
ZRANGE zset 0 -1        # Retrieve items in ascending order of their scores
ZREM zset "item1"       # Remove an item from the sorted set
ZSCORE zset "item1"     # Retrieve the score of an item

Use Cases:

  • Implement leaderboards or priority queues.

7. General Utility Commands

Commands:

FLUSHDB                 # Delete all keys in the current database
FLUSHALL                # Delete all keys in all databases
INFO                    # Display server information and statistics
PING                    # Test connectivity to the Redis server
SELECT 1                # Switch to database index 1
DBSIZE                  # Get the number of keys in the current database

Use Cases:

  • Manage and monitor Redis instances efficiently.

Conclusion

Mastering these basic Redis commands will help developers use Redis effectively for a wide range of applications, from simple caching to managing complex data structures. By practicing these commands, you'll be well-prepared to handle real-world Redis use cases with confidence.