Technology

#A simple guide to Redis and caching with NodeJS

#A simple guide to Redis and caching with NodeJS

In recent years, Redis has become a common occurrence in a Node.js application stack. Though its most popular use case is caching, Redis has many other use cases where you can take advantage of its blazing-fast in-memory database.

In this tutorial, we are going to give you a quick introduction to Redis. We’ll also use Redis to create a simple cache for a Node application to see how it impacts its performance.

What is Redis?

Redis is an open-source (BSD licensed), in-memory data structure store used as a database, cache, and message broker.

You can think of it as a No-SQL database, which stores data as a key-value pair in the system memory. Redis supports disk-persistent data storage, too, if needed.

Redis has support for storing multiple data structures and data types, including strings, lists, hashes, sets, and sorted sets. Supported data structures give Redis the versatility for many use cases.

Redis is best in situations that require data to be retrieved and delivered to the client in the least amount of time.

Redis use cases

One of the most popular use cases of Redis is caching.

What is caching?

Caching is the process of storing copies of data in caches to allow applications to access and retrieve data faster. The goal of caching is speeding up data access operations better than a database, or remote server could allow. It is especially the case for expensive (in time) operations.

As a back-end developer, our task is to complete the clients’ requests as fast as possible. Sometimes, queries require several operations like retrieving data from a database, performing calculations, retrieving additional data from other services, etc., that drag our performance down.

It is here where caching excels as we can process the data once, store it on a cache and then retrieve it later directly from the cache without doing all those expensive operations. We would then periodically update the cache so that users can see updated information.

[Read: Why AI is the future of home security]

Caching & Redis

Since Redis is an in-memory database, its data access operations are faster than any other disk-bound database could deliver. It makes Redis the perfect choice for caching. Its key-value data storage is another plus because it makes data storage and retrieval much simpler.

In this tutorial, we are going to see how to do caching with Redis and Node.js.

Redis for real-time analytics

Redis promises sub-millisecond long data processing operations. It makes Redis a perfect candidate for applications that rely on real-time data analysis.

For example, you can use Redis to store user identities and their transaction details when implementing a real-time fraud detection service. Redis even provides an AI-supported faster transaction scoring system and faster statistical models to perform this use case better.

Other use cases in real-time analytics include real-time inventory management systems and gaming leaderboards.

Redis for session management

If your application uses sessions to track authenticated users and manage user-specific data, Redis is a perfect fit to use as session storage. Using Redis could significantly improve the system’s performance while making it easier to process users’ data, including credentials, recent activities, and even a shopping cart like system.

Redis as a Queue

You can use Redis to queue application tasks that take a long time to complete. You can implement FIDO (first-in, first-out) queues or create delayed queues to delay task implementation until a pre-scheduled time.

Caching with Node and Redis

Now, let’s start with the primary focus of this tutorial: using Redis for caching in a NodeJS application.

The process of caching with Redis is quite simple. When we receive a user request to a route that has caching enabled, we first check if the requested data is already stored in the cache. If yes, we can quickly retrieve data from Redis and send the response.

However, if the data is not stored in the cache, which we call a cache miss, we have to first retrieve the data from the database or the external API and send it to the client. We also make sure to store the retrieved data in the cache so that the next time the same request is received, we can simply send the cached data to the user faster.

Now that you have a clear idea of what we are going to do let’s start the implementation.

Install Redis

If you haven’t already, you need to install Redis for this tutorial.

You can download the binaries and compile them easily using the following commands.

To make sure that the Redis server runs without an issue, send a ping to the server using the redis-cli.

If you receive pong as a response, the Redis server is running successfully.

Read the official quick start guide to get a better idea if something goes wrong.

Build the NodeJS application

Basic set-up

Set up the initial boilerplate for the Node application like this.

Note how we use two additional packages named axios and redis. redis is the standard Redis client for Node. We use axios to retrieve data from an external API for this tutorial.

Before continuing, make sure to install those two packages using npm.

Retrieve data from the external API

We will be using the GitHub Jobs API to get data related to programming jobs available in different locations in the world.

You can pass a search term related to the job you are looking for to the API and retrieve an array of available jobs in json format. A sample request to the API looks like this.