Introduction to the in-memory datastore Redis

Hemanthhari2000
featurepreneur
Published in
6 min readApr 1, 2021

Understand the concept of distributed, in-memory key-value database, cache, and message broker

Introduction

Modern world web applications require a lot of things up and running on the server to serve the purpose of users. Many developers don't know about the factors which can ruin a user’s experience. It depends on how well the code is optimized, how the server responds, how well the database is integrated and so. This may depend on the framework or the language developers use. But, Developers can optimize their application which can significantly increase the overall performance. One such thing they can optimize to increase the performance is by using efficient database connection and utilizing it for quick responses. Well, here is where Redis comes into play.

Overview

Let’s take a look at the contents that we will cover in this article.

  • What is Redis?
  • Where is Redis used?
  • Merits of using Redis
  • Installing Redis
  • Implementation of Redis
  • Conclusion

What is Redis?

Redis (or) Remote Dictionary Server is an open-source, in-memory data structure store, used as a distributed, in-memory key-value database, cache, and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indexes.

Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster. Redis supports cross platforms which means that you can simply run Redis on Windows, Mac, or any Linux-based systems. Redis recommends using Linux-Based Systems on their Official Documentation page.

Where is Redis used?

Redis can be used in a lot of applications that we use in our everyday life. Redis helps to store and retrieve data in memory very easily and efficiently. Some of the use cases of Redis are:

  • Caching
  • Messaging and Queue
  • Session Store
  • Machine Learning
  • Real-Time Analytics

Caching: Redis is perfect for implementing a high in-memory cache to decrease data access latency and increase throughput. They can respond in milliseconds which makes it pretty fast and it also enables you to easily scale for higher loads without spending a huge amount of money for just the backend. Database query results caching, persistent session caching, web page caching, and caching of frequently used objects such as images, files, and metadata are all popular examples of caching with Redis.

Messaging and Queue: Redis also supports Pub/Sub (Publish and Subscribe) with pattern matching and a variety of data structures. This enables Redis to support high-performance messaging, chat rooms, and server intercommunication. We have used a lightweight queue using the Redis List data structure. Lists are ideal for a variety of applications that needs a reliable message broker as it offers atomic operations as well as blocking capabilities.

Session Store: For internet-scale applications, Redis is the best choice as it supports in-memory. It provides sub-millisecond latency, scale, and resiliency required to manage session data like session state, credentials, and more.

Machine Learning: As we know, Machine Learning models can be a slow and time-consuming process to load some data as inputs and process it to produce outputs. This process can be handled by Redis and the application performance can be increased significantly. With this, many machine learning models can be deployed in production and have a smooth functionality with users.

Real-Time Analytics: As Redis handles data with sub-millisecond latency, it is the perfect choice for real-time analytics like social media analytics, ad targeting, personalization, Artificial Intelligence-based applications, Cyber Security, and more.

Merits of using Redis

Redis is great for delivering results faster and managing data that is to store and retrieve it. Some merits of Redis are:

  • Faster Database responses: They store data in RAM, rather than on an HDD (Hard Disk Drive) or SSD (Solid State Drive).
  • Has Control over Lifetime of a Data: Redis supports the Time to Live (TTL) feature where if the keys are set in TTL mode with the duration time to live, data will expire after this time elapses.
  • Key-Based Access: Redis is based on a key-value model. Key-Based access allows for extremely efficient access times. We can access the data using Redis GET and SET semantics. They are very fast though, they can perform around 110, 000 SETs per second and around 81, 000 GETs per second.
  • Language Support: Redis is supported by most of the programming languages like Python, C, C++, Java, JavaScript, and more. You can access the full language support here.

Okay, enough talking though, let’s try some Redis for ourselves, shall we….

Installing Redis

First of all, open your terminal and start typing the following:

Note: I am using Ubuntu here for windows or other operating systems you can install it by downloading from the official Redis website.

Update your Packages

sudo apt-get update
sudo apt-get upgrade

Install Redis Server

sudo apt-get install redis-server

This would have installed Redis on your Ubuntu System. Now follow one more command and you good to go.

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.default

That’s it. Redis is perfectly installed and to make sure it works use the following command.

redis-cli

this should have opened a Command Line Interface with your localhost and port number in it.

127.0.0.1:6379> 

if your output looks something like this then congratulations you have to install Redis on your Ubuntu.

Implementation of Redis

Let’s get started with a simple implementation of Redis. So, go ahead and start your terminal and start the Redis server using the redis-cli command. Here, we used very simple SET and GET operations but you can definitely explore more such commands from here.

  • SET in Redis: It sets a value to a key as shown below (used to store data).
> SET car "Audi"
  • GET in Redis: It gets the value using the key as shown below (used to retrieve data).
> GET car
  • SET using Key Spaces
> SET server:name google
> SET server:port 8080
  • GET using Key Spaces
> GET server:name
> GET server:port
  • Use FLUSHALL to Remove all the data
> FLUSHALL 

after executing all the commands our terminal should look like this.

Conclusion

In this article, we have seen Redis and its functionalities. We went through the concept of in-memory structure store, message broker, cache, and its effective use in the real-world scenario. We even installed Redis on Ubuntu and learned some Redis commands that help us store and retrieve data efficiently. I hope this article was useful to you all. Will see you in my next article until then, as always code learn repeat …….

Follow for more…

--

--