<?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 Mario Santos on Medium]]></title>
        <description><![CDATA[Stories by Mario Santos on Medium]]></description>
        <link>https://medium.com/@mgabrielfsantos9029?source=rss-3a913b16e576------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Be78TAzms2ZsbybT</url>
            <title>Stories by Mario Santos on Medium</title>
            <link>https://medium.com/@mgabrielfsantos9029?source=rss-3a913b16e576------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:31:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mgabrielfsantos9029/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[Making Django Cache Highly Available with Redis Sentinel]]></title>
            <link>https://medium.com/@mgabrielfsantos9029/making-django-cache-highly-available-with-redis-sentinel-efe4491b3442?source=rss-3a913b16e576------2</link>
            <guid isPermaLink="false">https://medium.com/p/efe4491b3442</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[redis-sentinel]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[redis]]></category>
            <dc:creator><![CDATA[Mario Santos]]></dc:creator>
            <pubDate>Wed, 16 Apr 2025 02:47:57 GMT</pubDate>
            <atom:updated>2025-04-16T12:13:12.040Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Ensuring High Availability in Redis with Sentinel</strong></h3><p>Nowadays, it’s increasingly common for developers to look for ways to optimize their web applications. Techniques like background processing and database query optimization are widely adopted to improve performance while also reducing resource consumption.<br> One of the most popular techniques is caching — and Redis stands out as one of the most widely used caching solutions.</p><h3><strong>Redis as Cache: Fast and Versatile</strong></h3><p>From broker to cache, Redis is widely used for its performance and simplicity. It stores data in memory, ensuring lightning-fast responses. In applications with high request loads, this can lead to significant savings in processing power and response time.</p><p>But it’s not perfect. One key aspect we need to consider is availability. And this is where the problem begins:</p><blockquote>What if my Redis Master node fails? Will my application keep running?</blockquote><h3>A Real-World Scenario</h3><p>Picture this: you’re working on an application that receives thousands of requests per minute. Each of these requests hits the Redis cache to quickly fetch data. You’ve already set up a replica to ensure redundancy, but your application is pointing directly to the Master node’s IP address.</p><p>Everything seems fine — until, for some reason, the Master fails. Even though there’s a replica available, your application has no idea. It keeps trying to access a node that’s no longer reachable. The result? Your application starts throwing errors, and the user experience takes a hit.</p><h3>Redis Sentinel: The High Availability Hero</h3><p>This is where Redis Sentinel comes into play.<br> Sentinel is a distributed system for monitoring, notification, and automatic failover in Redis. With it, you can detect failures in the Master node and automatically promote a replica to become the new Master — and the best part: it all happens without manual intervention.</p><h3><strong>How Does Sentinel Work?</strong></h3><p>Redis Sentinel operates across three main fronts:</p><ol><li><strong>Monitoring</strong><br> It continuously monitors Redis nodes (both Master and replicas), checking their health and connectivity.</li><li><strong>Notification</strong><br> If a problem occurs — such as an unreachable instance — Sentinel notifies administrators or dependent systems.</li><li><strong>Automatic Failover</strong><br> If Sentinel determines that the Master is truly unavailable (after reaching a consensus among the Sentinels), it automatically promotes one of the replicas to become the new Master.</li></ol><p>And here’s the cherry on top: your application can connect directly to Sentinel, and it will always point to the current Master.</p><h3>So How Does This Solve Our Problem?</h3><p>Remember the scenario above, where your application was pointing directly to the Master’s IP?</p><p>With Sentinel, you can configure your application to query the Master through Sentinel. This way, even if the original Master goes down, your application will keep running smoothly — because Sentinel will provide the new reference to the promoted Master.</p><h3><strong>Setting Up a Redis Sentinel Cluster</strong></h3><p>In this section, I will show you how to quickly and practically set up a Redis Sentinel cluster. The environment chosen for this setup is <a href="https://kubernetes.io/pt-br/"><strong>Kubernetes</strong></a>, keeping in mind that we are aiming for high availability. What better environment for a production application with such high demand than Kubernetes?</p><p>Now, back to the task. I will provide a <a href="https://github.com/Nodees/DjangoSentinel"><strong>repository</strong></a><strong> </strong>with the pre-configured Kubernetes YAML files. Please clone the repository. <strong>If you already have a Redis Sentinel cluster set up, you can skip this step</strong>.<br> I will be using <a href="https://k8slens.dev/"><strong>Lens</strong></a> for better visualization of the pods and services.</p><h4>Windows</h4><p>If you’re on Windows, simply enable Kubernetes in your Docker Desktop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1yEtVqSbxzYVVwGMML5cCQ.png" /></figure><h4>Linux</h4><p>In Linux environments, you have the option to use Minikube.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*albLe6ea-pDoqcEyHyl3fg.png" /><figcaption>Select the correct configuration for your machine. You can use it on Windows, but it’s faster and more practical with Docker Desktop.</figcaption></figure><pre>minikube start</pre><p>Got it! Now that your environment is set up, let’s move on to the first steps to bring up the cluster. With the repository cloned, navigate to the “kubernetes” folder:</p><pre>cd kubernetes</pre><p>Run the command to start all the pods, services, and other components:</p><pre>kubectl apply -k .</pre><p>This will apply all the configurations in the current directory and set up the necessary resources for your Redis Sentinel cluster.</p><pre>kubectl get pods</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9jXNRfViVrEqEUE40rxmkA.png" /><figcaption>The API in the image is located in the repository mentioned earlier.</figcaption></figure><h3>Configuring Django</h3><p>It’s very easy! The solution I will present here uses the <a href="https://github.com/jazzband/django-redis">django-redis</a> library, which is BSD-licensed. This library allows our Django application to communicate with a Redis Sentinel cluster.</p><p>In the settings.py file, create or update the following variables:</p><pre>DJANGO_REDIS_CONNECTION_FACTORY = &#39;django_redis.pool.SentinelConnectionFactory&#39;</pre><pre>CACHES = {<br>    &#39;default&#39;: {<br>        &#39;BACKEND&#39;: &#39;django_redis.cache.RedisCache&#39;,<br>        # Your master name<br>        &#39;LOCATION&#39;: &#39;redis://mymaster/0&#39;,<br>        &#39;OPTIONS&#39;: {<br>            &#39;CLIENT_CLASS&#39;: &#39;django_redis.client.SentinelClient&#39;,<br>            &#39;SENTINELS&#39;: [<br>                # Your service name<br>                (&#39;redis-sentinel-service&#39;, 26379),<br>                ...<br>            ],<br>            &#39;SENTINEL_KWARGS&#39;: {},<br>            &#39;CONNECTION_POOL_CLASS&#39;: &#39;redis.sentinel.SentinelConnectionPool&#39;<br>        }<br>    }<br>}</pre><p>Make sure to adjust the LOCATION, SENTINELS, and MASTER_NAME to fit your actual Redis Sentinel configuration.</p><h3>Sentinel Cluster</h3><p>Now, let’s see Sentinel in action. You will notice that we have a total of 3 Redis replicas: <strong>1 Master</strong> and <strong>2 Workers</strong>, along with <strong>3 Sentinels</strong> monitoring the setup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CXOaUz5JXOojgSzef7LZOw.png" /></figure><p>To test the connection of our application, run the following commands in your terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*JZfBljh8DqAVFn4U1SL4QQ.png" /></figure><p>This will check if the Django app can successfully set and retrieve data from the Redis cache.</p><blockquote>But what if my Master goes down?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yGpbUUwh-doK0BLkJGJcuA.png" /></figure><p>If your Master node fails, Redis Sentinel will automatically promote one of the replicas to become the new Master. Since your Django application is configured to connect through Sentinel (as we set up earlier), it will receive the new Master’s information from Sentinel without any manual intervention.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GDLkx2RIik491tLljHawDw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IV0QSb-rG2PwXFlJBmCVOQ.png" /><figcaption>Sentinel logs</figcaption></figure><p>As we can see in the image above, after the Master node failure, a voting process was initiated to decide who would become the new Master. After the vote, it was decided that redis-replica-1 would take on the role of the new Master within the cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*wS6yXza1Y5pzjLEkE4745A.png" /><figcaption>The same Commands After the Master Failure, and new Master assumes</figcaption></figure><h3>Conclusion</h3><p>Redis is undoubtedly a powerful tool for caching and other purposes. However, in production environments, we need to think beyond performance: resilience and high availability are essential.<br> Redis Sentinel provides a robust and efficient solution to ensure that your application continues to run smoothly even in the face of failures. If you’re not already using it, it might be time to consider adding it to your stack.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efe4491b3442" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>