Hamburger_menu.svg

FOR DEVELOPERS

Hands-on Tutorial on Running Redis on Google Colab

Running Redis on Google Colab.

Redis, which stands for Remote Dictionary Server, is a type of database similar to MySQL, PostgreSQL, and MongoDB. It’s an in-memory key-value data store, not unlike a dictionary where keys and their corresponding values are stored. You can perform many operations with Redis including assigning values to keys, extracting values, and more. It supports diverse operations with computational efficiency, making it an increasingly popular choice.

Today, Google Colab notebooks are widely used owing to their easy-to-understand interface, supporting functions, and packages. Even better, you can use them with Redis.

This article will take you through the basics of Redis, its practical applications, and how to run it on a Google Colab notebook.

How is Redis different?

In Redis, data is stored in the primary computer memory or RAM whereas in traditional databases, it’s stored on a disk. Some operations like caching require fast access to data. Since data stored in RAM is more quickly accessed than data stored in slower (secondary) memory such as on a disk, engine latency is reduced to microseconds. This is what makes Redis fast and suitable for applications like caching.

A significant factor to consider for any data store is durability. The goal is to avoid data loss in case of hardware crashes. With Redis, data stored in RAM is volatile and may be lost so to tackle the issue, it provides a range of persistence options like RBD, AOF, etc.

Setting up Redis on Google Colab

Google Colab is a browser-based integrated development environment (IDE) popularly used for data science and machine learning codes. It provides users free access to GPU to train heavy experiments. Traditionally, when using Redis on a local machine, you will set it up using Docker. However, setting it up on Google Colab is different.

The first step is to install Redis by following the command below:

​​%pip install redis-server redis

The command will install Redis-py, which is a Redis Python client. It’s essential to install it to use the Python programming language.

Next, execute the snippet below to get the Redis server running:

import redis_server
!$redis_server.REDIS_SERVER_PATH --daemonize yes

To ensure that Redis is connected and running, run the ping command on the server as shown below. If the output printed is ‘True’, it is successful.

image1_11zon.webp

Running Redis commands and operations

Once you have verified that Redis is up and running, you can get started on performing operations. Here’s a simple example to demonstrate the key value store functionality.

You can assign a value to the key using the ‘client.set()’ function:

client.set('Book', 'Harry Potter')

Here, ‘Book’ is the key and ‘Harry Potter’ is the value. If you know the key, you can extract its value through the ‘client.get()’ function.

image2_11zon.webp

You can also delete keys as shown below.

client.delete('Book')

If the deletion is successful, 1 will be printed as the output.

Advantages of Redis

  • Redis supports a diverse range of data structures like Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bitmaps, and Geospatial.
  • It provides enhanced flexibility when working with Python.
  • Unlike traditional databases, the code complexity is very low. This means that fewer lines of code are required for the same operation.
  • Redis can be used in applications like caching, messaging, fast data ingest, semantic search, and much more.
  • Unnecessary temporary data is written in relational or other primarily on-disk databases. The same data also needs to be scanned and deleted. With Redis, you can avoid all this and enhance performance.

Working with Redis data structures in Google Colab

As mentioned, Redis supports a variety of data structures apart from key value stores. Here are some of the common data structures and the commands associated with them.

Lists

Lists are a sequence of ordered elements. In Redis, the lists are implemented as Linked list. You can add new elements at the head or tail of the list. Use the ‘lpush’ command to prepend values to the list and the ‘lrange’ command to extract elements present in the list. You need to provide the name of the list, the starting and ending index as input parameters to this command. The code below demonstrates the same:

client.lpush('My books','Harry Potter')
client.lpush('My books','Angels and Demons')
client.lpush('My books','Kite runner')
client.lrange('My books',0,3)

Output: [b'Kite runner', b'Angels and Demons', b'Harry Potter']

Like a LinkedIn list, you can pop the first element from the Redis list using the ‘lpop()’ command. See the example below:

client.lpop('My books')

Output: b'Kite runner'

There are other commands similar to this such as rpop, rpush, etc.

Strings

String values can be stored in Redis and manipulated with commands. Using the set() command, you can store any string in a key. After this, you can use commands like append, strlen and many more. Here’s an example:

client.set('string','iphone')
client.getrange('string',1,3)

Output: 'pho'

In the code above, the ‘string’ is the key variable that stores the values ‘iphone’. The ‘getrange’ command is used to extract a substring by passing the starting and ending indexes. Similarly, you can append another value to this key as follows:

client.append('string','pro max')
client.get('string')

Output : ‘iphonepro max'

All other commands can be applied similarly.

Sets

In Redis, sets are a collection of unique items or strings. Unlike lists, there is no order here. The main feature is that the items cannot be repetitive. Redis provides you with simple commands to add elements to sets, delete elements, find the intersections of two sets, and more. Here’s an example:

client.sadd('cities','Bombay')
client.sadd('cities','Kharagpur')
client.sadd('cities','Bangalore')
client.sadd('cities','Bombay')
client.smembers('cities')

Output: {b'Bangalore', b'Bombay', b'Kharagpur'}

The ‘sadd’ command adds elements to the set and the ‘smembers’ command extracts all the elements of the set. Note that Bombay is stored only once in the set even though it’s added twice.

Hashes

The Redis hash maps the fields and values similar to the hash table. The hset command is used to set values to hash objects. Here’s the syntax and an example:

hset(key, value, number)

client.hset('set',10,679)

Think of this as a Python dictionary, where key[value] = number. The ‘hget’ command helps extract the value of a hash field stored at a key. Similarly, there are elaborate commands to set multiple hash values to multiple fields and so on.

For a list of all the commands for these data structures and information on Bitmaps, Sortedsets, etc., check out the official documentation.

Comparison against other databases

Redis is a non-SQL type of database as it does not have tables like relational databases. A popular alternative to Redis is Memcached, which is a key-value server like Redis. It has a very high performance as well.

However, Redis has more data storage options. In Memcached, the data storage option is just mapping of keys to values. On the other hand, Redis allows you to write data in the form of sets, lists, hashes, and more. These features give Redis an upper hand. Note that it’s common for users to store data in other relational databases and use Redis based on performance needed for tasks.

Redis has a wide range of applications in day-to-day life. For example, game developers prefer Redis for building real-time leaderboards. The most popular use case of Redis is caching as it’s computationally less expensive, thanks to the reduced data access latency. It’s also used in real-time geospatial data and rich media streaming.

Press

Press

What’s up with Turing? Get the latest news about us here.
Blog

Blog

Know more about remote work. Checkout our blog here.
Contact

Contact

Have any questions? We’d love to hear from you.

Hire remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.