Technology / DevOps

What is Redis and What Does It Do

What is Redis and What Does It Do picture: A
Follow us
Published on October 7, 2021

Have you ever wondered how popular web applications can achieve mind-blowing performance? Think about it. Some websites get more than a million views a day with thousands of active users at any given time. Even with multiple load-balanced servers, that's enough traffic to bring any web architecture to its knees. There's always a weak point in the chain, and that weak point can be database response times.

Using a service like Redis to cache database queries can fix those response time issues, though. Let's take a closer look at what Redis is and why you should use it in your web application.

What is Redis?

At its core, Redis is a caching service for databases. Some people might say Redis is a database itself. It kind of is, too. But what makes Redis different from an actual database?

Redis lives in system memory. System memory access is designed to be blazingly fast. In fact, the only component faster than RAM in a computer is the cache built into the CPU.

Juxtaposition to Redis, databases live on system storage, otherwise known as a hard drive. These drives can be mechanical drives or solid-state drives, and they can come in a variety of configurations. System storage is traditionally much, much slower than system memory, though. With specialized hardware under certain configurations, system storage can come close to matching the same throughput speed as RAM, but that hardware can cost tens of thousands of dollars. RAM, on the other hand, only costs a few hundred dollars. Nonetheless, this is why calling a database for information is so much slower than making a call to Redis cache.

Redis stores information in a key: value pair, too. Traditional databases typically come in one of two flavors: a relational database or NoSQL database. A good example of a relational database is something like MySQL where information is stored in rows and columns of organized data. MongoDB is a popular example of a NoSQL database where information is stored as documents that may not necessarily correspond to other information stored in that database. Redis is closer to a MongoDB database built using Mongoose, but Redis doesn't have a concept of object IDs. It simply uses keys with corresponding values.

We mentioned that Redis lives in system memory. That makes the information stored in a Redis cache instance volatile. If anything happens to that system memory, or if the computer system running Redis is powered off, that information will be lost. On the other hand, because databases live on system storage, it is far less likely information will be lost in the event of a system power failure. Because of this, important information stored in a Redis cache needs to be properly stored in a database, or another storage mechanism, for non-volatility.

An Overview of Redis [VIDEO]

In this video, Trevor Sullivan covers the Redis cache service: what it is, what it does, and how it helps. It’s an open source tool that runs as a service in the background that allows you to store data in memory for high-performance data retrieval and storage. That’s the technical explanation, but watch on to hear about how useful it is for all manner of use cases.

What is Redis Used for?

At its core, Redis is a caching service for databases. Databases can take a long time to respond with data. In a good scenario, a database can easily take 300+ milliseconds to respond. That isn't including the roundtrip flight time it takes for a remote system to make that call, travel to a web service, have that web service process that data, and then respond back with information from the database. On the other hand, Redis can respond with information in as little as 20 milliseconds.

The difference between 20 and 300 milliseconds might not seem like a lot of time, but that example is only a best-case scenario. During peak traffic times, large web applications can take seconds to respond with information. That's not a good user experience. Likewise, that kind of traffic can impose an undue burden on a database.

Instead of calling a database directly, a web application can check a Redis cache instance to see if the information it needs is available in that cache instance first. This can drastically reduce the time to respond to a client from that web application or reduce the workload for a database.

In many cases, databases are not typically located on the same system as the web application itself, too. This means that making a call to a database is imposing more load on a network. Utilizing a Redis cache instance can reduce network overhead as well.

So, at its core, Redis is used to make applications perform faster by caching data in a volatile storage mechanism (System Memory) that is much faster than traditional storage mechanisms (hard drives).

How to Use Redis with Docker, Mac, Windows

There are multiple ways that you can install and use Redis. Depending on whether you are using a production or test system, and the type of system you are using, the steps for installing Redis will be different.

The easiest solution for installing and using Redis is by using a Docker container. Redis has a container pre-made and ready-to-go for docker. All you need is Docker installed on your computer. Calling the Redis container will handle everything else. You can find more information about the Redis Docker container in the Docker Hub. The Redis Docker Hub page also has installation instructions for the Redis Docker Container and how to use that container.

If you are using Mac OS or Linux, download and install Redis with your favorite package manager. For Mac OS, simply use Brew. For Linux, your package manager will change depending on which distribution of Linux you are using. For example, if you are using a Debian-based OS, use Apt. Likewise, if you are using a Fedora or Redhat distribution, use RPM. The Redis package name in those package managers is simply called 'Redis'. The Redis Website also has a Tar container that can be used to install Redis from scratch in Mac OS, Linux, and Unix as well.

Installing Redis on Windows is more complicated. There isn't a native Redis client for Windows. So, Redis needs to be installed with the Windows Subsystem for Linux features. Installing WSL takes a bit of time, but it is not difficult. First, Windows Subsystems for Linux needs to be enabled in the Windows Features lists under Programs and Features in the Control Panel in Windows.

After the WSL feature is enabled, a Linux distribution needs to be installed from the Windows Store. You have the option of using various distributions like Fedora, Ubuntu, Suse, and more from the Windows app store. Once your preferred version of Linux is installed from the Windows app store, launch it from the Windows Start Menu. Then use the native Linux package manager for the version of Linux that you installed for WSL to install Redis as you normally would for Linux.

Redis is also available as a cloud service, too. The easiest way to use Redis is from Redis itself. Redis offers a free Redis instance with Redis Cloud Essentials from Redis Labs. Major cloud vendors like AWS, Azure, and Google offer Redis services, too.

Using Redis from a cloud provider is the best path to take for a production service. Cloud providers will automatically handle moving data from Redis to its corresponding database while configuring high-availability instances. If you install and use your own Redis instance, you will need to handle these tasks yourself.

How Do You Implement Redis?

Because a Redis cache instance is volatile, information needs to be moved to and from the Redis cache to its corresponding database. This can be done in one of two ways.

First, a function can be built that monitors web applications for database calls. That function will check the Redis cache instance for requested data first. If that data is not in the Redis cache, that function calls the database for that information first. Once that function receives that information, it responds back to the client that requested that data with it and then updates the Redis cache instance with that data as well. Likewise, that function may write data to the Redis cache instance as clients request data to be written to the database.

Another option is to use a Redis worker. A Redis worker will automatically move data back and forth from the database. It monitors the database for changes. Once it detects a change, that Redis worker takes the appropriate actions to make sure the Redis cache instance and the database are in sync.

This is the reason that using a cloud service provider for Redis cache is the preferable method for production systems. Cloud service providers will automatically create that Redis worker and manage it for you.

Final Thoughts

We covered a lot of information in this article, so let's recap all of it quickly. Redis is a cache service typically used in conjunction with databases. Redis is typically used for web applications.

Data in Redis is volatile. It lives in system memory, so data in Redis must be synced with a longer-term storage mechanism if that data should be saved.

Redis can easily be installed in Mac OS and Linux using your favorite package manager. Redis must be installed in Windows Subsystems for Linux if it is going to be used within Windows. Redis is also available through various cloud vendors as well.

Finally, you can implement Redis using either a function that checks Redis for data first, and if it does not exist, fetches it from a database, or you can use a Redis worker that monitors a database for changes and syncs those changes between the database and the Redis cache instance.


Download

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.


Don't miss out!Get great content
delivered to your inbox.

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.

Recommended Articles

Get CBT Nuggets IT training news and resources

I have read and understood the privacy policy and am able to consent to it.

© 2024 CBT Nuggets. All rights reserved.Terms | Privacy Policy | Accessibility | Sitemap | 2850 Crescent Avenue, Eugene, OR 97408 | 541-284-5522