In the past few years, Redis has been widely used in a Node.js application stack. Although the most popular use case is caching, Redis has many other use cases where you can take advantage of the lightning-fast in-memory database.
In this tutorial, we are going to give you a brief introduction to Redis. We’ll also be using Redis to create a simple cache for a node application to see how this affects performance.
What is redis
Redis is an open source data structure store (BSD licensed) in storage used as a database, cache and message broker.
You can think of a No-SQL database in which data is stored as key-value pairs in system memory. Redis also supports the storage of hard disk-persistent data if required.
Redis supports saving Multiple data structures and data types, including strings, lists, hashes, sets, and sorted sets. Supported data structures offer Redis the versatility for many use cases.
Redis is best for situations where data needs to be retrieved and delivered to the client in no time.
Redis use cases
One of the most popular use cases for Redis is caching.
What is caching?
Caching is the process of storing copies of data in caches so that applications can access and retrieve data more quickly. The goal of caching is to make data access faster than a database or remote server could allow. This is especially true for expensive (temporary) operations.
As a back-end developer, it is our job to meet customer requirements as quickly as possible. Sometimes queries require multiple operations like getting data from a database, doing calculations, getting additional data from other services, and so on, which affect our performance.
This is where caching stands out because we can process the data once, store it in a cache, and later retrieve it directly from the cache without having to perform all these expensive operations. We would then update the cache periodically so that users can see updated information.
[Read: Why AI is the future of home security]
Caching & Redis
Because Redis is an in-memory database, its data access operations are faster than any other disk-mounted database. This makes Redis the perfect choice for caching. Key-value data storage is another plus as it makes storing and retrieving data a lot easier.
This tutorial shows you how to do caching with Redis and Node.js.
Redis for real-time analysis
Redis promises data processing operations of less than a millisecond. This makes Redis a perfect candidate for applications based 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 offers an AI-assisted faster transaction scoring system and faster statistical models to better handle this use case.
Other real-time analytics use cases include real-time inventory management systems and game leaderboards.
Redis for session management
If your application uses sessions to track authenticated users and manage user-specific data, Redis is perfect as a Canister. Using Redis could greatly improve the system’s performance while also making it easier to process user data, including login information, recent activity, and even a cart-like system.
Redis as a queue
With Redis, you can queue application tasks that are taking a long time to complete. You can implement first-in, first-out (FIDO) queues or create deferred queues to delay the implementation of tasks until a predetermined time.
Caching with Node and Redis
Now let’s start with the main focus of this tutorial: Using Redis for Caching in a NodeJS Application.
Caching with Redis is pretty easy. When we receive a user request for a route that has caching enabled, we first check whether the requested data is already in the cache. If so, we can quickly pull data from Redis and send the response.
However, if the data is not cached, what we call cache miss, then we must first get the data from the database or external API and send it to the client. We also make sure that the data we retrieve is cached so that the next time we receive the same request, we can simply send the cached data to the user faster.
After you have a clear idea of what we’re going to do, let’s start implementing.
If you haven’t already, you’ll need to install Redis for this tutorial.
You can download the binaries and easily compile them using the commands below.
Ping the server to make sure the Redis server is running without problems redis-cli.
When you receive In response, the Redis server is running successfully.
read this official quick guide to get a better idea when something goes wrong.
Build the NodeJS application
Set up the initial boilerplate for the Node application as follows.
Notice how we use two additional packages named Axios and to repeat. to repeat is the standard Redis client for Node. We use Axios to get data from an external API for this tutorial.
Before going any further, make sure you install these two packages using npm.
Get data from the external API
We’ll use that GitHub Jobs API to get data on programming jobs available in different locations around the world.
You can pass a search term for the job you are looking for to the API and get a number of available jobs in JSON format. An example request to the API looks like this.
POSTMAN – GitHub Jobs API Results
In our node application we define a route with the name / jobs, which gets job data from the above API and sends it back to the client.
Here we use Axios to send a GET request to the GitHub Jobs API with the search term specified by the user.
Let’s see how the route works now with Postman.
POSTMAN – Our API results
Cache the results
Now let’s see how we can use caching to improve application performance.
First, we need to connect to the Redis server through our application. For this task we will use the installed Redis package.
The Redis server listens on port 6379 by default, so we’re passing the port number to connect to Redis and create a client.
Then implement the logic to store and retrieve data from the cache.
What’s going on here?
When we receive a client request to route / jobs, the search term with the query parameters of the request is sent first.
Then we try to get the requested data from the cache, bypassing the search term that we use as the key when storing data in the cache. Since the Redis package doesn’t have native support for Promises, we need to pass a callback to process the data that is being retrieved.
If the value returned by Redis is non-zero, it means the related data is in the cache, so it’s easy to return that data in the response. Just make sure to reset the string to JSON.
If the returned value was zero, we need to send a request to the external API to get relevant data.
When we get the data from the API, we store it in Redis before sending it back so that the next time we send the same request to the node server, it can respond with cached data rather than requesting it from the API.
Notice how we use that Setex Function for storing data in the cache. Use of Setex Function specific instead of the regular to adjust Function we can set an expiration time for the stored key-value pair. Since we are setting a value for the expiration time, Redis will automatically remove this key-value pair from the cache when the elapsed time has expired.
Complete source code
That’s it. We created a simple cache for our application. It wasn’t that hard, was it?
Moment of truth: time comparison
We will see how using a cache affects the performance of our app. I used Postman to send requests to the server and measure request completion time.
The first time you send a request to the server with a new search term, the application will take longer to respond (over 7 seconds) as the data must be fetched from the external API. The second time you make the same request, the server will respond more quickly because the results are already in the cache.
The request is completed within 10 ms. This is a huge increase in performance over the application we saw before without a cache.
In this tutorial, we gave you a brief introduction to Redis and used it to create a simple cache for a Node.js application. Now you can use Redis to cache frequently queried data in your application for a significant increase in performance.
You can also explore how you can leverage the best of Redis’ features in other use cases as well.
This items was originally published on Live code stream by Juan Cruz Martinez (Twitter: @bajcmartinez), Founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker and maker of things.
Live code stream is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI and computer science in general.
Read on: About Time! Toyota is finally announcing a fully electric vehicle