Redis Administration with redis-cli: A Quick Guide

Introduction:

In this blog post, we will explore the various commands and functionalities of redis-cli to help you in administrating a Redis cluster and managing databases.

Connecting to a Redis Cluster:

Before diving into database management, let’s start by connecting to a Redis cluster using redis-cli. The basic syntax is as follows:

redis-cli -c -h <redis-cluster-node> -p <port>

The -c flag indicates that redis-cli should operate in cluster mode.

Cluster Information:

To obtain essential information about the Redis cluster, the following command is useful:

cluster info

This command provides details about the cluster, such as node information, cluster state, and more.

Node Inspection:

Individual nodes within the Redis cluster can be inspected using:

cluster nodes

This command displays a list of nodes, along with their IDs, IP addresses, ports, and flags.

Managing Databases:

Redis supports multiple databases, each identified by a unique numeric index. To switch between databases, use:

select <index>

For example, to switch to the second database, use select 1.

Key Operations:

Managing keys is fundamental in Redis. Here are some key-related redis-cli commands:

  • Listing Keys: keys * This command returns all keys present in the currently selected database.
  • Getting Key Values: get <key> Retrieve the value stored at a specific key.
  • Deleting Keys: del <key> Remove a key and its associated value from the database.

Database Administration:

Administrative tasks related to the Redis cluster can be performed using the following commands:

  • Flushing the Current Database: flushdb This command removes all keys from the currently selected database.
  • Flushing the Entire Redis Cluster: flushall Use this command cautiously as it removes all keys from all databases in the entire Redis cluster.

Monitoring:

Monitoring the performance and resource utilization of the Redis cluster can be accomplished through redis-cli:

  • Monitoring Commands: info This command provides information about various aspects of the Redis server, including memory usage, commands processed, and more.
  • Real-time Monitoring: monitor Enter interactive monitoring mode to see live commands processed by the Redis server.