<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kushagra Gupta on Medium]]></title>
        <description><![CDATA[Stories by Kushagra Gupta on Medium]]></description>
        <link>https://medium.com/@kushagragupta625?source=rss-8191f9299f10------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oOfT8yohKTiLSc21dw72tg.jpeg</url>
            <title>Stories by Kushagra Gupta on Medium</title>
            <link>https://medium.com/@kushagragupta625?source=rss-8191f9299f10------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 21:03:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kushagragupta625/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Server-Side Caching Using Redis]]></title>
            <link>https://medium.com/@kushagragupta625/server-side-caching-using-redis-d044d968121b?source=rss-8191f9299f10------2</link>
            <guid isPermaLink="false">https://medium.com/p/d044d968121b</guid>
            <category><![CDATA[design-decision]]></category>
            <category><![CDATA[optimization-techniques]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[server-side-caching]]></category>
            <dc:creator><![CDATA[Kushagra Gupta]]></dc:creator>
            <pubDate>Thu, 15 May 2025 23:08:32 GMT</pubDate>
            <atom:updated>2025-05-15T23:08:32.088Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everybody!</p><p>In this article, we will be exploring server-side caching along with an example implementation. We will be using the Node-Express framework for our server, PostgreSQL for the primary database, and for caching, we will be using an in-memory database, Redis.</p><h3>Introduction to Caching</h3><p>Before diving deep into the implementations, let’s have a quick look at what caching is and why it is needed.</p><p>In computer science, a <strong>cache</strong> is a high-speed data storage layer that stores specific data. This data can be accessed very quickly so that future requests for this data are served swiftly compared to requests that involve data fetching from the primary storage location. The entire process of storing data in the form of a cache is called <strong>caching</strong>.</p><h3>Why Do We Need Caching?</h3><p>Now, coming to the point of why we need caching.</p><p>Okay, let’s deal with that with a real-life situation.<br>Suppose your mom asked you to get 1 kg of sugar from a nearby store. Being a good mumma’s boy that you are, you’ll go fetch 1 kg of sugar from the particular shop. Now, just as you sat down on your laptop planning to binge “Breaking Bad” — specifically, the episode “Ozymandias” (Season 5, Episode 14) — your mom calls you again! This time she wants you to get her some coconuts for the puja that’s about to happen. Life doesn’t want you to reach the climax, right? You pull up in your 3 AM drip to get those coconuts for your mom since you still are her good boy.</p><p>You see how frustrating it was to make so many round-trips for tasks that could have been done in a single go, right?</p><p>So while designing backend systems to boost performance and improve response time, we need to observe such round trips and identify the data that has to be accessed frequently, and then come up with techniques to store this data as a cache for faster access.</p><h3>Understanding In-Memory Databases and Redis</h3><p>One last thing, what do we mean when we say Redis is an <strong>in-memory database</strong>?</p><p>In-memory databases are those database management systems that store information in the main memory of the computer. So in our case, with the help of Redis, we are actually storing data in the main memory (RAM) of our server.</p><h3>Shifting Focus to Implementation</h3><p>Okay, now we are done with the caching basics and why it is needed. Now let’s shift our focus towards a more technical side.</p><p>To give you some context, I was working on this project named <strong>met-data visualization</strong> that involved creating a data visualization platform for Arghyam’s India Water Portal. This platform aids the scientists and researchers in academia who wish to get some visual insights on the meteorological datasets possessed by the India Water Portal.</p><p>To give you more clarity about the project, let’s have a look at the high-level system architecture diagram of the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*Rk8ctuaosZkp3anAKcq4jg.png" /><figcaption>HLD without caching</figcaption></figure><p>Also, take a look at the schema of our current database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/161/1*rhXNsCkTx-68oYAE2d9zwg.png" /><figcaption>The data modeling was fixed since we were following legacy data ingestion pipeline</figcaption></figure><h3>Identifying Opportunities for Optimization</h3><p>Okay, now think of the use case wherein a person wants to know about the precipitation stats of Rajasthan, Gujarat, and Haryana. They will have to query the <strong>Precipitation</strong> table multiple times. As you can see, we have a data table for each weather parameter that gets queried upon for the corresponding request. Cutting all the unnecessary parts out, let’s see if we have an opportunity for any sort of optimizations regarding the query.</p><p>If we can store the entire Precipitation table data at some place in the first request itself, then we can save a lot of round trips to the database to query on the same table, just with different filters.</p><p>So can we somehow store the data table in our cache?</p><p>To further support our idea of storing the entire table, we can see that the data stored in our database is <strong>static in nature</strong> (it does not change frequently). Thus, we can easily store it on the server without worrying about data consistency between the cache layer and primary data storage.</p><p>So boom, tada!</p><p>We have found a way to optimize the performance of our project by reducing the API response time, simply by adding a cache layer to the server.</p><p>Now the high-level design of the project looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*8nfuXuO86RYc-rLeUm5VOw.png" /></figure><h3>Moving Towards Implementation</h3><p>Now that we are clear with what we have to do, let’s move towards the implementation.</p><p><strong>Some prerequisites for this section are:</strong></p><ul><li>Basic knowledge of web servers</li><li>Familiarity with ExpressJS</li><li>Understanding of MVC architecture</li><li>Familiarity with PostgreSQL and Redis</li></ul><p>You can check out the following articles to help you know enough about the above-mentioned topics to follow through this blog:</p><p>Links : <a href="https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Web_mechanics/What_is_a_web_server">Web servers</a>, <a href="https://expressjs.com/">ExpressJs</a>, <a href="https://dev.to/abhishekjaiswal_4896/understanding-the-mvc-pattern-with-nodejs-1fpg">MVC</a>, <a href="https://www.postgresql.org/">PostgereSQL</a>, <a href="https://redis.io/">Redis</a></p><h3>Server Structure Overview</h3><p>Okay, so here’s how our server looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*8slIQPpmBfqv3MJZp2CofQ.png" /></figure><p>As you can see, we are following a very standard design pattern, dividing our code into <strong>Controllers</strong>, <strong>Routes</strong>, <strong>Utils</strong>, and having a single entry point index.js file.</p><h3>The Server Without Caching</h3><p>Here’s how our server looks when there’s no caching involved:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*e3RiXwSvwgETI4SFBgmCOA.png" /></figure><p>Now you see, every request-response cycle time is in seconds, since we are fetching from the server for every incoming request.</p><p>In a real-world situation with hundreds of requests coming in, our website will become very annoyingly slow to the users, which is something we never want!</p><h3>Implementing a Caching Strategy with Redis</h3><p>So to solve this problem, we aim to implement a caching strategy.</p><p>We start by picking <strong>Redis</strong> as our choice of caching tool.</p><p>Redis stores data in the form of key-value pairs, so firstly we need to think of some way to assign keys to the data that we are storing as cache. The keys must be <strong>unique</strong> and <strong>descriptive</strong>.</p><p>Let’s rewind a little. Just some time ago, we inferred that we can store an entire data table corresponding to a weather parameter as cache. So why not just name the key as that particular weather parameter?</p><p>It ticks all the boxes:</p><ul><li><strong>Unique</strong>: Yes, we can uniquely identify the data corresponding to a weather condition based on the name of that condition itself!</li><li><strong>Descriptive</strong>: Yes, it is very obvious by now how easily “Precipitation” describes the data that’s stored against it as a key.</li></ul><p>Done! Now that we have a clear understanding of the cache key, let’s see exactly what we have to do with it.</p><p>While making a request to the database for some data, we can:</p><ol><li><strong>Check the Cache</strong>: Verify if the data is already present in the cache.</li><li><strong>Use Cached Data</strong>: If it’s there, use it and move to the next request.</li><li><strong>Query the Database</strong>: If it’s not, query the primary database.</li><li><strong>Store in Cache</strong>: Use the data and store the results in the cache with a valid key for future use.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*n7E8hTgmcFuQZHJHIX49bA.png" /><figcaption>Now we first check for cache and then move ahead</figcaption></figure><p>Are we done now?<br>We are nearly there, just a small task left. That is to <strong>remove the cache</strong>.</p><p>You know, in life, it’s really important to move on. Move on from things, people, places, and in our case, to move on from data as well.</p><p>We can’t keep the data in the RAM of our server forever! It will impact the b overall performance as the project scales.</p><p>So what do we do? How to say goodbye to the data that we cached?<br>I know, goodbyes are hard, but you have to let go as we move forward, as <strong>TIME</strong> passes.</p><p><strong>Time</strong>, yes!</p><p>We can just keep the data in cache for some particular time and remove it after. We can simply set a <strong>TTL (Time to Live)</strong> while storing data as cache. As soon as the TTL expires, the data vanishes. Thus, we can control the amount of time we want the data to be present in the cache.</p><h3>Wrapping It Up</h3><p>By implementing server-side caching with Redis, we’ve unlocked a significant performance boost for our application. Not only have we reduced the load on our primary database, but we’ve also enhanced the user experience by decreasing response times.</p><p>Caching is a powerful optimization technique, especially when dealing with static or infrequently changing data. By smartly storing and managing our data in Redis, we’ve turned those tedious “round trips” into swift, efficient journeys.</p><p>So next time you’re building an application and notice those sluggish response times, remember the power of caching. It’s like having a handy notepad for your server — quick to jot down frequent data and even quicker to retrieve it.</p><p>Happy coding, and may your applications run faster than ever before!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d044d968121b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>