Running Redis on Google Colab

Open source Redis is being increasingly used in Machine Learning, but running it on Colab is different compared to on your local machine or with Docker. Read on for a 2-step tutorial on how to do it.



Google Colab is a popular browser based environment for executing Python code on hosted Jupyter notebooks and training models for machine learning, including free access to GPUs! It is a great platform for data scientists and machine learning (ML) engineers for learning and quickly developing ML models in Python. Redis is an in-memory open source database that is increasingly being used in machine learning - from caching, messaging and fast data ingest, to semantic search and online feature stores. In fact, NoSQL databases - and specifically Redis - was named by Ben Weber, Director of Applied Data Science at Zynga as one of the 8 new tools he learned as a data scientist in 2020. 

 

Redis with Colab for Machine Learning

 
Because of the increasing use of Redis for data science and machine learning, it is very handy to be able to run Redis directly from your Google Colab notebook!  However, running Redis on Google Colab differs from how you would set it up on your local machine or using Docker. Below I’m going to show you how in two simple steps you can run Redis on your Colab notebook, all directly from your browser.
 

Running Redis on Google Colab
Image created by author using Colab logo (Image credits: Medium) and Redis logo (fair use)

 
 

Installing and Running Redis on Colab

 

Step 1: Install

 
To install Redis and the Redis Python client:

​​%pip install redis-server redis


Notes:

*While Jupyter Notebooks support many languages, Colab supports only Python. To use Redis with Python, you need a Redis Python client. In this tutorial we demonstrate the use of redis-py, a Redis Python Client, which we install using the %pip install redis command. 

**You can run a shell command in Jupyter Notebook or Google Colab with IPython by prefixing it with the ! character or % to use magic commands. A list of useful magic commands for data scientists are described in the article - top 8 magic commands in Jupyter Notebook.



 

Step 2: Start the Redis Server

 
To start the Redis server run:

import redis_server
!$redis_server.REDIS_SERVER_PATH --daemonize yes


Note:

Alternately you can start the Redis server without shell commands, using a Python subprocess:

import subprocess
import redis_server
subprocess.Popen([redis_server.REDIS_SERVER_PATH])


That’s it! It’s that simple.

 

Connecting to the Redis Server and Redis command functions

 
Let’s now look at the commands we’ll need to verify that Redis is running, connect to it and read and write data. 
 

Verify that Redis is Running

 
If you want to verify that Redis is up and running you can connect to the server and run the "PING command".  We create a connection to Redis using the Python client redis-py and then we ‘ping’ the server:

import redis
client = redis.Redis(host = 'localhost', port=6379)

client.ping()


If you get True, then you are good to go! 

 

Example code for Redis commands 

 
Once connected to Redis, you can read and write data with Redis command functions. In this example we use Redis as a key value database (also called key value store). The following code snippet assigns the value bar to the Redis key foo, reads it back, and returns it:

client.set('foo', 'bar')
client.get('foo')


 

Summary

 
In this blog post we have seen how to run the Redis database on Google Colab, all from your browser! We first installed Redis and the Redis Python client, then started the Redis server and verified that it’s running by creating a connection to it. Finally we saw how to read and write data from the Redis database using Redis command functions. If you want to play with commands yourself, here is a link to the Redis with Colab notebook that includes the code in this tutorial.

 
 
Nava Levy is a Developer Advocate for Data Science and MLOps at Redis. She started her career in tech with an R&D Unit in the IDF and later had the good fortune to work with and champion Cloud, Big Data, and DL/ML/AI technologies just as the wave of each of these was starting. Nava is also a mentor at the MassChallenge accelerator and the founder of LerGO—a cloud-based EdTech venture. In her free time she enjoys cycling, 4-ball juggling, and reading fantasy and sci-fi books.