Skip to content

Redis - Commands

Redis commands are the cornerstone of interacting with the Redis data store. They allow you to manage data, monitor the server, and perform administrative tasks. While all commands follow a simple text-based protocol (RESP), you will typically interact with them through a client.

The most direct way to run commands is using the redis-cli, a powerful terminal client bundled with the Redis server. It’s an essential tool for development, debugging, and administration.

To start redis-cli and connect to a local Redis server running on the default port (6379), simply type the command in your terminal:

$ redis-cli
127.0.0.1:6379>

Once connected, you can issue any Redis command. A good first command is PING, which checks the server’s availability.

127.0.0.1:6379> PING
PONG
127.0.0.1:6379> PING "Hello, Redis!"
"Hello, Redis!"

Connecting to a Remote or Protected Server

Section titled “Connecting to a Remote or Protected Server”

In production environments, Redis is almost always run on a remote server and protected with a password or more advanced security.

The recommended method for connecting is with a URI (Uniform Resource Identifier), which cleanly encapsulates all connection details. This format is supported by Redis 6 and newer.

# Syntax: redis://[<user>:<password>@]<host>[:<port>][/<db>]
# Example connecting to a remote host with a password
$ redis-cli -u redis://:mysecretpassword@redis.example.com:6380
redis.example.com:6380> PING
PONG

The older syntax using individual flags (-h for host, -p for port, -a for password) still works but is considered less modern.

Beyond the CLI: Using Redis in Applications

Section titled “Beyond the CLI: Using Redis in Applications”

While redis-cli is great for administration, your applications will use a Redis client library. These libraries are available for virtually every programming language and provide a more idiomatic way to execute commands.

Here’s how you might connect to Redis and run a few commands in a Node.js application.

// First, install the library: npm install ioredis
const Redis = require('ioredis');
// The client can take a connection string, just like the CLI
const redis = new Redis('redis://:mysecretpassword@redis.example.com:6380');
async function runDemo() {
try {
// Set a key
await redis.set('user:1:name', 'Alice');
// Get the key
const name = await redis.get('user:1:name');
console.log(`User's name is: ${name}`); // Output: User's name is: Alice
// Use a hash for more complex data
await redis.hset('user:1:profile', { 'email': 'alice@example.com', 'plan': 'premium' });
const email = await redis.hget('user:1:profile', 'email');
console.log(`User's email is: ${email}`); // Output: User's email is: alice@example.com
} catch (error) {
console.error('Failed to connect or run command:', error);
} finally {
// Close the connection
redis.quit();
}
}
runDemo();

This modern approach, using dedicated libraries, is the standard for application development and includes benefits like connection pooling, error handling, and type safety (especially when used with TypeScript).