{"id":1643,"date":"2020-12-08T14:00:24","date_gmt":"2020-12-08T14:00:24","guid":{"rendered":"https:\/\/thenextweb.com\/?p=1330789"},"modified":"2020-12-08T14: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:\/\/www.londonchiropracter.com\/?p=1643","title":{"rendered":"A simple guide to Redis and caching with NodeJS"},"content":{"rendered":"\n<p>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.<\/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\">What is Redis?<\/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>&nbsp;<\/span><a href=\"https:\/\/redis.io\/topics\/data-types\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">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\">Redis use cases<\/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:&nbsp;<a class=\"c-link c-message_attachment__title_link\" href=\"https:\/\/thenextweb.com\/plugged\/2020\/11\/30\/why-ai-is-the-future-of-home-security\/\" target=\"_blank\" rel=\"noreferrer noopener\" data-qa=\"message_attachment_title_link\"><span dir=\"auto\">Why AI is the future of home security<\/span><\/a>]<\/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\">Caching with Node and Redis<\/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\">Install Redis<\/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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330791 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.00.png\" alt width=\"736\" height=\"245\" sizes=\"(max-width: 736px) 100vw, 736px\" data-lazy=\"true\" data-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>&nbsp;<\/span><code>redis-cli<\/code>.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330793 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.33.04.png\" alt width=\"753\" height=\"117\" sizes=\"(max-width: 753px) 100vw, 753px\" data-lazy=\"true\" data-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>&nbsp;<\/span><code>pong<\/code>&nbsp;as a response, the Redis server is running successfully.<\/p>\n<p>Read the<span>&nbsp;<\/span><a href=\"https:\/\/redis.io\/topics\/quickstart\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">official quick start guide<\/a><span>&nbsp;<\/span>to get a better idea if something goes wrong.<\/p>\n<h2 id=\"build-the-nodejs-application\">Build the NodeJS application<\/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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330794 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.00.png\" alt width=\"783\" height=\"370\" sizes=\"(max-width: 783px) 100vw, 783px\" data-lazy=\"true\" data-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>&nbsp;<\/span><code>axios<\/code><span>&nbsp;<\/span>and<span>&nbsp;<\/span><code>redis<\/code>.<span>&nbsp;<\/span><code>redis<\/code><span>&nbsp;<\/span>is the standard Redis client for Node. We use<span>&nbsp;<\/span><code>axios<\/code><span>&nbsp;<\/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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330795 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.34.04.png\" alt width=\"792\" height=\"116\" sizes=\"(max-width: 792px) 100vw, 792px\" data-lazy=\"true\" data-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>&nbsp;<\/span><a href=\"https:\/\/jobs.github.com\/api\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GitHub Jobs API<\/a><span>&nbsp;<\/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=\"\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-1_hu5935e5169b0e79450e8ed42b0f55070e_245322_700x0_resize_q75_box.jpg\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-1_hu5935e5169b0e79450e8ed42b0f55070e_245322_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"398\" class=\" lazy\" data-lazy=\"true\"><figcaption><a 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\"><\/i><\/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>&nbsp;<\/span><code>\/jobs<\/code>, which retrieves job data from the above API and send them back to the client.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330796 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.35.51.png\" alt width=\"780\" height=\"809\" sizes=\"(max-width: 780px) 100vw, 780px\" data-lazy=\"true\" data-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>&nbsp;<\/span><code>axios<\/code><span>&nbsp;<\/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=\"\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-2_hu13f59c5d2fe6cbb8d4ae1a0212c50d15_235479_700x0_resize_q75_box.jpg\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/postman-2_hu13f59c5d2fe6cbb8d4ae1a0212c50d15_235479_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"398\" data-lazy=\"true\"><figcaption><a 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\"><\/i><\/a>POSTMAN \u2013 Our API results<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h3 id=\"caching-the-results\">Caching the results<\/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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330797 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.00.png\" alt width=\"786\" height=\"327\" sizes=\"(max-width: 786px) 100vw, 786px\" data-lazy=\"true\" data-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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330798 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.37.06.png\" alt width=\"653\" height=\"807\" sizes=\"(max-width: 653px) 100vw, 653px\" data-lazy=\"true\" data-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><\/span><\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330802 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.09.png\" alt width=\"780\" height=\"110\" sizes=\"(max-width: 780px) 100vw, 780px\" data-lazy=\"true\" data-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><\/span><\/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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330799 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.12.png\" alt width=\"808\" height=\"191\" sizes=\"(max-width: 808px) 100vw, 808px\" data-lazy=\"true\" data-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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330800 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.14.png\" alt width=\"659\" height=\"248\" sizes=\"(max-width: 659px) 100vw, 659px\" data-lazy=\"true\" data-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><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1330801 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.38.20.png\" alt width=\"706\" height=\"330\" sizes=\"(max-width: 706px) 100vw, 706px\" data-lazy=\"true\" data-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>&nbsp;<\/span><code>setex<\/code><span>&nbsp;<\/span>function to store data in the cache. Using the<span>&nbsp;<\/span><code>setex<\/code><span>&nbsp;<\/span>function specifically, instead of the regular<span>&nbsp;<\/span><code>set<\/code><span>&nbsp;<\/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\">Full source code<\/h2>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1330804 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/12\/Screenshot-2020-12-08-at-09.41.15.png\" alt width=\"668\" height=\"1410\" sizes=\"(max-width: 668px) 100vw, 668px\" data-lazy=\"true\" data-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>\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\">Moment of the truth: time comparison<\/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=\"\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/performance_hu79e5d0973b84b1342ea5e2e32fcfdc0d_25866_700x0_resize_q75_box.jpg\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/performance_hu79e5d0973b84b1342ea5e2e32fcfdc0d_25866_700x0_resize_q75_box.jpg\" alt width=\"700\" height=\"218\" class=\" lazy\" data-lazy=\"true\"><figcaption><a 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\"><\/i><\/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\">Summary<\/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>This <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/post\/beginners-guide-to-redis-and-caching-with-nodejs\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>article<\/span><\/i><\/a><i><span> was originally published on <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> by <\/span><\/i><a href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Juan Cruz Martinez<\/span><\/i><\/a><i><span> (twitter: <\/span><\/i><a href=\"https:\/\/twitter.com\/bajcmartinez\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>@bajcmartinez<\/span><\/i><\/a><i><span>), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a href=\"https:\/\/livecodestream.dev\/subscribe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.<\/span><\/i><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/syndication\/2020\/12\/08\/a-simple-guide-to-redis-and-caching-with-nodejs\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1644,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/1643"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1643"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/1643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/1644"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}