{"id":128700,"date":"2020-12-08T17:00:24","date_gmt":"2020-12-08T14:00:24","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/a-simple-guide-to-redis-and-caching-with-nodejs\/"},"modified":"2020-12-08T17:00:24","modified_gmt":"2020-12-08T14:00:24","slug":"a-simple-guide-to-redis-and-caching-with-nodejs","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/","title":{"rendered":"#A simple guide to Redis and caching with NodeJS"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2dae51f0ebd\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2dae51f0ebd\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#What_is_Redis\" >What is Redis?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Redis_use_cases\" >Redis use cases<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Caching_with_Node_and_Redis\" >Caching with Node and Redis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Install_Redis\" >Install Redis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Build_the_NodeJS_application\" >Build the NodeJS application<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Caching_the_results\" >Caching the results<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Full_source_code\" >Full source code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Moment_of_the_truth_time_comparison\" >Moment of the truth: time comparison<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/a-simple-guide-to-redis-and-caching-with-nodejs\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#A simple guide to Redis and caching with NodeJS<\/strong>&#8221;<\/p>\n<div>\n                            In recent years, Redis has become a common occurrence in a Node.js <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lication 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.<\/p>\n<p>In this tutorial, we are going to give you a quick introduction to Redis. We\u2019ll also use Redis to create a simple cache for a Node application to see how it impacts its performance.<\/p>\n<h2 id=\"what-is-redis\"><span class=\"ez-toc-section\" id=\"What_is_Redis\"><\/span>What is Redis?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Redis is an open-source (BSD licensed), in-memory data structure store used as a database, cache, and message broker.<\/p>\n<p>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.<\/p>\n<p>Redis has support for storing<span>\u00a0<\/span><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/redis.io\/topics\/data-types\">multiple data structures and data types<\/a>, including strings, lists, hashes, sets, and sorted sets. Supported data structures give Redis the versatility for many use cases.<\/p>\n<p>Redis is best in situations that require data to be retrieved and delivered to the client in the least amount of time.<\/p>\n<h2 id=\"redis-use-cases\"><span class=\"ez-toc-section\" id=\"Redis_use_cases\"><\/span>Redis use cases<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>One of the most popular use cases of Redis is caching.<\/p>\n<p><strong>What is caching?<\/strong><\/p>\n<p>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.<\/p>\n<p>As a back-end developer, our task is to complete the clients\u2019 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.<\/p>\n<p>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.<\/p>\n<p><em>[Read:\u00a0<span dir=\"auto\">Why AI is the future of home security<\/span>]<\/em><\/p>\n<p><strong>Caching &amp; Redis<\/strong><\/p>\n<p>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.<\/p>\n<p>In this tutorial, we are going to see how to do caching with Redis and Node.js.<\/p>\n<p id=\"redis-for-real-time-analytics\"><strong>Redis for real-time analytics<\/strong><\/p>\n<p>Redis promises sub-millisecond long data processing operations. It makes Redis a perfect candidate for applications that rely on real-time data analysis.<\/p>\n<p>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.<\/p>\n<p>Other use cases in real-time analytics include real-time inventory management systems and gaming leaderboards.<\/p>\n<p id=\"redis-for-session-management\"><strong>Redis for session management<\/strong><\/p>\n<p>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\u2019s performance while making it easier to process users\u2019 data, including credentials, recent activities, and even a shopping cart like system.<\/p>\n<p id=\"redis-as-a-queue\"><strong>Redis as a Queue<\/strong><\/p>\n<p>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.<\/p>\n<h2 id=\"caching-with-node-and-redis\"><span class=\"ez-toc-section\" id=\"Caching_with_Node_and_Redis\"><\/span>Caching with Node and Redis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now, let\u2019s start with the primary focus of this tutorial: using Redis for caching in a NodeJS application.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Now that you have a clear idea of what we are going to do let\u2019s start the implementation.<\/p>\n<h2 id=\"install-redis\"><span class=\"ez-toc-section\" id=\"Install_Redis\"><\/span>Install Redis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you haven\u2019t already, you need to install Redis for this tutorial.<\/p>\n<p>You can download the binaries and compile them easily using the following commands.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330791 lazy\" alt=\"\" width=\"736\" height=\"245\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00.png 1044w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00-280x93.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00-540x180.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00-270x90.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00-796x265.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>To make sure that the Redis server runs without an issue, send a ping to the server using the<span>\u00a0<\/span><code>redis-cli<\/code>.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330793 lazy\" alt=\"\" width=\"753\" height=\"117\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04.png 1042w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04-280x44.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04-540x84.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04-270x42.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04-796x124.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>If you receive<span>\u00a0<\/span><code>pong<\/code>\u00a0as a response, the Redis server is running successfully.<\/p>\n<p>Read the<span>\u00a0<\/span><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/redis.io\/topics\/quickstart\">official quick start guide<\/a><span>\u00a0<\/span>to get a better idea if something goes wrong.<\/p>\n<h2 id=\"build-the-nodejs-application\"><span class=\"ez-toc-section\" id=\"Build_the_NodeJS_application\"><\/span>Build the NodeJS application<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p id=\"basic-set-up\"><strong>Basic set-up<\/strong><\/p>\n<p>Set up the initial boilerplate for the Node application like this.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330794 lazy\" alt=\"\" width=\"783\" height=\"370\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00.png 1036w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00-280x132.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00-540x255.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00-270x128.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00-796x376.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Note how we use two additional packages named<span>\u00a0<\/span><code>axios<\/code><span>\u00a0<\/span>and<span>\u00a0<\/span><code>redis<\/code>.<span>\u00a0<\/span><code>redis<\/code><span>\u00a0<\/span>is the standard Redis client for Node. We use<span>\u00a0<\/span><code>axios<\/code><span>\u00a0<\/span>to retrieve data from an external API for this tutorial.<\/p>\n<p>Before continuing, make sure to install those two packages using npm.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330795 lazy\" alt=\"\" width=\"792\" height=\"116\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04.png 1040w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04-280x41.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04-540x79.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04-270x39.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04-796x116.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p id=\"retrieve-data-from-the-external-api\"><strong>Retrieve data from the external API<\/strong><\/p>\n<p>We will be using the<span>\u00a0<\/span><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/jobs.github.com\/api\">GitHub Jobs API<\/a><span>\u00a0<\/span>to get data related to programming jobs available in different locations in the world.<\/p>\n<p>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.<\/p>\n<figure class=\"lph\" data-src=\"https:\/\/thenextweb.com\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-1_hu5935e5169b0e79450e8ed42b0f55070e_245322_700x0_resize_q75_box.jpg\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" width=\"700\" height=\"398\" class=\" lazy\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-1_hu5935e5169b0e79450e8ed42b0f55070e_245322_700x0_resize_q75_box.jpg\" data-lazy=\"true\"\/><figcaption><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/08\/a-simple-guide-to-redis-and-caching-with-nodejs\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F08%2Fa-simple-guide-to-redis-and-caching-with-nodejs%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: POSTMAN \u2013 GitHub Jobs API results\" data-title=\"Share POSTMAN \u2013 GitHub Jobs API results on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share POSTMAN \u2013 GitHub Jobs API results on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>POSTMAN \u2013 GitHub Jobs API results<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>In our Node application, we define a route named<span>\u00a0<\/span><code>\/jobs<\/code>, which retrieves job data from the above API and send them back to the client.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330796 lazy\" alt=\"\" width=\"780\" height=\"809\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51.png 1040w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51-203x210.png 203w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51-260x270.png 260w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51-130x135.png 130w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51-796x825.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Here, we use<span>\u00a0<\/span><code>axios<\/code><span>\u00a0<\/span>to send a GET request to the GitHub Jobs API with the user-provided search term.<\/p>\n<p>Let\u2019s see how the route works now using Postman.<\/p>\n<figure class=\"\" data-src=\"https:\/\/thenextweb.com\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-2_hu13f59c5d2fe6cbb8d4ae1a0212c50d15_235479_700x0_resize_q75_box.jpg\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"lazy loaded lazy\" alt=\"\" width=\"700\" height=\"398\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-2_hu13f59c5d2fe6cbb8d4ae1a0212c50d15_235479_700x0_resize_q75_box.jpg\" data-lazy=\"true\"\/><figcaption><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/08\/a-simple-guide-to-redis-and-caching-with-nodejs\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F08%2Fa-simple-guide-to-redis-and-caching-with-nodejs%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: POSTMAN \u2013 Our API results\" data-title=\"Share POSTMAN \u2013 Our API results on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share POSTMAN \u2013 Our API results on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>POSTMAN \u2013 Our API results<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h3 id=\"caching-the-results\"><span class=\"ez-toc-section\" id=\"Caching_the_results\"><\/span>Caching the results<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now, let\u2019s see how we can improve the performance of the application by caching.<\/p>\n<p>First, we need to connect to the Redis server through our application. We use the installed redis package for this task.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330797 lazy\" alt=\"\" width=\"786\" height=\"327\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00.png 1038w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00-280x117.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00-540x225.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00-270x112.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00-796x331.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Redis server listens on port 6379 on default. So, we pass the port number to connect to Redis and create a client.<\/p>\n<p>Then, implement the logic to store and retrieve data from the cache.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330798 lazy\" alt=\"\" width=\"653\" height=\"807\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06.png 1048w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06-170x210.png 170w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06-218x270.png 218w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06-109x135.png 109w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06-796x984.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p><strong>What\u2019s going on here?<\/strong><\/p>\n<p>When we receive a client request to the \/jobs route, first, we get the search term sent with the request\u2019s query parameters.<span style=\"font-family: Consolas, Monaco, monospace;\"\/><\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330802 lazy\" alt=\"\" width=\"780\" height=\"110\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09.png 1046w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09-280x40.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09-540x76.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09-270x38.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09-796x113.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09-1044x148.png 1044w\"\/><\/figure>\n<p><\/span><span style=\"font-family: Consolas, Monaco, monospace;\"\/><\/p>\n<p>Then, we try to retrieve the requested data from the cache bypassing the search term, which we use as the key when storing data in the cache. Since the Redis package doesn\u2019t have native support for promises, we have to pass a callback to process the retrieved data.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330799 lazy\" alt=\"\" width=\"808\" height=\"191\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12.png 1048w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12-280x66.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12-540x128.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12-270x64.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12-796x188.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12-1044x248.png 1044w\"\/><\/figure>\n<p><\/span><\/p>\n<p>If the value returned from Redis is not null, it means the related data exists in the cache, so it\u2019s easy to return that data in the response. Just make sure you cast back the string to JSON.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330800 lazy\" alt=\"\" width=\"659\" height=\"248\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14.png 1048w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14-280x105.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14-540x203.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14-270x102.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14-796x299.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14-1044x394.png 1044w\"\/><\/figure>\n<p><\/span><\/p>\n<p>If the returned value was null, we have to send a request to the external API to retrieve relevant data.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330801 lazy\" alt=\"\" width=\"706\" height=\"330\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20.png 1056w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20-280x131.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20-540x253.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20-270x126.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20-796x372.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<p>When we get the data from the API, before sending it back, we store it in Redis so that the next time the same request is sent to the Node server, it can respond with data stored in the cache instead of requesting them from the API.<\/p>\n<p>Note how we use the<span>\u00a0<\/span><code>setex<\/code><span>\u00a0<\/span>function to store data in the cache. Using the<span>\u00a0<\/span><code>setex<\/code><span>\u00a0<\/span>function specifically, instead of the regular<span>\u00a0<\/span><code>set<\/code><span>\u00a0<\/span>function, we can set an expiration time to the stored key-value pair. Because we set a value for expiration time, Redis will automatically remove that key-value pair from the cache when the elapsed time expires.<\/p>\n<h2 id=\"full-source-code\"><span class=\"ez-toc-section\" id=\"Full_source_code\"><\/span>Full source code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1330804 lazy\" alt=\"\" width=\"668\" height=\"1410\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15.png 668w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15-99x210.png 99w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15-128x270.png 128w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15-64x135.png 64w\"\/><\/figure>\n<p>That\u2019s it. We have created a simple cache for our application. That wasn\u2019t so hard, was it?<\/p>\n<h2 id=\"moment-of-the-truth-time-comparison\"><span class=\"ez-toc-section\" id=\"Moment_of_the_truth_time_comparison\"><\/span>Moment of the truth: time comparison<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ll see how the use of a cache impacted the performance of our app. I used Postman to send requests to the server and measure request completion time.<\/p>\n<figure class=\"lph\" data-src=\"https:\/\/thenextweb.com\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/performance_hu79e5d0973b84b1342ea5e2e32fcfdc0d_25866_700x0_resize_q75_box.jpg\">\n<figure class=\"post-image post-mediaBleed aligncenter\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" width=\"700\" height=\"218\" class=\" lazy\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/performance_hu79e5d0973b84b1342ea5e2e32fcfdc0d_25866_700x0_resize_q75_box.jpg\" data-lazy=\"true\"\/><figcaption><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/08\/a-simple-guide-to-redis-and-caching-with-nodejs\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2020%2F12%2F08%2Fa-simple-guide-to-redis-and-caching-with-nodejs%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Performance\" data-title=\"Share Performance on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Performance on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"\/><\/a>Performance<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>The first time you send a request to the server with a new search term, the application takes longer to respond (over 7 seconds) because it has to get the data from the external API. The second time you make the same request, the server responds faster as the results already exist in the cache.<\/p>\n<p>The request completes within 10 ms. It\u2019s a huge performance increase from the application we saw before without the cache.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this tutorial, we gave you a quick introduction to Redis and created a simple cache with it for a Node.js application. Now you can use Redis to cache frequently queried data in your application to gain a considerable performance increase.<\/p>\n<p>You can also look into how to take advantage of the best features of Redis in other use cases too.<\/p>\n<p><i><span style=\"font-weight: 400;\">This <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/\"><i><span style=\"font-weight: 400;\">article<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> was originally published on <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> by <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\"><i><span style=\"font-weight: 400;\">Juan Cruz Martinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> (<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">twitter<\/a>: <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/twitter.com\/bajcmartinez\"><i><span style=\"font-weight: 400;\">@bajcmartinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\">), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/subscribe\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> is also available as a free weekly <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">news<\/a>letter. Sign up for updates on everything related to programming, AI, and computer <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a> in <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>.<\/span><\/i><\/p>\n<\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/08\/a-simple-guide-to-redis-and-caching-with-nodejs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#A simple guide to Redis and caching with NodeJS&#8221; 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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":128701,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/1-copy-13.jpg&signature=6f7f9ae9eb2545a6cc9fe0fd044f3a46","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[83027,73825,72366,75432,75083,76456,70759],"class_list":["post-128700","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-analytics","tag-application-software","tag-data","tag-data-analysis","tag-developer","tag-management-system","tag-tech"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/128700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=128700"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/128700\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/128701"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=128700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=128700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=128700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}