<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Caching on Ben Mezger</title><link>https://seds.nl/tags/caching/</link><description>Recent content in Caching on Ben Mezger</description><generator>Hugo</generator><language>en</language><lastBuildDate>Thu, 16 Apr 2026 15:37:21 +0200</lastBuildDate><atom:link href="https://seds.nl/tags/caching/index.xml" rel="self" type="application/rss+xml"/><item><title>System Design - Caching Strategies</title><link>https://seds.nl/notes/system_design_caching_strategies/</link><pubDate>Wed, 15 Apr 2026 18:40:00 +0200</pubDate><guid>https://seds.nl/notes/system_design_caching_strategies/</guid><description>&lt;ul>
&lt;li>Related pages
&lt;ul>
&lt;li>&lt;a href="https://seds.nl/notes/system_design_databases_data_models_and_indexing/">System Design - Databases, data models and indexing&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://seds.nl/notes/system_design_network/">System Design - Network&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://seds.nl/notes/system_design_raid_and_storage/">System Design - RAID and Storage&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://seds.nl/notes/system_design_cap_theorem_acid_base_and_pacelc/">System Design - CAP theorem, ACID, BASE and PACELC&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="what-is-cache">What is cache?&lt;a class="anchor" href="#what-is-cache">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Intermediary layer between client and data source&lt;/li>
&lt;li>Stores results from heavy/expensive operations&lt;/li>
&lt;li>Reduces latency and dependency load&lt;/li>
&lt;li>Cheap but hard to do it right :)&lt;/li>
&lt;li>Needs to be reconstructible &amp;ndash; can only be cached what can be
retrieved from the origin&lt;/li>
&lt;/ul>
&lt;h2 id="definitions">Definitions&lt;a class="anchor" href="#definitions">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Weak consistency&lt;/li>
&lt;li>Eventual consistency&lt;/li>
&lt;li>Extra layer of the golden source &amp;ndash; cheap layer from the expensive
layer&lt;/li>
&lt;li>Challeges of guaranteeing aligment of cache and the data source&lt;/li>
&lt;li>Writes need to update or invalidate cache&lt;/li>
&lt;li>Question: how many times can the registration data for a single user
change?&lt;/li>
&lt;li>We need to ensure cache data remains up to date, as non-updated data
can be a problem (i.e. returning an old profile picture when the
user already updated)&lt;/li>
&lt;/ul>
&lt;h3 id="ttls">TTLs&lt;a class="anchor" href="#ttls">#&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>Soft-state (BASE)&lt;/li>
&lt;li>Expires (removes from cache) data&lt;/li>
&lt;li>TTLs to solve soft-state issues&lt;/li>
&lt;li>Ensures periodic data recycling&lt;/li>
&lt;/ul>
&lt;h2 id="eviction-policies">Eviction policies&lt;a class="anchor" href="#eviction-policies">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Defines which items to remove once the cache storage reach its limit&lt;/li>
&lt;li>&lt;strong>LRU&lt;/strong>: removes the least recently used
&lt;ul>
&lt;li>Assumes that if the item hasn&amp;rsquo;t been used recently, it will
probably not be used in the future&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>LFU&lt;/strong>: removes the least frequently used
&lt;ul>
&lt;li>Counter; counts the items that hasn&amp;rsquo;t been used frequently&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>FIFO&lt;/strong>: removes the oldest
&lt;ul>
&lt;li>Removes the oldest key (the first key that was created)&lt;/li>
&lt;li>Not very performatic&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>RR&lt;/strong>: Random replacements
&lt;ul>
&lt;li>Randomly removes a key&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Mechnism of defense, but it costs a lot to perform; we try to
prevent this using good cache validation approaches&lt;/li>
&lt;/ul>
&lt;h2 id="cache-invalidation">Cache invalidation&lt;a class="anchor" href="#cache-invalidation">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Mechanism of removing or marking itens as invalid&lt;/li>
&lt;li>Can be done manually, logically or through TTLs&lt;/li>
&lt;li>Consistency&lt;/li>
&lt;li>Biggest challenges of cache&lt;/li>
&lt;li>We do cache validation to avoid eviction policies to run&lt;/li>
&lt;/ul>
&lt;h2 id="cache-hit-miss-and-hit-rate">Cache Hit, Miss and Hit Rate&lt;a class="anchor" href="#cache-hit-miss-and-hit-rate">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Caching metrics&lt;/li>
&lt;li>&lt;strong>Cache Hit&lt;/strong>: data is in cache; so response is immediate&lt;/li>
&lt;li>&lt;strong>Cache Miss&lt;/strong>: data is not in cache; so we need to fetch from the
source
&lt;ul>
&lt;li>We want to decrease cache miss and increase cache hits&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Hit Rate&lt;/strong>: cache hits / number of requests (miss + hits)&lt;/li>
&lt;li>High hit rates = efficient system&lt;/li>
&lt;li>Low hit rate = inneficient system&lt;/li>
&lt;/ul>
&lt;h2 id="cache-implementation">Cache implementation&lt;a class="anchor" href="#cache-implementation">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>Memory cache (In-Memory)&lt;/li>
&lt;li>Distributed cache (Redis, Memcached, etc.)&lt;/li>
&lt;li>Database cache and data layers
&lt;ul>
&lt;li>Cache-Aside (Lazy Loading)&lt;/li>
&lt;li>Write-Through (Double Write)&lt;/li>
&lt;li>Write-Behind (Lazy Writing)&lt;/li>
&lt;li>Distributed Content Cache (CDN Cache)&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="in-memory">In-Memory&lt;a class="anchor" href="#in-memory">#&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>Local memory cache&lt;/li>
&lt;li>Great access performance&lt;/li>
&lt;li>Common data structures&lt;/li>
&lt;li>Key-Value (Hashmap)&lt;/li>
&lt;li>Scope limited to a process/thread&lt;/li>
&lt;li>Requires manual invalidation to avoid leaks&lt;/li>
&lt;/ul>
&lt;h3 id="cache-in-distributed-systems">Cache in distributed systems&lt;a class="anchor" href="#cache-in-distributed-systems">#&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>Performance, latency reduction and escalability&lt;/li>
&lt;li>Serves dynamic and static content&lt;/li>
&lt;li>Shared across N replicas&lt;/li>
&lt;li>Horizontal scalability throught clustering&lt;/li>
&lt;li>Replicas and high availability&lt;/li>
&lt;li>Supports expiration and eviction policies nativaly&lt;/li>
&lt;li>Technologis: Redis, Memcached&lt;/li>
&lt;/ul>
&lt;h3 id="cache-in-data-layers">Cache in data layers&lt;a class="anchor" href="#cache-in-data-layers">#&lt;/a>&lt;/h3>
&lt;ul>
&lt;li>Additional layer of data&lt;/li>
&lt;li>Computational cheaper data&lt;/li>
&lt;li>Relieves critical components&lt;/li>
&lt;li>Saves computational resources of the database&lt;/li>
&lt;li>Caches items least modified&lt;/li>
&lt;li>Stores the results of hot queries&lt;/li>
&lt;/ul>
&lt;h4 id="cache-aside">Cache-Aside&lt;a class="anchor" href="#cache-aside">#&lt;/a>&lt;/h4>
&lt;ul>
&lt;li>Lazy Loading&lt;/li>
&lt;li>Only creates the cache once the application needs&lt;/li>
&lt;li>Most common strategy in data layers&lt;/li>
&lt;li>If not found, it fetches from the DB&lt;/li>
&lt;li>Populates the cache and return (creating cache takes a bit of time)&lt;/li>
&lt;li>Consistency challenges of the DB&lt;/li>
&lt;/ul>
&lt;h4 id="write-through">Write-Through&lt;a class="anchor" href="#write-through">#&lt;/a>&lt;/h4>
&lt;ul>
&lt;li>Write is done simultanously &amp;ndash; once the data is stored in the DB,
its immediatly written to cache&lt;/li>
&lt;li>DB and cache&lt;/li>
&lt;li>Ideal for when read operations needs to be done fast since the
beginning&lt;/li>
&lt;li>Consistency challenge&lt;/li>
&lt;li>Combined with Cache-Aside (Fallback)&lt;/li>
&lt;/ul>
&lt;h4 id="write-behind">Write-Behind&lt;a class="anchor" href="#write-behind">#&lt;/a>&lt;/h4>
&lt;ul>
&lt;li>Write happens first in the cache, and then to the DB (asynchronous)&lt;/li>
&lt;li>Prioritizes application performance&lt;/li>
&lt;li>Low latency of writes&lt;/li>
&lt;li>Risks loosing data before the flush&lt;/li>
&lt;li>Used with queues, events and intermediary services&lt;/li>
&lt;/ul>
&lt;h4 id="distributed-content-cache--cdn-cache">Distributed Content Cache (CDN cache)&lt;a class="anchor" href="#distributed-content-cache--cdn-cache">#&lt;/a>&lt;/h4>
&lt;ul>
&lt;li>Network of distributed geographically servers&lt;/li>
&lt;li>Edge location (near the user)&lt;/li>
&lt;li>Stores static content&lt;/li>
&lt;li>Images, videos, CSS and JS&lt;/li>
&lt;li>Cache-Aside strategy (miss -&amp;gt; origin, hit -&amp;gt; edge)&lt;/li>
&lt;li>Geographic replication&lt;/li>
&lt;li>Invalidation through expiration or manual&lt;/li>
&lt;li>Reduces geographic latency&lt;/li>
&lt;li>Reduces origin load&lt;/li>
&lt;li>Includes segurity (DDoS, Firewall, thread detection, etc)&lt;/li>
&lt;li>Asynchronous invalidation and non-performatic&lt;/li>
&lt;li>Large-scale operations&lt;/li>
&lt;/ul></description></item></channel></rss>