<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cache - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/cache/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sun, 04 Feb 2024 21:22:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Cache - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Redis Distributed Cache in C#.NET with Examples</title>
		<link>https://thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/</link>
					<comments>https://thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 16 Oct 2022 03:23:00 +0000</pubDate>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[idistributedcache c# example]]></category>
		<category><![CDATA[redis cache .net core example]]></category>
		<category><![CDATA[redis cache .net framework]]></category>
		<category><![CDATA[redis cache c# example]]></category>
		<category><![CDATA[redis cache c# net core web api example]]></category>
		<category><![CDATA[redis cache in asp.net core]]></category>
		<category><![CDATA[redis cache sliding expiration c#]]></category>
		<category><![CDATA[stackexchange.redis c# example]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=18514</guid>

					<description><![CDATA[<p>Redis Distributed Cache in C#.NET with Examples Today in this article, we shall see how to use Redis Distributed Cache in C#.NET with Examples. We will see how cache can play important role improving a system&#8217;s performance and scalability and best user experience. We will cover below aspects, I have used API with SQL server [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/">Redis Distributed Cache in C#.NET with Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading" id="block-71809369-9662-4e9c-b614-fe60537bcfc9"><strong>Redis Distributed Cache in C#.NET with Examples</strong></h1>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" width="1024" height="465" src="https://www.thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-1024x465.jpg" alt="Redis Distributed cache in C#.NET" class="wp-image-18515" srcset="https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-1024x465.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-300x136.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-768x349.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-1536x697.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-785x356.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c.jpg 1606w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p id="block-7c6ef384-0d64-4ab5-ac9d-a6332d7e8960">Today in this article, we shall see how to use Redis Distributed Cache in C#.NET with Examples. </p>



<p id="block-7c6ef384-0d64-4ab5-ac9d-a6332d7e8960">We will see how cache can play important role improving a system&#8217;s performance and scalability and best user experience. </p>



<p></p>



<p>We will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-what-is-caching">What is Caching?</a></li><li><a href="#aioseo-what-is-redis-distributed-cache">What is Redis Distributed Cache</a></li><li><a href="#block-1a692ce6-e90c-4748-911b-615faaac5a95">Getting started</a></li><li><a href="#aioseo-redis-distributed-cache-in-c-net">Redis Distributed cache in C#.NET</a></li><li><a href="#aioseo-redis-cache-using-idistributedcache-interface">Redis Cache using IDistributedCache interface</a></li><li><a href="#aioseo-redis-cache-timeout">Redis cache timeout</a></li><li><a href="#aioseo-starting-redisserver">Starting RedisServer</a></li><li><a href="#aioseo-redis-dependency-injection-of-the-connectionmultiplexer">Redis-Dependency Injection of the ConnectionMultiplexer</a></li></ul></div>



<p></p>



<p>I have used API with SQL server data access using the Entity framework example in this article. </p>



<p></p>



<p>Once data with key-value pair is cached, then every consecutive data access for the same will be done through the cache.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-caching">What is Caching?</h2>



<p></p>



<ul class="wp-block-list">
<li>Caching is a strategy for improving a system&#8217;s performance and scalability.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Caching lets you temporarily <strong>copy and let you access frequently used data from the storage near the application</strong>. </li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Caching increases response times for client applications by supplying the data more quickly.</li>
</ul>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="717" src="https://www.thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-1024x717.jpg" alt="" class="wp-image-27991" srcset="https://thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-1024x717.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-300x210.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-768x538.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-1536x1075.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/07/redis-cache-best-practices-memory-management-743x520.jpg 743w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-redis-distributed-cache">What is Redis Distributed Cache</h2>



<p></p>



<p>Redis is a distributed in-memory database that allows you to read and write data. </p>



<p></p>



<p><strong>Faster than Disk</strong> &#8211; Redis can respond in milliseconds or less. </p>



<p></p>



<p>They can read data faster than disk-based databases because they store data in memory.</p>



<p></p>



<ul class="wp-block-list">
<li>In Redis, all writes are asynchronous, so clients can read and write data without being interrupted. </li>
</ul>



<ul class="wp-block-list">
<li>Redis reads the data from the snapshot or logs file and utilizes it to build the in-memory cache when it starts up.</li>
</ul>



<ul class="wp-block-list">
<li>Redis is a key-value store that accepts simple types as well as complicated data structures like hashes, lists, and sets as values. </li>
</ul>



<ul class="wp-block-list">
<li>It supports a set of atomic operations.</li>
</ul>



<p></p>



<p id="block-f47b1558-b433-4ec4-9f64-7cb34e136119">We shall cover the basic aspects of Redis cache implementation below,</p>



<p></p>



<ul class="wp-block-list">
<li>Install the Redis Nuget package.</li>
</ul>



<ul class="wp-block-list">
<li>Configure Redis Cache in the API pipeline.</li>
</ul>



<ul class="wp-block-list">
<li>Get the data from the cache for the input request.</li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="block-1a692ce6-e90c-4748-911b-615faaac5a95">Getting started</h2>



<p></p>



<p id="block-8b0ae6c2-1c94-4200-92b7-13285c8fa29c">Create .NET <strong><em>3.1 or 6 API</em></strong> or ASP.NET Core MVC applications.</p>



<p></p>



<p id="block-4161c405-d64c-4900-87b3-4bfcf3a14725">Please install the <strong><em>Redis </em></strong>NuGet package <a href="https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis/5.0.1" target="_blank" rel="noreferrer noopener">Microsoft.Extensions.Caching.StackExchangeRedis</a></p>



<p id="block-4161c405-d64c-4900-87b3-4bfcf3a14725"> as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Install-Package<a href="https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis/5.0.1" target="_blank" rel="noreferrer noopener"> Microsoft.Extensions.Caching.StackExchangeRedis</a> -Version 5.0.0
</pre>



<p>Note- Please use any latest available compatible version.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-distributed-cache-in-c-net">Redis Distributed cache in C#.NET</h2>



<p></p>



<p>Please add Redis services to the Service collection as below,</p>



<p></p>



<pre class="wp-block-code"><code>public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddScoped&lt;IEmployeeRepository, EmployeeRepository&gt;();
            services.AddSingleton&lt;IRedisCacheService, RedisCacheService&gt;();
            services.AddDbContext&lt;EmployeeContext&gt;(options =&gt;
            {
                options.UseSqlServer(Configuration.GetConnectionString("EmployeeDB"),
                sqlServerOptionsAction: sqlOptions =&gt;
                {
                    sqlOptions.EnableRetryOnFailure();
                });
            });
            services.AddStackExchangeRedisCache(options =&gt;
            {
                options.Configuration = $"{Configuration.GetValue&lt;string&gt;("RedisCache:Host")}:{Configuration.GetValue&lt;int&gt;("RedisCache:Port")}";
            });</code></pre>



<p></p>



<p></p>



<p><strong><em>AddStackExchangeRedisCache &#8211;  </em></strong>It let you <a href="https://www.thecodebuzz.com/redis-cache-health-check-route-java-nodejs-asp-net-core/">add Redis-distributed caching</a> services to the specified IServiceCollection.</p>



<p></p>



<p>Let&#8217;s define a simple interface with Read and Write access,</p>



<p></p>



<pre class="wp-block-code"><code>public interface IRedisCacheService
    {
        T Get&lt;T&gt;(string key);
        T Set&lt;T&gt;(string key, T value);
    }</code></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-cache-using-idistributedcache-interface">Redis Cache using IDistributedCache interface</h2>



<p></p>



<p>Below is the basic implementation of the interface method.</p>



<p>Above we have registered the implementation of&nbsp;IDistributedCache&nbsp;using a wrapper interface i.e <strong><em>IRedisCacheService</em></strong>.</p>



<p></p>



<p><strong>IRedisCacheService </strong>interface provides the below methods to work with the distributed cache.</p>



<p></p>



<pre class="wp-block-code"><code>public class RedisCacheService : IRedisCacheService
    {
        private readonly IDistributedCache _cache;
        public RedisCacheService(IDistributedCache cache)
        {
            _cache = cache;
        }
        public T Get&lt;T&gt;(string key)
        {
            var value = _cache.GetString(key);
            if (value != null)
            {
                return JsonSerializer.Deserialize&lt;T&gt;(value);
            }
            return default;
        }
    }</code></pre>



<p></p>



<p>Below is a generic Set method that will save or write data to Redis Cache.</p>



<p></p>



<pre class="wp-block-code"><code> public T Set&lt;T>(string key, T value)
        {
            var timeOut = new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24),
                SlidingExpiration = TimeSpan.FromMinutes(60)
            };
            _cache.SetString(key, JsonSerializer.Serialize(value), timeOut);
            return value;
        }</code></pre>



<p></p>



<p><strong><em>The IRedisCacheService </em></strong>interface is defined as below</p>



<p></p>



<pre class="wp-block-code"><code>public interface IRedisCacheService
    {
        T Get&lt;T>(string key);
        T Set&lt;T>(string key, T value);
    }</code></pre>



<p></p>



<p>AbsoluteExpirationRelativeToNow and SlidingExpiration usage is explained below.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-cache-timeout">Redis cache timeout </h2>



<p></p>



<p><strong>AbsoluteExpirationRelativeToNow </strong>&#8211; That means the element added to the cache, will remain in memory for the specified time/hours, and then entry will expire from the cache whether the element is accessed or not during that time.</p>



<p></p>



<p>Example: Above <em><strong>AbsoluteExpirationRelativeToNow</strong></em> is set as 24 hours. </p>



<p></p>



<p>Item will be held in cache for the entire 24 hours and then entry will be expired.</p>



<p></p>



<p>It will be removed early only if <em><strong>SlidingExpiration </strong></em>is set, and its condition is met.</p>



<p></p>



<p></p>



<p><strong><em>SlidingExpiration</em></strong> &#8211; Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed. </p>



<p></p>



<p>This will not extend the entry lifetime beyond the absolute expiration (if it is set).</p>



<p></p>



<p><strong><em>Example</em></strong>: Above <strong><em>SlidingExpiration </em></strong>is set as 60 min. </p>



<p></p>



<p>If the item is not accessed within 60 min then the item will be removed from the cache. </p>



<p></p>



<p>If it is getting accessed at least one time within 60 min then it will be held in the cache let&#8217;s say for the entire 24 hours and then entry will get expired based on <strong>AbsoluteExpiration</strong> time.</p>



<p></p>



<p>Let&#8217;s do a dependency injection of our custom Interface.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="826" src="https://www.thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example-1024x826.jpg" alt="redis c# example" class="wp-image-18526" srcset="https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example-1024x826.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example-300x242.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example-768x620.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example-644x520.jpg 644w, https://thecodebuzz.com/wp-content/uploads/2021/09/redis-asp-net-core-csharp-c-example.jpg 1487w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-starting-redisserver">Starting RedisServer </h2>



<p></p>



<p>Lets start Redis Server and start its instance to accept the data for caching.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="287" src="https://www.thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-1024x287.jpg" alt="Redis Distributed cache in .NET " class="wp-image-18527" srcset="https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-1024x287.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-300x84.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-768x215.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-785x220.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2.jpg 1523w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Once we execute the API with the required input ID, first-time Employee details will be fetched from the database but next time same ID-related will be fetched directly from the cache instead of the SQL server.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-dependency-injection-of-the-connectionmultiplexer">Redis-Dependency Injection of the ConnectionMultiplexer</h2>



<p></p>



<p><strong><em>ConnectionMultiplexer&nbsp;</em></strong>StackExchange’s principal object can be leveraged using dependency injection and is recommended to use. </p>



<p></p>



<p>It provides multipurpose usage like accessing the Redis database and lets you perform read, write, update, or delete operations, provide pub/sub features, etc.</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/" target="_blank" rel="noreferrer noopener" title="Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices"><strong><em>Redis-Dependency Injection using ConnectionMultiplexer </em></strong></a></li>
</ul>



<p></p>



<p></p>



<p><strong><em>Useful </em>References: </strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-cache-best-practices-development/" target="_blank" rel="noreferrer noopener" title="Redis Cache Best Practices for Development"><strong><em>Redis Cache Best Practices for Development</em></strong></a></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/getting-started-with-ncache-in-asp-net-core/" target="_blank" rel="noreferrer noopener" title="Getting started with NCache In ASP.NET Core"><em><strong>Using NCache for Distributed Cache in C#.NET</strong></em></a></li>
</ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/">Redis Distributed Cache in C#.NET with Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Redis Error &#8211; It was not possible to connect to the Redis server</title>
		<link>https://thecodebuzz.com/redis-error-it-was-not-possible-to-connect-to-the-redis-server/</link>
					<comments>https://thecodebuzz.com/redis-error-it-was-not-possible-to-connect-to-the-redis-server/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 04 Nov 2021 01:04:00 +0000</pubDate>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[abortonconnectfail redis connection string]]></category>
		<category><![CDATA[initializing/notstarted]]></category>
		<category><![CDATA[it was not possible to connect to the redis server(is). unable to connect on 6379/interactive]]></category>
		<category><![CDATA[it was not possible to connect to the redis server(s). connecttimeout]]></category>
		<category><![CDATA[it was not possible to connect to the redis server(s). socketfailure]]></category>
		<category><![CDATA[it was not possible to connect to the redis server(s). there was an authentication failure]]></category>
		<category><![CDATA[no connection is active/available to service]]></category>
		<category><![CDATA[redisconnectionexception unable to connect to redis server]]></category>
		<category><![CDATA[unabletoconnect on localhost:6379/interactive]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=19432</guid>

					<description><![CDATA[<p>Redis Error &#8211; It was not possible to connect to the redis server. There was an authentication failure; check that passwords or client certificates. Today in this article, we will see how to fix Redis error &#8220;It was not possible to connect to the redis server. There was an authentication failure; check that passwords or [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/redis-error-it-was-not-possible-to-connect-to-the-redis-server/">Redis Error – It was not possible to connect to the Redis server</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">Redis Error &#8211; It was not possible to connect to the redis server. <span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-black-color">There was an authentication failure; check that passwords or client certificates.</span></h2>



<p>Today in this article, we will see how to fix Redis error &#8220;It was not possible to connect to the redis server. <span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-black-color">There was an authentication failure; check that passwords or client certificates.</span>&#8220;</p>



<p></p>



<p>We will cover few possible resolutions which could help you fix the issue.</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-issue-description">Issue Description</a></li><li><a href="#aioseo-resolution">Resolution</a></li><li><a href="#aioseo-resolution-1">Resolution 1</a></li><li><a href="#aioseo-resolution-2">Resolution 2</a></li><li><a href="#aioseo-resolution-3">Resolution 3</a></li><li><a href="#aioseo-resolution-4">Resolution 4</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-issue-description">Issue Description</h2>



<p></p>



<p>Redis connection throws an error,</p>



<p></p>



<p><span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">StackExchange.Redis.RedisConnectionException: &#8216;It was not possible to connect to the redis server(s). There was an authentication failure; check that passwords (or client certificates) are configured correctly. SocketFailure on localhost:6379/Interactive, Initializing/NotStarted, last: NONE, origin:</span></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="389" src="https://www.thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password-1024x389.jpg" alt="it was not possible to connect to the redis server(s). There was an authentication failure; check that passwords (or client certificates) are configured correctly" class="wp-image-19433" srcset="https://thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password-1024x389.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password-300x114.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password-768x292.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password-785x298.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/11/StackExchange.Redis_.RedisConnectionException.It-was-not-possible-to-connect-to-the-redis-server-password.jpg 1471w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution">Resolution</h2>



<p></p>



<p>I found the error is due to configuration settings required for a Redis connection being missing.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution-1">Resolution 1 </h2>



<p></p>



<p>Before trying any fix, please make sure the Redis server is up and running. This error may occur if Redis Server is down or faulted.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution-2">Resolution 2 </h2>



<p></p>



<p>Please make sure your connection details including username and password are accurate.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution-3">Resolution 3</h2>



<p></p>



<p>Sometimes a network bleep could lead to such connectivity issues. Mainly <a href="https://www.thecodebuzz.com/transient-errors-in-cloud-guidelines-and-resolution/" target="_blank" rel="noopener" title="What is Transient Error in Cloud – Guidelines and Resolution">transient </a>errors are common in cloud environment.  </p>



<p></p>



<p>Please make sure <strong><em>resiliency </em></strong>for the connection is implemented. </p>



<p></p>



<p>Please visit: &#8211;  <a href="https://www.thecodebuzz.com/redis-cache-best-practices-development/" target="_blank" rel="noreferrer noopener" title="best practices for Redis cache managment "><strong><em>Best practices for Redis cache </em></strong><b><i>manag</i></b><strong><em>ement</em></strong> </a></p>



<p></p>



<p>It&#8217;s recommended that if you set <strong><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-pale-pink-color">abortConnect=false</mark></em></strong> in your connection string. </p>



<p></p>



<p>As default behavior, <strong><em>Redis&nbsp;</em></strong>will <em><strong>auto-reconnect</strong></em> if a network has any transient error occurs.</p>



<p></p>



<p>The default value for&nbsp;<strong>abortConnect&nbsp;</strong>is&nbsp;true.</p>



<p></p>



<p>I was able to fix the error by using the below code (for .NET). However, you may try identifying similar settings in project based on other language like Java, python or node etc.</p>



<p></p>



<pre class="wp-block-code"><code>var configurationOptions = new ConfigurationOptions
                {
                    EndPoints = { $"{Configuration.GetValue&lt;string>("RedisCache:Host")}: 
                    {Configuration.GetValue&lt;int>("RedisCache:Port")}" },
                    Ssl = true,
                    AbortOnConnectFail = false,
                };
                var multiplexer = ConnectionMultiplexer.Connect(configurationOptions)</code></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-resolution-4">Resolution 4 </h2>



<p></p>



<p>Please re-verify <strong>certificates </strong>configuration are accurate. Please visit this for more details.</p>



<p></p>



<p><strong>Reference </strong>:</p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-cache-best-practices-development/" title="best practices for Redis cache managment " target="_blank" rel="noreferrer noopener"><strong><em>Best practices for Redis cache managment</em></strong> </a> </li>
</ul>



<ul class="has-medium-font-size wp-block-list">
<li><strong><em><a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/" title="Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices">Redis-Dependency Injection of the ConnectionMultiplexer</a></em></strong></li>
</ul>



<ul class="has-medium-font-size wp-block-list">
<li><strong><em><a href="http://redis.io/topics/rediscli" target="_blank" rel="noreferrer noopener">REDIS CLI COMMANDS</a></em></strong> </li>
</ul>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/redis-error-it-was-not-possible-to-connect-to-the-redis-server/">Redis Error – It was not possible to connect to the Redis server</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/redis-error-it-was-not-possible-to-connect-to-the-redis-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Redis Cache Connection Resiliency ASP.NET Core</title>
		<link>https://thecodebuzz.com/connection-resiliency-redis-cache-stackexchange-redi-best-practices/</link>
					<comments>https://thecodebuzz.com/connection-resiliency-redis-cache-stackexchange-redi-best-practices/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 29 Oct 2021 22:06:00 +0000</pubDate>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[azure redis cache best practices]]></category>
		<category><![CDATA[azure redis cache response size]]></category>
		<category><![CDATA[azure redis cache scaling]]></category>
		<category><![CDATA[redis best practices]]></category>
		<category><![CDATA[redis cache c# net core example]]></category>
		<category><![CDATA[Redis Cache Connection Resiliency ASP.NET Core]]></category>
		<category><![CDATA[redis cache connection string example]]></category>
		<category><![CDATA[redis cache resiliency]]></category>
		<category><![CDATA[redis cache sample]]></category>
		<category><![CDATA[redis cache sliding expiration c#]]></category>
		<category><![CDATA[redis connection string]]></category>
		<category><![CDATA[redis multiplexer]]></category>
		<category><![CDATA[redis timeout config]]></category>
		<category><![CDATA[stackexchange.redis .net core example]]></category>
		<category><![CDATA[stackexchange.redis multiple connections]]></category>
		<category><![CDATA[stackexchange.redis timeout]]></category>
		<category><![CDATA[stackexchangeredis timeout]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=19463</guid>

					<description><![CDATA[<p>Redis Cache Connection Resiliency &#8211; Best Practices Today in this article we shall see how to implement Redis Cache Connection Resiliency with examples in the .NET application. We will use StackExchange.Redis NuGet package for getting supported connection manager class for performing connection or re-connections or timeout configuration. Connection resiliency is a critical component of application [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/connection-resiliency-redis-cache-stackexchange-redi-best-practices/">Redis Cache Connection Resiliency ASP.NET Core</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Redis Cache Connection Resiliency</strong> <strong>&#8211; Best Practices</strong></h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-1024x465.jpg" alt="Redis Cache Connection Resiliency" class="wp-image-19494" width="732" height="332" srcset="https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-1024x465.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-300x136.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-768x349.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-1536x697.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency-785x356.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/12/redis-cache-connection-resiliency.jpg 1606w" sizes="auto, (max-width: 732px) 100vw, 732px" /></figure>



<p>Today in this article we shall see how to implement Redis Cache Connection Resiliency with examples in the .NET application.</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-configuring-the-connectionmultiplexer-with-resiliency-options">Configuring the ConnectionMultiplexer with resiliency options</a></li><li><a href="#aioseo-connection-resiliency-for-redis--retry-with-exponential-backoff">Connection Resiliency for Redis- Retry with exponential backoff</a></li><li><a href="#aioseo-connection-resiliency-for-redis---retry-using-string-options">Connection Resiliency for Redis &#8211; Retry using string options</a></li><li><a href="#aioseo-using-connectionmultiplexer-via-dependency-injection">Using ConnectionMultiplexer via Dependency Injection</a></li><li><a href="#retry-usage-guidance-1">Redis Retry guidelines</a></li></ul></div>



<p>We will use <strong><em>StackExchange.Redis</em></strong> NuGet package for getting supported connection manager class for performing connection or re-connections or timeout configuration.</p>



<p></p>



<p>Connection resiliency is a critical component of application communication that uses shared resources (particularly in the <strong><em>cloud</em></strong>) because these resources are more vulnerable to transient faults.</p>



<p></p>



<p>The <strong><em><a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/" target="_blank" rel="noreferrer noopener" title="Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices">StackExchange.Redis </a></em></strong>client uses a connection manager class that is configured through a set of options, including:</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-configuring-the-connectionmultiplexer-with-resiliency-options"><strong>Configuring the ConnectionMultiplexer with resiliency options</strong></h2>



<p></p>



<p>Below is a sample example of how to configure the <strong><em>ConnectionMultiplexer </em></strong>with resiliency options. Below we have used an instance of the&nbsp;configuration options&nbsp;class, with required options defined and passing it to the&nbsp;<strong>Connect</strong>&nbsp;method.</p>



<p></p>



<p><strong>Retry </strong>policies are configured before connecting to the cache.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-connection-resiliency-for-redis--retry-with-exponential-backoff">Connection Resiliency for Redis- Retry with exponential backoff</h2>



<p></p>



<p>The below example shows a retry strategy using exponential <em><strong>backoff</strong></em>.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [6,8,11]; title: ; notranslate">
  


                var options = new ConfigurationOptions
                {
                    EndPoints = { $&quot;{Configuration.GetValue&lt;string&gt;(&quot;RedisCache:Host&quot;)}: 
                                {Configuration.GetValue&lt;int&gt;(&quot;RedisCache:Port&quot;)}&quot; },

                    ConnectRetry = 5,

                    ReconnectRetryPolicy = new ExponentialRetry(deltaBackOffms , 
                                                                maxdeltaBackOffms),

                    ConnectTimeout = 1000

                };

                var redisMultiplexer = ConnectionMultiplexer.Connect(options);

           

</pre></div>


<p></p>



<p>Above time in <strong><em>milliseconds </em></strong>can be defined as the below example,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
                var deltaBackOffms = 

                Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);

                var maxdeltaBackOffms = 

                Convert.ToInt32(TimeSpan.FromSeconds(20).TotalMilliseconds);
</pre></div>


<p></p>



<p><strong><em>ConfigurationOptions </em></strong>discussed above are defined as below,</p>



<p></p>



<ul class="wp-block-list"><li><strong>ConnectRetry</strong> &#8211; The number of times to repeat connect attempts during the initial connection operation. The default value is 3.</li></ul>



<ul class="wp-block-list"><li><strong>ReconnectRetryPolicy</strong>. The retry strategy to use. The default value is LinearRetry 5000 ms</li></ul>



<ul class="wp-block-list"><li><strong>ConnectTimeout</strong>. The maximum waiting time is in milliseconds.</li></ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-connection-resiliency-for-redis---retry-using-string-options">Connection Resiliency for Redis &#8211; Retry using string options </h2>



<p></p>



<p>Alternatively, we can define configuration via string declaration as below</p>



<p></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">         
           var configOptions = $"{host},connectRetry=3,connectTimeout=1000";
           var redisMultiplexer = ConnectionMultiplexer.Connect(configOptions);

           services.AddSingleton&lt;IConnectionMultiplexer&gt;(redisMultiplexer);</pre>



<p></p>



<p></p>



<p>Where &#8216;<strong><em>host</em></strong>&#8216; is defined as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">var host = $"{Configuration.GetValue("RedisCache:Host")}:<br>{Configuration.GetValue("RedisCache:Port")}";</pre>



<p></p>



<p>Above we are making use of the configuration to read<strong><em> </em></strong>the <strong><em>apsetting.json</em></strong> file,</p>



<p></p>



<p>For more details: <a href="https://www.thecodebuzz.com/configuration-in-net-core-2-1/" title="Configuration in ASP.NET Core" target="_blank" rel="noreferrer noopener"><strong><em>Configuration in ASP.NET Core</em></strong></a></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-connectionmultiplexer-via-dependency-injection">Using ConnectionMultiplexer via Dependency Injection </h2>



<p></p>



<p>if you are using <strong><em>ASP.NET Core or MVC</em></strong> application, then <strong><em>ConnectionMultiplexer  </em>via </strong>Dependency Injection can be injected by defining the service scope in the API pipeline as below,  </p>



<p></p>



<pre class="wp-block-preformatted has-normal-font-size">services.AddSingleton(redisMultiplexer);
</pre>



<p>For more details: <a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/" title="Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices" target="_blank" rel="noreferrer noopener"><strong><em>Redis-Dependency Injection of the ConnectionMultiplexer </em></strong></a></p>



<p></p>



<h2 class="wp-block-heading" id="retry-usage-guidance-1">Redis Retry guidelines</h2>



<p></p>



<p>Consider the following guidelines when using Azure Cache for Redis,</p>



<p></p>



<ul class="wp-block-list"><li>Retry policies become effective when establishing a connection to the cache (when the application first starts.)</li></ul>



<p></p>



<ul class="wp-block-list"><li>It&#8217;s recommended to use falling back by accessing an original data source. This is preferred over a number of retry attempts.</li></ul>



<p></p>



<p></p>



<p><strong><em>Other useful references :</em></strong></p>



<p></p>



<ul class="has-medium-font-size wp-block-list"><li><a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/" title="Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices" target="_blank" rel="noreferrer noopener"><em>Redis-Dependency Injection of the ConnectionMultiplexer </em></a></li></ul>



<ul class="has-medium-font-size wp-block-list"><li><a href="https://www.thecodebuzz.com/what-is-cache-best-practices-guidelines/" title="What is Cache? Cache guidelines and Best practices " target="_blank" rel="noreferrer noopener"><em>Cache Design and Best practices </em></a></li></ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/connection-resiliency-redis-cache-stackexchange-redi-best-practices/">Redis Cache Connection Resiliency ASP.NET Core</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/connection-resiliency-redis-cache-stackexchange-redi-best-practices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Connect to Remote Redis Server &#8211; Guidelines</title>
		<link>https://thecodebuzz.com/connect-to-remote-redis-server-cache-ssl-tls-auth/</link>
					<comments>https://thecodebuzz.com/connect-to-remote-redis-server-cache-ssl-tls-auth/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 07 Oct 2021 02:39:00 +0000</pubDate>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[connect to redis server]]></category>
		<category><![CDATA[Connect to Remote Redis Server]]></category>
		<category><![CDATA[redis allow remote connections install redis-cli]]></category>
		<category><![CDATA[redis connection refused]]></category>
		<category><![CDATA[redis-cli connect to remote]]></category>
		<category><![CDATA[redis-cli connect to remote cluster]]></category>
		<category><![CDATA[redis-cli connect to remote server with password]]></category>
		<category><![CDATA[redis-cli connect to remote ssl]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=18625</guid>

					<description><![CDATA[<p>Connect to Remote Redis Server &#8211; Guidelines Today in this article we shall see how to connect to Remote Redis Server. We shall see all the basic commands and various ways of connecting. Today in this article, we will cover below aspects, Redis-cli connects to remote Redis Server &#8211; using host &#38; port By default redis-cli connects [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/connect-to-remote-redis-server-cache-ssl-tls-auth/">Connect to Remote Redis Server – Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Connect to Remote Redis Server &#8211; Guidelines</h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/how-do-i-run-redis-on-windows-readiserver-cache.jpg" alt="redis-cli connect to remote" class="wp-image-18733" width="379" height="333" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/how-do-i-run-redis-on-windows-readiserver-cache.jpg 784w, https://thecodebuzz.com/wp-content/uploads/2021/10/how-do-i-run-redis-on-windows-readiserver-cache-300x263.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/how-do-i-run-redis-on-windows-readiserver-cache-768x674.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/how-do-i-run-redis-on-windows-readiserver-cache-593x520.jpg 593w" sizes="auto, (max-width: 379px) 100vw, 379px" /></figure>



<p>Today in this article we shall see how to connect to Remote Redis Server. We shall see all the basic commands and various ways of connecting.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-redis-cli-connect-to-remote-redis-server-using-host-port">Redis-cli connects to remote Redis Server &#8211; using host &#038; port</a></li><li><a href="#aioseo-connect-to-remote-redis-server-using-password">Connect to Remote Redis Server using the password</a></li><li><a href="#aioseo-connect-to-remote-redis-using-uri-option-in-command">Connect to Remote Redis using uri option in command</a></li><li><a href="#aioseo-connect-to-remote-redis-server-using-ssl-tls">Connect to Remote Redis Server using SSL/TLS</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-cli-connect-to-remote-redis-server-using-host-port">Redis-cli connects to remote Redis Server &#8211; using host &amp; port </h2>



<p></p>



<p>By default <code>redis-cli</code> connects to the server at <strong><em>127.0.0.1</em></strong> i.e your <em><strong>localhost </strong></em>and port <strong><em>6379</em></strong>. </p>



<p></p>



<p>You can change the host or port using command-line options. </p>



<p></p>



<p>To use a different hostname or an <strong>IP addres</strong>s, use&nbsp;-h. To use a different port, use&nbsp;<code><strong><em>-p</em></strong></code>.</p>



<p></p>



<p><strong>Command</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong>redis-cli -h host -p port
</strong></pre>



<p></p>



<p><strong>Example</strong></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="979" height="96" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Remote-Redis-server-Using-host-port.jpg" alt="redis-cli connect to remote" class="wp-image-18721" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Remote-Redis-server-Using-host-port.jpg 979w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Remote-Redis-server-Using-host-port-300x29.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Remote-Redis-server-Using-host-port-768x75.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Remote-Redis-server-Using-host-port-785x77.jpg 785w" sizes="auto, (max-width: 979px) 100vw, 979px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-connect-to-remote-redis-server-using-password">Connect to Remote Redis Server using the password</h2>



<p></p>



<p>If your Redis server is password protected then additionally -a command is used.</p>



<p></p>



<p><strong><em>-a</em></strong> command is actually equivalent of the below <strong><em>AUTH </em></strong>command,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">AUTH &lt;password&gt;
</pre>



<p><strong>Command</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">redis-cli -h host -p port -a password
</pre>



<p></p>



<p><strong>Example</strong></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="941" height="85" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-Using-host-port-and-password-1.jpg" alt="redis cli connect to remote,Connect to Remote Redis Server " class="wp-image-18723" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-Using-host-port-and-password-1.jpg 941w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-Using-host-port-and-password-1-300x27.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-Using-host-port-and-password-1-768x69.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-Using-host-port-and-password-1-785x71.jpg 785w" sizes="auto, (max-width: 941px) 100vw, 941px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-connect-to-remote-redis-using-uri-option-in-command">Connect to Remote Redis using <code>uri</code>&nbsp;option in command</h2>



<p></p>



<p>You can connect to the Redis server using the valid URL option &nbsp;<code>-u &lt;uri&gt;</code>&nbsp;option.</p>



<p></p>



<p><strong>Command</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><code>$ redis-cli -u redis://password@host:port</code>
</pre>



<p><strong>Example</strong>:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">redis-cli -u redis://passowrdkey@thecodebuzz....com:6379 ping</pre>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="108" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri-1024x108.jpg" alt="redis-cli connect to remote" class="wp-image-18725" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri-1024x108.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri-300x32.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri-768x81.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri-785x83.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/10/Connect-to-Redis-server-using-uri.jpg 1175w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-connect-to-remote-redis-server-using-ssl-tls">Connect to Remote Redis Server using SSL/TLS</h2>



<p></p>



<p>By&nbsp;default,&nbsp;redis-cli&nbsp;connects&nbsp;to&nbsp;Redis&nbsp;via&nbsp;a&nbsp;standard&nbsp;<strong><em>TCP&nbsp;</em></strong>connection.&nbsp;</p>



<p></p>



<p>Command using —<strong><em>tls&nbsp;</em></strong>or &nbsp;—<em><strong>cacert&nbsp;</strong></em>or&nbsp;— <em><strong>cacertdir&nbsp;</strong></em>can&nbsp;be&nbsp;used&nbsp;to&nbsp;enable&nbsp;SSL/TLS.</p>



<p></p>



<p><strong>Reference </strong>:</p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/how-to-run-redis-on-windows-guidelines/" target="_blank" rel="noreferrer noopener" title="How to Run Redis on Windows – Guidelines"><strong><em>How to Run Redis on Windows – Guidelines</em></strong></a></li>
</ul>



<ul class="wp-block-list">
<li><a href="http://redis.io/topics/rediscli" target="_blank" rel="noreferrer noopener"><strong><em>REDIS CLI COMMANDS</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/connect-to-remote-redis-server-cache-ssl-tls-auth/">Connect to Remote Redis Server – Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/connect-to-remote-redis-server-cache-ssl-tls-auth/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Redis-Dependency Injection of the ConnectionMultiplexer &#8211; Best Practices</title>
		<link>https://thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/</link>
					<comments>https://thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 26 Sep 2021 01:00:00 +0000</pubDate>
				<category><![CDATA[Cache]]></category>
		<category><![CDATA[addstackexchangerediscache]]></category>
		<category><![CDATA[connectionmultiplexer c#]]></category>
		<category><![CDATA[connectionmultiplexer dependency injection]]></category>
		<category><![CDATA[connectionmultiplexer redis net core]]></category>
		<category><![CDATA[connectionmultiplexer vs idistributedcache]]></category>
		<category><![CDATA[stackexchange redis best practices]]></category>
		<category><![CDATA[stackexchange.redis c# example]]></category>
		<category><![CDATA[stackexchange.redis multiple connections]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=18632</guid>

					<description><![CDATA[<p>Today in this article, we shall see how to use Redis Distributed cache in .NET or .NET Core C# application using Redis-Dependency Injection Of The ConnectionMultiplexer. Today in this article, we will cover below aspects, Redis is a distributed in-memory database that allows you to read and write data.&#160;Faster than Disk&#160;– Redis can respond in [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/">Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="465" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-1024x465.jpg" alt="Redis-Dependency Injection Of The ConnectionMultiplexer" class="wp-image-18666" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-1024x465.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-300x136.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-768x349.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-1536x697.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer-785x356.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/10/redis-asp-net-core-csharp-c-ConnectionMultiplexer.jpg 1606w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Today in this article, we shall see how to use Redis Distributed cache in .NET or .NET Core C# application using Redis-Dependency Injection Of The ConnectionMultiplexer.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-what-is-connectionmultiplexer">What is ConnectionMultiplexer</a></li><li><a href="#block-1a692ce6-e90c-4748-911b-615faaac5a95">Getting started</a></li><li><a href="#aioseo-configure-redis-using-dependency-injection-of-the-connectionmultiplexer-redis">Configure Redis using Dependency Injection of the ConnectionMultiplexer Redis</a></li><li><a href="#aioseo-redis-connection-for-replica-set">Redis Connection for Replica set</a></li><li><a href="#aioseo-starting-redisserver">Starting RedisServer</a></li><li><a href="#aioseo-redis-idistributedcache-interface">Redis &#8211; IDistributedCache interface</a></li></ul></div>



<p></p>



<p>Redis is a distributed in-memory database that allows you to read and write data.&nbsp;<strong>Faster than Disk</strong>&nbsp;– Redis can respond in milliseconds or less. </p>



<p></p>



<p>We already covered the basic approach of using <em><strong>StackExchange.Redis </strong></em>and generic <strong><em>IDistributedCache </em></strong>interface. </p>



<p>Please refer to the below article for more details,</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/" target="_blank" rel="noreferrer noopener" title="Redis Distributed cache in .NET with examples"><strong><em>Redis Distributed cache in .NET with examples</em></strong></a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-connectionmultiplexer">What is ConnectionMultiplexer</h2>



<p></p>



<ul class="wp-block-list">
<li><strong><em>ConnectionMultiplexer </em></strong>StackExchange&#8217;s principal object has multipurpose usage like accessing the Redis database and letting you perform read, write, update or delete operations, provide pub/sub features, etc.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li> It is a thread-safe and ready-to-use application. All of the following examples will presume you have a <em><strong>ConnectionMultiplexer </strong></em>instance saved for later usage. </li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>The <strong><em>ConnectionMultiplexer </em></strong>is designed to be shared and reused between callers. </li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Per operation, you should not establish a <strong><em>ConnectionMultiplexer</em></strong>.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>It recommends sharing and reusing the <strong>ConnectionMultiplexer </strong>object. In the below example, we are creating a singleton instance and using it for all request processing.</li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="block-1a692ce6-e90c-4748-911b-615faaac5a95">Getting started</h2>



<p></p>



<p id="block-8b0ae6c2-1c94-4200-92b7-13285c8fa29c">Create .NET 3.1 or 6 API or ASP.NET Core MVC applications.</p>



<p></p>



<p id="block-4161c405-d64c-4900-87b3-4bfcf3a14725">Please install the&nbsp;<strong><em>Redis&nbsp;</em></strong>NuGet package&nbsp;<a href="https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis/5.0.1" target="_blank" rel="noreferrer noopener"><em>Microsoft.Extensions.Caching.StackExchangeRedis</em></a> as below,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><strong>&gt; Install-Package <a href="https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis/5.0.1" target="_blank" rel="noreferrer noopener">Microsoft.Extensions.Caching.StackExchangeRedis</a> -Version &lt;version&gt;</strong></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-configure-redis-using-dependency-injection-of-the-connectionmultiplexer-redis">Configure Redis using Dependency Injection of the ConnectionMultiplexer Redis</h2>



<p></p>



<p>Configure Redis using Dependency Injection of <em><strong>stackexchange.redis a</strong></em>s below by registering <em><strong>IConnectionMultiplexer</strong></em></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddScoped&lt;ISalaryRepository, SalaryRepository&gt;();
            services.AddDbContext&lt;EmployeeContext&gt;(options =&gt;
            {
                options.UseSqlServer(Configuration.GetConnectionString(&quot;EmployeeDB&quot;),
                sqlServerOptionsAction: sqlOptions =&gt;
                {
                    sqlOptions.EnableRetryOnFailure();
                });
            });
            if (_hostEnvironment.IsDevelopment())
            {
                services.AddDistributedMemoryCache();//Use this for only DEV testing purpose
            }
            else
            {
                 services.AddSingleton&lt;IConnectionMultiplexer&gt;(sp =&gt; 
                 ConnectionMultiplexer.Connect(new ConfigurationOptions
                {
                  EndPoints = { $&quot;{Configuration.GetValue&lt;string&gt;(&quot;RedisCache:Host&quot;)}: 
                  {Configuration.GetValue&lt;int&gt;(&quot;RedisCache:Port&quot;)}&quot; },
                  AbortOnConnectFail = false,
               }));
            }

</pre></div>


<p></p>



<p>Above you can use <code><strong><em>ConnectionMultiplexer.Connect</em></strong></code>&nbsp;or<strong>&nbsp;<code>ConnectionMultiplexer.ConnectAsync</code></strong>, passing in either a configuration string or a&nbsp;<code>ConfigurationOptions</code>&nbsp;object.&nbsp;</p>



<p></p>



<p>If you have SSL enabled please use the below setting to connect to the Redis cache setting,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">




services.AddSingleton&lt;IConnectionMultiplexer&gt;(sp =&gt; ConnectionMultiplexer.Connect(new ConfigurationOptions
            {
                EndPoints = { $&quot;{Configuration.GetValue&lt;string&gt;(&quot;RedisCache:Host&quot;)}:{Configuration.GetValue&lt;int&gt;(&quot;RedisCache:Port&quot;)}&quot; },
                Ssl = true,
                AbortOnConnectFail = false,
            }));




</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-connection-for-replica-set">Redis Connection for Replica set</h2>



<p></p>



<p>If you have a master/replica setup then specify the connection using the below approach. Redis will identify the master automatically and establish the connection.</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">ConnectionMultiplexer.Connect("ServerA:6379,ServerB:6379, ServerC:6379");</pre>



<p></p>



<p>Configure ConnectionMultiplexer using Dependency Injection as below,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="337" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-1024x337.jpg" alt="What is ConnectionMultiplexer - distributed cache redis" class="wp-image-18658" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-1024x337.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-300x99.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-768x253.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-1536x506.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-2048x675.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/10/read-all-keys-values-from-redis-cache-785x259.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-starting-redisserver">Starting RedisServer</h2>



<p></p>



<p>I have below used the WSL way to install Redis Server and started its instance to accept the data for caching.</p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1024" height="287" src="https://www.thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-1024x287.jpg" alt="Redis Distributed cache .net core" class="wp-image-18527" srcset="https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-1024x287.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-300x84.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-768x215.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2-785x220.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/09/how-do-i-run-redis-on-windows-2.jpg 1523w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Now once you start API and hit the endpoint first access of the requested ID will serve from the actual DB and any consecutive access will fetch directly from the cache instead of the SQL server.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="475" src="https://www.thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer-1024x475.jpg" alt="Redis Dependency Injection of ConnectionMultiplexer" class="wp-image-18659" srcset="https://thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer-1024x475.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer-300x139.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer-768x356.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer-785x364.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/10/Redis-Dependency-Injection-of-the-ConnectionMultiplexer.jpg 1045w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Redis supports both <strong><em>synchronous </em></strong>and <em><strong>asynchronous </strong></em>implementations.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-redis-idistributedcache-interface">Redis &#8211; IDistributedCache interface</h2>



<p></p>



<p>If you want to leverage the <strong><em>IDistributedCache</em></strong> interface, please see the below article for more details</p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/" target="_blank" rel="noreferrer noopener" title="Redis Distributed cache in .NET with examples"><em>Redis Distributed cache in .NET with examples</em></a>  </li>
</ul>



<p></p>



<p><strong><em>References:</em></strong></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/serialization-and-deserialization-using-system-text-json/" target="_blank" rel="noreferrer noopener" title="System.Text.Json -Serialization and Deserialization in .NET"><strong><em>System.Text.Json -Serialization and Deserialization in .NET</em></strong></a></li></ul>



<p></p>



<p>That&#8217;s all! Hope you find this article useful,</p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/">Redis-Dependency Injection of the ConnectionMultiplexer – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/redis-dependency-injection-connectionmultiplexer-redis-cache-netcore-csharp/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
	</channel>
</rss>
