skip to content
Just Change Direction

How to connect to Redis from the cli

/ 2 min read

Sometimes I find myself needing to connect to a Redis instance from the command line. Normally during an incident where we need to manually invalidate some cache. This is how I do it.

Terminal window
docker run -it redis redis-cli -h <url-to-redis-without:port>

It is important to not include the :port part (e.g. :6379) in the url as you get some weird error that doesn’t make it obvious that this is the issue.

Once you have run this command you will be connected to the Redis instance and can run any command you need to.

GET

Returns whatever is stored at key

Terminal window
GET key

DEL

Deletes whatever is stored at key

Terminal window
DEL key

SETEX

Set with expiry

Terminal window
SETEX key seconds value

Manually pipe a large amount of data into a key

If you have a large amount of data that you wish to save to a specific key, the CLI might not let you. To get around this, you can pipe the data in using the following command (from your standard CLI, not from within the Redis CLI - i.e. you do not need to have connected to the Redis CLI first)

In this example, redis.txt is a text file in the current working directory:

Terminal window
cat redis.txt | docker run -i redis redis-cli -h redis-link-from-aws-console --pipe

redis.txt should contain the full command AND the payload. Make sure the payload is in the right format too (likely stringified). For example:

Terminal window
SETEX key exp stringified-payload