# redis-cli — The Redis Command-Line Client

> Practical guide to redis-cli — the interactive client for Redis and Valkey: manage keys and data structures, use pub/sub and monitor the server.

Source: https://www.jpkc.com/db/en/cheatsheets/build-languages/redis-cli/

<!-- PROSE:intro -->
redis-cli is the interactive command-line client for Redis – and equally for its compatible fork Valkey. With it you connect to an in-memory data store and work directly with keys and data structures such as strings, hashes, lists, sets and sorted sets. Beyond that, you use pub/sub for messaging, monitor the server while it runs and import commands in bulk. This guide walks you through the commands you reach for daily, from establishing a connection through the typical data-type operations to monitoring.
<!-- PROSE:intro:end -->

## Connect

`redis-cli` — Connect to local Redis (localhost:6379).

```bash
redis-cli
```

`redis-cli -h <host> -p <port>` — Connect to a remote Redis server.

```bash
redis-cli -h redis.example.com -p 6379
```

`redis-cli -a <password>` — Connect with authentication.

```bash
redis-cli -a mysecretpassword
```

`redis-cli -u <uri>` — Connect using a Redis URI.

```bash
redis-cli -u redis://user:pass@host:6379/0
```

`redis-cli -n <db>` — Select a specific database number.

```bash
redis-cli -n 2
```

`redis-cli --tls` — Connect with TLS encryption.

```bash
redis-cli --tls -h redis.example.com
```

## Keys & Strings

`SET <key> <value>` — Set a key to a string value.

```bash
SET user:1:name "John"
```

`GET <key>` — Get the value of a key.

```bash
GET user:1:name
```

`SET <key> <value> EX <seconds>` — Set a key with expiration time.

```bash
SET session:abc123 "data" EX 3600
```

`DEL <key>` — Delete one or more keys.

```bash
DEL user:1:name user:1:email
```

`KEYS <pattern>` — Find keys matching a pattern (use with caution in production).

```bash
KEYS user:*
```

`SCAN <cursor> MATCH <pattern> COUNT <n>` — Iterate keys safely (production-friendly).

```bash
SCAN 0 MATCH user:* COUNT 100
```

`TTL <key>` — Get remaining time to live in seconds.

```bash
TTL session:abc123
```

`INCR <key>` — Increment a numeric value by 1.

```bash
INCR page:views
```

## Hashes & Lists

`HSET <key> <field> <value>` — Set a field in a hash.

```bash
HSET user:1 name "John" email "john@example.com"
```

`HGET <key> <field>` — Get a field value from a hash.

```bash
HGET user:1 name
```

`HGETALL <key>` — Get all fields and values from a hash.

```bash
HGETALL user:1
```

`LPUSH <key> <value>` — Push a value to the left of a list.

```bash
LPUSH queue:tasks "task1"
```

`RPOP <key>` — Pop a value from the right of a list.

```bash
RPOP queue:tasks
```

`LRANGE <key> <start> <stop>` — Get a range of elements from a list.

```bash
LRANGE queue:tasks 0 -1
```

`LLEN <key>` — Get the length of a list.

```bash
LLEN queue:tasks
```

## Sets & Sorted Sets

`SADD <key> <member>` — Add a member to a set.

```bash
SADD tags:post:1 "redis" "database" "cache"
```

`SMEMBERS <key>` — Get all members of a set.

```bash
SMEMBERS tags:post:1
```

`SISMEMBER <key> <member>` — Check if a value is in a set.

```bash
SISMEMBER tags:post:1 "redis"
```

`ZADD <key> <score> <member>` — Add a member with a score to a sorted set.

```bash
ZADD leaderboard 100 "player1" 200 "player2"
```

`ZRANGE <key> <start> <stop> WITHSCORES` — Get members from a sorted set by rank.

```bash
ZRANGE leaderboard 0 -1 WITHSCORES
```

## Server & Monitoring

`INFO` — Show server information and statistics.

```bash
INFO server
```

`INFO memory` — Show memory usage statistics.

```bash
INFO memory
```

`DBSIZE` — Show the number of keys in the current database.

```bash
DBSIZE
```

`MONITOR` — Stream all commands received by the server in real-time.

```bash
MONITOR
```

`SLOWLOG GET <n>` — Show the last N slow queries.

```bash
SLOWLOG GET 10
```

`FLUSHDB` — Delete all keys in the current database.

```bash
FLUSHDB
```

`BGSAVE` — Trigger a background save to disk.

```bash
BGSAVE
```

## Pub/Sub & Scripting

`SUBSCRIBE <channel>` — Subscribe to a channel for messages.

```bash
SUBSCRIBE notifications
```

`PUBLISH <channel> <message>` — Publish a message to a channel.

```bash
PUBLISH notifications "New order received"
```

`PSUBSCRIBE <pattern>` — Subscribe to channels matching a pattern.

```bash
PSUBSCRIBE user:*
```

`redis-cli --pipe < <file>` — Bulk import Redis commands from a file.

```bash
redis-cli --pipe < commands.txt
```

<!-- PROSE:outro -->
## Conclusion

redis-cli is the Swiss Army knife for everyday work with Redis and Valkey: from a quick `GET`/`SET` through data-structure operations to live monitoring with `MONITOR` and `INFO`. Be careful with the destructive commands: `FLUSHALL` and `FLUSHDB` delete all keys (of an entire instance or the current database) irreversibly. Avoid `KEYS *` in production – the command blocks the server until every key has been scanned; use `SCAN` or `redis-cli --scan` instead. Prefer passing passwords through the `REDISCLI_AUTH` environment variable rather than in clear text, because `-a` shows up in your shell history and in the process list. Note that `CONFIG SET` changes runtime behavior live, too – a single typo hits the whole instance instantly.

## Further Reading

- [Redis documentation](https://redis.io/docs/latest/) – official reference for redis-cli, commands and data types
- [redis-cli command-line interface](https://redis.io/docs/latest/develop/tools/cli/) – in-depth guide to the client
- [Valkey documentation](https://valkey.io/docs/) – open, Redis-compatible alternative
<!-- PROSE:outro:end -->

## Related Commands

- [artisan](https://www.jpkc.com/db/en/cheatsheets/build-languages/artisan/) – command-line tool of the Laravel framework
- [cargo](https://www.jpkc.com/db/en/cheatsheets/build-languages/cargo/) – package manager and build tool for Rust
- [composer](https://www.jpkc.com/db/en/cheatsheets/build-languages/composer/) – dependency manager for PHP

