Redis CLI & Basic Commands

7 questions found

What is redis-cli, and how do you use it to connect to and interact with a Redis server?

Beginner
redis-cli is the official command line tool for connecting directly to a Redis server, letting you run commands interactively and see their results immediately, which is useful for quick testing, troubleshooting, and learning how specific commands behave before using them in application code.
redis-cli -h localhost -p 6379
127.0.0.1:6379> SET greeting 'hello'
127.0.0.1:6379> GET greeting
Real-world example A developer uses redis-cli to quickly test how a new command behaves before writing the equivalent code in their application, confirming the exact syntax and expected response format.

Common follow-ups: How do you connect redis-cli to a remote Redis server with a password?;How do you exit the redis-cli interactive session?

Redis Architecture & Installation;Redis Security & ACL

What do the basic SET, GET, and DEL commands do in Redis?

Beginner
SET stores a value under a specified key, GET retrieves the current value stored under a specified key, and DEL removes one or more keys entirely from Redis, together forming the fundamental building blocks for storing, retrieving, and removing simple data.
SET username 'alice'
GET username
DEL username
Real-world example A simple caching layer uses SET to store a computed result, GET to quickly retrieve it on subsequent requests, and DEL to remove it once it is no longer needed or has become outdated.

Common follow-ups: What does GET return if the requested key does not exist?;Can DEL remove multiple keys in a single command call?

Data Types;Expiration & Eviction

How do you search for keys matching a specific pattern in Redis, and why is the KEYS command generally discouraged in production?

Intermediate
You can use the KEYS command with a wildcard pattern to find matching key names, but it is generally discouraged in production because it scans the entire keyspace in a single blocking operation, which can significantly slow down the server on a large dataset, and the SCAN command is recommended instead since it iterates through the keyspace incrementally without blocking.
-- Avoid in production on a large dataset
KEYS user:*

-- Preferred alternative
SCAN 0 MATCH user:* COUNT 100
Real-world example A team investigating a brief production slowdown discovers a monitoring script had been calling KEYS on a large dataset, and switches it to use SCAN instead, eliminating the blocking behavior that was causing the issue.

Common follow-ups: How does SCAN's cursor based iteration actually work?;Are there any guarantees SCAN provides about not missing or duplicating keys?

Redis Memory Optimization;Redis Performance Tuning & Benchmarking

How do you check basic information about a Redis key, such as its data type and remaining time to live?

Intermediate
You use the TYPE command to see what kind of data type a key holds, such as string, list, or hash, and the TTL command to see how many seconds remain before the key automatically expires, returning a negative value if the key either has no expiration set or does not exist at all.
TYPE mykey
TTL mykey
Real-world example A developer debugging an issue where cached data seemed to disappear too quickly checks the TTL of several keys, discovering an expiration time had been set much shorter than intended.

Common follow-ups: What do the different negative values returned by TTL specifically mean?;How do you check the exact byte size a specific key is consuming in memory?

Expiration & Eviction;Data Types

How would you use redis-cli's built-in monitoring and debugging features to troubleshoot a live performance issue on a production Redis instance?

Advanced
You could use the MONITOR command to see a live stream of every command being processed by the server, though carefully since it adds overhead and should be used briefly, the SLOWLOG command to review commands that have taken longer than a configured threshold to execute, and the LATENCY tool to identify specific sources of delay within Redis's internal operations.
redis-cli slowlog get 10
redis-cli latency history command
Real-world example A team investigating intermittent slow responses uses SLOWLOG to identify a specific command pattern that was consistently taking much longer than expected, quickly pinpointing the root cause of their performance issue.

Common follow-ups: Why should MONITOR be used sparingly on a busy production server?;How do you clear the slow log once you have finished reviewing it?

Redis Performance Tuning & Benchmarking;Redis Monitoring & Observability

How do you use redis-cli in scripting mode to automate repetitive administrative tasks rather than running commands interactively?

Advanced
You can pass commands directly as arguments to redis-cli for a single non interactive execution, or pipe a series of commands from a file, letting you build automated scripts for tasks like bulk data loading, scheduled maintenance operations, or integrating Redis commands into larger shell based automation workflows.
redis-cli set mykey myvalue
cat commands.txt | redis-cli
Real-world example An operations team automates a routine cleanup task by writing a script that pipes a series of Redis commands through redis-cli, running it as a scheduled job without any manual interactive steps required.

Common follow-ups: What is the performance difference between piping many commands versus using pipelining?;How do you handle errors that occur partway through a scripted series of commands?

Redis Performance Tuning & Benchmarking;Connection Pooling & Client Libraries

How do you use the EXISTS and RENAME commands, and what should you be careful about when using RENAME?

Intermediate
EXISTS checks whether one or more specified keys are currently present in Redis, returning a count of how many of them actually exist, while RENAME changes an existing key's name, but you should be careful since RENAME will silently overwrite any existing key that already has the new target name, potentially causing accidental data loss.
EXISTS mykey
RENAME oldname newname
Real-world example A developer accidentally overwrites important cached data using RENAME, learning to first check with EXISTS or use RENAMENX, which only renames if the target name does not already exist, to avoid this kind of mistake in the future.

Common follow-ups: What does RENAMENX do differently from the regular RENAME command?;What happens if you try to rename a key that does not exist?

Data Types;Redis CLI & Basic Commands