What is Redis and What Does It Do
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.
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 might say Redis is a database itself—it 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. The only component that is 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 calling Redis cache.
Redis stores information in a key-value pair, too. Traditional databases typically come in one of two flavors: a relational database or a NoSQL database. An excellent example of a relational database is MySQL, which stores information 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 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 to data. In a good scenario, a database can easily take 300+ milliseconds to respond. That doesn't include 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. Large web applications can take seconds to respond with information during peak traffic times. That's not a good user experience. Likewise, that 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 there first. This can drastically reduce the time it takes 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. 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 to install and use Redis. The steps for installing Redis will be different depending on whether you are using a production or test system and the type of system you are using.
The easiest solution for installing and using Redis is by using a Docker container. Redis has a container that is pre-made and ready to go for Docker. All you need is a 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 Hub. The Redis Docker Hub page also has installation instructions for the Redis Docker Container and instructions on how to use that container.
If you use Mac OS or Linux, download and install Redis with your favorite package manager. For Mac OS, use Brew. For Linux, your package manager will change depending on your Linux distribution. For example, if you are using a Debian-based OS, use Apt. Likewise, RPM is used for a Fedora or Redhat distribution. 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 need to be enabled in the Windows Features lists under Programs and Features in the Control Panel in Windows.
After enabling the WSL feature, a Linux distribution must be installed from the Windows Store. You can use 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 by using 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 move data from Redis to its corresponding database while configuring high-availability instances. If you install and use your own Redis instance, you must 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 first check the Redis cache instance for requested data. 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 to the client requesting that data and then updates the Redis cache instance with that data. 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 and, once it detects a change, takes the appropriate actions to ensure the Redis cache instance and the database are in sync.
This is why 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 it quickly. Redis is a cache service typically used in conjunction with databases. Redis is generally 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. If it is going to be used within Windows, it must be installed in Windows Subsystems for Linux. Redis is also available through various cloud vendors.
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 a Redis worker that monitors a database for changes and syncs those changes between the database and the Redis cache instance.
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.