Redis is a very popular data store used across all kinds of web apps it’s supported natively by Mac and Linux operating systems. Heck, you can even use Docker to run Redis.

The purpose of this guide is to setup Redis so we can use in our Python applications including Django, Flask, Fastapi, Celery, and pure python apps.

Are you a windows user? Check out this redis install guide

Do you need to use Redis on Docker? The windows install guide shows you how.

  1. Brew services start redis I assume that you have redis server running on your development platform, now let’s proceed further to learn how to configure redis with Laravel project and see real-time examples using redis.
  2. Alias redisstart='sudo launchctl start io.redis.redis-server' alias redisstop='sudo launchctl stop io.redis.redis-server' then you can just start and stop redis by typing redisstart and redisstop. Another way you can go about this is adding redis as a background service.

Redis 127.0.0.1:6379 set key val OK redis 127.0.0.1:6379 get key 'val'.

3.1. Install Redis on macOS¶

3.1.1. Install Homebrew¶

3.1.2. Install redis¶

Every once and a while run brewupgraderedis

3.1.3. Start / Stop Redis¶

Starting redis this way turns redis into a background service. You can easily stop redis with:

3.1.4. Verify redis is running:¶

What result do you see?

  • PONG – great, redis is working and ready.

  • CouldnotconnecttoRedisat127.0.0.1:6379:Connectionrefused – this means that: (1) you did not install redis correctly or (2) redis is not running.

3.2. Install Redis on Linux (using Ubuntu)¶

Below was adapted from the production-ready guide in our series Hello Linux

3.2.1. Update System¶

3.2.2. Install redis-server

3.2.3. Update configuration¶

Update supervised to the following:

Save and exit nano.

Restart running service:

3.2.4. Verify installation:¶

What result do you see?

  • PONG – great, redis is working and ready.

  • CouldnotconnecttoRedisat127.0.0.1:6379:Connectionrefused – this means that: (1) you did not install redis correctly, (2) redis is not running, or (3) the system service did not start (check with sudosystemctlstatusredis)

3.3. Install on Windows 10 using Docker¶

Naturally, you need to install docker desktop on your windows 10. There’s a huge caveat that might push you towards using Memurai:

Docker requires you to have Windows 10 professional

Windows 10 professional allows for the OS virtualization that is just not provided in Windows 10 home.

  • Are you new to docker? Check out this basic intro

3.3.1. Verify Docker¶

Open PowerShell and run:

3.3.2. Create a project directory¶

Brew services start redis

3.3.3. Update (or create) a Dockerfile

You don’t have to build the redis image yourself but I prefer to so I can make changes easily and it’s already prepared for it.

3.3.4. Build our docker image:¶

Personally, I like adding commands like dockerbuild-t'cfe-redis'. to a build.ps1 file so I can just call ./build.ps1 if I ever want to build it again in the future.

3.3.5. Run our docker image:¶

A couple things to note:

-it--rm is so this docker container only runs when we want it to and the container will be removed after we’re done running it.

-p6379:6379 is exposing the port 6379 on our local system AND in our docker container.

cfe-redis is merely the tag name of the image we created in the build process.

Just like with build.ps1, make a run.ps1 file containing dockerrun-it--rm-p6379:6379'cfe-redis' as a shortcut to running this container.

3.3.6. Verify Docker connection¶

On Linux and Mac, you can just run redis-cliping but, since we’re using docker, we don’t have the redis-cli command available on our system.

We need another way to test. Since I use redis in my Python projects, I’ll create a simple python program to ensure our non-container items can connect to redis.

I already have Python 3.8 installed. If you don’t, check out this guide.

3.3.6.1. Create virtual environment¶

You can use pipenv as well. I’m just keeping things simple by using the built-in virtual environment manager

3.3.6.2. Activate virtual environment¶

3.3.6.3. Run pipinstallredis

This command does not install the redis server; it just installs a redis connector that we can use in python.

3.3.6.4. Create ping_redis.py

Notice the port=6379 I used? This is the default redis port and it matches to what I exposed above.

3.3.6.5. Run ping_redis.py

What result do you have?

  • Trueb'bar' – this means redis is running correctly

  • Noconnectioncouldbemadebecausethetargetmachineactivelyrefusedit. – this means your version of redis is either (1) not running or (2) running incorrectly or (3) you got the port number wrong.

3.3.7. Future running¶

Now, whenever you need to use redis in your projects. Just run:

Anywhere on your system.

3.4. Install Redis on Windows using Memurai.com¶

I haven’t used memurai much myself but it seems promising. Let’s see

3.4.2. Run downloaded installer¶

3.4.3. Agree to all defaults except port.¶

For the port, I’ll use 6380.

I’m leaving 6379 open. It’s the default redis port and I’m not 100% convinced I’ll use memurai in the future. I’m positive I’ll use Docker and so I’ll leave 6379 open for docker versions of redis like above.

3.4.4. Verify in Python – Create a virtual environment.¶

memurai should be running by default after you installed it so we need to verify.

Assuming you did not do the docker portion above, let’s create a new virtual environment to test redis is running via memurai. Open up PowerShell and run:

I already have Python 3.8 installed. If you don’t, check out this guide.

3.4.4.1. Activate virtual environment¶

3.4.4.2. Run pipinstallredis

This command does not install the redis server; it just installs a redis connector that we can use in python.

3.4.4.3. Create ping_redis.py

Notice the port=6380 I used? This is the redis port I used during the memurai installation.

3.4.4.4. Run ping_redis.py

What result do you have?

  • Trueb'bar' – this means redis is running correctly

  • Noconnectioncouldbemadebecausethetargetmachineactivelyrefusedit. – this means your version of redis is either (1) not running or (2) running incorrectly or (3) you got the port number wrong.

3.4.4.5. Future running¶

If you installed the defaults, memurai should start with your system startup just remember which port (6380) you used for the memurai redis.

Redis

Introduction

Redis is an in-memory key-value store that can execute millions of requests per second for running real-time applications. While the program can be used as a cache or message broker, Redis is most often used as a database. It supports basic and advanced data structures from strings, list and sets to geospatial indexes. The Redis CLI, an acronym for command line interface, is a straightforward program that allows users to communicate with Redis directly from the terminal. This tutorial will provide an overview and instructions on how to use the Redis CLI.

Prerequisites

  • Redis must be properly installed and configured on the local device. Executing the sudo systemctl status redis command in Linux distros that use systemd will display the current status of the Redis database on the local machine.

Install Redis

Install Redis on machines having a Windows or Mac OS by downloading the file here. Homebrew on Mac OS can also be used to install Redis.

Install Redis on Linux

For Debian-based Linux distros, such as Ubuntu or Linux Mint, execute the following APT-GET Update command on the local machine to get the current version of Redis:

sudoapt-get update

Once updated, execute the following command to install Redis:

Brew redis start up page

Now execute the following command to set Redis to run automatically on the local machine:

sudo systemctl enable redis-server

Install Redis using Homebrew

To install Redis on a Mac OS with the Homebrew package manager, first update the package to get the latest version of the Redis database. Once the update is completed, then execute the following command to install the updated package:

After installation, execute the following command to start the database:

brew services start redis

Now execute the following command to have Redis continually run in the background:

Now use the Redis-command line interface to confirm the program is working properly:

redis-cli
127.0.0.1:6379>ping
PONG

Redis command line usage

Redis use a straightforward command-line interface to provide features that can work around database functionality and other issues.

Begin by executing the following command to create a connection:

linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p6379-a mypassword

NOTE: The -a option will perform the authentication of the password.

The next two examples will cover two of the basic commands in the Redis command-line interface.

This first example uses the INCR operator:

linux@linux-NECq:~$ redis-cli incr count
(integer)3

Here the function displays an integer value of 3, but it can also be used to list the returned value of a string, arrays, the null or any errors.

The next example will create an output of the Redis CLI in a file that is readable to humans:

linux@linux-NECq:~$ redis-cli incr count >/tmp/output.txt
linux@linux-NECq:~$ cat/tmp/output.txt
4

Here the INCR key operation causes the integer value to increase incrementally. Note the value is shown in the output, and not in the terminal. However, executing the following --raw option will force the output to display on the terminal:

Redis host and password

The Redis command-line interface can be used to change the default value of Redis to the default host 127.0.0.1 and the default port 6379. This will permit an easier connection to the server to Redis.

The next example shows how to connect to and use the Redis instance in the command line:

linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p6379-a mypassword ping
PONG

Brew Services Start Redis

Now execute the INCR key function, covered earlier, to produce the values that will be inserted into five rows of data:

linux@linux-NECq:~$ redis-cli -r5 incr count
(integer)6
(integer)7
(integer)8
(integer)9
(integer)10

Now execute the following operation to confirm the status of the Redis database was properly set:

linux@linux-NECq:~$ redis-cli -h 127.0.0.1 -p6379-a mypassword --stat
------- data --------------------------- load -------------------- - child -
keys mem clients blocked requests connections
36 887.05K 405315(+0)6
36 887.02K 405317(+2)6
36 887.02K 405321(+4)6
36 887.02K 405323(+2)6
36 887.05K 405326(+3)6
36 887.02K 405328(+2)6
36 887.05K 405331(+3)6
36 887.02K 405333(+2)6
36 887.05K 405336(+3)6
36 887.02K 405338(+2)6
36 887.05K 405341(+3)6
36 887.02K 405343(+2)6
36 887.05K 405347(+4)6
36 887.02K 405349(+2)6
36 923.04K 405352(+3)6
36 887.02K 405354(+2)6
36 923.02K 405356(+2)6
36 923.02K 405359(+3)6
36 887.02K 405360(+1)6
36 887.05K 405363(+3)6

This gives an overview of how many of the keys are set in the Redis database, the total memory usage and generates an overall view of Redis, as a whole.

Brew Redis Start Download

Conclusion

Brew Redis Start Date

This tutorial provided a basic overview of how to use the Redis CLI. The tutorial explained how to install Redis on machines with Windows and Mac OS, Debian-based Linux distros and how to use Homebrew to install Redis on Mac. The article also covered how to start the database and set Redis to continually run in the background. The tutorial then explained how to use the Redis command-line interface to confirm the program is working, how to change the default value of Redis for easier connection and then how to confirm that the status of the Redis database was properly set. Remember that the INCR key operation will causes the integer value to increase incrementally, but the value is not displayed in the terminal unless the --raw option is used.