<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tharindu Sathischandra</title>
    <description>The latest articles on DEV Community by Tharindu Sathischandra (@tharindu).</description>
    <link>https://dev.to/tharindu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F478615%2F6e1659b8-0366-4f6e-8ea4-76c9e5cc760b.jpeg</url>
      <title>DEV Community: Tharindu Sathischandra</title>
      <link>https://dev.to/tharindu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tharindu"/>
    <language>en</language>
    <item>
      <title>A quick introduction to Distributed Caching</title>
      <dc:creator>Tharindu Sathischandra</dc:creator>
      <pubDate>Sat, 17 Oct 2020 12:47:09 +0000</pubDate>
      <link>https://dev.to/tharindu/a-quick-introduction-to-distributed-caching-48e3</link>
      <guid>https://dev.to/tharindu/a-quick-introduction-to-distributed-caching-48e3</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869192632%2FYoW_gWOti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869192632%2FYoW_gWOti.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Cache?
&lt;/h1&gt;

&lt;p&gt;Cache is simply the storage capacity for data on a system that is reserved for the quicker servicing of future requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Caching:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Reduce Network calls&lt;/li&gt;
&lt;li&gt;Avoid Re-computations&lt;/li&gt;
&lt;li&gt;Avoid load on DB&lt;/li&gt;
&lt;li&gt;Improve availability of data, by providing continued service even if the backend server is unavailable&lt;/li&gt;
&lt;li&gt;Improved data access speeds brought about by locality of data&lt;/li&gt;
&lt;li&gt;Smoothing out load peaks (e.g.: by avoiding round-trips between middle-tier and data-tier)&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Distributed Caching
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869340940%2FVASSOX9wo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869340940%2FVASSOX9wo.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache is maintained as an external service to application servers (front-end servers)&lt;/li&gt;
&lt;li&gt;Typically deployed on a cluster of multiple nodes forming a large logical cache (horizontal scalability)&lt;/li&gt;
&lt;li&gt;Managed in a synchronized fashion, making the same cached data remotely available to all application servers (consistent)&lt;/li&gt;
&lt;li&gt;Survives application server restarts and deployments&lt;/li&gt;
&lt;li&gt;Preserves high availability&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use Cases Of Distributed Caches
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Database Caching to reduce DB bottleneck:
&lt;/h3&gt;

&lt;p&gt;The Cache layer in-front of a database saves frequently accessed data in-memory to cut down latency &amp;amp; unnecessary load on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Storing User Sessions:
&lt;/h3&gt;

&lt;p&gt;User sessions are stored in the cache to avoid losing the user state in case any of the instances go down.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cross-Module Communication &amp;amp; Shared Storage:
&lt;/h3&gt;

&lt;p&gt;Saves the shared data which is commonly accessed by all the services. It acts as a backbone for microservice communication.&lt;/p&gt;

&lt;h3&gt;
  
  
  In-memory Data Stream Processing &amp;amp; Analytics:
&lt;/h3&gt;

&lt;p&gt;Real-time processing (opposed to traditional ways of storing data in batches &amp;amp; then running analytics on it)&lt;/p&gt;

&lt;p&gt;E.g.: anomaly detection, fraud monitoring, online gaming real-time stats, real-time recommendations, payment processing, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Distributed Hash Tables
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Generally, a Distributed Cache is based on a &lt;strong&gt;Distributed Hash Table&lt;/strong&gt; (DHT) which is similar to hash-table but &lt;strong&gt;spread across multiple nodes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Key-value pairs are stored in a DHT, and any participating node can efficiently retrieve the value associated with a given key&lt;/li&gt;
&lt;li&gt;DHT allows a Distributed cache to scale on the fly, by managing the addition, deletion, failure of nodes continually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869440338%2FVtB6oi3sc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869440338%2FVtB6oi3sc.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Distributed Caching Strategies
&lt;/h1&gt;

&lt;p&gt;There are different kinds of caching strategies that serve specific use cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache Aside&lt;/li&gt;
&lt;li&gt;Read-Through&lt;/li&gt;
&lt;li&gt;Write-Through&lt;/li&gt;
&lt;li&gt;Write-Back&lt;/li&gt;
&lt;li&gt;Write-Around&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By selecting proper strategy caches can reduce response times, decrease the load on the database, and save costs. The strategy is selected based on the &lt;strong&gt;data&lt;/strong&gt; and &lt;strong&gt;data access patterns&lt;/strong&gt; ( data size, data uniqueness, access frequency, …).&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Cache Aside Strategy
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869607425%2FhG-8j4-2P.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869607425%2FhG-8j4-2P.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache sits on the side and the application directly talks to both the cache and the DB.&lt;/li&gt;
&lt;li&gt;When the user sends a request for particular data: The system first looks for it in the cache &amp;amp; If present it's simply returned. Else, the data is fetched from the database, the cache is updated &amp;amp; is returned to the user.&lt;/li&gt;
&lt;li&gt;Data is &lt;strong&gt;lazy loaded&lt;/strong&gt; into cache.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In this strategy, the data is &lt;strong&gt;written directly to the database&lt;/strong&gt;. This means things between the cache and the database might get inconsistent.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To avoid this, data on the cache has a &lt;strong&gt;TTL&lt;/strong&gt; (Time to Live). After TTL the data is invalidated from the cache.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Resilient to cache failures. (If, cache cluster goes down, the system can still operate by going directly to the database.)&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Best suits for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Read-heavy workloads&lt;/li&gt;
&lt;li&gt; Data that is not much frequently updated: e.g.: user profile data (name, birthday, etc.).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Read-Through
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869766036%2F9yor0caB1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869766036%2F9yor0caB1.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache sits in-line with the database.&lt;/li&gt;
&lt;li&gt;When there is a cache miss, it loads missing data from the database, populates the cache and returns it to the application.&lt;/li&gt;
&lt;li&gt;Cache always stays consistent with the database.&lt;/li&gt;
&lt;li&gt;Best suits for: Read-heavy workloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Write-Through
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869802995%2FICFJmmOTI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869802995%2FICFJmmOTI.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data is first written to the cache and then to the database.&lt;/li&gt;
&lt;li&gt;The cache sits in-line with the database and writes always go through the cache to the main database.&lt;/li&gt;
&lt;li&gt;Maintains high consistency between the cache and the database (but, adds a little latency during the write operations as data is to be updated in the cache additionally.)&lt;/li&gt;
&lt;li&gt;Best suits for: write-heavy workloads like online multiplayer games&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Write-Back (write-behind)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869840432%2F-_QntNKcD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869840432%2F-_QntNKcD.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application writes data to the cache which acknowledges immediately and after some delay, it writes the data back to the database.&lt;/li&gt;
&lt;li&gt;Resilient to database failures and can tolerate some database downtime&lt;/li&gt;
&lt;li&gt;Risky as if the cache fails before the DB is updated, the data might get lost.
(Write-back strategy is used with other caching strategies to more advantage)
Write Back Distributed Cache Strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Cache Eviction
&lt;/h1&gt;

&lt;p&gt;A cache eviction algorithm is a way of deciding which element to evict when the cache is full.&lt;/p&gt;

&lt;p&gt;Some common cache eviction policies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;First In First Out (FIFO)&lt;/strong&gt;: First discards the first block accessed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last In First Out (LIFO)&lt;/strong&gt;: First discards the block accessed most recently&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Least Recently Used (LRU)&lt;/strong&gt;: First discards the least recently used items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most Recently Used (MRU)&lt;/strong&gt;: First discards the most recently used items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Least Frequently Used (LFU)&lt;/strong&gt;: Counts how often an item is needed. Those that are used least often are discarded first.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Some popular distributed caches used in the industry are: &lt;em&gt;Redis, Hazelcast, Eh-cache, Memcached, Riak&lt;/em&gt;, …&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869918222%2FRCg8bb0HX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602869918222%2FRCg8bb0HX.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Useful resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://codeahoy.com/2017/08/11/caching-strategies-and-how-to-choose-the-right-one/" rel="noopener noreferrer"&gt;Caching Strategies and How to Choose the Right One&lt;/a&gt; &lt;/li&gt;
&lt;li&gt; &lt;a href="https://www.8bitmen.com/distributed-cache-101-the-only-guide-youll-ever-need/" rel="noopener noreferrer"&gt;Distributed Cache 101 - The Only Guide You'll Ever Need&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://blog.eduonix.com/web-programming-tutorials/redis-memcached-select-caching-strategy/" rel="noopener noreferrer"&gt;Redis or Memcached – How to Select the Caching Strategy?&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Distributed_hash_table" rel="noopener noreferrer"&gt;Distributed Hash Table on Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cache</category>
      <category>distributedchaching</category>
    </item>
  </channel>
</rss>
