In this article, we will demonstrate in a simple example how to accelerate web applications using Redis cache. We will start with a small introduction to Redis. Then, we will build a simple web application backend in Node.js to retrieve the number of Github repositories for a certain Github username. We will measure the API’s request-response time. After that, we will implement a Redis cache middleware to store the retrieved data and serve it to the user, and compare the request-response time to the previous implementation without Redis.
Web applications are becoming more and more complex. They now contain a lot of data that need to be retrieved from different sources online. These web apps, becoming more and more client-side, which means executed in the client’s browser, need to send a greater number of requests to retrieve, post and update the resources they interact with online. These frequent calls to APIs can make these applications very slow and buggy, thus decreasing user satisfaction. This problem can be addressed by Redis.
To better understand the situation, imagine that you are trading financial instruments online using a web application provided by your online broker or bank. Let’s say that every 10 seconds, the web application should update the price of the financial instruments you see in front of you. On the client side, you only need the new price after 10 seconds since you already have the previous prices. So instead of sending requests to retrieve all data again every 10 seconds, which no application does, by the way, the old prices can be stored in the cache using Redis, and only new information will be retrieved. This can significantly improve your application speed as we will see in this article.
Note: Trading web applications usually use other systems to notify user of new prices, but the above example constitutes a probable use-case for redis.
What is Redis?
Redis, which stands for Remote Dictionary Server, is a fast, open-source, in-memory key-value data store for use as a database, cache, message broker, and queue. The project started when Salvatore Sanfilippo, the original developer of Redis, was trying to improve the scalability of his Italian startup. Redis now delivers sub-millisecond response times enabling millions of requests per second for real-time applications in Gaming, Ad-Tech, Financial Services, Healthcare, and IoT. Redis is a popular choice for caching, session management, gaming, leaderboards, real-time analytics, geospatial, ride-hailing, chat/messaging, media streaming, and pub/sub apps.
Redis is technically a database that resides in a computer’s memory in contrast to databases that store data on disk or SSDs. By eliminating the need to access disks, in-memory data stores such as Redis avoid seek time delays and can access data in microseconds.
Redis has a lot of benefits, we will mention the most important ones:
- In-memory data storage, which is way faster to access than traditional databases usually stored on disk or SSD
- Flexible data structures: Redis data types include strings, lists, sets, hashes, bitmaps and more
- Simplicity and ease-of-use by writing few lines of code
Simple Github Repository Number Retriever
In this first step of the tutorial, we will build a simple Node.js API that we will call with a certain Github username. The API will return the number of Github repositories for the specific username provided in the request.
Here is the following code:
The express server will be listening on PORT 5000. The root we will be calling is
/repos/yourgithubusername . This root will trigger the function
getRepos wich will itself call the Github API
/api.github.cim/users/yourgithubusername to get your public details. The number of public repositories in your Github account can be found in the field
public_repos of the
data object. The response sent to the client is built by the
setResponse function that will put the data in proper HTML format. Test your API by requesting the number of public repos in your own Github account for example.
Note: you might need to install modules
Let’s look at the request-response time of our API. Open the developer tools in your browser and go to the network section. It is showing me a request-response time of 186 milliseconds. If you refresh your page many time, the request-response time will stay the same. Not so bad, but let’s make it way faster !
Adding Redis to the previous web application
Let us now add Redis to our web application. In order to be able to do so, you will need to install Redis locally on your machine. On MacOs, you can do this easily using Homebrew:
brew install redis
To start the Redis server as a background service, you can use the command:
brew services start redis
And to stop the service, you can use:
brew services stop redis
Another thing you might want to install is the
redis module for node. Do so by running
npm install redis is your project home directory.
We have modified the code in our previous paragraph to include redis-related functionality.
Between lines 1–8 we have imported and initialized our redis client. On line 31, we are saving the number of repos of a certain username in the redis cache. On lines 41–53, we are writing the redis cache middleware function. This function is given as a second argument to the
get function on line 55, to tell express that we want this
cache function to be called prior to the function
getRepos . What this function will do is check if the redis client called
client in our code has already stored anything related to the
username . If so, then the cache function will directly send a response to the client containing the number of repositories stored in cache, without calling the Github API. If nothing is stored,
next() will be called which means that the request will be forwarded to the
getRepos function that will call the Github API and return the result to the browser.
Let’s see this code in action.
The first call to the API will have the same request-response time since the information is nowhere to be found in the redis cache. But when you refresh, you will see a response time of 5 milliseconds! We have made our application 33.6x faster on known data ! You can also check in your console that the function
getRepos was not called except the first time since there is only one log line saying
This is a small example where we have demonstrated the power of Redis. Big organisations are now adopting cache management using Redis which strengthens my conviction that Redis will soon become a new normal in the professional space.
For more content related to Web application development in Node.js, check out this article: