Skip to main content

Redis CLI: Your Command-Line Interface to Redis

Introduction to Redis CLI

The Redis CLI (Command Line Interface) is a powerful command-line tool for interacting with a Redis server. It allows you to execute Redis commands, manage the Redis server, and perform various operations on Redis data. This document provides a comprehensive overview of how to use the Redis CLI effectively.

Connecting to Redis

To connect to a Redis server using the Redis CLI, you can use the following command:

redis-cli

By default, this command will attempt to connect to a Redis server running on localhost and port 6379.

Connecting to a Specific Host and Port:

If your Redis server is running on a different host or port, you can specify them using the -h and -p options:

redis-cli -h <hostname> -p <port>

For example:

redis-cli -h redis.example.com -p 6380

Connecting with a Password:

If your Redis server requires a password for authentication, you can provide it using the -a option:

redis-cli -a <password>

Or, you can connect without the password, and then authenticate using the AUTH command within the CLI:

redis-cli
AUTH <password>

Basic Redis Commands

Once connected to the Redis server, you can execute various Redis commands. Here are some essential commands:

  • PING: Checks if the server is running. Returns "PONG" if successful.

    PING
  • ECHO <message>: Prints the given string. Useful for testing the connection.

    ECHO "Hello, Redis!"
  • SET <key> <value>: Sets the value of a key.

    SET mykey "Hello World"
  • GET <key>: Gets the value of a key.

    GET mykey
  • DEL <key>: Deletes a key.

    DEL mykey
  • EXISTS <key>: Checks if a key exists. Returns 1 if it exists, 0 otherwise.

    EXISTS mykey
  • KEYS <pattern>: Returns all keys matching the given pattern. Use with caution in production, especially with large datasets, as it can be slow. Use SCAN instead (see below).

    KEYS *
    KEYS user:*
  • FLUSHDB: Deletes all keys in the current database. Use with caution in production!

    FLUSHDB
  • FLUSHALL: Deletes all keys in all databases. Extremely dangerous - avoid in production!

    FLUSHALL
  • INFO: Provides information about the Redis server, including memory usage, connected clients, and more. You can filter the output by section (e.g., INFO memory).

    INFO
    INFO memory
  • CLIENT LIST: Lists all connected clients. Useful for debugging connection issues.

    CLIENT LIST
  • SHUTDOWN: Shuts down the Redis server. Requires appropriate permissions.

    SHUTDOWN

Working with Different Data Types

Redis supports various data types, including strings, lists, sets, sorted sets, and hashes. The Redis CLI allows you to interact with these data types using specific commands:

  • Strings: SET, GET, MSET, MGET, INCR, DECR, APPEND, STRLEN
  • Lists: LPUSH, RPUSH, LPOP, RPOP, LRANGE, LINDEX, LSET, LLEN
  • Sets: SADD, SREM, SMEMBERS, SISMEMBER, SCARD, SINTER, SUNION, SDIFF
  • Sorted Sets: ZADD, ZREM, ZRANGE, ZREVRANGE, ZRANK, ZSCORE, ZCARD
  • Hashes: HSET, HGET, HGETALL, HDEL, HKEYS, HVALS, HLEN, HEXISTS

For detailed information about these commands, refer to the official Redis documentation.

Advanced Usage

  • SCAN: A cursor-based iterator for incrementally iterating over keys in the Redis keyspace. This is the preferred way to iterate over keys in production, as it avoids blocking the server.

    SCAN 0 MATCH mypattern* COUNT 10  # Start a scan
    SCAN <cursor> MATCH mypattern* COUNT 10 # Continue the scan with cursor
  • Pipelining: Sending multiple commands to the server in a single batch to improve performance. This reduces network round-trip time.

    redis-cli pipeline < commands.txt

    Where commands.txt contains a list of Redis commands, one per line. Alternatively, use the redis-cli --pipe option.

  • Lua Scripting: Executing Lua scripts directly on the Redis server for complex operations.

    EVAL "return redis.call('set', KEYS[1], ARGV[1])" 1 mykey "myvalue"
  • Monitoring: Using the MONITOR command to see a real-time stream of all commands executed on the server.

    MONITOR

    Note: MONITOR should never be used in production due to its high performance impact.

Redis CLI Options

The Redis CLI provides various options for customizing its behavior:

  • -u <uri>: Connect to a Redis server using a URI.
  • -r <repetitions>: Repeat a command multiple times.
  • -i <interval>: Execute a command periodically.
  • --latency: Run in latency check mode.
  • --scan: Use the SCAN command instead of KEYS for listing keys.
  • --pattern <pattern>: Specify a pattern for the --scan command.
  • --slave: Connect as a slave (replica) to a master instance.

Examples

  • Incrementing a counter:

    SET counter 10
    INCR counter
    GET counter
  • Adding members to a set:

    SADD myset "value1" "value2" "value3"
    SMEMBERS myset
  • Getting all keys that start with "user:" using SCAN:

    SCAN 0 MATCH user:* COUNT 10

Conclusion

The Redis CLI is an indispensable tool for interacting with Redis servers. By mastering its commands and options, you can efficiently manage your Redis data, troubleshoot issues, and optimize performance. Remember to consult the official Redis documentation for complete details on all available commands and their options.