Crash Course Redis - What is Redis?

kalkwst

Kostas Kalafatis

Posted on January 30, 2023

Crash Course Redis - What is Redis?

Redis is an open source in-memory data store that works well as a cache or message broker, but it may also be used as a database when all of the characteristics of a typical database are not required. It has high performance, having the capacity to read and write data to memory quickly. Also, Redis lets you do atomic operations, which makes it great for caching cases that need quick access.

What is an in-memory database?

An "in-memory database" is one that is entirely stored in main memory (RAM) rather than on disk. The main advantage of this design is its ability to provide rapid data access due to the high speed of main memory, which is significantly faster than disk storage.

In-memory databases are commonly used in applications requiring rapid access to large amounts of data, such as real-time analytics, online gaming, e-commerce, and social media. They are also used in scenarios that require high performance and scalability because they can handle large amounts of data and transactions without slowing down.

One of the main disadvantages of in-memory databases is that they are more vulnerable to data loss in the case of a crash or shutdown because the data is solely stored in memory and is not persisted to disk. To solve this problem, many in-memory databases, like Redis, have features like persistence and replication, which allow data to be stored on disk and copied across different servers to make sure the data is always available and correct.

Redis is an in-memory database that is typically used as a cache in front of a more substantial database, such as MySQL or PostgreSQL, to boost application performance. By using the speed of the memory and taking some of the pressure off the main application database, which is caused by:

  • data with low turnover rates yet high request volumes
  • data that isn't essential to the operation of the company and is constantly changing.

These types of data include session or data caches, leaderboards, and roll-up analytics for dashboards.

Image description

Benefits of Redis

Redis provides several advantages, some of which are discussed below:

Performance

Because all Redis data is stored in memory, it is possible to achieve both low latency and high throughput when accessing the data. In-memory data stores, in contrast to conventional databases, do not need to make a trip to the disk in order to retrieve data. As a result, engine latency can be reduced to microseconds. In-memory data stores are able to support an order of magnitude more operations and boast significantly faster response times as a direct result of this. The end result is performance that is lightning fast, with the average time required to read or write data being less than a millisecond and support for millions of operations per second.

Simplicity and ease-of-use

Redis enables you to write typically complex code with a reduced number of lines and a more straightforward structure. When you store, access, and make use of data in your applications using Redis, you will need to write fewer lines of code. In contrast to the complex query languages used by traditional databases, developers working with Redis have access to a straightforward command structure. This sets Redis apart from traditional databases. For instance, transferring data from Redis to a data store requires only one line of code if you use the hash data structure provided by Redis. Converting from one format to another would require a significant number of lines of code if it were performed on a data store that did not use hash data structures. Redis comes pre-packaged with native data structures and a wide variety of options for interacting with and manipulating your data. There are more than one hundred open-source clients that can be used by Redis developers. Java, Python, PHP, C, C++, C#, JavaScript, Node.js, Ruby, R, Go, and a large number of other languages are among those that are supported.

Replication and persistence

Redis uses an architecture known as primary-replica and supports asynchronous replication, which allows data to be copied to multiple replica servers simultaneously. This not only improves read performance by allowing requests to be distributed across multiple servers, but it also allows for a more expedient recovery in the event that the primary server experiences an outage. Redis supports backups at a specific point in time to ensure data persistence (copying the Redis data set to disk).

High availability and scalability

Redis provides a primary-replica architecture that can either have a single node as the primary or can have a clustered topology. Because of this, you will have the ability to construct highly available solutions that deliver reliable and consistent performance. If you need to make a change to the size of your cluster, you have several options available, including scaling up, scaling in or scaling out. This enables your cluster to grow in accordance with the demands you place on it.

Use Cases

Due to its speed, scalability, flexibility, persistence, and support for transactions, Redis has become a popular choice for a wide range of use cases.

Caching

Redis is an excellent option for implementing a highly available in-memory cache to decrease data access latency, increase throughput, and ease the load off of your relational or NoSQL database and application. These goals can be accomplished by reducing data access latency, increasing throughput, and using Redis. Redis is able to provide frequently requested items with response times of less than one millisecond, and it enables you to easily scale for higher loads while preventing the growth of more expensive backend infrastructure. Caching is a common way to use Redis. Some examples are caching database query results, caching web pages, and caching frequently used objects like images, files, and metadata.

Chat, Messaging and Queues

Redis is able to support the Pub/Sub protocol with pattern matching and a wide variety of data structures, including sorted sets, hashes, and lists. Redis can now support high-performance chat rooms, real-time comment streams, social media feeds, and server-to-server communication as a result of this. A straightforward implementation of a lightweight queue is made possible by the Redis List data structure. As a result of the fact that lists are capable of both atomic operations and blocking, they are well-suited for a wide variety of applications, including those that call for a dependable message broker or a circular list.

Gaming leaderboards

Game developers who want to construct real-time leaderboards frequently go with Redis as their database of choice. Use the Redis Sorted Set data structure instead. This data structure ensures that each element is truly unique while also keeping the list sorted according to the users' ratings. Making a real-time ranked list is as simple as keeping track of each user's score and updating it whenever it shifts. Managing data from time series can also be accomplished with the help of Sorted Sets by making the timestamps the score.

Session storage

Application developers frequently go with Redis as an in-memory data store because of its high availability and persistence. This makes it a popular option for the storage and management of session data for internet-scale applications. Redis is able to manage session data such as user profiles, credentials, session state, and user-specific personalization with the sub-millisecond latency, scale, and resiliency that is required.

Rich media streaming

Redis is a data store that can be used to power live streaming use cases. It is both quick and in memory. Redis can be used to store authentication information and tokens for millions of users, metadata about users' profiles and viewing histories, and manifest files to enable content delivery networks (CDNs) to stream videos to millions of mobile and desktop users at the same time.

Geospatial

Redis provides purpose-built data structures and operators that are stored in memory for the purpose of managing real-time geospatial data at scale and with speed. Redis makes geospatial analysis simple and quick to perform by providing commands such as GEOADD, GEODIST, GEORADIUS, and GEORADIUSBYMEMBER. These commands store, process, and analyze geospatial data in real time. Your applications can benefit from the addition of location-based features like drive time and distance, as well as points of interest, if you use Redis.

Machine Learning

Machine learning is essential for the development of modern data-driven applications because it enables the rapid processing of a vast volume, variety, and velocity of data while also automating decision making. The capacity to process live data and arrive at decisions in a matter of tens of milliseconds is of the utmost significance for use cases such as fraud detection in gaming and financial services, real-time bidding in ad-tech, and matchmaking in dating and ride sharing, among other applications. Redis provides a quick data store that runs in memory, allowing you to rapidly construct, train, and deploy machine learning models.

Installing Redis

Install on Ubuntu/Debian

If you are running a very minimal distribution, such as a Docker container, you probably need to install the lsb-release tool first:

sudo apt install lsb-release

Add the repository to the apt index:

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
Enter fullscreen mode Exit fullscreen mode

Update it:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

And finally install Redis:

sudo apt-get install redis
Enter fullscreen mode Exit fullscreen mode

Install on macOS (using Homebrew)

Make sure that you have Homebrew installed. From the terminal, run:

brew --version

If the command succeeds, you have Homebrew installed on your machine.

From the terminal, run:

brew install redis
Enter fullscreen mode Exit fullscreen mode

This will install Redis on your system.

To test your Redis installation, you can run the redis-server executable from the command line:

redis-server
Enter fullscreen mode Exit fullscreen mode

If successful, you'll see the startup logs for Redis, and Redis will be running in the foreground.

To stop Redis, press Ctrl + C.

Install on Windows (using WSL2)

Redis is not officially supported on Windows. However you can install Redis on Windows by using WSL2. This guide assumes Ubuntu.

Add the repository to the apt index:

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
Enter fullscreen mode Exit fullscreen mode

Update it:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

And finally install Redis:

sudo apt-get install redis
Enter fullscreen mode Exit fullscreen mode

You can test that your Redis server is running by connecting with the Redis CLI:

redis-cli
127.0.0.1:6432> ping
PONG
Enter fullscreen mode Exit fullscreen mode

Conclusion

Redis is a powerful and adaptable data store that can be used for a variety of tasks such as web application caching, real-time analytics, session management, and leaderboards. Its in-memory storage, support for complex data structures, and fast performance make it an excellent choice for real-time data processing and analysis applications.

The following post in this series will delve deeper into the heart of Redis and examine the various data types that it supports. We will look at how each data type is used, its strengths, and how you can use them to create powerful and scalable applications. So, keep an eye out for the next post to learn everything there is to know about Redis data types!

💖 💪 🙅 🚩
kalkwst
Kostas Kalafatis

Posted on January 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related