<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[DE's Notes & Thoughts]]></title><description><![CDATA[Demystifying the engineering marvels and dissecting the complex architectural decisions behind today's most popular digital platforms.]]></description><link>https://blog.diana-enache.com</link><image><url>https://substackcdn.com/image/fetch/$s_!R5Si!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6b4625c-aab8-4926-be6c-35b6b1b5b802_500x500.png</url><title>DE&apos;s Notes &amp; Thoughts</title><link>https://blog.diana-enache.com</link></image><generator>Substack</generator><lastBuildDate>Mon, 04 May 2026 20:54:08 GMT</lastBuildDate><atom:link href="https://blog.diana-enache.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Diana Darie]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[theengineeringcompass@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[theengineeringcompass@substack.com]]></itunes:email><itunes:name><![CDATA[Diana Darie]]></itunes:name></itunes:owner><itunes:author><![CDATA[Diana Darie]]></itunes:author><googleplay:owner><![CDATA[theengineeringcompass@substack.com]]></googleplay:owner><googleplay:email><![CDATA[theengineeringcompass@substack.com]]></googleplay:email><googleplay:author><![CDATA[Diana Darie]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Building an Audit Logging Solution: Production Scars and Evolutionary Architectures]]></title><description><![CDATA[On lessons learnt when mvps and balancing trade offs aren't enough]]></description><link>https://blog.diana-enache.com/p/building-an-audit-logging-solution</link><guid isPermaLink="false">https://blog.diana-enache.com/p/building-an-audit-logging-solution</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Wed, 18 Dec 2024 14:59:19 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/70bb2644-5d26-4484-935d-6db5619390c7_4194x1892.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>System design is making tough trade offs and understanding why one approach is better than another. Sometimes this decision ends up being the wrong one. So you need to live with the consequences and make another trade off, do you start from scratch and take another approach or do you live with a not so perfect solution?</p><p>As reflection is an essential part of the learning process, this article is all about acknowledging that even with the right intentions and much dedication, you don&#8217;t always get it right. This is about accepting this reality and reminding myself that growth is a learning curve and despite setbacks we need to continue to take ownership.</p><div><hr></div><h3>What problem did we try to solve?</h3><p>Maintaining accountability within data-driven systems along with keeping track of who did what and when within isn't just a security requirement&#8212;sometimes it's a crucial business necessity. Whether if it&#8217;s just for meeting compliance requirements, troubleshooting and understanding system behaviour, investigating production incidents or providing insights for post-mortem analysis, a robust audit logging solution serves as your system's source of truth.</p><p>But building such a solution comes with a lot of challenges in terms of scalability, performance and most of all, data integrity. In this article, we&#8217;ll explore what are the possible approaches to implement such a system that captures meaningful events, stores them reliably, and makes them easily accessible when you need them most. We'll cover the essential components that make up a production-grade audit logging solution and emphasise some lessons learnt from implementing a &#8220;tamper-proof solution&#8221;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fqAS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fqAS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 424w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 848w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 1272w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fqAS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png" width="451" height="331.0943396226415" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:583,&quot;resizeWidth&quot;:451,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fqAS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 424w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 848w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 1272w, https://substackcdn.com/image/fetch/$s_!fqAS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e16e2f-01b5-40cf-b827-e07919b1a84e_583x428.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>What is an audit log? </h3><p>We want to know all database interactions that happened during a period of time, along with detailed information about each event. Those logs should be queryable and be able to answer questions like "who changed this value" or "which process caused this item to be removed." <strong>Audit logs</strong> record the occurrence of an <strong>event</strong>, the <strong>time</strong> at which it occurred, the responsible <strong>user</strong> or <strong>service</strong>, and the impacted <strong>entity</strong>.</p><p>A good audit logging system should not only record event data but also consider various usability criteria for the teams inspecting the logs (i.e the end users) and the future maintainers (e.g. engineers) of the system. The main requirements we&#8217;re going to work with: </p><ul><li><p>audit logs should be<strong> reliable</strong> - any database change should have a corresponding audit log entry.</p></li><li><p>have <strong>no</strong> or <strong>minimal</strong> <strong>setup </strong>- should be easy to set up across the ecosystem and ideally require no or minimal code changes for new services.</p></li><li><p>audit logs should be<strong> immutable - </strong>users and services should only have the ability to read data the logs and not update them.</p></li><li><p>audit logs should be <strong>easily queryable</strong> (by service, action, date, user etc) - users must be able to efficiently search through the logs by applying any one or more filters.</p></li><li><p>audit logs should be<strong> parsable</strong> - logs should also be easy to parse and work with. </p></li></ul><p>Since the ecosystem we work with is only using DynamoDB as a storage option, we&#8217;ll design the audit log solution to work especially with this kind of database. </p><h3>Solutions Considered </h3><p><strong>1. via the Transactional Outbox Pattern</strong></p><p>One solution to implement the audit logs would be to emit an event with every database change. This event would contain all the necessary information to track the change (what got changed, who changed it etc). The consumer of those events would process the data and store it in a way that would be easily accessible and queryable by all devs. </p><p>In our current ecosystem, we use an internal library go-kit (similar to the <a href="https://github.com/go-kit/kit">open source one go-kit</a>) to help us with the scaffolding and management of our golang microservices. This library also provides a standardised way for doing all db interactions. This approach would mean just modifying this library to emit an event when doing a CRUD operation. The advantage would be services don&#8217;t need to be  aware of this logic and no logic would need to be updated.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wo5S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wo5S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 424w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 848w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 1272w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wo5S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png" width="500" height="526.7857142857143" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1534,&quot;width&quot;:1456,&quot;resizeWidth&quot;:500,&quot;bytes&quot;:168358,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Wo5S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 424w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 848w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 1272w, https://substackcdn.com/image/fetch/$s_!Wo5S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3634b637-9adc-4ba7-b208-83ff153d2c13_1653x1742.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A possible design for the audit log solution where we emit an event for every CRUD operation</figcaption></figure></div><p>The problem with this solution is the general <em>dual-write issue</em> where you want to make 2 write operations in one atomic transaction. If we want to update 2 different systems, persist data in the database and send an event to notify other systems, because these two systems aren't linked, we have no way to update both in a transactional fashion. When a microservice sends an event notification after a database update, these two operations should run atomically to ensure data consistency and reliability.</p><p>The way to solve this is usually through the Outbox Pattern. If we have a database that supports transactional updates, then we can use it to overcome the dual-write problem. In our case, we would need another table, say an &#8220;Outbox Table&#8220;, and on a DB CRUD operation we would 1. do the CRUD operation and 2. save the change we did in the outbox table (both as part of a transaction which DynamoDB supports). The records in the outbox table will then be read by some workers which will emit an event with the changed data. The events will be read by an &#8220;Auditor Service&#8220; that will process them and finally store them in a queryable way.</p><p></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FS9r!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FS9r!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 424w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 848w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 1272w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FS9r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png" width="634" height="583.0616929698708" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1282,&quot;width&quot;:1394,&quot;resizeWidth&quot;:634,&quot;bytes&quot;:130971,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FS9r!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 424w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 848w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 1272w, https://substackcdn.com/image/fetch/$s_!FS9r!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F378dc940-3b7b-4689-a3e7-fc82b4016cdb_1394x1282.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">An audit log solution based on the outbox pattern approach</figcaption></figure></div><p>This approach comes with a fair bit of complexities. We would have at least one other table (we can choose to use one single outbox table for all services or create a seperate outbox table for each service). We would introduce some latency due to the additional database write for every transaction (to outbox). There is also the consideration of how we&#8217;re going to manage the outbox table growth/cleanup. We would need to think how to coordinate the allocation of work between the worker services. </p><p><strong>2. via the CDC Approach</strong></p><p>A second solution would be relying on a Change Data Capture (CDC) approach. CDC is <em>the process of identifying and capturing changes made to data in a database and then delivering those changes in real-time to a downstream process or system</em>). The basic idea is that we will store our data to a storage that supports CDC. Then the CDC infrastructure will notify external systems of the changes. Fortunately for us, DynamoDB provides such an option, called <strong>DynamoDB Streams.</strong></p><p>DynamoDB Streams captures a time-ordered sequence of item-level modifications in any DynamoDB table and stores this information in a log for up to 24 hours (can be extended to 7 days). Applications can access this log and view the data items as they appeared before and after they were modified, in near-real time.</p><p>At first glance this approach sounds like a good solution for our use case. No other resources are required to be setup. DynamoDB provides a CDC capability out of the box. Scalability and reliability are provided natively by AWS. The code what we would need to add would be minimal as we just need a service which reads the data from the dynamodb streams. So we went ahead with this. </p><h3>What&#8217;s in a DynamoDB Stream?</h3><p>Any dynamodb table with dynamodb streams enabled can have one dynamodb stream active. At any point in time, we can only have one active stream. A stream can have multiple shards. A shard can contain zero or more records. A record is the change that we want to be captured.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lWmO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lWmO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 424w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 848w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lWmO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png" width="3485" height="1200" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1200,&quot;width&quot;:3485,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:188180,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lWmO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 424w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 848w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!lWmO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5fe887e-b3ee-40f6-8b3c-454ae450cb23_3485x1200.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Overview of the DynamoDB Stream Components</figcaption></figure></div><p>Depending on how the stream was set up when it was enabled, the shard record can contain different types of data (<a href="https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_StreamSpecification.html">source</a>): </p><ul><li><p><code>KEYS_ONLY</code> - Only the key attributes of the modified item are written to the stream.</p></li><li><p><code>NEW_IMAGE</code> - The entire item, as it appears after it was modified, is written to the stream.</p></li><li><p><code>OLD_IMAGE</code> - The entire item, as it appeared before it was modified, is written to the stream.</p></li><li><p><code>NEW_AND_OLD_IMAGES</code> - Both the new and the old item images of the item are written to the stream.</p></li></ul><p>We&#8217;re interested in the last type, <code>NEW_AND_OLD_IMAGES</code>, and so in our case, we&#8217;re expecting our record to look similar to the following:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NK-U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NK-U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 424w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 848w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 1272w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NK-U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png" width="606" height="574.7843406593406" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1381,&quot;width&quot;:1456,&quot;resizeWidth&quot;:606,&quot;bytes&quot;:291675,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!NK-U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 424w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 848w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 1272w, https://substackcdn.com/image/fetch/$s_!NK-U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f6bd9e3-0975-4627-bd44-6e1c003bfa62_1596x1514.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Costs</h3><p>DynamoDB Streams does not cost anything to enable. DynamoDB charges <strong>on the number of read requests </strong>made towards the table&#8217;s DynamoDB Stream. Each read request in terms of the stream is in the form of a <code>GetRecords</code> API call that can return up to 1000 records or 1 MB worth of records in the response, whichever is reached first. None of the <a href="https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations_Amazon_DynamoDB_Streams.html">other DynamoDB Stream APIs</a> (<a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CostOptimization_StreamsUsage.html">source</a>) For DynamoDB Streams the cost is <strong>$0.25 per million reads.</strong></p><h3><strong>Approaches to Reading from Streams</strong></h3><p>DynamoDB streams can be processed in multiple ways. The most common approaches (<a href="https://aws.amazon.com/blogs/database/dynamodb-streams-use-cases-and-design-patterns/">source</a>) are:</p><ul><li><p>trigger based via AWS Lambdas</p></li><li><p>polling based with a standalone application that uses the Kinesis Client Library with the DynamoDB Streams Kinesis Adapter</p></li><li><p>polling based with a standalone application connecting directly to the Dynamo Stream APIs through the AWS SDK</p></li></ul><p><strong>The AWS Lambdas Approach</strong></p><p>Amazon DynamoDB comes integrated with AWS Lambda so that you can create <em><strong>triggers</strong></em>&#8212;pieces of code that automatically respond to events in DynamoDB Streams. Lambdas will poll the DynamoDB stream and, when they detect a new record, invoke your Lambda function and pass in one or more events.</p><p>Under the hood though, the AWS Lambda service will poll the stream for new records four times per second. (<a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.Lambda.html">source</a>) The polling interval cannot be changed, you can only change things like the <em>batch size</em> or the <em>parallelization factor</em>. (<a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-dynamodb.html">source</a>)</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bGST!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bGST!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 424w, https://substackcdn.com/image/fetch/$s_!bGST!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 848w, https://substackcdn.com/image/fetch/$s_!bGST!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!bGST!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bGST!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg" width="1456" height="491" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:491,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:192091,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bGST!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 424w, https://substackcdn.com/image/fetch/$s_!bGST!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 848w, https://substackcdn.com/image/fetch/$s_!bGST!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!bGST!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01c335ce-0171-4d6a-9f0b-3ce6ad7d0848_4096x1382.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Reading DynamoDB Streams using Lambda functions</figcaption></figure></div><p>Going with this approach would have a couple of advantages. The polling logic for data streams is automatically managed by Lambda functions, removing the need for manual implementation. Lambda functions provide built-in autoscaling capabilities, handling varying workloads efficiently without manual intervention. When using Lambda functions, you avoid the costs associated with making GetRecords calls to the streams, as this is handled by the AWS infrastructure.</p><p>On the other hand, there are significant additional costs involved, with an estimated expense of approximately $194 per month when handling 100 tables. The setup process requires extra effort, introducing changes to the deployment topology and adding complexity when establishing new data pipelines. Observability is limited as CloudWatch is the only available tool for monitoring and understanding what occurs within Lambda functions. Lambda functions must be designed to execute quickly, with minimal processing time, making them unsuitable for complex or long-running operations within the function logic.</p><p><strong>Using the DynamoDB Streams Kinesis Adapter</strong></p><p>The Amazon Kinesis Adapter is Amazon's recommended solution for consuming DynamoDB streams. However, a significant limitation is that this adapter is exclusively available in Java, making it unsuitable for non-Java applications. The Kinesis Client Library (KCL) offers a higher level of abstraction above the basic Kinesis Data Streams API, making the development process more straightforward and efficient. It provides robust batch processing capabilities, enabling efficient handling of multiple records simultaneously. The library includes automatic checkpointing functionality, maintaining accurate records of which data has been processed without requiring manual tracking.</p><p>The most significant limitation is that the DynamoDB Streams Kinesis Adapter is only implemented in Java and available through the awslabs/dynamodb-streams-kinesis-adapter repository, making it inaccessible for applications written in other programming languages.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0TuM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0TuM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 424w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 848w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0TuM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png" width="534" height="475.31868131868134" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1296,&quot;width&quot;:1456,&quot;resizeWidth&quot;:534,&quot;bytes&quot;:225159,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0TuM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 424w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 848w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!0TuM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F065f2df7-bf4f-4ab2-922a-653c8e9412fe_1550x1380.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Directly through the Dynamo Streams APIs using the AWS SDK</strong></p><p>The third option is to have a service that constantly polls the streams for new records using the available aws sdk for go. This has a couple of implications, we need to keep track of an index to know which records have been processed so far and due to the replicated nature of our systems, we need an idempotent way to achieve this.</p><p>And some things worth considering:</p><ul><li><p>Streams have rate limits - the <em>DescribeStream </em>endpoint can be called at a maximum rate of 10 times per second; the <em>GetRecords </em>endpoint can be be called at 5 TPS per stream shard or 2MB/s, whichever comes first (<a href="https://github.com/aws/aws-sdk-java-v2/issues/470">source</a>). Therefore, we cannot read more than 10 streams per sec.</p></li><li><p>You cannot have more than one active stream per table.</p></li><li><p>DynamoDB streams guarantee <strong>no duplicates </strong>and<strong> ordering of records</strong>.</p></li></ul><h3>Implemented Solution </h3><p>Most of the time reinventing the wheel is the worst solution you can choose. But what do you do when the current company policies and/or ecosystem makes it hard to integrate these libraries or this new set up in particular? Do you use the existing wheel but build a completely special car to work with it, or build a new wheel that fits into your existing car? All of the sudden, crafting this new wheel sounds like the easiest option. So here we are, reinventing the wheel because&#8230;it looked easy enough.</p><h3>Polling Processing Logic</h3><p>Before going into the high-level overview on how we&#8217;re using the aws go sdk to continuously poll the Dynamo Streams APIs, a couple of key concepts on how DynamoDB Streams work (which took us quite a lot of searching, digging and calls with AWS Support):</p><ul><li><p>We can have 2 types of shards: <strong>OPEN</strong> and <strong>CLOSED.</strong> </p></li><li><p>There is a one to one mapping between the dynamodb table partition and an open shard.</p></li><li><p>A shard is <strong>OPEN</strong> if <em><strong>EndingSequenceNumber == nil </strong></em>and <strong>CLOSED</strong> otherwise.</p></li><li><p>A shard can have (a lot of) successor shards (but every shard can only 1 child)</p><ul><li><p>for <strong>CLOSED</strong> shards, you can iterate through all its successors until you get to the end denoted by <em><strong>NextShardIterator == nil.</strong></em></p></li><li><p>for <strong>OPEN</strong> shards, <em><strong>NextShardIterator </strong></em>is never<em><strong> nil.</strong></em></p></li></ul></li><li><p>Every shard (parent or child) can contain records and the <em><strong>SequenceNo</strong></em> is a pointer to that shard record.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WajZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WajZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 424w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 848w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 1272w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WajZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png" width="6395" height="3009" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:3009,&quot;width&quot;:6395,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1083982,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WajZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 424w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 848w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 1272w, https://substackcdn.com/image/fetch/$s_!WajZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fee2280d7-9812-4d37-9e67-769dbd06f5ae_6395x3009.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Overview of the main processing logic</figcaption></figure></div><p>We will store some shard/record information to be able to keep track of where we left off: </p><ul><li><p>a <em><strong>streams</strong></em> table - containing the <em><strong>table_name</strong></em> (which is the dynamodb table name we&#8217;re going to audit log), <em><strong>is_enabled</strong></em> (whether the dynamodb streams are currently enabled for it) and <em><strong>stream_arn</strong></em> (which is dynamodb stream identifier for the stream) </p></li><li><p>a <em><strong>streams_shards</strong></em> table - containing the <em><strong>stream_arn</strong></em>, <em><strong>shard_id </strong></em>(the identifier of the stream shard), <em><strong>sequence_no</strong></em> (the sequence number of the given shard which we will use to know when in the shard we left off processing, similar to how an index works) and <em><strong>status</strong></em> (which for us can be <em><strong>open</strong></em> meaning the shard is still open on dynamodb side, <em><strong>closed</strong></em> meaning the shard is closed on dynamodb shard but we still have records left to process and <em><strong>closedAndProcessed</strong></em> meaning the shard is closed on dynamodb side and we have processed all records)</p></li></ul><p>The way our processing logic will work is that given a stream_arn: </p><ul><li><p>we get a list of all the parent shards with their dynamodb statuses.</p></li><li><p>if it's dynamodb CLOSED:</p><ul><li><p>we check if we have already processed all records in it (where our status is <em><strong>closedAndProcessed</strong></em>) and if so skip</p></li><li><p>otherwise (where our status=closed) loop through all the successor shards until NextShardIterator == nil, process all the records in them and update shard status to <em><strong>closedAndProcessed</strong></em>. </p></li></ul></li><li><p>if OPEN:</p><ul><li><p>loop through the shards using NextShardIterator. </p></li></ul></li></ul><p>Our main issue is what do we do with open shards, since NextShardIterator is never nil? When do we know when to stop? At first, we made the assumption that if we get to a couple of consecutive shards with 0 length records, we can assume the shard is caught up. Unfortunately, this is not the case (confirmed with AWS Support team). Even if we encounter multiple consecutive shards with 0 records, it's generally recommended to continue iterating through subsequent shards, as we can have data in later shards. Now you would say, that&#8217;s not a big deal, just continuously loop and if you see new records, save those. To point out, you might not need extra logic here for making sure you&#8217;re not processing duplicate records, remember we&#8217;re always storing the map of shard_id with sequence_no. So when you&#8217;re processing a shard that&#8217;s already been seen, we&#8217;re going to start from the given sequence_no. </p><p>The problem with this approach is that we don&#8217;t have just a couple of dozens of successor shards for open shards, we proved to have a couple of thousands. A couple of thousands with zero records in them. This is for a table with quite a reasonable (not very high) throughput. Overall, this overcomplicates our approach quite a bit as the time it takes to complete doing a full read looking for new changes increased significantly. </p><p>How is dynamodb streams spinning successor shards so chaotically? Why do we have so many with zero records in them? Why aren&#8217;t records written sequentially, why do we have large &#8220;gaps&#8221; (zero length record shards) in between shards with data? Would love to get an answer to those questions. We ended up not getting one so far.</p><h3>Lessons Learnt</h3><p>Even though every project starts with an MVP to prove the feasibility of the solution, as it started in this case, it might be worth to do a test on a production scale environment. If we would have faked production workload on local/staging to test the MVP, maybe we would have noticed some of the issues and started asking questions at an earlier stage.</p><p></p>]]></content:encoded></item><item><title><![CDATA[Decentralised Social Media Explained: Inside Bluesky's Federated Architecture]]></title><description><![CDATA[On what makes a system decentralised and federated, Bluesky's current architecture and AT Protocol]]></description><link>https://blog.diana-enache.com/p/decentralised-social-media-explained</link><guid isPermaLink="false">https://blog.diana-enache.com/p/decentralised-social-media-explained</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 13 Dec 2024 15:42:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d747b79-df0f-468a-a0f7-1ca326adcd0d_2901x1547.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This article is part of <em><strong>&#8220;Under the Hood&#8221;</strong></em>, a fairly new article series where I aim to deep dive into how real world systems work. Checkout the latest ones in this series: <em><strong><a href="https://blog.theengineeringcompass.com/p/from-pixels-to-information-a-comprehensive">From Pixels to Information: A Comprehensive Guide on QR Codes</a></strong></em> and <em><strong><a href="https://blog.theengineeringcompass.com/p/advanced-data-structures-for-discrete">How to Cron: Advanced Data Structures for Discrete Event Simulation</a>. </strong></em></p><div><hr></div><p>This time, we&#8217;ll have a look at how Bluesky is setup and go through the novel approaches they use to solve certain challenges. As of December 2025, Bluesky has crossed the 20 million users mark while being maintained by no more than 15 engineers. Martin Kleppman, author of the &#8220;Designing Data Intensive Applications&#8221;, currently a technical advisor, published a paper<a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-1" href="#footnote-1" target="_self">1</a> outlining how Bluesky is functioning as a federated and decentralised social media platform. Gergely Orosz's has two good articles covering the company&#8217;s early days, culture and evolutionary architecture if you would like to check them out.<a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-2" href="#footnote-2" target="_self">2</a> In the meantime, let&#8217;s start with the basics.</p><h2>Centralised, decentralised and federated</h2><p><strong>Decentralised networks</strong> have no central authority or server, and all participants (nodes) are equal and have the same capabilities. Data and control are distributed across all nodes. If one node fails, the network continues to function. One example of a fully decentralised network is Bitcoin - no single entity controls it, and all participants maintain a copy of the ledger.</p><p><strong>Federated networks</strong> have multiple independent servers that can communicate with each other, where each server has its own administrator and user base. Users on different servers can interact with each other. Each server follows its own rules while adhering to a common protocol. For example, email providers (gmail, yahoo, and other providers) run their own servers but can communicate with each other via standard protocols.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QnhJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QnhJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 424w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 848w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 1272w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QnhJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png" width="578" height="170.41337386018236" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:388,&quot;width&quot;:1316,&quot;resizeWidth&quot;:578,&quot;bytes&quot;:43931,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QnhJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 424w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 848w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 1272w, https://substackcdn.com/image/fetch/$s_!QnhJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64f1de28-b5a6-4ec1-944a-36ea880c0a47_1316x388.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">Comparison between the three types of networks: centralised, decentralised and federated. Red nodes represent servers or hubs. Green nodes represent users. Solid lines show direct connections. Dashed lines show federation between servers.</figcaption></figure></div><p>A system can be both federated and decentralised. This is often called a <strong>federated decentralised network</strong>. The federation part provides standard protocols for communication, interoperability between different servers/instances and local administration and rules. The decentralisation part ensures no central authority controls the entire network, anyone can set up their own server/instance and there is no single point of failure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ggB3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ggB3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 424w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 848w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 1272w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ggB3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png" width="310" height="256.0031104199067" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ebec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1062,&quot;width&quot;:1286,&quot;resizeWidth&quot;:310,&quot;bytes&quot;:96682,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ggB3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 424w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 848w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 1272w, https://substackcdn.com/image/fetch/$s_!ggB3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febec0bc4-d77a-495b-8793-5a9844a4a104_1286x1062.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example of how a federated decentralised network would look like. Each cluster represents a federation of servers. Each federation has multiple servers (red nodes). Users (green nodes) are connected to different servers. Solid lines show direct connections within federations. Dashed lines aka standard protocols show federation connections between groups.</figcaption></figure></div><h2>In which category should X/Mastodon be part of?</h2><p>X maintains complete centralised control over its platform. They have exclusive authority over your personal data, your identity, and content management (which includes both moderation policies and feed generation). While X has taken steps toward transparency by partially open-sourcing their algorithm<a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-3" href="#footnote-3" target="_self">3</a>, users remain largely passive observers - able to understand parts of how the system works but with minimal ability to influence or modify its operation.</p><p>Mastodon is federated because different servers can communicate with each other using the ActivityPub protocol. It's partially decentralised because no one owns or controls the entire network and anyone can set up their own Mastodon server. If one server goes down, the rest of the network continues functioning. The key difference from purely decentralised systems (like Bitcoin) is that these networks maintain some organisational structure through federation while still preventing central control. They strike a balance between complete decentralisation and practical usability. One disadvantage though, is that in the same way there are huge email providers, there are big Mastodon servers. Your user identity becomes tied to a server and it might get challenging (if not impossible) to move to another server. </p><h2>Bluesky architecture high-level overview</h2><p>Bluesky uses a protocol called the <strong>AT Protocol (Authenticated Transfer Protocol)</strong><a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-4" href="#footnote-4" target="_self">4</a> aka <strong>atproto</strong>, which works similar to email in some ways. Just like how email allows you to use Gmail but still message someone with a Yahoo account, Bluesky lets you choose different providers (called<strong> Personal Data Servers </strong>or<strong> PDS</strong>) to host your account, communicate with users on different servers, and take your account and data with you if you want to switch providers. </p><p>The key aspects through which Bluesky <em>encourages</em> decentralisation is that anyone can run their own server and connect to the network, users have data portability (can move their account between providers) and the protocol is open, meaning anyone can build compatible apps or services.</p><p>Let&#8217;s go through the main components that make the Bluesky&#8217;s architecture.</p><p>We have the <strong>PDS (Personal Data Server)</strong> which is the central part of the architecture. It hosts user repositories and data, manages user identity, orchestrates requests to other services, handles data sync and federation, can be self-hosted or provided (like Bluesky PDS). <strong>User Data Repository</strong> holds the <strong>User Identity (DID)</strong>, which is the decentralised identifier for authentication. It handles the cryptographic authentication, signing and verification and holds the records structure like posts, follows, likes and profile data. In order to consume data, aggregate it to your liking and distribute it in real time, you have the <strong>Network Relays</strong> which index and collect content from PDSs and then use <strong>Firehose</strong> to distribute it so that you can process updates in real time. The firehose data is consumed by the <strong>Feed Generator</strong> (<strong>Discover</strong>) to create custom content feeds and by the <strong>Labeling Service</strong> (<strong>Ozone</strong>) to provide content moderation, tagging and content classification. The data from those two Opinionated Services is consumed by the App Views, which acts as the user-facing platform.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!otZi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!otZi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 424w, https://substackcdn.com/image/fetch/$s_!otZi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 848w, https://substackcdn.com/image/fetch/$s_!otZi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 1272w, https://substackcdn.com/image/fetch/$s_!otZi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!otZi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png" width="2948" height="1564" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1564,&quot;width&quot;:2948,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:296646,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!otZi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 424w, https://substackcdn.com/image/fetch/$s_!otZi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 848w, https://substackcdn.com/image/fetch/$s_!otZi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 1272w, https://substackcdn.com/image/fetch/$s_!otZi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5a61cc1-7392-4ca9-b0b3-b42ea5fb3779_2948x1564.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">High level overview of Bluesky&#8217;s architecture</figcaption></figure></div><h2>Interoperation and federability</h2><p>The protocol uses a system called <strong>"Lexicons"</strong>, these are like agreed-upon vocabularies that define how different types of data (posts, likes, follows, etc.) should be structured. This standardization allows different servers and clients to understand each other, similar to how different email clients all understand what an email header should look like.</p><p>Each server supports specific features by implementing "lexicons" - there are core ones (starting with <code>com.atproto.*</code>) that handle basic stuff like syncing user data, and social-focused ones (starting with <code>app.bsky.*</code>) that enable social media features.</p><p>Unlike the regular web, which shares HTML pages that determine both content and appearance, the AT Protocol only shares the underlying data in a structured format. This means apps can display the same information in their own unique ways, without needing to download and run someone else's display code (HTML/JS/CSS). It's like getting pure information that each app can then present however it wants. <a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-5" href="#footnote-5" target="_self">5</a></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dioQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dioQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 424w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 848w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 1272w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dioQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png" width="1652" height="735" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:735,&quot;width&quot;:1652,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:78526,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dioQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 424w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 848w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 1272w, https://substackcdn.com/image/fetch/$s_!dioQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89063c15-62ff-401f-8d6b-853373b38e4c_1652x735.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2>The user data repository</h2><p>A noteworthy feature is <strong>repositories</strong> ("<strong>repos</strong>"). Each user has their own personal repository that contains all their data - posts, follows, likes, etc. These repos are designed to be efficiently synced between servers, maintain a verifiable history of changes and allow users to take their entire social presence with them if they switch providers. </p><p>The AT Protocol (ATP) has some interesting technical characteristics that enable decentralisation. The core of ATP is built around something called <strong>"Self-Authenticating Data Structures"</strong>. Every post, profile, and interaction is signed cryptographically, which means data can be verified as authentic regardless of which server it comes from, users truly own their identity through cryptographic keys and content can't be tampered with without detection.</p><p>An interesting technical choice is that ATP uses <strong>DIDs (Decentralised Identifiers)</strong> as the foundation for user identity, which are persistent identifiers that remain valid even if you change providers. </p><p>Every user has two identifiers: </p><ul><li><p>A human-readable handle (like username.bsky.social or custom domain)</p></li><li><p>A permanent DID (Decentralised Identifier, like did:plc:xyz123...)</p></li></ul><pre><code>User Handle (e.g., alice.bsky.social)
       &#8595;
Resolves to DID (did:plc:xyz123...)
       &#8595;
DID looks up in PLC server
       &#8595;
Returns DID Document containing:
- Public key
- PDS server location
- Handle verification</code></pre><p>Users can change their handle without losing their identity because the DID remains constant. The DID document points back to your handle, creating a verification loop. Any changes to the DID document must be signed with your private key and create a verifiable chain of updates. All updates require cryptographic signatures. Changes create a verifiable chain (like a "mini-blockchain"). Public data is self-verifiable. Third parties can detect manipulation. </p><p>Some limitations with the current approach are that it relies on centralised DNS system, there&#8217;s currently a single PLC server managed by Bluesky and most users' private keys are currently managed by Bluesky.</p><h2>How decentralised and federated is Bluesky actually? </h2><p>Bluesky doesn't use a traditional message-passing system like other federated networks (ActivityPub, XMPP, email). Instead, it uses a "shared heap" architecture where data exists in one global pool. There's no direct message-passing between hosts, which is a common federation pattern. Most users are currently hosted on Bluesky's PDS (Personal Data Server) instances. The chat/DMs service is completely centralised and most services rely on Bluesky's relay as a firehose.</p><p>The platform is not yet fully decentralised as the power is not evenly diffused throughout the system, most users run the Bluesky app, most developers work with Bluesky-defined application schemas (Lexicons), most self-hosted users run Bluesky's PDS software. Running a full-network relay requires significant resources (16TB of fast NVMe disk). Most DID PLC accounts don't have independently controlled PLC rotation keys configured.</p><blockquote><p><em>One thing I would not debate is that much of the protocol and the network is reliant on Bluesky in material terms today. Almost everybody in the atproto network is hosted on a Bluesky PDS instance. Most self-hosted folks run the Bluesky PDS software. Most services use a Bluesky relay as a firehose. Most users run the Bluesky app. Most developers are working with the Bluesky-defined application schemas (Lexicons). - Bryan Newbold, Protocol Engineer at Bluesky </em><a class="footnote-anchor" data-component-name="FootnoteAnchorToDOM" id="footnote-anchor-6" href="#footnote-6" target="_self">6</a></p></blockquote><p>While Bluesky's underlying protocol (atproto) <strong>is designed to support decentralisation</strong>, in practice most of the network currently runs through Bluesky the company. Almost everyone uses Bluesky's servers, app, and relay services - making it functionally similar to Twitter/X today. The key difference is in the architecture.</p><p>It allows for <em>"credible exit"</em>, users can leave without losing their data or connections. It Supports independent Lexicons (like the whtwnd.com blogging platform). Every major infrastructure component can be substituted without friction. Users can self-host their own instances. Lets users own their identity and data. The protocol is designed to prevent any single party from moderating the entire network.</p><p>So, unlike Twitter, Bluesky's infrastructure is built to allow users and developers to run their own independent services, migrate their data, and participate in the network without relying on Bluesky's systems. This capability exists even though relatively few are using it right now.</p><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-1" href="#footnote-anchor-1" class="footnote-number" contenteditable="false" target="_self">1</a><div class="footnote-content"><p><a href="https://arxiv.org/pdf/2402.03239">Bluesky and the AT Protocol: Usable Decentralised Social Media</a></p></div></div><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-2" href="#footnote-anchor-2" class="footnote-number" contenteditable="false" target="_self">2</a><div class="footnote-content"><p><a href="https://newsletter.pragmaticengineer.com/p/bluesky">Building Bluesky: a Distributed Social Network (Real-World Engineering Challenges)</a></p></div></div><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-3" href="#footnote-anchor-3" class="footnote-number" contenteditable="false" target="_self">3</a><div class="footnote-content"><p><a href="https://github.com/twitter/the-algorithm">X&#8217;s Recommendation Algorithm</a></p></div></div><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-4" href="#footnote-anchor-4" class="footnote-number" contenteditable="false" target="_self">4</a><div class="footnote-content"><p><a href="https://atproto.com/">AT Protocol Documentation</a></p></div></div><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-5" href="#footnote-anchor-5" class="footnote-number" contenteditable="false" target="_self">5</a><div class="footnote-content"><p><a href="https://atproto.com/guides/overview#interoperation">AT Protocol Interoperation Overview Guide</a></p></div></div><div class="footnote" data-component-name="FootnoteToDOM"><a id="footnote-6" href="#footnote-anchor-6" class="footnote-number" contenteditable="false" target="_self">6</a><div class="footnote-content"><p><a href="https://whtwnd.com/bnewbold.net/3lbvbtqrg5t2t">Bryan Newbold&#8217;s Reply on Bluesky and Decentralisation</a></p></div></div>]]></content:encoded></item><item><title><![CDATA[How to Cron: Advanced Data Structures for Discrete Event Simulation]]></title><description><![CDATA[On algorithmic improvements in the event set management, from O(&#8730;n) to O(1).]]></description><link>https://blog.diana-enache.com/p/advanced-data-structures-for-discrete</link><guid isPermaLink="false">https://blog.diana-enache.com/p/advanced-data-structures-for-discrete</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Mon, 09 Dec 2024 16:49:41 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/a94c23c0-15b8-453f-b19b-2fefcecfb027_2376x1388.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Recently, I was working on a service whose main responsibility was to schedule tasks and trigger their run at regular intervals. As with all projects, I check to see what's out there and if there are solutions that have been proved better and more efficient than what I already had in mind. So, I started looking into how the unix cron is doing its scheduling and stumbled upon a couple of interesting data structures that I wanted to take a deep dive into. Here's what I hope is a successful summary.</p><h2>The early cron days</h2><p>The original cron was designed by Ken Thompson in Unix V7 (around 1979) and was later rewritten by Dennis Ritchie. It used to work in a simple way. It would read the crontab files from <code>/usr/lib/crontab</code>, used a simple in memory table for job scheduling and had one main process that woke up every minute and determine if any commands must run at the current date and time, and if so, run them as the superuser, root.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0s7G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0s7G!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 424w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 848w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 1272w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0s7G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png" width="2683" height="873" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:873,&quot;width&quot;:2683,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:106976,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0s7G!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 424w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 848w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 1272w, https://substackcdn.com/image/fetch/$s_!0s7G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffadc2c66-ac94-425c-ab0e-8ab47d96adb1_2683x873.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Unix V7 Cron Scheduling</figcaption></figure></div><p>This method presented a couple of challenges. It could easily overload the system if too many jobs ran simultaneously. It consumed resources every minute whether there was a job to be run or not. Performance degraded with more entries. It wasn&#8217;t suitable for large systems with many users. Memory usage grew linearly with number of jobs. There was no way to limit the number of jobs per user.</p><p>These limitations led to the development of more sophisticated cron implementations for multi-user capability.</p><h2>The Franta-Maly Event List Approach</h2><p>The Unix System V came with an upgraded version of cron based on the 1977 paper by W.R. Franta and Kurt Maly, <em>An Efficient Data Structure for the Simulation Event Set</em>. [1] In this paper, they presented a new event scheduling algorithm that improves on previously published algorithms. Robert Brown, a Purdue graduate student, noticed similarities between cron and discrete event simulators, leading him to implement the Franta-Maly event list manager. In 1979, Keith Williamson, a new graduate student, developed Brown's prototype into a production-ready multi-user cron service at Purdue.</p><p>The cron algorithm begins by searching for <code>.crontab</code> files in all users' home directories at startup. For each discovered crontab file, it calculates the next future execution time for each command and adds these commands to the Franta-Maly event list, along with their execution times and five-field time specifiers. The main loop then continuously examines the first task in the queue, calculating its future execution time. The system sleeps until that time, then wakes to verify the time and execute the queued task in the background using the original user's privileges. After execution, it recalculates the next runtime for that command and reinserts it into the event list accordingly.</p><p>The daemon implements two monitoring mechanisms for crontab file changes: it processes SIGHUP signals to trigger immediate rescans of modified crontab files, and it maintains scheduled wake-up checks at 30-minute intervals (on the hour and half-hour) to detect any crontab modifications. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oxEk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oxEk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 424w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 848w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 1272w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oxEk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png" width="5870" height="8819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:8819,&quot;width&quot;:5870,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1932703,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oxEk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 424w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 848w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 1272w, https://substackcdn.com/image/fetch/$s_!oxEk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96caaefc-9565-4fe1-8478-72d1b5d597f5_5870x8819.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Unix System V Cron Scheduling based on the Franta-Maly event list</figcaption></figure></div><p>The Franta-Maly event list (Two-Level algorithm) has a time complexity of <strong>O(&#8730;n)</strong> in the worst case scenario, driven by its two-tier data structure design and event insertion mechanism.</p><h4>The Two-Level (TL) Algorithm Data Structure</h4><p>The Two-Level (TL) algorithm proposed by Franta and Maly utilises a sophisticated data structure to efficiently manage event notices in discrete event simulations. The primary goal of this structure is to reduce the number of comparisons required to find the correct position to insert a new event notice, thus optimising the simulation's performance. The algorithm utilises a data structure inspired by indexed lists and balanced tree concepts (3-2 trees), to minimise the computational cost of scheduling and processing events.</p><p>The data structure consists of 3 layers:</p><ul><li><p><strong>the index list layer</strong> - The TL algorithm uses an index list to divide the range of event times into a number of equal-sized intervals. Each element in the index list points to a "dummy key" which represents the boundary of an interval.</p></li><li><p><strong>the secondary key layer</strong> - The efficiency of the TL algorithm lies in the introduction of secondary keys within each interval. These keys, associated with the right boundary dummy key, point to sublists of event notices within the interval<strong>. </strong>This two-level indexing scheme enables more efficient searching and insertion operations.</p></li><li><p><strong>the event notices layer</strong> - Each interval is further divided into sublists, each headed by a secondary key. The sublists are designed to be balanced, meaning they don't grow excessively large. The maximum size of each sublist is controlled by <strong>nlim</strong> parameter.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-7im!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-7im!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 424w, https://substackcdn.com/image/fetch/$s_!-7im!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 848w, https://substackcdn.com/image/fetch/$s_!-7im!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 1272w, https://substackcdn.com/image/fetch/$s_!-7im!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-7im!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png" width="4829" height="1595" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1595,&quot;width&quot;:4829,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:405897,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-7im!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 424w, https://substackcdn.com/image/fetch/$s_!-7im!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 848w, https://substackcdn.com/image/fetch/$s_!-7im!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 1272w, https://substackcdn.com/image/fetch/$s_!-7im!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff52f4b66-7afc-40ba-b064-9aa99acc7235_4829x1595.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Representation of the Two-Layer/ Franta Maly Event List</figcaption></figure></div><p><strong>The Insert Operation - Worst Case Complexity: O(&#8730;n)</strong></p><p>Let&#8217;s say we want to insert a new event with t=1.3. The Franta-Maly insertion process follows a systematic sequence through the data structure's layers. First, we calculate the target interval by applying the formula i = [(t - lowerbound)/dt] to determine the precise interval containing the event time, which leads us to the corresponding dummy key entry in the index list.</p><p>Next, we traverse the sequence of secondary keys associated with the identified dummy key until locating the appropriate insertion point. For an event with time t=1.3, this involves scanning until we find it belongs between secondary keys 1.2 and 1.5.</p><p>The actual insertion occurs by placing the event notice in its correct time-ordered position within the identified sublist. In our example, the new event with t=1.3 is inserted between existing events with times 1.2 and 1.4, maintaining the temporal ordering of the sublist.</p><p>Finally, we execute a balance check to maintain the data structure's efficiency. The system verifies if the insertion has caused the sublist to exceed its maximum size (nlim). When the size limit is exceeded, the structure triggers one of two rebalancing operations: either transferring the last notice to an adjacent list or creating a new secondary key. If the sublist remains within size constraints, the insertion completes without requiring rebalancing.</p><p>The worst case complexity occurs when all n notices are associated with the same dummy key and the new event being inserted has a time smaller than all other notices in the structure. The complexity is calculated as: <strong>cw(n) = n/nlim + nlim</strong>,<strong> </strong>where nlim is optimally set to &#8730;n to minimize the worst case complexity.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rcUc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rcUc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 424w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 848w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 1272w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rcUc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png" width="5031" height="1826" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1826,&quot;width&quot;:5031,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:532565,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rcUc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 424w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 848w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 1272w, https://substackcdn.com/image/fetch/$s_!rcUc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcacdc3c7-a00c-47d9-a795-6d016e3c3de0_5031x1826.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Representation of how inserting a new event looks like</figcaption></figure></div><p><strong>The Search Operation -</strong> <strong>Worst Case Complexity: O(&#8730;n)</strong></p><p>Let&#8217;s say we want to search for an event with t=1.3.</p><p>The search process in the Franta-Maly structure executes in three sequential phases. The first phase calculates the target interval using the formula i = [(t - lowerbound)/dt], which maps directly to the corresponding dummy key in the index list. This initial lookup operates in constant time O(1).</p><p>The second phase traverses the secondary key sequence within the identified interval until locating the appropriate sublist. For a target time t=1.3, the algorithm scans between secondary keys 1.2 and 1.5, ultimately selecting SK2's sublist. This traversal has a worst-case complexity of O(&#8730;n).</p><p>The final phase executes a linear search within the selected sublist to find the exact match or appropriate position. Since the maximum sublist size is constrained by nlim (&#8730;n), this search also operates with worst-case complexity O(&#8730;n). The combination of these three phases yields an overall worst-case complexity of O(&#8730;n), though practical performance often approaches constant time.</p><h2>Calendar Queues (CQ)</h2><p>A calendar queue is a priority queue data structure specifically designed for efficient handling of time-based events. It was introduced by <em>Randy Brown in 1988 </em>[2] and is particularly useful in discrete event simulation systems. </p><p>The queue is organized like a calendar, divided into "buckets" (like days or time slots). Each bucket represents a fixed-width time interval. Events within each bucket are sorted by their timestamp. The buckets are arranged in a circular array (wrapping around like a year calendar)</p><p>The data structure consists of 4 components:</p><ul><li><p><strong>Buckets:</strong> An array representing days in a year, each element pointing to a sorted linked list of events scheduled for that day<strong>.</strong></p></li><li><p><strong>Event Nodes:</strong> Contain information about the event, including its time (priority) and any associated data. These are organised into sorted linked lists within each bucket.</p></li><li><p><strong>Circular Calendar:</strong> Events can be scheduled up to a year in advance. Once the last day of the year is reached, the calendar wraps around to the first day, allowing for continuous event scheduling without data structure manipulation<strong>2</strong>.</p></li><li><p><strong>Event Dates:</strong> Each event node stores its scheduled date to handle events scheduled beyond a year. Events with future dates are skipped during dequeue operations until the calendar reaches their scheduled year</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!p1wv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!p1wv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 424w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 848w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 1272w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!p1wv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png" width="1456" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/22747f86-0032-425d-a892-b0b03657293f_2240x936.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:608,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!p1wv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 424w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 848w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 1272w, https://substackcdn.com/image/fetch/$s_!p1wv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22747f86-0032-425d-a892-b0b03657293f_2240x936.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Representation of Brown&#8217;s Calendar Queues</figcaption></figure></div><p>The above image shows how Brown&#8217;s Calendar Queue looks like, where the year starts at 12.0 and end at 16.0 and the bucket width is 0.5 time units. </p><h4><strong>How are events inserted?</strong></h4><p>Events are assigned to buckets using this formula:</p><pre><code>bucket_number = floor(priority/bucket_width) % number_of_buckets</code></pre><p>Let&#8217;s say we want to insert a new element 14.6 and we have the following initial state:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LcUa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LcUa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 424w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 848w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 1272w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LcUa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png" width="1456" height="345" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:345,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LcUa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 424w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 848w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 1272w, https://substackcdn.com/image/fetch/$s_!LcUa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52cefb96-93ed-4018-bb53-c729cbcaf4cc_2328x552.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We have the current year that starts at 12.0 and ends at 16.0 (range of 4.0 units), with a bucket width = 0.5 time units and 8 buckets. We calculate the </p><pre><code>bucket_number = floor(14.6/0.5)%8 = 29%8 = 5</code></pre><p>The element belongs to bucket 5 and will be inserted into sorted position into the bucket&#8217;s linked list.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E9to!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E9to!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 424w, https://substackcdn.com/image/fetch/$s_!E9to!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 848w, https://substackcdn.com/image/fetch/$s_!E9to!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 1272w, https://substackcdn.com/image/fetch/$s_!E9to!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E9to!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png" width="1456" height="325" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:325,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E9to!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 424w, https://substackcdn.com/image/fetch/$s_!E9to!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 848w, https://substackcdn.com/image/fetch/$s_!E9to!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 1272w, https://substackcdn.com/image/fetch/$s_!E9to!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F498f0a26-3f1d-4e30-bc43-c2568c346fc4_2476x552.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This insertion process maintains <strong>O(1)</strong> average complexity since bucket calculation is <strong>O(1)</strong>, each bucket contains a small, roughly constant number of events, and searching within a bucket's linked list is therefore bounded by a constant.</p><h4><strong>How is bucket width calculated?</strong> </h4><p>According to Brown's paper, the bucket width is calculated in a clever way to maintain efficiency: </p><ul><li><p>bucket width is adjusted each time the queue is copied onto a new calendar (when resizing)</p></li><li><p>events are sampled to find the average separation:</p><ul><li><p>For small queues (&#8804; 5 events): sample all events</p></li><li><p>For larger queues: sample 5 + size/10 events (up to max of 25 samples)</p></li><li><p>Dequeue these events, record their priorities, then re-insert them</p></li></ul></li></ul><ul><li><p>there&#8217;s a two-step average calculation:</p><ul><li><p>First calculate initial average separation between sampled events</p></li><li><p>Then recalculate average using only separations smaller than twice the initial average (this handles bimodal distributions by ignoring large gaps)</p></li></ul></li><li><p>and finally, the bucket width = 3.0 * final average</p></li></ul><p>The reasoning behind this method is that we aim to have 2-3 events per bucket on average. Multiplier of 3.0 was found experimentally to give best performance. Ignoring large gaps helps handle skewed/bimodal distributions. Using a sample rather than all events makes the calculation efficient. </p><h4>When does resizing occur?</h4><p>According to Brown's paper, resizing occurs based on queue size thresholds, more specifically: </p><ul><li><p>Growing (Doubling):</p><ul><li><p>Resize occurs when queue size &gt; 2 * number_of_buckets</p></li><li><p>The new calendar gets twice as many buckets</p></li><li><p>Example: If current size is 50 buckets, resize at 101 events</p></li></ul></li><li><p>Shrinking (Halving):</p><ul><li><p>Resize occurs when queue size &lt; number_of_buckets/2</p></li><li><p>The new calendar gets half as many buckets</p></li><li><p>Example: If current size is 50 buckets, resize at 24 events</p></li></ul></li></ul><p>During the resizing process, a new calendar array is created, a new bucket width is calculated using the sampling method, all events are copied to their new bucket positions and finally the switch to the new calendar is done.</p><p>Brown analyzed the impact of resizing, the average event is copied less than 2 times during its lifetime. In the worst case, the queue size oscillates between just above 2^n and just below 2^(n-1), and even in worst case, only about 3 events are copied per hold operation.</p><p>The key innovation of Brown's Calendar Queue was simplifying the two-level approach by using fixed-size buckets instead of dynamic sublists, eliminating the overflow problem through circular wrapping and providing automatic adaptation through bucket resizing. This made the Calendar Queue both simpler to implement and more efficient in practice compared to the TL algorithm, especially for larger queue sizes and varying event distributions.</p><h2>FELT - An optimisation to CQ</h2><p><em>One of the disadvantages of Brown&#8217;s CQs is that resize operation would involve creating a new CQ structure and then moving each item from the old CQ to the new CQ before discarding the old CQ. Hence, such resizes can be costly if the size of the queue is very large</em>. And this is what <em>Hui&#8217;s and Thng&#8217;s FELT (Far Future Event Leaf Tree)</em> [3] structure is trying to solve. </p><p>FELT uses two different structures together to manage events:</p><ol><li><p>A regular calendar queue for near-future events (Primary Tier)</p></li><li><p>A special tree structure for far-future events (FELT) (Secondary Tier)</p></li></ol><p><strong>Primary Tier (Calendar Queue)</strong></p><p>Works like a normal calendar queue with a fixed maximum size (typically 4096 events)</p><p>The FELT primary Calendar Queue works like a normal calendar queue with dual thresholds. The UPPBOUND threshold, typically set at 4096 events, represents the maximum capacity of the primary queue. When exceeded, the system initiates a transfer operation, moving the oldest events into the secondary tree structure. This transfer specifically moves UPPBOUND minus LOWBOUND events, maintaining optimal queue size for performance.</p><p>Conversely, the LOWBOUND threshold, typically set at 256 events, establishes the minimum required event count in the primary queue. When the queue size drops below LOWBOUND, the system initiates a replenishment operation. During replenishment, the system identifies the leaf node containing events with the earliest timestamps in the secondary tier, transfers its entire contents to the primary queue, and removes the depleted leaf node. This process ensures the primary queue maintains sufficient events for efficient operation.</p><p><strong>Secondary Tier (Tree Structure)</strong></p><p>FELT acts as a <strong>semi sorted binary tree</strong>, where the left branch always holds events with higher priority (earlier timestamps) than the right branch. Only the <strong>leaf nodes</strong> store events. These events are kept in an unsorted linked list called <strong>NodeList</strong>. <strong>Parent nodes</strong> act as branching points. They do not store events themselves but maintain information about the minimum timestamp (<strong>MinTimeStamp</strong>) of all events in the branches below them. This helps efficiently route new events to the correct leaf node during insertion. </p><p>FELT is size-based, meaning it manages events based on the number of events stored in the primary CQ. This contrasts with time-based approaches like the Future Event Tree (FET) used in the Dynamic Lazy Calendar Queue (DLCQ), which can lead to inefficiencies when the CQ resizes.</p><pre><code><code>                   [Parent Node(PN): MinTimeStamp(t) = X]
                         /                 \
    [Parent Node: MinTimeStamp = Y]    [Parent Node: MinTimeStamp = Z]
             /   \                                 /  \
 [Leaf Node(LN): MinTimeStamp = A] ... ... [Leaf Node: MinTimeStamp = B]
               |                                    |
[Unsorted Linked List of Events(ULLE)]          [(ULLE)]</code></code></pre><h4>How are events inserted?</h4><p>FELT processes each new event through a two-stage decision process for optimal placement. Initially, the system examines the event's timestamp to determine whether it belongs in the near-future or far-future category. This classification happens by comparing the event's timestamp against a threshold calculated as the current simulation time plus one calendar period.</p><p>For near-future events, the system directs them into the primary calendar queue using standard calendar queue insertion operations. However, if this insertion causes the primary queue to exceed its size threshold, the system automatically transfers the oldest events from the primary queue into the secondary tree structure to maintain optimal queue performance.</p><p>Far-future events take a different path, entering the secondary tree structure where they navigate through internal nodes based on timestamp comparisons. The event travels down the tree, with each internal node guiding it left or right until it reaches an appropriate leaf node. Once at the leaf node, the event is simply appended to an unsorted list maintained at that node:</p><ol><li><p><strong>Start at the Top Node:</strong> The insertion process begins at the topmost node of the FELT structure (<strong>TopNode</strong>).</p></li><li><p><strong>Check if Current Node is a Leaf Node:</strong></p><ol><li><p><strong>If the current node is a leaf node:</strong> The event is inserted into the unsorted linked list (<em>NodeList</em>) at that leaf node.</p></li><li><p><strong>If the current node is a parent node:</strong> Proceed to step 3.</p></li></ol></li><li><p><strong>Compare Event Timestamp with Right Child's MinTimeStamp:</strong></p><ol><li><p><strong>If the event's timestamp is greater than or equal to the MinTimeStamp of the right child node, AND the right child node is not NULL:</strong> Move to the right child node and repeat step 2.</p></li><li><p><strong>Otherwise:</strong> Proceed to step 4.</p></li></ol></li><li><p><strong>Check Left Child:</strong></p><ol><li><p><strong>If the left child node is not NULL:</strong> Move to the left child node and repeat step 2.</p></li><li><p><strong>Otherwise:</strong> Insert the event into the <em>NodeList</em> of the current node (which must be a leaf node).</p></li></ol></li><li><p><strong>Check for Node Splitting:</strong> After the event is inserted, the algorithm checks if the leaf node has exceeded its maximum capacity (MaxSize). When a leaf node has reached its maximum capacity (MaxSize) of events that it can store, which is set a priori, it will spawn two new leaf nodes and split NodeList into 2 and transfer the events into the left and right leaf nodes.</p><ol><li><p><strong>If the node needs splitting:</strong> The splitNode function is called to create two new child leaf nodes and redistribute the events based on their timestamps.</p></li></ol></li></ol><pre><code><code>                  [Parent Node(PN): MinTimeStamp(t) = 10]
                    /                             \
               [PN:t = 5]                     [PN:t = 15]
                /   \                             /   \
       [LN:t = 2]   [LN:t = 7]          [LN:t = 12]   [LN:t = 17]
          |              |                  |           |
     [(ULLE)]         [(ULLE)]          [(ULLE)]      [(ULLE)]</code></code></pre><p>New Event (Timestamp = 8)  </p><ol><li><p>Start at TopNode (MinTimeStamp = 10).  </p></li><li><p>8 &lt; 15 (Right Child's MinTimeStamp), so move to Right Child (MinTimeStamp = 12).</p></li><li><p>Left Child is NULL, so insert event into current node. </p></li></ol><pre><code><code>Result:
                  [Parent Node(PN): MinTimeStamp(t) = 8]
                    /                             \
               [PN:t = 5]                     [PN:t = 8]
                /   \                             /   \
       [LN:t = 2]   [LN:t = 7]          [LN:t = 8]   [LN:t = 17]
          |              |                  |           |
     [(ULLE)]         [(ULLE)]          [(ULLE)]      [(ULLE)]
                                            |   
                                 [New Event (Timestamp = 8)]</code></code></pre><p>When it comes to complexity, the tree traversal is <strong>O(log n)</strong> in best case when tree is balanced and <strong>O(n)</strong> when it&#8217;s a completely skewed tree.</p><p>While the theoretical worst case is <strong>O(n)</strong>, the actual performance in practice tends toward <strong>O(1)</strong> because: the size-based splitting keeps the tree reasonably balanced. The large maxSize (3840) means splits are rare, no sorting is required within leaf nodes and the split cost is bounded by a constant.</p><p>The authors show empirically that FELT maintains near O(1) performance even for large queue sizes and skewed event distributions, performing better than other approaches like DLCQ.</p><h2>The Snoopy CQ</h2><p>The Snoopy Calendar Queue, proposed by <em>Tan, Kah Leong, and Li-Jin Thng. "SNOOPy Calendar Queue."</em> [4], builds upon the concepts of the conventional Calendar Queue (CQ) and the Dynamic Calendar Queue (DCQ), addressing their limitations, particularly in handling skewed event distributions. SNOOPy CQ extends the calendar queue with statistical optimization mechanisms. Instead of using sampling for bucket width calculation like traditional CQ, it tracks operational costs and uses these statistics to optimize queue parameters. The name "SNOOPy" stands for <em>Statistically eNhanced with Optimum Operating Parameter Calendar Queue</em>. The core of SNOOPy CQ lies in its ability to dynamically adjust its operating parameter &#8211; the bucket width &#8211; based on the queue's performance statistics. This adaptive mechanism ensures consistent and efficient performance, achieving near-O(1) complexity even in challenging scenarios.</p><pre><code><code>                       +-----------------------+
                       |      SNOOPy CQ        |
                       +-----------------------+
                                 |  ^
                                 |  |
                                 v  |
                       +-----------------------+
                       |  Enqueue/Dequeue      |
                       |  Operations           |
                       +-----------------------+
                                  |  ^
                                  |  |
                                  v  |
                       +-----------------------+
                       | Performance Statistics|
                       |     (CE, CD)          |
                       +-----------------------+
                                  |  ^
                                  |  |
                                  v  |
      +---------------+-----------------------+----------------+
      |               |                       |                |
      v               v                       v                v
  +-------------+-------------------+--------------+--------------+
  |Trigger Check|  Bucket Width     |Calendar Resize|   Cost       |
  | (CE, CD,    |  Optimization     |  (BW, NB)     |  Calculation |
  | Thresholds) |(Minimize CE + CD) |               |  (CE, CD)    |
  +-------------+-------------------+---------------+--------------+</code></code></pre><ol><li><p><strong>Enqueue/Dequeue Operations:</strong> These are the fundamental operations performed on the SNOOPy CQ. Events are enqueued (added to the queue) based on their timestamps, and the event with the earliest timestamp is dequeued (removed from the queue) for processing.</p></li><li><p><strong>Performance Statistics Collection:</strong> As events are enqueued and dequeued, SNOOPy CQ meticulously tracks two crucial performance metrics:</p><ol><li><p><strong>Average Enqueue Cost (CE):</strong> This represents the average number of events traversed before an insertion can be made in a bucket's linked list.</p></li><li><p><strong>Average Dequeue Cost (CD):</strong> This signifies the average number of buckets that need to be examined before finding the event with the earliest timestamp.</p></li></ol></li><li><p><strong>Trigger Check:</strong> SNOOPy CQ continuously monitors the collected performance statistics (CE and CD). It employs multiple trigger mechanisms:</p><ol><li><p><strong>Inherited from CQ and DCQ:</strong> These triggers are based on thresholds for CE and CD (e.g., if either exceeds a value of 2 or 3), as in the CQ and DCQ structures.</p></li><li><p><strong>Unique to SNOOPy CQ:</strong> Additional triggers are based on the relative difference between CE and CD over multiple slots (time intervals corresponding to a certain number of enqueue or dequeue operations). If the difference exceeds a predefined factor, it signals a need for adjustment.</p></li></ol></li><li><p><strong>Bucket Width Optimization:</strong> When any of the trigger conditions are met, indicating a suboptimal bucket width, SNOOPy CQ initiates its bucket width optimization process. The goal is to find a new bucket width (BW) that minimizes the total cost (CE + CD), keeping the number of buckets (NB) constant. SNOOPy CQ employs a novel approach to determine the optimum BW based on the relationship between CE and CD, as detailed in section 3.1 of "download.pdf".</p></li><li><p><strong>Calendar Resize:</strong> Once the optimal bucket width is calculated, the SNOOPy CQ undergoes a resize operation. A new calendar queue is created with the calculated BW and the same NB as before. Events from the old calendar are then transferred to their appropriate buckets in the new calendar.</p></li><li><p><strong>Cost Calculation:</strong> After each slot (a series of enqueue or dequeue operations), the average enqueue cost (CE) and average dequeue cost (CD) are recalculated based on the accumulated statistics from that slot. These updated costs are then used in subsequent trigger checks and optimization processes.</p></li></ol><h2>Conclusion</h2><p>Starting from the simple Unix V7 cron implementation, each subsequent event scheduling structure addressed specific limitations of its predecessors.</p><p>The Franta-Maly event list introduced sophisticated two-level indexing, achieving O(&#8730;n) complexity through its hierarchical structure. Brown's Calendar Queue (CQ) then simplified this approach with fixed-size buckets and circular wrapping, providing O(1) average performance for uniform distributions.</p><p>FELT built upon CQ's foundation by introducing a dual-tier architecture that efficiently manages near and far-future events, effectively addressing the resize overhead issues. Its size-based approach, rather than time-based, provides consistent performance across varying event distributions.</p><p>SNOOPy CQ represents the current state-of-the-art, employing statistical optimisation to dynamically adjust bucket widths based on operational costs. This adaptive approach ensures robust O(1) performance even under skewed distributions.</p><h2>Data Structures Comparison</h2><h3><strong>Franta-Maly Structure (1977)</strong></h3><h4><strong>Primary Components</strong></h4><ul><li><p>Employs a two-level structure based on indexed lists and the balanced terminal node structure of 3-2 trees.</p></li><li><p>Divides the range of event times into intervals, bounded by "dummy keys (notices)" pointed to by an index list. </p></li><li><p>Each interval further divided into sublists, marked by secondary keys associated with the right boundary dummy key.</p></li></ul><h4><strong>Key Operations Performance</strong></h4><p><strong>Insertion (O(&#8730;n) worst case):</strong></p><ul><li><p>Binary search index array to find appropriate terminal node</p></li><li><p>Linear search within terminal node for insertion position</p></li><li><p>Split terminal node if size exceeds 2&#8730;n</p></li><li><p>Update index pointers if split occurs</p></li></ul><p><strong>Deletion (O(&#8730;n) worst case):</strong></p><ul><li><p>Search index array for target terminal node</p></li><li><p>Remove event from terminal node</p></li><li><p>Merge underfull terminal nodes if necessary</p></li><li><p>Update index pointers after merge</p></li></ul><p><strong>Terminal Node Management:</strong></p><ul><li><p>Split Threshold: 2&#8730;n events</p></li><li><p>Merge Threshold: &#189;&#8730;n events</p></li><li><p>Maintains roughly balanced terminal node sizes</p></li></ul><h4><strong>Performance Bottlenecks</strong></h4><p><strong>Index Array Search:</strong></p><ul><li><p>Binary search cost increases with structure size</p></li><li><p>More frequent searches in skewed distributions</p></li></ul><p><strong>Terminal Node Operations:</strong></p><ul><li><p>Linear search within nodes</p></li><li><p>Split/merge overhead with uneven distributions</p></li></ul><p><strong>Maintenance Overhead:</strong></p><ul><li><p>Index updates after node modifications</p></li><li><p>Balance maintenance across terminal nodes</p></li></ul><h4><strong>Advantages, Limitations and Selection Criteria</strong></h4><p><strong>Advantages:</strong></p><ol><li><p>Bounded Operations:</p><ol><li><p>Guaranteed O(&#8730;n) worst case</p></li><li><p>Predictable performance characteristics</p></li></ol></li><li><p>Memory Locality:</p><ol><li><p>Events clustered in terminal nodes</p></li><li><p>Efficient cache utilisation for sequential access</p></li></ol></li><li><p>Flexible Structure:</p><ol><li><p>Adapts to varying event distributions</p></li><li><p>Self-balancing capabilities</p></li></ol></li></ol><p><strong>Consider Using When:</strong></p><ol><li><p><strong>Predictable Performance is critical:</strong></p><ol><li><p>Operations must have guaranteed bounds</p></li><li><p>Worst-case performance matters more than average case</p></li></ol></li><li><p><strong>Memory Access Patterns are important:</strong></p><ol><li><p>System benefits from clustered event storage</p></li><li><p>Cache efficiency is a priority</p></li></ol></li></ol><p><strong>Limitations: </strong></p><ol><li><p>Complex Implementation:</p><ol><li><p>Significant code complexity for node management</p></li><li><p>Error-prone split/merge operations</p></li></ol></li><li><p>Performance Issues:</p><ol><li><p>High overhead for small event sets</p></li><li><p>Degraded performance with skewed distributions</p></li></ol></li><li><p>Maintenance Overhead:</p><ol><li><p>Frequent index updates</p></li><li><p>Complex rebalancing operations</p></li></ol></li></ol><p><strong>Avoid Using When:</strong></p><ol><li><p>Simple Implementation is preferred:</p><ol><li><p>Calendar queue or modern alternatives offer simpler solutions</p></li><li><p>Development time is constrained</p></li></ol></li><li><p>Optimal Average Performance is required:</p><ol><li><p>Calendar queue provides better average-case performance</p></li><li><p>Event distribution is relatively uniform</p></li></ol></li></ol><h3>Calendar Queue (Brown, 1988)</h3><h4><strong>Primary Components</strong></h4><p>Based on the analogy of a desk calendar, with each "day" represented by a sorted linked list. An array of pointers provides access to each day's linked list.</p><p><strong>Bucket Array:</strong></p><ul><li><p>Fixed-size array of bucket heads</p></li><li><p>Each bucket contains sorted linked list of events</p></li><li><p>Array size maintained as power of 2 for efficient modulo operations</p></li><li><p>Circular structure with wrap-around for year boundaries</p></li></ul><p><strong>Year Mechanism:</strong></p><ul><li><p>Year = NB * BW (Number of buckets * Bucket width)</p></li><li><p>Tracks current year for wrap-around handling</p></li><li><p>Enables infinite future time handling with finite structure</p></li></ul><h4><strong>Key Operations Performance</strong></h4><p><strong>Enqueue (O(n) worst case, O(1) average case):</strong></p><ul><li><p>Calculate virtual bucket using timestamp</p></li><li><p>Map to actual bucket using modulo</p></li><li><p>Insert in sorted order within bucket</p></li><li><p>O(n) complexity when all events are in one bucket</p></li></ul><p><strong>Dequeue (O(n) worst case, O(1) average case):</strong></p><ul><li><p>Start from lastBucket</p></li><li><p>Remove earliest event from current bucket</p></li><li><p>Advance to next bucket if empty</p></li><li><p>Handle year wrap-around when needed</p></li><li><p>O(n) when searching empty buckets</p></li></ul><p><strong>Resize (O(n)):</strong></p><ul><li><p>Triggered when queue size crosses thresholds:</p></li><li><p>Upsize: size &gt; 2 * nbuckets</p></li><li><p>Downsize: size &lt; nbuckets/2</p></li><li><p>Creates new bucket array with doubled/halved size</p></li><li><p>Recalculates bucket width by sampling</p></li><li><p>Redistributes all events</p></li></ul><h4><strong>Performance Bottlenecks</strong></h4><p><strong>Bucket Width Impact:</strong></p><ul><li><p>Too small: Events spread across too many buckets</p></li><li><p>Too large: Too many events per bucket</p></li><li><p>Optimal: 2-3 events per bucket near dequeue point</p></li></ul><p><strong>Distribution Effects:</strong></p><ul><li><p>Uniform: Achieves O(1) performance</p></li><li><p>Skewed: Performance degrades</p></li><li><p>Bimodal: May require frequent resizing</p></li></ul><h4><strong>Advantages, Limitations and Selection Criteria</strong></h4><p><strong>Advantages:</strong></p><ol><li><p>Uniform Distributions - for network simulation, regular event patterns, predictable timings etc</p></li><li><p>Medium-sized Queues - for 1,000 to 10,000 events, stable event counts, regular dequeue patterns</p></li></ol><p><strong>Limitations:</strong></p><ol><li><p>Skewed Distributions - lead to performance degradation, frequent resizing, bucket imbalance</p></li><li><p>Very Large Queues - lead to resize overhead, memory locality issues, width calculation challenges</p></li></ol><h3>FELT (Far Future Event Leaf Tree, 2002)</h3><h4><strong>Primary Components</strong></h4><p>Uses a Primary Tier (Calendar Queue) and a Secondary Tier (Tree Structure). <br><br><strong>Primary Tier (Calendar Queue):</strong></p><ul><li><p>Works like a normal calendar queue with a fixed maximum size (typically 4096 events)</p></li><li><p>Has both an upper limit (UPPBOUND) and lower limit (LOWBOUND) for number of events</p></li><li><p>When too full, moves events to the secondary tier</p></li><li><p>When too empty, gets events back from the secondary tier</p></li></ul><p><strong>Secondary Tier (Tree Structure):</strong></p><ul><li><p>Uses a binary tree where only leaf nodes store events</p></li><li><p>Leaf nodes contain unsorted lists of events</p></li><li><p>Parent nodes just help route to the right leaf node</p></li><li><p>Each node keeps track of the smallest timestamp in its subtree</p></li></ul><h4><strong>Key Operations Performance</strong></h4><p><strong>When Adding a New Event:</strong></p><ol><li><p>Decision Point: System first checks if the event is near-future or far-future</p></li><li><p>Near-Future Events:</p><ol><li><p>Go directly into the calendar queue</p></li><li><p>If this makes the queue too full, oldest events are moved to the tree</p></li></ol></li><li><p>Far-Future Events:</p><ol><li><p>Go into the tree structure</p></li><li><p>Get routed down the tree to the appropriate leaf node</p></li><li><p>Added to that leaf node's unsorted list</p></li></ol></li></ol><p><strong>Near-future events (Primary Calendar Queue): O(1)</strong> average case, as it uses standard calendar queue insertion<br><strong>Far-future events (Secondary Tree): O(log n)</strong> to traverse the tree to the correct leaf node, then O(1) to append to unsorted list<br><strong>Overall: O(1)</strong> for near events, O(log n) for far events<br><br><strong>When Retrieving Events:</strong></p><ol><li><p>Always take events from the calendar queue first</p></li><li><p>When calendar queue gets too empty:</p><ol><li><p>Find the leaf node with earliest events in the tree</p></li><li><p>Move those events to the calendar queue</p></li><li><p>Continue processing from calendar queue</p></li></ol></li><li><p>Primary Calendar Queue: O(1) average case</p><ol><li><p>When primary queue needs replenishment:</p><ol><li><p>Finding minimum leaf node: O(1) using tree's cached minimum timestamps</p></li><li><p>Transferring batch of events: O(k) where k is transfer batch size</p></li></ol></li></ol></li></ol><p><strong>Overall</strong>: Amortized <strong>O(1)</strong> since transfer costs are spread across many operations</p><h4><strong>Performance Bottlenecks</strong></h4><p><strong>Transfer Operations Impact:</strong> <br>When the primary queue hits its UPPBOUND or LOWBOUND thresholds, the transfer operations between tiers can cause significant overhead. These transfers require moving large batches of events between structures and can temporarily pause normal operations. While the cost is amortized, it can cause noticeable performance spikes.<br><br><strong>Leaf Node Management:</strong> <br>When leaf nodes in the secondary tier get too full and need splitting, the operation requires scanning all events in the node to find the average timestamp, then redistributing them. This can be expensive for large leaf nodes and may happen frequently with certain event patterns.<br><br><strong>Threshold Tuning:</strong> <br>Choosing optimal values for UPPBOUND and LOWBOUND is critical but challenging. Poor choices can lead to either too frequent transfers or inefficient primary queue operation, significantly impacting performance.<br><br>These bottlenecks become particularly apparent in scenarios with:<br>- Highly skewed temporal distributions<br>- Memory-constrained environments<br>- Large numbers of far-future events</p><h4><strong>Advantages, Limitations and Selection Criteria</strong></h4><p><strong>Optimal Use Cases</strong></p><ol><li><p>Large Event Sets (10,000 events)</p><ol><li><p>Wide timestamp distribution</p></li><li><p>Frequent far-future events</p></li></ol></li><li><p>Variable Distributions:</p><ol><li><p>Skewed timestamp patterns</p></li><li><p>Mixed temporal localities</p></li><li><p>Dynamic workload patterns</p></li></ol></li></ol><p><strong>Limitations</strong></p><ol><li><p>Implementation Complexity:</p><ol><li><p>Complex maintenance logic</p></li><li><p>Debugging challenges</p></li><li><p>Higher development overhead</p></li></ol></li><li><p>Resource Requirements:</p><ol><li><p>Additional memory overhead</p></li><li><p>Complex pointer management</p></li><li><p>Transfer operation costs</p></li></ol></li></ol><h3>SNOOPy Calendar Queue (2000)</h3><h4><strong>Primary Components</strong></h4><p>The structure maintains traditional calendar queue elements plus statistical tracking:<br><br><strong>Calendar Base Structure:</strong></p><ul><li><p>Array of buckets containing sorted event lists</p></li><li><p>Each bucket represents a time interval</p></li><li><p>Year size = number of buckets &#215; bucket width</p></li></ul><p><strong>Statistical Enhancement Layer:</strong></p><ul><li><p>CE (Enqueue Cost): Tracks average number of events traversed during insertion</p></li><li><p>CD (Dequeue Cost): Tracks average number of empty buckets traversed during dequeuing</p></li><li><p>Moving average window over multiple operations</p></li><li><p>Performance metrics for bucket width optimisation</p></li></ul><h4><strong>Key Operations Performance</strong></h4><p><strong>Enqueue Process (O(1) average case):</strong></p><ul><li><p>Calculates target bucket using timestamp</p></li><li><p>Inserts event in sorted order within bucket</p></li><li><p>Updates enqueue cost statistics based on traversal length</p></li><li><p>Triggers resize if statistical thresholds exceeded</p></li></ul><p><strong>Dequeue Process (O(1) average case):</strong></p><ul><li><p>Retrieves earliest event from current bucket</p></li><li><p>Advances through empty buckets if necessary</p></li><li><p>Updates dequeue cost statistics</p></li><li><p>May trigger optimisation based on performance metrics</p></li></ul><p><strong>Resize Operation (O(n) when restructuring):</strong></p><ul><li><p>Triggered by statistical cost metrics rather than just size</p></li><li><p>Uses performance history to calculate optimal bucket width</p></li><li><p>Redistributes events across new structure</p></li><li><p>Resets statistical counters after resize</p></li></ul><h4><strong>Performance Bottlenecks</strong></h4><p><strong>Statistical Overhead:</strong></p><ul><li><p>Continuous cost tracking adds per-operation overhead</p></li><li><p>Moving average calculations require additional computation</p></li><li><p>Statistical threshold checks on every operation</p></li></ul><p>Resize Decisions:</p><ul><li><p>More complex than traditional CQ resize triggers</p></li><li><p>May require more frequent resizes initially while optimising</p></li><li><p>Statistical calculation overhead during resize operations</p></li></ul><p>Dynamic Adaptation:</p><ul><li><p>Initial period of suboptimal performance while gathering statistics</p></li><li><p>May oscillate between different configurations before settling</p></li><li><p>Additional memory references for statistical tracking</p></li></ul><h4><strong>Advantages, Limitations and Selection Criteria</strong></h4><p><strong>Advantages:</strong></p><ul><li><p>Robust Performance:</p><ul><li><p>Better handling of skewed distributions than traditional CQ</p></li><li><p>Self-tuning capability reduces manual parameter tuning</p></li><li><p>More consistent performance across varying workloads</p></li></ul></li><li><p>Adaptive Optimisation:</p><ul><li><p>Automatically adjusts to changing event patterns</p></li><li><p>Reduces impact of poor initial parameter choices</p></li><li><p>Maintains efficiency under varying conditions</p></li></ul></li><li><p>Statistical Insight:</p><ul><li><p>Provides performance metrics for monitoring</p></li><li><p>Enables informed optimization decisions</p></li><li><p>Can predict and prevent performance degradation</p></li></ul></li></ul><p><strong>Best Used When:</strong></p><ul><li><p>Event distribution is unknown or varies</p></li><li><p>Long-running simulations justify optimization overhead</p></li><li><p>Performance consistency is critical</p></li><li><p>Manual tuning is impractical</p></li></ul><p><strong>Limitations:</strong></p><ul><li><p>Implementation Complexity:</p><ul><li><p>More complex than basic calendar queue</p></li><li><p>Additional overhead for statistical tracking</p></li><li><p>More difficult to debug and maintain</p></li></ul></li></ul><p><strong>Avoid When:</strong></p><ul><li><p>Simple, known event distributions</p></li><li><p>Short-running simulations</p></li><li><p>Memory or CPU resources are very constrained</p></li><li><p>Implementation simplicity is priority</p></li></ul><h3>Resources</h3><p>[1] Franta, W. R., and Kurt Maly. "An Efficient Data Structure for the Simulation Event Set." Communications of the ACM 20, no. 8 (August 1977): 596-602.</p><p>[2] Brown, Randy. "Calendar Queues: A Fast O(1) Priority Queue Implementation for the Simulation Event Set Problem." Communications of the ACM 31, no. 10 (October 1988): 1220-1227.</p><p>[3] Hui, Timothy Chee-Kin, and Ian Li-Jin Thng. "FELT: A Far Future Event List Structure Optimized for Calendar Queues." SIMULATION: Transactions of The Society for Modeling and Simulation International 78, no. 6 (June 2002): 343-361.</p><p>[4] Tan, Kah Leong, and Li-Jin Thng. "SNOOPy Calendar Queue." In Proceedings of the 2000 Winter Simulation Conference, edited by J. A. Joines, R. R. Barton, K. Kang, and P. A. Fishwick, 487-495. IEEE, 2000.</p>]]></content:encoded></item><item><title><![CDATA[Payments Systems at Scale: Lessons from Uber's Unified Platform]]></title><description><![CDATA[On the story of how Uber's engineering team transformed a maze of payment systems into a unified platform that powers millions of transactions daily]]></description><link>https://blog.diana-enache.com/p/payments-systems-at-scale-lessons</link><guid isPermaLink="false">https://blog.diana-enache.com/p/payments-systems-at-scale-lessons</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Sat, 16 Nov 2024 15:52:57 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Picture this: You're at a restaurant that accepts credit cards for dine-in but only cash for takeout, Apple Pay for drinks but not desserts. Sounds absurd? This was essentially Uber's reality across its services.</p><p>As Uber evolved from a simple ride-hailing app into a super-app offering food delivery, grocery shopping, and even car rentals, each new service built its own payment system. The result? A labyrinth of 70 different payment endpoints, each speaking its own language.</p><p>This fragmentation created real-world headaches. A user could pay for an immediate ride with Apple Pay but couldn't use it for a scheduled pickup. Popular payment methods like PIX (Brazil) and UPI (India) worked inconsistently across services. For any new payment feature or compliance requirement that needed to be integrated, engineers would have to spend countless more hours maintaining this new duplicate code.</p><p>The cost wasn't just technical. When the European Union mandated Strong Customer Authentication for transactions, requiring features like fingerprinting and biometric verification, it became clear: Uber needed to revolutionize its payment infrastructure or risk losing hundreds of millions in revenue.</p><h3>Unified Checkout - One Gateway to Rule Them All</h3><p>What Uber&#8217;s engineering team created is a payment universal translator, which they called <strong>Unified Checkou</strong>t. The way it works is that instead of each service (rides, eats, rentals) handling payments differently, everything flows through one complex system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!G1rJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!G1rJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 424w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 848w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 1272w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!G1rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png" width="1456" height="782" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:782,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Image&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Image" title="Image" srcset="https://substackcdn.com/image/fetch/$s_!G1rJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 424w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 848w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 1272w, https://substackcdn.com/image/fetch/$s_!G1rJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb6ca8b-883f-42ce-8c95-4a51212b3b68_1600x859.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Uber&#8217;s architecture without and with Unified Checkout (<a href="https://www.uber.com/en-GB/blog/unified-checkout/">Source</a>)</figcaption></figure></div><p>This payments orchestration layer while it helps to process millions of transactions and to maintain consistency across all services, it keeps individual services blissfully unaware of the underlying complexity.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!S1wW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!S1wW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 424w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 848w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 1272w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!S1wW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png" width="1456" height="900" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:900,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Image&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Image" title="Image" srcset="https://substackcdn.com/image/fetch/$s_!S1wW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 424w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 848w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 1272w, https://substackcdn.com/image/fetch/$s_!S1wW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce68a52e-d971-4f41-810c-bf3e95a02962_1600x989.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Architecture of the Unified Checkout (<a href="https://www.uber.com/en-GB/blog/unified-checkout/">source</a>)</figcaption></figure></div><p>The architecture revolves around a chain of microservices that handle everything from payment profile preparation to risk evaluation. When a user starts a transaction, the platform exchanges transactional data with third-party processors, generates authentication tokens, and coordinates with risk systems to determine the optimal payment strategy. The system is able to adapt its approach based on the risk algorithms, whether if it&#8217;s for an authorization hold or an upfront charge.</p><p>One notable capability of the Unified Checkout is that it provides <strong>two distinct integration paths.</strong></p><ul><li><p>The <strong>modular approach</strong> allows services to embed pre-built components into their existing checkout flows, maintaining their unique user experience while leveraging the unified payment infrastructure.</p></li><li><p>For newer or smaller services, the <strong>hosted solution</strong> provides a complete out-of-the-box payment experience, accelerating time-to-market without compromising on functionality.</p></li></ul><p>Another key advantage is the <strong>"Checkout Actions" framework</strong> - a stateless system that orchestrates complex payment flows, from two-factor authentication to identity challenges, this framework handles every possible payment scenario while maintaining a consistent user experience.</p><p>By treating each Uber service as an external company, the platform maintains strict boundaries between payment logic and business operations. This architectural decision forced the team to build a solution that could handle any payment scenario while remaining service-agnostic, and hence building a good foundation for future payment innovations.</p><h3>The Business Impact</h3><p>The Unified Checkout delivered a remarkable 3% increase in checkout conversion rate and a 4.5% improvement in session recovery, which meant saving hundreds of millions in additional annual revenue. These results were shown in an experiment on UberEats, where the team compared user behaviour between the old and new systems, leading to rapid adoption across all services.</p><p>This transformation dramatically simplified the payment landscape at Uber. Adding another payment method like Apple Pay doesn&#8217;t require a full implementation anymore, but only some configuration changes. Moving away from 70 endpoints to one single unified system streamlined operations and changed on a fundamental level how Uber does payments. Duplicate development work vanished. The launch of new payment methods accelerated dramatically. A consistent user experience across services built stronger trust with customers. Payment method decisions shifted from technical limitations to pure strategy, allowing Uber to optimise its offerings based on market needs rather than implementation constraints.</p><p>Maintenance costs plummeted as teams no longer needed to maintain multiple implementations. Compliance became more straightforward, error rates dropped significantly, and customer support issues decreased. And probably most importantly, new services could now launch faster, equipped with a complete payment infrastructure from day one.</p><h3>Lessons Learnt</h3><p>It comes a time in a company&#8217;s scaling journey where maintaining multiple systems becomes more expensive than building a unified platform. For Uber, this moment came when they faced 70 different payment endpoints across their services, each requiring separate maintenance and updates.</p><p>Uber's experience demonstrates how external pressures, like EU regulations, can serve as catalysts for positive change. What began as a compliance requirement evolved into a platform that generated hundreds of millions in additional revenue.</p><p>Looking toward the future, the key is creating modular systems that can evolve with changing requirements, while maintaining a clear separation between business logic and payment processing. This separation allows for easier updates and maintenance as regulatory requirements change or new payment methods emerge.</p><p>The most valuable lesson from Uber's journey is recognizing that technical debt isn't always a burden &#8211; it can be a signal that your system is ready for platformization. When multiple teams struggle with similar problems, it might be time to step back and consider a unified approach. </p><p>The Uber article is available here:<br><strong><a href="https://www.uber.com/en-GB/blog/unified-checkout/">Unified Checkout: Streamlining Uber&#8217;s Payment Ecosystem</a></strong></p><div><hr></div><h3>Similar stories you might enjoy &#128218;</h3><ul><li><p><a href="https://stripe.com/blog/how-stripes-document-databases-supported-99.999-uptime-with-zero-downtime-data-migrations?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=the-architecture-of-stripe-s-document-database">How Stripe&#8217;s document databases supported 99.999% uptime with zero-downtime data migrations</a></p></li><li><p><a href="https://medium.com/paypal-tech/scaling-kafka-to-support-paypals-data-growth-a0b4da420fab">How PayPal Scaled Kafka to 1.3 Trillion Messages a Day</a></p></li><li><p><a href="https://monzo.com/blog/vertically-scaling-ordered-consumption-using-kafka">How Monzo vertically scaled ordered consumption using Kafka</a></p></li></ul>]]></content:encoded></item><item><title><![CDATA[Understanding the Dual-Write Challenge in Multi-System Environments]]></title><description><![CDATA[On the risks of data consistency, the causes behind synchronisation issues, and strategies to mitigate these problems.]]></description><link>https://blog.diana-enache.com/p/understanding-the-dual-write-challenge</link><guid isPermaLink="false">https://blog.diana-enache.com/p/understanding-the-dual-write-challenge</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Sun, 25 Aug 2024 07:32:08 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b3ff8862-4f4d-4a4f-9879-616497ba71d3_2420x736.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>The Dual-Write Problem</h3><p>The dual-write problem is a critical issue that arises when updating multiple systems simultaneously. This challenge significantly impacts data consistency, system reliability, and overall performance in modern software architectures.</p><p>At its core, the dual-write problem stems from the difficulty of ensuring that all write operations across different systems either succeed or fail together. Traditional database transactions provide atomicity within a single database, but this guarantee doesn't extend to multiple systems. This limitation creates a risk of <em><strong>partial updates</strong></em>, which can lead to <em><strong>data inconsistencies</strong></em> and compromise the integrity of the overall system.</p><p>Failure scenarios further complicate the dual-write problem. System crashes, network failures, and partial outages can occur at any point during the update process, potentially leaving systems out of sync. For instance, if a failure occurs after writing to one system but before updating another, the result is inconsistent data across the environment.</p><p>To illustrate the dual-write problem, consider a common scenario in microservice architectures: updating a database and sending a notification message. Ideally, these operations should be atomic to ensure data consistency and reliability. However, the lack of a unified transaction mechanism across different systems makes achieving this atomicity challenging.</p><ul><li><p>If the database update is successful but the event notification fails, the downstream service will not be aware of the change, and the system can enter an inconsistent state.</p></li><li><p>If the database update fails but the event notification is sent, data could get corrupted, which might affect the reliability of the system.</p></li></ul><p>As organisations continue to adopt microservices and distributed architectures, addressing the dual-write problem becomes increasingly crucial. </p><p></p><h3>The Transactional Outbox Pattern</h3><p>One way to solve this is through the Transactional Outbox pattern. If we have a database that supports transactional updates, then we can use it to overcome the dual-write problem. </p><p>Let&#8217;s take the example of a food delivery company system. When a customer places an order, several things need to happen: 1. The order details need to be saved in the database 2. A notification needs to be sent to the restaurant 3. The customer should receive an order confirmation 4. The order should be made available for driver assignment and so on. </p><p>The <strong>Order Service</strong> receives the order request. Then it starts a database transaction. It inserts the order details into the <strong>Orders table</strong> and in the same transaction, it also inserts messages into the <strong>Outbox table</strong>. These messages might include: a) OrderCreated event b) NotifyRestaurant message c) SendCustomerConfirmation message. The database transaction is committed, ensuring both the order and the outbox messages are saved atomically. </p><p>The <strong>Publisher Service</strong> runs continuously or at frequent intervals. It queries the Outbox table for unpublished messages. When it finds messages, it reads them and publishes them to the <strong>Message Broker</strong>. After successful publication, it marks the messages as processed or deletes them from the Outbox table.</p><p>The <strong>Message Broker</strong> receives the published messages from the <strong>Publisher Service</strong> and routes these messages to the appropriate queues or topics.</p><p>Then we have all the <strong>Downstream Services</strong>. A <strong>Restaurant Service</strong> might subscribe to the "OrderCreated" event. When received, it notifies the restaurant about the new order. A <strong>Notification Service</strong> might pick up the "SendCustomerConfirmation" message and sends an order confirmation to the customer. A <strong>Driver Assignment Service</strong> might listen for "OrderCreated" events to begin the process of assigning a driver.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gQ39!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gQ39!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 424w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 848w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 1272w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gQ39!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png" width="1456" height="509" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:509,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!gQ39!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 424w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 848w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 1272w, https://substackcdn.com/image/fetch/$s_!gQ39!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3e94b-29d5-4cc0-a574-ac5b3e39db4a_1600x559.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A Transactional Outbox Pattern for a Food Delivery System</figcaption></figure></div><p><strong>When to use:</strong></p><ul><li><p>You need to ensure consistency between your database and a message broker.</p></li><li><p>You want to implement reliable messaging in a microservices architecture.</p></li><li><p>You need to guarantee that a message is sent if and only if a database transaction commits.</p></li></ul><p><strong>Pros:</strong></p><ul><li><p>Ensures consistency between database and messaging system.</p></li><li><p>Works well with existing relational databases.</p></li><li><p>Simpler to implement than full event sourcing.</p></li><li><p>Doesn't require changing your entire architecture.</p></li></ul><p><strong>Cons:</strong></p><ul><li><p>Doesn't provide a full audit trail like event sourcing.</p></li><li><p>Requires polling or additional mechanisms to publish messages.</p></li><li><p>Can add complexity to your database schema.</p></li></ul><p></p><h3>Change Data Capture</h3><p>Store the state record to a data store that supports CDC. Then the CDC infrastructure will notify external systems of the changes. One such example would be DynamoDB, using its DynamoDB Streams.</p><p>Coming back to the example earlier, in a CDC approach, the <strong>Order Service</strong> receives the order request, it processes the order and inserts the order details into the <strong>Orders table</strong> in the database. The <strong>CDC</strong> <strong>tool</strong> continuously monitors the database's transaction log. When it detects a change in the Orders table (in this case, a new order insertion), it captures this change. The CDC tool transforms the captured change into a structured event message and publishes the captured change event to a <strong>Message Broker/Streams</strong>. The event contains details about the new order, such as order ID, customer ID, restaurant ID, and other relevant information. And same as above, multiple <strong>Downstream Services</strong> can then subscribe to the Order change events from the Message Broker.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RWBt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RWBt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 424w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 848w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 1272w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RWBt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png" width="1456" height="494" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:494,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!RWBt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 424w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 848w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 1272w, https://substackcdn.com/image/fetch/$s_!RWBt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdda434c7-c642-4f8f-bda2-b1daa2f2b3df_1600x543.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A CDC Approach for a Food Delivery System</figcaption></figure></div><p><strong>When to use:</strong></p><ul><li><p>You want to track changes in your database for replication or integration purposes.</p></li><li><p>You need to sync data between different systems in near real-time.</p></li><li><p>You want to implement event-driven architectures without changing your existing systems.</p></li><li><p>You need to create data lakes or data warehouses from operational databases.</p></li></ul><p><strong>Pros:</strong></p><ul><li><p>Non-intrusive - can be implemented without changing existing applications.</p></li><li><p>Captures all changes, including those made outside your application.</p></li><li><p>Can be used with legacy systems.</p></li><li><p>Supports real-time data integration.</p></li></ul><p><strong>Cons:</strong></p><ul><li><p>May not capture the full business context of changes.</p></li><li><p>Can be complex to set up and manage.</p></li><li><p>May impact database performance. CDC can add some overhead to the database, as it needs to read the transaction logs.</p></li><li><p>Might require specific database features or additional tools.</p></li><li><p>When the Orders table schema changes, downstream consumers need to be prepared to handle both old and new event formats.</p></li></ul><p></p><h3>Event Sourcing</h3><p>Event sourcing is a pattern for storing data or changes to data as events in an append-only log. This represents a business decision to accept the request and start processing it.  </p><blockquote><p><em>The idea of event sourcing is that the event log is the source of truth, and all other databases are projections of the event log. Any write must first be made to the event log. After this write succeeds, one or more event handlers consume this new event and writes it to the other databases. </em>(<em>Cloud Native Patterns</em> by Cornelia Davis (Manning Publications, 2019))</p></blockquote><p>In event sourcing, every change to an entity's state is captured as a discrete, fine-grained event. These events are not merely notifications, each event represents a specific, atomic alteration to the entity's state.</p><p>As these state-changing events occur, they are published to interested parties and simultaneously persisted in a chronological log. This log becomes the source of truth for all the changes that have occurred to the entity over time. It's akin to a detailed historical ledger, capturing every nuance of the entity's evolution.</p><p>The power of event sourcing lies in its ability to reconstruct an entity state. Subscribers to this event log can process the sequence of events to derive the current state of an entity at any given point in time. By "replaying" the events in order, these subscribers can build a complete and accurate picture of the entity's current condition.</p><p>Take the design of an order processing system for a food delivery platform as an example. Event sourcing simplifies this process. All these events (orderCreated, orderAcceptedByRestaurant, orderPreparing etc.) are stored in an append-only log. Each event contains details like timestamp, entity_id, event_type and relevant data. The current state of an order can be reconstructed by replaying all events for that order from the beginning.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Bm2_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Bm2_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 424w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 848w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 1272w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Bm2_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png" width="1456" height="492" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:492,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!Bm2_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 424w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 848w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 1272w, https://substackcdn.com/image/fetch/$s_!Bm2_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9705934d-1190-46c9-a115-13f3930d7dba_1600x541.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Event Sourcing Architecture for a Food Delivery Platform</figcaption></figure></div><p><strong>When to use:</strong></p><ul><li><p>You need a complete audit trail of all changes.</p></li><li><p>You want to reconstruct past states of your system.</p></li><li><p>Your domain model is complex with many state transitions.</p></li><li><p>You need to support temporal queries (e.g., "What was the state at time X?").</p></li><li><p>You want to enable easy implementation of new features based on historical data.</p></li></ul><p><strong>Pros:</strong></p><ul><li><p>Provides a full history of changes.</p></li><li><p>Enables easy debugging and auditing.</p></li><li><p>Allows for event replay and system recovery.</p></li><li><p>Supports complex domain modelling.</p></li></ul><p>Cons:</p><ul><li><p>Can be complex to implement and maintain.</p></li><li><p>May require more storage.</p></li><li><p>Querying current state can be slower (mitigated by snapshots).</p></li></ul><p></p><h3>Listen to Yourself </h3><p>The Listen to Yourself pattern is a variation of event-driven architecture that aims to solve the problem of data consistency between a service's database and its published events. The service publishes events to a message broker as part of its operations. The service then subscribes to its own events from the message broker. Upon receiving its own event, the service updates its database.</p><p>The <strong>Order Service</strong> receives the order request. It prepares the order data and instead of writing directly to the database, it publishes an "OrderCreated" event to the <strong>Message Broker</strong>. This event contains all necessary order details (order ID, customer ID, restaurant ID, items, total, etc.). The <strong>Message Broker</strong> receives the "OrderCreated" event from the <strong>Order Service</strong> and makes the event available to all subscribed services, including the <strong>Order Service</strong> itself. The <strong>Order Service</strong> subscribes to its own "OrderCreated" events. When it receives the event it just published: a) It writes the order details to the Orders table in the database. b) It updates its internal state if necessary. Multiple <strong>Downstream Services</strong> also subscribe to the "OrderCreated" events.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!inMD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!inMD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 424w, https://substackcdn.com/image/fetch/$s_!inMD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 848w, https://substackcdn.com/image/fetch/$s_!inMD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 1272w, https://substackcdn.com/image/fetch/$s_!inMD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!inMD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png" width="1456" height="425" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:425,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!inMD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 424w, https://substackcdn.com/image/fetch/$s_!inMD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 848w, https://substackcdn.com/image/fetch/$s_!inMD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 1272w, https://substackcdn.com/image/fetch/$s_!inMD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80f1a8cd-d0dd-40f9-bb8c-ade74406b3c9_1600x467.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Listen to Yourself pattern applied to a Food Delivery System</figcaption></figure></div><p><strong>How it's different:</strong></p><ul><li><p>Unlike the Transactional Outbox pattern, it doesn't require a separate outbox table in the database.</p></li><li><p>It's simpler than full Event Sourcing but still provides a level of eventual consistency.</p></li><li><p>It differs from CDC in that the service actively publishes events, rather than relying on database log parsing.</p></li></ul><p><strong>Pros:</strong></p><ul><li><p>Simplifies the architecture by using the message broker as the source of truth.</p></li><li><p>Ensures eventual consistency between the published events and the database state.</p></li><li><p>Can be easier to implement than some other patterns.</p></li></ul><p><strong>Cons:</strong></p><ul><li><p>There's a potential for temporary inconsistency between the event being published and the database being updated.</p></li><li><p>It may introduce additional latency due to the round-trip through the message broker.</p></li><li><p>It can be more difficult to debug, as the flow of data is less straightforward.</p></li></ul><p></p><h3>Conclusion</h3><p>We had a look at the different solutions that can be used to solve the dual-write problem. Which approach is used varies based on a given use case, specific requirements or any constraints of the system being designed or modified. To wrap things up, below is a comparison that might help you in making your decision:</p><p><strong>Data Consistency:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> High consistency, events are the source of truth</p></li><li><p><strong>Transactional Outbox:</strong> Strong consistency between database and messaging</p></li><li><p><strong>Listen to Yourself:</strong> Eventual consistency, with potential for brief inconsistencies</p></li><li><p><strong>CDC:</strong> Eventual consistency, with potential for small delays</p></li></ul><p><strong>Complexity of Implementation:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> High - requires significant architectural changes</p></li><li><p><strong>Transactional Outbox:</strong> Moderate - requires additional table and message relay process</p></li><li><p><strong>Listen to Yourself:</strong> Low to Moderate - requires event publishing and self-subscription</p></li><li><p><strong>CDC:</strong> Moderate - requires CDC tool setup and configuration</p></li></ul><p><strong>Performance Impact:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Can be high for read operations, depending on implementation</p></li><li><p><strong>Transactional Outbox:</strong> Low impact on write operations, additional load for message relay</p></li><li><p><strong>Listen to Yourself:</strong> Moderate - additional network round-trip for each operation</p></li><li><p><strong>CDC:</strong> Low to Moderate - some overhead on database for log reading</p></li></ul><p><strong>Scalability:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Highly scalable, especially for write operations</p></li><li><p><strong>Transactional Outbox:</strong> Scalable, with separate scaling for application and message relay</p></li><li><p><strong>Listen to Yourself:</strong> Scalable, but may require careful management of self-subscriptions</p></li><li><p><strong>CDC:</strong> Highly scalable, especially for high-volume changes</p></li></ul><p><strong>Auditability:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Excellent - full history of all changes</p></li><li><p><strong>Transactional Outbox:</strong> Good - outbox can serve as an audit log</p></li><li><p><strong>Listen to Yourself:</strong> Good - events can serve as an audit log</p></li><li><p><strong>CDC:</strong> Excellent - captures all database changes</p></li></ul><p><strong>Ability to Reconstruct Past States:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Excellent - core feature of the pattern</p></li><li><p><strong>Transactional Outbox:</strong> Limited - not a primary feature</p></li><li><p><strong>Listen to Yourself:</strong> Possible but not straightforward</p></li><li><p><strong>CDC:</strong> Possible if all changes are retained, but not a primary feature</p></li></ul><p><strong>Impact on Existing Systems:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> High - requires fundamental architectural changes</p></li><li><p><strong>Transactional Outbox:</strong> Moderate - requires changes to data access layer</p></li><li><p><strong>Listen to Yourself:</strong> Moderate - requires changes to service logic</p></li><li><p><strong>CDC:</strong> Low - can often be implemented without changing existing applications</p></li></ul><p><strong>Handling of Schema Changes:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Challenging - requires careful event versioning</p></li><li><p><strong>Transactional Outbox:</strong> Moderate - affects outbox table and consumers</p></li><li><p><strong>Listen to Yourself:</strong> Moderate - affects event structure and consumers</p></li><li><p><strong>CDC:</strong> Challenging - changes in source database schema need to be carefully managed</p></li></ul><p><strong>Real-time Data Integration:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Excellent - events can be processed in real-time</p></li><li><p><strong>Transactional Outbox:</strong> Good - near real-time, depending on relay frequency</p></li><li><p><strong>Listen to Yourself:</strong> Excellent - events are processed immediately</p></li><li><p><strong>CDC:</strong> Very Good - near real-time, with minimal delay</p></li></ul><p><strong>Resilience to System Failures:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> High - can rebuild state from event log</p></li><li><p><strong>Transactional Outbox:</strong> Good - messages persist in outbox until processed</p></li><li><p><strong>Listen to Yourself:</strong> Moderate - depends on message broker reliability</p></li><li><p><strong>CDC:</strong> Good - can resume from last processed transaction log position</p></li></ul><p><strong>Latency:</strong> </p><ul><li><p><strong>Event Sourcing:</strong> Can be high for complex queries on current state</p></li><li><p><strong>Transactional Outbox:</strong> Low for writes, potential delay in message processing</p></li><li><p><strong>Listen to Yourself:</strong> Moderate - additional network round-trip</p></li><li><p><strong>CDC:</strong> Low to Moderate - depends on CDC tool and configuration</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Common microservice failures and how DoorDash mitigates them]]></title><description><![CDATA[On the different pitfalls that arise in distributed systems, what are some of the localised solutions, what can be done at a more global level and how DoorDash tries to mitigate their failures]]></description><link>https://blog.diana-enache.com/p/common-microservice-failures-and</link><guid isPermaLink="false">https://blog.diana-enache.com/p/common-microservice-failures-and</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Mon, 08 Jul 2024 07:18:04 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/1658362d-e60f-457a-821c-d93c3e3b887f_2406x1332.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a href="https://www.doordash.com/">DoorDash</a> is an on-demand food delivery service that connects customers with local restaurants through its app and website. It&#8217;s currently one of the largest food marketplaces in the US with almost <a href="https://backlinko.com/doordash-users">37 millions users</a>. The platform enables users to browse menus, place orders, and have meals delivered directly to their doorstep. </p><p>In 2020, with the constant increase in their user base, the team decided to move from a Django monolith to a microservice architecture. This allowed for better scalability options, shorter waits for tests completion, faster deployment times and increased developer velocity. They wrote a great <a href="https://doordash.engineering/2020/12/02/how-doordash-transitioned-from-a-monolith-to-microservices/?utm_source=blog.quastor.org&amp;utm_medium=newsletter&amp;utm_campaign=scaling-microservices-at-doordash">blog post</a> on how they managed the transition.  But this change also brought a lot of complexity with it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2h0_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2h0_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2h0_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg" width="1456" height="750" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:750,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:171370,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2h0_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2h0_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F875e9ff4-9802-4032-aecc-c4ba20b04154_2786x1435.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Courtesy of <a href="https://doordash.engineering/2020/12/02/how-doordash-transitioned-from-a-monolith-to-microservices/?utm_source=blog.quastor.org&amp;utm_medium=newsletter&amp;utm_campaign=scaling-microservices-at-doordash">DoorDash</a></figcaption></figure></div><p>The new architecture introduced other types of issues which we&#8217;re going to talk about in this article. We&#8217;re going to have a look at some of the common pitfalls and anti-patterns that appear in a microservice architecture, how Doordash solved them at a local level and how they&#8217;re attempting to mitigate them at a global level.</p><h3>Common pitfalls with microservice architectures</h3><p><strong>1. Cascading Failure - </strong>A cascading failure happens when the failure of one service leads to the failure of other dependent services. This can cause a chain reaction, potentially bringing down the entire system.</p><p>DoorDash had an outage of this kind that they talked about in this <a href="https://doordash.engineering/2022/05/13/doordashs-may-12th-outage/?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=scaling-microservices-at-doordash">blog post</a>. In their case, the chain of failure started from a seemingly innocuous database maintenance, which increased the database latency. The latency then bubbled up to the upstream services, causing errors from timeouts and resource exhaustion. The increased error rates triggered a misconfigured circuit breaker, which stopped traffic between a lot of unrelated services, resulting in an outage with a wide blast radius.</p><p><strong>Why it happens:</strong> </p><ul><li><p><strong>Tight Coupling</strong>: Services are too dependent on each other, leading to a domino effect.</p></li><li><p><strong>Lack of Isolation</strong>: Failures in one service are not contained and propagate to others.</p></li><li><p><strong>Resource Exhaustion</strong>: Failure in a critical service can lead to resource exhaustion (e.g., CPU, memory) in dependent services.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HjW6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HjW6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 424w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 848w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HjW6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg" width="1456" height="334" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:334,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:162270,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HjW6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 424w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 848w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!HjW6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6c1193b7-2753-42f1-a879-e06346b51437_4177x957.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Diagram based on <a href="https://doordash.engineering/2023/03/14/failure-mitigation-for-microservices-an-intro-to-aperture/?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=scaling-microservices-at-doordash">DoorDash&#8217;s</a> illustrating a distributed system experiencing a cascading failure</figcaption></figure></div><p><strong>2.</strong> <strong>Retry Storm</strong> - A retry storm occurs when a service failure triggers multiple retries from dependent services, which can overwhelm the failing service even more. Retries can worsen the issue when the downstream services are unavailable or slow, leading to work amplification, as each failed request is retried multiple times, which can cause an already degraded service to deteriorate further.</p><p><strong>Why it happens:</strong> </p><ul><li><p><strong>Uncontrolled Retries</strong>: Services automatically retry failed requests without considering the state of the failing service.</p></li><li><p><strong>Lack of Backoff</strong>: Retries happen too frequently, without appropriate delay, exacerbating the problem.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mu4S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mu4S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 424w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 848w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mu4S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg" width="1456" height="472" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:472,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:190516,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!mu4S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 424w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 848w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!mu4S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa6a52abf-e3a3-44ed-a31a-20cb7960a181_3510x1139.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Diagram based on <a href="https://doordash.engineering/2023/03/14/failure-mitigation-for-microservices-an-intro-to-aperture/?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=scaling-microservices-at-doordash">DoorDash&#8217;s</a> illustrating how a retry storm spreads in a system. The issue earlier that started with the db latency, spreads to the RPC calls and triggers retries in service 2, 3 and 5 which increase the load on the db even more.</figcaption></figure></div><p><strong>3. Death Spiral</strong> - A death spiral happens when the system starts to fail under load, and the attempts to handle the failure (like retries or additional resource allocation) further degrade the system&#8217;s performance, leading to a vicious cycle of deteriorating performance.</p><p>We&#8217;ve seen earlier how issues can spread vertically through different services dependant on one another. But they can also spread horizontally, inside the service cluster, from one node to another.</p><p><strong>Why this happens:</strong> </p><ul><li><p><strong>Resource Contention</strong>: Excessive retries and fallback operations consume more resources, reducing availability for normal operations.</p></li><li><p><strong>Unbalanced Load</strong>: Efforts to handle failures, like adding more instances, might lead to other parts of the system becoming overloaded.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jEB-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jEB-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 424w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 848w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jEB-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg" width="1456" height="647" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:647,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:166761,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jEB-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 424w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 848w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!jEB-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F920dbb1f-33c6-42c2-ace1-344c6503e53b_3000x1333.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Diagram based on <a href="https://doordash.engineering/2023/03/14/failure-mitigation-for-microservices-an-intro-to-aperture/?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=scaling-microservices-at-doordash">DoorDash&#8217;s</a> illustrating a service running on 3 nodes. 2 nodes go down due to some network issue so the incoming requests get routed to the only other available node left. This causes significantly higher resource utilisation which leads to the remaining node to crash due to resource saturation.</figcaption></figure></div><p><strong>4. Metastable Failure</strong> - Metastable failure refers to a state where the system appears to be stable under low load but becomes unstable and fails when the load increases beyond a certain threshold. <a href="https://sigops.org/s/conferences/hotos/2021/papers/hotos21-s11-bronson.pdf">Metastable failures occur in open systems with an uncontrolled source of load where a trigger causes the system to enter a bad state that persists even when the trigger is removed.</a> When any of several potential triggers cause the system to enter a metastable state, a feedback loop sustains the failure, keeping the system in this state until a significant (usually manual) corrective action is applied.</p><p>For example, an initial trigger such as a surge in users, might cause one of the backend services to load shed and start responding to certain requests with 429 (rate limit). The callers will then retry their calls, but these retries, combined with requests from new users, overwhelm the backend service even more, leading to further load shedding. This results in a positive feedback loop where calls are continuously retried (along with new calls), get rate limited, and then retried again, perpetuating the cycle.</p><p>The above is called a <strong>Thundering Herd problem</strong> and is one example of a Metastable failure.</p><p><strong>Why this happens:</strong> </p><ul><li><p><strong>Insufficient Capacity</strong>: The system is not designed to handle peak loads, leading to instability.</p></li><li><p><strong>Hidden Bottlenecks</strong>: Bottlenecks that are not apparent under normal conditions become critical under high load.</p></li><li><p><strong>Latency Sensitivity</strong>: Small increases in latency can have a disproportionate effect on the system's stability.</p></li><li><p><strong>Request Retries</strong>: Retrying failed requests is widely used to mask transient issues. However, it also results in work amplification, which can lead to additional failures</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8KA5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8KA5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 424w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 848w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8KA5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg" width="590" height="280.00686813186815" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:691,&quot;width&quot;:1456,&quot;resizeWidth&quot;:590,&quot;bytes&quot;:128827,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8KA5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 424w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 848w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!8KA5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61e5fdbb-000d-4fdb-ac7e-7825ae7201dd_2902x1378.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Lifecycle of a metastable failure</figcaption></figure></div><h3>Solutions at a local level</h3><p>There are a couple of known techniques which are used to solve the above, some of which are being used by DoorDash as well:</p><ul><li><p><strong>Exponential Backoff</strong> - by gradually increasing the delay between retries to reduce the load</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!opIm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!opIm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 424w, https://substackcdn.com/image/fetch/$s_!opIm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 848w, https://substackcdn.com/image/fetch/$s_!opIm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!opIm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!opIm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg" width="660" height="189.47802197802199" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:418,&quot;width&quot;:1456,&quot;resizeWidth&quot;:660,&quot;bytes&quot;:84641,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!opIm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 424w, https://substackcdn.com/image/fetch/$s_!opIm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 848w, https://substackcdn.com/image/fetch/$s_!opIm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!opIm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F411aa263-bd7d-4635-85aa-decef5022f1f_3730x1072.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><ul><li><p><strong>Retry Limits</strong> - by setting a maximum limit on the number of retries to prevent endless retry loops.</p></li><li><p><strong>Circuit Breakers - </strong>by activating and reducing the number of calls a dependent service makes to another one. Some examples of how circuit breakers are used by other companies:</p><ul><li><p><strong><a href="https://github.com/Netflix/Hystrix">Netflix's Hystrix library</a></strong>: Netflix developed Hystrix as a library to manage failures within their distributed system. When Hystrix identifies that a remote service is down or unresponsive, it activates the circuit breaker, stopping further requests from being sent to the problematic service. It can also offer alternative responses or retry the request after a designated period.</p></li><li><p><strong><a href="https://aws.amazon.com/blogs/containers/announcing-amazon-ecs-deployment-circuit-breaker/">AWS ECS</a></strong>: ECS employs a circuit breaker pattern to automatically isolate failing services, thus preventing cascading failures within applications. This pattern also reduces latency and resource consumption during recovery from a service failure. It effectively ensures high availability and reliability for containerized applications.</p></li><li><p><strong><a href="https://developers.soundcloud.com/blog/lessons-in-resilience-at-SoundCloud">SoundCloud</a></strong>: SoundCloud also uses circuit breaker to handle failures in their distributed architecture.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fEC-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fEC-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 424w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 848w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fEC-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg" width="1456" height="470" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:470,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114651,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fEC-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 424w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 848w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!fEC-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45d1785c-d125-48be-a171-d841a7cb4b6f_3518x1136.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li></ul><ul><li><p><strong>Bulkheads</strong> - by isolating critical resources (like thread pools, database connections, or service instances) ensuring that failures or high resource usage in one microservice do not impact others sharing the same resources.</p></li><li><p><strong>Timeouts &amp; Fallbacks</strong> - by defining the maximum acceptable duration for a microservice to respond to a request and preventing indefinite waiting and and resource consumption when a service is slow or unresponsive. </p></li><li><p><strong>Load Shedding</strong> - by prioritising and limiting incoming requests to prevent overload.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E0X4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E0X4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 424w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 848w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E0X4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg" width="616" height="212.80769230769232" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:503,&quot;width&quot;:1456,&quot;resizeWidth&quot;:616,&quot;bytes&quot;:107957,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E0X4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 424w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 848w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!E0X4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fd230cd-18e6-4dbd-af20-1d960055a423_3401x1175.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><ul><li><p><strong>Graceful Degradation</strong> - by minimising the amount of work that needs to be done, degrading non-essential functionalities or reduce service levels during times of high load, resource scarcity, or service unavailability. This can be achieved by determining essential functionalities that must remain operational even under degraded conditions, simplifying non-critical features to prioritise core functionalities and implementing fallback strategies to maintain basic service levels when primary functionalities are unavailable.</p></li><li><p><strong>Capacity Planning</strong> - by analysing historical data, user patterns, and trends to forecast future demand for each microservice and determining the scalability requirements based on expected growth, seasonal fluctuations, and special events.</p></li></ul><h3>Shortcomings of the local countermeasures</h3><p>The current localised mechanisms face similar limitations:</p><ul><li><p>They rely on <strong>metrics specific to the individual service</strong> to determine its health. However, many types of failures involve interactions across multiple components, requiring a comprehensive system-wide perspective to effectively address overload conditions.</p></li><li><p>They use <strong>general metrics and heuristics</strong> to assess the system health, which may lack precision. For instance, high latency alone may not indicate service overload; it could stem from slow downstream services.</p></li><li><p>Their corrective actions are <strong>constrained</strong>. Operating within the local service, these mechanisms can only take local actions which may not be optimal for restoring the system health, as the root cause of the issue might lie elsewhere.</p></li></ul><h3>Solutions at a global level</h3><p>One limitation of load shedding, circuit breakers, and graceful degradation is their narrow perspective within the system. These tools assess factors like their own resource usage, immediate dependencies, and incoming request volume. However, they lack the ability to adopt a global view of the entire system and decide based on that.</p><p><a href="https://github.com/fluxninja/aperture">Aperture</a>, an open-source system for reliability management, goes beyond local solutions by implementing a centralised load control. It provides a unified system for managing loads across multiple services during outages. It has 3 main components:</p><ul><li><p><strong>Observe</strong>: Aperture gathers reliability metrics from each node and consolidates them using Prometheus.</p></li><li><p><strong>Analyse</strong>: A standalone Aperture controller continuously monitors these metrics and detects deviations from Service Level Objectives (SLOs).</p></li><li><p><strong>Actuate</strong>: Upon detecting anomalies, the Aperture controller triggers policies tailored to observed patterns and applies actions on each node, such as load shedding or distributed rate limiting.</p></li></ul><p>Aperture utilizes YAML-based policies that guides its actions during system disruptions. When an alert is triggered, Aperture automatically executes actions based on these configured policies. Some of the actions it offers include distributed rate limiting and concurrency control (also known as load shedding). By maintaining a centralized oversight and control of the entire system, Aperture enables various strategies to mitigate outages. For instance, it can be configured to implement a policy that throttles traffic to an upstream service when a downstream service is overwhelmed, preventing excessive requests from reaching the problematic area and thereby enhancing system responsiveness and cost-efficiency.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3dQx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3dQx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 424w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 848w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 1272w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3dQx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1124119,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3dQx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 424w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 848w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 1272w, https://substackcdn.com/image/fetch/$s_!3dQx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe836a95a-9dfb-4fb2-8301-05efcb479947_2026x1140.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example of a policy for a load scheduler as shown in the <strong><a href="https://www.youtube.com/watch?v=ws9__JjaJsE&amp;ab_channel=USENIX">SREcon23 Asia/Pacific</a> </strong>talk</figcaption></figure></div><p><a href="https://doordash.engineering/2023/03/14/failure-mitigation-for-microservices-an-intro-to-aperture/?utm_source=blog.quastor.org&amp;utm_medium=referral&amp;utm_campaign=scaling-microservices-at-doordash">DoorDash</a> deployed Aperture within one of their core services and conducted artificial request simulations to evaluate its performance. They discovered that Aperture effectively operated as a robust and user-friendly global rate limiter and load shedding solution, providing a <a href="https://docs.fluxninja.com/reference/policies/bundled-blueprints/policies/latency-aimd-concurrency-limiting">concurrency limiting algorithm</a> which minimises the impact of unexpected load or latency.</p><p>The Aperture blog also provides good use cases of how their solution can be used to solve production problems: </p><ul><li><p><a href="https://blog.fluxninja.com/blog/protecting-postgresql-with-adaptive-rate-limiting">Protecting PostgreSQL with Adaptive Rate Limiting</a></p></li><li><p><a href="https://blog.fluxninja.com/blog/concurrency-scheduling-in-mistral-ai">Balancing Cost and Efficiency in Mistral with Concurrency Scheduling</a></p></li><li><p><a href="https://blog.fluxninja.com/blog/coderabbit-openai-rate-limits">Squeezing Water from Stone: Managing OpenAI Rate Limits with Request Prioritization</a></p></li></ul><h3>Conclusion</h3><p>We had a look into the different pitfalls that arise in distributed systems, what are the triggers that might cause them, what are some of the localised solutions to prevent them from happening and what can be done at a more global level.</p><p>In the next articles, we&#8217;re going to take a deeper look into how Aperture works, how we can configure it and how to define a set of policies for a given use case. </p>]]></content:encoded></item><item><title><![CDATA[Coding Theory: Hamming Distance, Code, and Bound]]></title><description><![CDATA[Attempting to dive into the basics of coding theory]]></description><link>https://blog.diana-enache.com/p/coding-theory-hamming-distance-code</link><guid isPermaLink="false">https://blog.diana-enache.com/p/coding-theory-hamming-distance-code</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Tue, 14 May 2024 16:10:42 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Some time ago I wrote an article trying to dive into how <a href="https://thecraftydev.substack.com/p/from-pixels-to-information-a-comprehensive">QR codes actually work</a> by trying to decode a simple one. All went well until I got to the part of understanding how the error correction bits work and how they help restore any lost information. While the pseudocode is available online and it would be doable to write your own text to QR encoder (that&#8217;s what she said), I couldn&#8217;t grasp the maths behind the Reed-Solomon codes and how it helps with error correction. It seems I was missing a lot of concepts and theory. </p><p>So here I am, starting a new series of articles diving into Coding Theory. From Hamming distance, parity checks, linear codes, Reed-Solomon codes to BCH and maybe beyond? We&#8217;ll see how long my curiosity lasts. </p><p>This article will talk about:</p><ul><li><p>what is noise, error detection and error correction</p></li><li><p>definitions - hamming distance, ball, volume, code dimension, rate, weight and hamming bound</p></li></ul><p>And at the end of it, you should be able to: </p><ul><li><p>find and use the Hamming distance for a code</p></li><li><p>understand the trade-off between rate and distance</p></li></ul><p></p><h3>The Goal of Coding Theory</h3><p>One of the main goals of coding theory is developing methods to detect/correct errors caused by the transmission of information through noisy channels.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_t9M!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_t9M!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_t9M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg" width="1456" height="244" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:244,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:171530,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_t9M!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_t9M!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb97df35d-6281-42be-82a4-cafd9ac7382d_4888x818.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><pre><code><strong>message m of length k</strong> &#8594; <em>(encoding)</em> <strong>codeword c</strong>  &#8594; <em>(channel)</em> received <strong>word c&#8217;</strong> &#8594;  <em>(decoding)</em> <strong>message m&#8217; of length n</strong></code></pre><p>Let&#8217;s say we have a message <strong>m</strong> of length <strong>k</strong> which we are going to encode (somehow) and get a word <strong>c</strong> of length <strong>n</strong>. Here <strong>n &gt; k</strong> as we are adding redundancy data to our original message. We transmit the encoded word c through some communication channel but some of the bits get corrupted and so we receive a corrupted codeword <strong>c~</strong> of length n.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tYti!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tYti!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 424w, https://substackcdn.com/image/fetch/$s_!tYti!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 848w, https://substackcdn.com/image/fetch/$s_!tYti!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 1272w, https://substackcdn.com/image/fetch/$s_!tYti!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tYti!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png" width="664" height="153.02057613168725" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:224,&quot;width&quot;:972,&quot;resizeWidth&quot;:664,&quot;bytes&quot;:30109,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tYti!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 424w, https://substackcdn.com/image/fetch/$s_!tYti!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 848w, https://substackcdn.com/image/fetch/$s_!tYti!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 1272w, https://substackcdn.com/image/fetch/$s_!tYti!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa84f4808-969b-467d-a2d9-7c010eec34d9_972x224.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We are concerned about 4 things:</p><ol><li><p>Handling the communication noise.</p></li><li><p>Recovering the initial message. </p></li><li><p>Minimising overhead, meaning k/n should be large.</p></li><li><p>Being able to do the above through an encoding method that is efficient.</p></li></ol><p></p><h3>Binary bit-error correcting codes</h3><p>To start off, there are a couple of assumptions we&#8217;ll make. All messages that we want to send are binary strings and we will only be transmitting binary strings across the channel and the only errors that occur are bit-errors, which consist of replacing 0 by 1 or 1 by 0.</p><p>Let&#8217;s start by providing the formal definition of what a <strong>codeword c</strong> is.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b4_k!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b4_k!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 424w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 848w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 1272w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!b4_k!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png" width="644" height="153.15399610136453" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:244,&quot;width&quot;:1026,&quot;resizeWidth&quot;:644,&quot;bytes&quot;:34475,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!b4_k!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 424w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 848w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 1272w, https://substackcdn.com/image/fetch/$s_!b4_k!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85c7dc42-1657-4ad3-b262-5660fdcacef3_1026x244.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Example 1</strong> An example would be the following where C is a matrix of vectors of length 4 over an alphabet {0,1}. C is a code which has 8 codewords, each of length 4 and all digits are either 0 or 1. Such codes are called <strong>binary codes</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IFQh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IFQh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 424w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 848w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 1272w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IFQh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png" width="580" height="126.67706708268331" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:280,&quot;width&quot;:1282,&quot;resizeWidth&quot;:580,&quot;bytes&quot;:39933,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IFQh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 424w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 848w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 1272w, https://substackcdn.com/image/fetch/$s_!IFQh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F558ae65b-d3b8-4311-987c-d517d0ff6e90_1282x280.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Example 2</strong> </p><p>Suppose we have the following code (also known as the <strong>Hamming Code</strong>)</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!42ra!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!42ra!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 424w, https://substackcdn.com/image/fetch/$s_!42ra!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 848w, https://substackcdn.com/image/fetch/$s_!42ra!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 1272w, https://substackcdn.com/image/fetch/$s_!42ra!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!42ra!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png" width="688" height="62.654991243432576" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:104,&quot;width&quot;:1142,&quot;resizeWidth&quot;:688,&quot;bytes&quot;:18486,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!42ra!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 424w, https://substackcdn.com/image/fetch/$s_!42ra!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 848w, https://substackcdn.com/image/fetch/$s_!42ra!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 1272w, https://substackcdn.com/image/fetch/$s_!42ra!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d366ae4-692f-45a5-b718-57e5a78e5517_1142x104.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Then C is a binary code of blocklength 7. There is a nice way to visualise the bove encoding using 3 circles. Our message bits will be at the intersection of any 2 circles and all the other 3 parity bits ((x2,x3,x4)%2, etc) correspond to the remaining empty cells. You can also notice that each circle sums up to 0mod2.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Z-9G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Z-9G!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Z-9G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg" width="530" height="279.19642857142856" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:767,&quot;width&quot;:1456,&quot;resizeWidth&quot;:530,&quot;bytes&quot;:123847,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Z-9G!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Z-9G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b098c38-bfa4-4b9e-9cac-57eab4316e3d_2756x1451.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The Hamming Code is <em><strong>capable of detecting up to two simultaneous bit errors and correcting single-bit errors. </strong></em>Let&#8217;s say we get the following corrupted code word c~={0111010}. Are we able to figure out c? One way to check which bit got flipped is to use the same circle representation above and use the bits that we got: </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_qOD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_qOD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_qOD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg" width="552" height="290.7857142857143" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:767,&quot;width&quot;:1456,&quot;resizeWidth&quot;:552,&quot;bytes&quot;:113965,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_qOD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 424w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 848w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!_qOD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6e0b955-c560-46d8-91b8-1b0427c1981f_2756x1451.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We know that the sum of the bits in each circle should sum up to 0mod%2 but the yellow and blue one don&#8217;t, which means the bit at their intersection (aka the 3rd bit) has been corrupted. So now we know the corrected codeword is c={0101010}</p><p>Let&#8217;s continue with some more formal definitions. The <strong>Hamming Distance</strong> between two strings is the number of positions in which they differ.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lv2H!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lv2H!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 424w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 848w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 1272w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lv2H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png" width="618" height="151.6067415730337" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:262,&quot;width&quot;:1068,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:32085,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lv2H!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 424w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 848w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 1272w, https://substackcdn.com/image/fetch/$s_!lv2H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0f0ace41-9409-4902-9080-acfd235bea0e_1068x262.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>For example &#916;(001, 100) = 2.</p><p> The Relative Hamming Distance and the Minimum Distance are defined as:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8g7U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8g7U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 424w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 848w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 1272w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8g7U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png" width="618" height="144.51330798479088" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:246,&quot;width&quot;:1052,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:35947,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8g7U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 424w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 848w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 1272w, https://substackcdn.com/image/fetch/$s_!8g7U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff73814bc-8d8d-489e-a144-7e64f33ba2df_1052x246.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SOSJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SOSJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 424w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 848w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 1272w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SOSJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png" width="616" height="138.45333333333335" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:236,&quot;width&quot;:1050,&quot;resizeWidth&quot;:616,&quot;bytes&quot;:27199,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SOSJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 424w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 848w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 1272w, https://substackcdn.com/image/fetch/$s_!SOSJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ff3824c-9f79-4bba-ab86-7963a68f3e82_1050x236.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p>The <strong>distance of a code</strong> is the minimum Hamming distance between two distinct codewords in the code.</p></blockquote><p>For example, the distance of {001, 010, 100, 111} is 2.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!p0U5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!p0U5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 424w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 848w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 1272w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!p0U5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png" width="1270" height="338" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:1270,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:55988,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!p0U5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 424w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 848w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 1272w, https://substackcdn.com/image/fetch/$s_!p0U5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55e53945-50f0-43e1-af09-b3ba49c2b7e7_1270x338.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Another definition of the volume of a Hamming Ball can be defined as: </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JrUn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JrUn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 424w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 848w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 1272w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JrUn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png" width="564" height="186.3862660944206" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:308,&quot;width&quot;:932,&quot;resizeWidth&quot;:564,&quot;bytes&quot;:56339,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JrUn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 424w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 848w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 1272w, https://substackcdn.com/image/fetch/$s_!JrUn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02d96bf4-1a17-4d31-818c-89e895a4dd84_932x308.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V8Gk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V8Gk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 424w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 848w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 1272w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V8Gk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png" width="550" height="137.7995642701525" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:230,&quot;width&quot;:918,&quot;resizeWidth&quot;:550,&quot;bytes&quot;:22223,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!V8Gk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 424w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 848w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 1272w, https://substackcdn.com/image/fetch/$s_!V8Gk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb832d685-fea5-4e9d-bf90-aed9fb10bf17_918x230.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Example </strong>Given the following code C = {001,101,100,111} where first 2 bits are information bits, the Hamming Ball can be calculated as follows: </p><ul><li><p><strong>Calculate the Hamming Distance:</strong> For each codeword, calculate the Hamming distance to all other codewords within the specified radius. Let's say we want to calculate the Hamming ball of radius 1.</p><ul><li><p>For codeword 001: Hamming distance of 1 to 010 and 100.</p></li><li><p>For codeword 010: Hamming distance of 1 to 001 and 111.</p></li><li><p>For codeword 100: Hamming distance of 1 to 001 and 111.</p></li><li><p>For codeword 111: Hamming distance of 1 to 010 and 100.</p></li></ul></li><li><p><strong>Count the Codewords within the Radius:</strong> Count the number of codewords that fall within the specified radius from each codeword.</p><ul><li><p>For codeword 001: 2 codewords (010, 100) within radius 1.</p></li><li><p>For codeword 010: 2 codewords (001, 111) within radius 1.</p></li><li><p>For codeword 100: 2 codewords (001, 111) within radius 1.</p></li><li><p>For codeword 111: 2 codewords (010, 100) within radius 1.</p></li></ul></li><li><p><strong>Calculate the Volume:</strong> The volume of the Hamming ball is the sum of the counts of codewords within the radius from each codeword.</p><ul><li><p>Volume = 2 (from 001) + 2 (from 010) + 2 (from 100) + 2 (from 111) = 8.</p></li></ul></li></ul><p></p><h3>Detecting and Correcting Errors</h3><p>The error-correcting capability of a code refers to its ability to detect and correct errors that occur during transmission. A code with a larger minimum distance can correct more errors.</p><p>Detecting errors involves finding out if an error has occurred.</p><blockquote><p>We say a code can<strong> detect d errors</strong> if the following holds: <br><em>Whenever a codeword is transmitted across a channel capable of producing d bit-errors, it is possible to deduce whether an error occurred using only the received word.</em></p><p><strong>If the distance of a code is d, then the code can detect exactly d&#8722;1 errors.</strong></p></blockquote><p><strong>Proof </strong>If a channel can produce at most d&#8722;1 bit-errors, it cannot turn one codeword into another by flipping bits, because the minimum Hamming distance between any two codewords is at least d. Thus, if the received word c&#8217; is not in the code, we can deduce that some errors occurred. The code is not guaranteed to detect d errors, because one codeword can be transformed into another by d bit-errors.</p><blockquote><p>We say a code can <strong>correct d errors</strong> if the following holds: <br><em>Whenever a codeword is transmitted across a channel capable of producing d bit-errors, one can deduce which codeword was sent using only the received word.<br><br></em><strong>If the distance of a code is d, then the code can correct exactly </strong>&#8970;<strong>(d &#8722; 1)/2</strong>&#8971;<strong> errors.</strong></p></blockquote><p><strong>Proof</strong> Suppose <em>C</em> is a code of distance <em>d</em>, and a codeword <em>c</em> is sent across a channel capable of producing at most &#8970;(d &#8722; 1)/2&#8971; bit-errors. Consider we have received the word <em>c&#8217;</em> that is the result of transmitting the codeword <em>c</em> and introducing some errors. We claim that <em>c</em> is the only codeword within distance &#8970;(d &#8722; 1)/2&#8971; of <em>c&#8217;</em>. If there were another codeword, say c&#8217;&#8217;, then it would mean &#916;(c,c&#8217;&#8217;) &#8804; 2&#8970;(d &#8722; 1)/2&#8971; &#8804; d-1 (which is a contradiction based on the properties of non-overlapping neighborhoods)</p><p><strong>Example</strong> The following code C = {001,101,100,111} has distance 2 which means it can detect 1 error, but it can correct 0 errors.</p><blockquote><p><strong>A code with minimum Hamming distance d between its codewords, can:</strong></p><ul><li><p><strong>detect at most d-1 errors</strong></p></li><li><p><strong>correct at most &#8970;(d &#8722; 1)/2&#8971; errors</strong></p></li></ul></blockquote><p></p><h3>How much information can we send: the Rate</h3><p>The rate of a code is defined as the ratio of the number of information bits to the total number of bits in a codeword, which is a value between [0,1]. The closer to 1 the better.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!75Tw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!75Tw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 424w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 848w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 1272w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!75Tw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png" width="1250" height="368" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:368,&quot;width&quot;:1250,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:53292,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!75Tw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 424w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 848w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 1272w, https://substackcdn.com/image/fetch/$s_!75Tw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe51b4e6e-3765-4ccc-a5ef-6b5776957add_1250x368.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Given the following code <em>C</em>={000,101,011,110} where let&#8217;s say we use an encoding where the 2 bits are the information bits. This means our rate is: <br>R = number of information bits/length of the codeword = 2/3</p><p>In coding theory, higher-rate codes are desirable because they allow more information to be transmitted per unit of time or bandwidth. However, increasing the rate often comes at the cost of reduced error-correction capability, as higher-rate codes typically have shorter minimum distances. Therefore, there is often a trade-off between rate and error-correction performance in code design.</p><p></p><h3>Rate and Distance Trade-off: the Hamming Bound</h3><p>How does rate and distance influence each other?</p><ul><li><p><strong>Higher Rate, Lower Distance:</strong> Increasing the rate of a code often comes at the expense of reducing its distance. This is because increasing the rate typically involves reducing redundancy in the codewords, which means fewer bits are available for error detection and correction. As a result, the distance of the code decreases, making it less capable of correcting errors.</p></li><li><p><strong>Lower Rate, Higher Distance:</strong> Conversely, decreasing the rate of a code allows for more redundancy to be added to the codewords, which increases the distance of the code. This provides better error-correcting capability but at the cost of transmitting fewer information bits per codeword.</p></li></ul><p>Increasing the rate improves efficiency but may reduce error-correcting capability, while increasing the distance enhances error-correcting capability but may reduce efficiency.</p><p>So what is the best trade-off between rate and distance? The Hamming Bound establishes some limitations of how good this trade off can be.</p><p>Let&#8217;s try and visualise this. Let&#8217;s say we have our alphabet &#8721; with codewords c in code C, where C has distance d. All of the codewords neighborhoods are non-overlapping since each ball has radius &#8970;(d &#8722; 1)/2&#8971; and the distance between any 2 codewords is d. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uQhU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uQhU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 424w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 848w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 1272w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uQhU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png" width="1456" height="667" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/efb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:667,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:125382,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uQhU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 424w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 848w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 1272w, https://substackcdn.com/image/fetch/$s_!uQhU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefb8224b-55a1-4829-a01d-4a945860cfc7_2402x1100.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>From this we can get an idea of what the Hamming Bound is all about. We have those |C| disjoint balls of radius &#8970;(d &#8722; 1)/2&#8971;but all of those balls need to fit in &#8721;^n, so we can&#8217;t have too many balls. Therefore, we can&#8217;t have too many codewords and the rate of the code can&#8217;t be too big.  All those disjoint Hamming Balls need to live in &#8721;^n</p><p>Using the definition for the volume of the Hamming Ball, we can define the following:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kTyJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kTyJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 424w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 848w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 1272w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kTyJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png" width="1456" height="1001" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1001,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:236970,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kTyJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 424w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 848w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 1272w, https://substackcdn.com/image/fetch/$s_!kTyJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a245bef-8f5b-4ea1-996a-c4e87b9487ed_1716x1180.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The Hamming Bound gives us a bound in terms of the distance and rate and is important in coding theory as it places an upper limit on the number of distinct codewords that can be encoded using a block of symbols of particular length, to provide a specified error-correcting capability.</p><h3>Conclusion</h3><p>As this article is getting quite long and there are still a lot of things to talk about we&#8217;ll stop here. Now that we have most of the definitions and theorems defined, next article will cover more practical examples on how to calculate the Hamming Bound for different codes. We&#8217;ll also discuss what are generator and parity matrixes and how to decode corrupted codewords.</p><h3>Resources:</h3><ul><li><p>Huge appreciation to <a href="https://sites.google.com/site/marywootters">Mary Wootters</a>&#8217; for her free <a href="https://www.youtube.com/playlist?list=PLkvhuSoxwjI_UudECvFYArvG0cLbFlzSr">Algebraic Coding Theory YT Lessons</a></p></li></ul>]]></content:encoded></item><item><title><![CDATA[The All-in-One Compendium]]></title><description><![CDATA[A place to keep track of all the articles published and to come. Provides an easy way to access all discussed topics. Updated as needed.]]></description><link>https://blog.diana-enache.com/p/the-all-in-one-compendium</link><guid isPermaLink="false">https://blog.diana-enache.com/p/the-all-in-one-compendium</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 10 May 2024 07:07:33 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/bb7b6114-b0f4-4121-a61c-d32639b23971_2694x1484.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Articles with no hyperlink are topics under draft and which will be published in the near future.</p><h3>System Design</h3><ol><li><p><strong>SYSTEM DESIGN COMPONENTS</strong></p><ol><li><p><strong>CACHING</strong></p><ol><li><p><a href="https://open.substack.com/pub/thecraftydev/p/mastering-cache-management-eviction?r=17kelr&amp;utm_campaign=post&amp;utm_medium=web&amp;showWelcomeOnShare=true">Mastering Cache Management: Eviction, Invalidation, and Optimal Data Access Strategies</a></p></li><li><p><a href="https://thecraftydev.substack.com/p/least-frequently-used-lfu-cache-design">Least Frequently Used (LFU) Cache - Design and Implementation</a></p></li></ol></li></ol></li><li><p><strong>PATTERNS OF DISTRIBUTED TRANSACTIONS</strong></p><ol><li><p><a href="https://blog.theengineeringcompass.com/p/understanding-the-dual-write-challenge">Understanding the Dual-Write Challenge in Multi-System Environments</a></p></li></ol></li><li><p><strong>MICROSERVICES</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/refactoring-legacy-code-the-strangler">Refactoring legacy code: The Strangler Fig Migration Pattern</a></p></li></ol></li><li><p><strong>REAL WORLD SCALING STORIES</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/common-microservice-failures-and">Common Microservice Failures and How DoorDash Mitigates Them</a></p></li><li><p><a href="https://blog.theengineeringcompass.com/p/payments-systems-at-scale-lessons">Payments Systems at Scale: Lessons from Uber's Unified Platform</a></p></li></ol></li><li><p><strong>UNDER THE HOOD - HOW EXISTING SYSTEMS WORK</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/from-pixels-to-information-a-comprehensive">From Pixels to Information: A Comprehensive Guide to QR Codes</a></p><ol><li><p><a href="https://thecraftydev.substack.com/p/coding-theory-hamming-distance-code">Coding Theory: Hamming Distance, Code, and Bound</a></p></li></ol></li><li><p><a href="https://blog.theengineeringcompass.com/p/advanced-data-structures-for-discrete">Advanced Data Structures for Discrete Event Simulation</a></p></li><li><p><a href="https://blog.theengineeringcompass.com/p/decentralised-social-media-explained">Decentralised Social Media Explained: Inside Bluesky's Federated Architecture</a></p></li></ol></li><li><p><strong>OPTIMISATION TECHNIQUES</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/optimising-performance-the-cool-magic">Optimising Performance: The Cool Magic of Bloom Filters</a></p></li></ol></li><li><p><strong>MY LEARNINGS</strong></p><ol><li><p><a href="https://blog.theengineeringcompass.com/p/building-an-audit-logging-solution">Building an Audit Logging Solution: Production Scars and Evolutionary Architectures</a></p><p></p></li></ol></li></ol><h3>Engineering Interviews</h3><ol><li><p><strong>INTERVIEWS&#8217; HOW-TO&#8217;S </strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/cracking-the-product-interview-for">Cracking the Product Interview for Engineers</a></p></li><li><p><a href="https://thecraftydev.substack.com/p/cracking-the-system-design-interview">Cracking the System Design Interview</a></p></li><li><p><a href="https://thecraftydev.substack.com/p/red-flags-to-look-for-in-software">Red Flags to Look for in Software Engineering Interviews</a></p></li></ol></li><li><p><strong>DATA STRUCTURES &amp; ALGORITHMS</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/least-frequently-used-lfu-cache-design">LFU Cache</a> (<a href="https://leetcode.com/problems/lfu-cache/description/">leetcode 460</a>)</p></li><li><p><a href="https://thecraftydev.substack.com/p/optimising-performance-the-cool-magic">Bloom Filter Data Structure</a></p></li></ol></li></ol><p></p><h3>Golang in Depth</h3><ol><li><p><strong>MEMORY MANAGEMENT</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/breaking-free-navigating-golangs">Breaking Free: Navigating Golang's Memory Management</a></p></li></ol></li><li><p><strong>CONCURRENCY</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/cracking-the-go-concurrency-basics">Cracking the Go Concurrency Basics - Part 1</a></p></li><li><p><a href="https://thecraftydev.substack.com/p/beyond-goroutines-exploring-advanced">Beyond Goroutines: Exploring Advanced Concurrency Patterns in Go - Part 2</a></p></li></ol></li><li><p><strong>GOTCHAS</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/unbreaking-the-loop-go-122-fixes">Unbreaking the Loop: Go 1.22 fixes for loops</a></p></li></ol></li></ol><p></p><h3>Coding Theory</h3><ol><li><p><a href="https://thecraftydev.substack.com/p/coding-theory-hamming-distance-code">Coding Theory: Hamming Distance, Code, and Bound</a></p></li></ol><p></p><h3>Career Growth/Leadership</h3><ol><li><p><strong>BUILDING TEAMS</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/the-bear-how-a-show-about-a-restaurant">The Bear: How a show about a restaurant provides the best leadership takeaways</a></p></li></ol></li><li><p><strong>MENTORSHIP</strong></p><ol><li><p><a href="https://thecraftydev.substack.com/p/mentorship-that-matters-building">Mentorship That Matters: Building Effective Knowledge-Sharing Systems</a></p></li></ol><p></p></li></ol><h3>Book Reviews</h3><ol><li><p><a href="https://thecraftydev.substack.com/p/book-review-the-worlds-i-see-curiosity">Book Review: "The Worlds I See: Curiosity, Exploration, and Discovery at the Dawn of AI", by Dr. Fei-Fei Li</a></p><p></p></li></ol><h3><strong>AI/ML</strong></h3><ol><li><p><a href="https://thecraftydev.substack.com/p/crafting-conversations-techniques">Crafting Conversations: Techniques and Tips for Effective Prompting</a></p><p></p></li></ol><h3>Non-Tech</h3><ol><li><p><a href="https://thecraftydev.substack.com/p/the-lazy-to-crazy-runners-guide-to">The 'Lazy-to-Crazy' Runner's Guide to Marathon Glory</a></p></li></ol>]]></content:encoded></item><item><title><![CDATA[From Pixels to Information: A Comprehensive Guide on QR Codes]]></title><description><![CDATA[On what QR codes are, why do we use them, and how do we encode/decode information through them]]></description><link>https://blog.diana-enache.com/p/from-pixels-to-information-a-comprehensive</link><guid isPermaLink="false">https://blog.diana-enache.com/p/from-pixels-to-information-a-comprehensive</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 05 Apr 2024 15:37:11 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/9037d9a9-ef24-40b8-9e2c-14ad1d2869dc_1936x1008.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You&#8217;ve probably scanned a QR code before. They&#8217;re everywhere. But have you wondered how they work? Because this has been on my mind a lot recently. So I did some investigation and here is a summary of the key findings, notable points and fun facts.</p><p><strong>How and why QR codes came into being</strong></p><p>Masahiro Hara, a Japanese engineer who worked at Denso Wave then, invented the <strong>QR Code</strong> (or <strong>Quick Response codes</strong>) in 1994 to meet the need for a more efficient and versatile method of tracking automotive parts during manufacturing. Hara and his team developed QR codes as an improvement over traditional barcodes. </p><p><strong>QR Codes vs Barcodes</strong></p><p>Traditional barcodes could only store a limited amount of information horizontally, while QR codes can store much more data both horizontally and vertically. A barcode can store 1,556 bytes, whereas a QR Code can store up to 7,089 numeric characters, 4,296 alphanumeric characters, 2,953 bytes, and 1,817 kanji characters.</p><h1>Components</h1><p>And now let&#8217;s get into the fun part, what do all those black and white squares represent?  QR Codes consist of black and white elements called <strong>modules</strong>, where black represents 1 and white 0. These modules can be combined into meaningful binary messages.  </p><p>Before diving into how we encode/decode the information, let&#8217;s first analyse what the main components of the QR code are:</p><p><strong>Function Patterns</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!79Lu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!79Lu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 424w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 848w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 1272w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!79Lu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png" width="1456" height="441" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:441,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:124356,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!79Lu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 424w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 848w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 1272w, https://substackcdn.com/image/fetch/$s_!79Lu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db2a228-291f-49df-9f22-888a8a936b01_1994x604.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p><strong>Quiet Zone</strong> - helps the scanner to distinguish between the QR code and its surroundings, making it easier to detect the QR Code.</p></li><li><p><strong>Position Pattern</strong> - located in 3 of the QR code&#8217;s corners, it allows the scanner to quickly recognise the code orientation. This means the code can be scanned at any angle. </p></li><li><p><strong>Separator</strong> - used to keep space between the position pattern and the other information, helping the scanner to identify the position pattern.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dyK3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dyK3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 424w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 848w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 1272w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dyK3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png" width="1456" height="407" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:407,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94414,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dyK3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 424w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 848w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 1272w, https://substackcdn.com/image/fetch/$s_!dyK3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36e6f2e5-abdc-4b6a-aa5d-74588b9d3d20_1910x534.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p><strong>Alignment Pattern</strong> - used for correcting distortion by straightening the code on a curved surface. On higher versions, QR codes tend to have more alignment patterns. Ver40 which is the largest QR code consists of 46 alignment patterns. Ver1 doesn&#8217;t require any alignment patterns.</p></li><li><p><strong>Timing Pattern</strong> - helps the scanner to determine the width of a single module.</p></li></ol><p><strong>Encoding Region</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IZBr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IZBr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 424w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 848w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 1272w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IZBr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png" width="1456" height="449" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:449,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:98595,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IZBr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 424w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 848w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 1272w, https://substackcdn.com/image/fetch/$s_!IZBr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F318b5273-56dc-4c08-8a1d-c8e7a74d920e_1726x532.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ol><li><p><strong>Version Information</strong> -  there are 40 versions of QR codes, from 1 up to 40; this region specifies which version is being used.</p></li><li><p><strong>Format Information</strong> - contains information such as error correction level, mask pattern etc.</p></li><li><p><strong>Data and error correction keys</strong> - the remaining available code is used for storing the data along with the error correction data. The correction data is done by the Reed-Solomon codes which allows the QR code to still be scanned if some information is corrupted/not visible etc. </p></li></ol><p>Putting everything together:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AqV5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AqV5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 424w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 848w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 1272w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AqV5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png" width="1456" height="809" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:809,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:219623,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AqV5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 424w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 848w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 1272w, https://substackcdn.com/image/fetch/$s_!AqV5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70e35dcd-afbc-4668-b7f1-6c4c9b6725f8_2008x1116.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>How does versioning work?</h1><p>There are <strong>40 versions</strong> of the conventional QR code. Each version has a different number of data modules, commencing with Version 1 (21 &#215; 21 modules) up to Version 40 (177 &#215; 177 modules). </p><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/IysLC/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e2947fe2-46f9-4c50-8134-488c722c1fe9_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:283,&quot;title&quot;:&quot;QR Code Versioning&quot;,&quot;description&quot;:&quot;&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/IysLC/1/" width="730" height="283" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><p>With a new higher version, another 4 additional modules per side are added.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0A-3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0A-3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 424w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 848w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 1272w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0A-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png" width="1456" height="784" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:784,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:210370,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0A-3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 424w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 848w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 1272w, https://substackcdn.com/image/fetch/$s_!0A-3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51ffa78d-c310-48e8-bcf1-7df81bfb3975_1886x1016.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The higher the version, the higher the number of modules</figcaption></figure></div><p><strong>How would we know which version we need to use? </strong></p><p><a href="https://www.qrcode.com/en/about/version.html">QRCode.com</a> provides a comprehensive list of versions given the type of input data you want to use (numeric, alphanumeric, binary, kanji) and the correction level. </p><p>Let&#8217;s say I want to create a QR Code for the text &#8220;The Crafty Dev&#8220; which means we&#8217;re only interested in a 14 letter alphanumeric input. And let&#8217;s suppose we&#8217;re going to stick the QR code in various places outside, so we need a pretty high error correction capability (in case it rains or it gets stained etc). We need to find a figure in the QRCode versioning table, that is 14 or over and with an H correction level, so a good version for us to use would be version 2.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ebeM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ebeM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 424w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 848w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 1272w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ebeM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png" width="636" height="430.6978021978022" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:986,&quot;width&quot;:1456,&quot;resizeWidth&quot;:636,&quot;bytes&quot;:221094,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ebeM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 424w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 848w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 1272w, https://substackcdn.com/image/fetch/$s_!ebeM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ce96fa0-70d4-4d36-9820-127c617f7401_1474x998.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Finding the QR Code version we can use for encoding the text &#8220;The Crafty Dev&#8220;</figcaption></figure></div><h1>The Error Correction Level</h1><p>The QR code error correction is based on the Reed-Solomon algorithm which works in a similar way to the parity error correction - it adds extra bits to the data so that errors can be corrected.</p><blockquote><p><strong>Fun Fact</strong></p><p>The Voyager spacecraft uses the Reed-Solomon error correction to ensure that data transmitted from the outer reaches of the solar system can be reconstructed even if it encounters interference, which is highly probable. Given that signals from Voyager 1 can take up to 20 hours to reach Earth, employing an ARQ (Automatic Repeat reQuest) mechanism to request retransmission in case of errors would be highly impractical. (<a href="https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction#:~:text=In%201977%2C%20Reed%E2%80%93Solomon%20codes,Reed%E2%80%93Solomon%20codes%20are%20used.">source</a>)</p></blockquote><p>The way Reed-Solomon does encoding deserves a completely different article so I&#8217;ll link it here once it&#8217;s published, so stay tuned.</p><h1>Unmasking</h1><p>Now comes the crazy part. Let&#8217;s say we have a QR code and we want to manually decode it. Where do we start? The first thing we&#8217;ll need to look at is the Format Information section.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!onjw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!onjw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 424w, https://substackcdn.com/image/fetch/$s_!onjw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 848w, https://substackcdn.com/image/fetch/$s_!onjw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 1272w, https://substackcdn.com/image/fetch/$s_!onjw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!onjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png" width="574" height="357.96153846153845" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:908,&quot;width&quot;:1456,&quot;resizeWidth&quot;:574,&quot;bytes&quot;:163041,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!onjw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 424w, https://substackcdn.com/image/fetch/$s_!onjw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 848w, https://substackcdn.com/image/fetch/$s_!onjw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 1272w, https://substackcdn.com/image/fetch/$s_!onjw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3153a3c-e662-40db-97df-624052fb8fcf_1654x1032.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The first 2 modules (colored red in the picture above) will give you the error correction level. And the next 3 (colored green) will give you the mask pattern.</p><p>This means we have a <strong>00</strong> error correction level, which means high, 30% of data bytes can be restored; and a <strong>111</strong> masking pattern. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dGTv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dGTv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 424w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 848w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 1272w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dGTv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png" width="1456" height="700" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8687091-250e-48fb-99b0-7870a399922f_1640x788.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:700,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:257246,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dGTv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 424w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 848w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 1272w, https://substackcdn.com/image/fetch/$s_!dGTv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8687091-250e-48fb-99b0-7870a399922f_1640x788.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The QR codes are usually masked, so we&#8217;ll have to take off the mask first. We put the coordinates of the current bit into the formula, and if the result is 0, we use the opposite bit of that. After removing the mask on the original QR code, we&#8217;ll get the following unmasked QR code:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bl1x!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bl1x!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 424w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 848w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 1272w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bl1x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png" width="1456" height="526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:526,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:175182,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bl1x!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 424w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 848w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 1272w, https://substackcdn.com/image/fetch/$s_!bl1x!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe566cca1-b67f-4902-9bf9-f0a8e2caf52a_1864x674.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1>Decoding</h1><p>Now, finally getting into the decoding part. The way a QR code is read is from bottom right to top left following the path shown below. The first 4 bits give us the encoding and the next 8 the length of the message.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Htod!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Htod!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 424w, https://substackcdn.com/image/fetch/$s_!Htod!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 848w, https://substackcdn.com/image/fetch/$s_!Htod!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 1272w, https://substackcdn.com/image/fetch/$s_!Htod!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Htod!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png" width="1456" height="554" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:554,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:303077,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Htod!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 424w, https://substackcdn.com/image/fetch/$s_!Htod!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 848w, https://substackcdn.com/image/fetch/$s_!Htod!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 1272w, https://substackcdn.com/image/fetch/$s_!Htod!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4541514-8cf6-4408-b63d-57ee758f2926_1960x746.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This means, for our QR code, we would have a <strong>0100</strong> encoding, which is a <strong>byte encoding</strong> and a <strong>00001110</strong> message length which is <strong>14</strong>. Byte encoding tells us we&#8217;re expecting blocks of <strong>8 bits</strong> and that <strong>we're expecting 14 of them.</strong> </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ULga!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ULga!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 424w, https://substackcdn.com/image/fetch/$s_!ULga!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 848w, https://substackcdn.com/image/fetch/$s_!ULga!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 1272w, https://substackcdn.com/image/fetch/$s_!ULga!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ULga!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png" width="686" height="371.2692307692308" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:788,&quot;width&quot;:1456,&quot;resizeWidth&quot;:686,&quot;bytes&quot;:163048,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ULga!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 424w, https://substackcdn.com/image/fetch/$s_!ULga!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 848w, https://substackcdn.com/image/fetch/$s_!ULga!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 1272w, https://substackcdn.com/image/fetch/$s_!ULga!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F114ab16f-4d48-4011-b3a3-2d82786680a3_1998x1082.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;re going to continue splitting our QR code into blocks of 8 bits and following the pattern of interleaved blocks below, we should have something similar to the right picture below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bRcx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bRcx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 424w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 848w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 1272w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bRcx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png" width="1456" height="773" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:773,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:409870,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bRcx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 424w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 848w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 1272w, https://substackcdn.com/image/fetch/$s_!bRcx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F701148d5-ba44-4c3d-86d6-ec3ca4f98bf0_1988x1056.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can see we have indeed 14 blocks. The last block containing the 4 bits of white modules is the <strong>End of message (Terminator) </strong>representing the end of our message. Now that we have our blocks splitted we can start decoding each character. We do this by summing up all the turned on (black) bits and converting the decimal number obtained into its specific character using an ASCII Table. <br><br>This will give us the following:<br><br>64+16+4 = 84 representing T<br>64+32+8 = 104 representing h <br>64+32+4+1 = 101 representing e<br>32 representing " "<br>64+2+1 = 67 representing C<br>64+32+14+4 = 114 representing r<br>64+32+1 = 97 representing a<br>64+32+4+2 = 102 representing f<br>64+32+16+4 = 116 representing t <br>64+32+16+8+1 = 121 representing y<br>32 representing " "<br>64+4 = 68 representing D <br>64+32+4+1 = 101 representing e <br>64+32+16+4+2 = representing v</p><p>We finally decoded our QR code which says &#8220;The Crafty Dev&#8220; &#128588; &#128588; &#128588; .<br><br>The rest of the QR code message is part of the error correction data which we&#8217;ll have a look at in the next article, where we&#8217;ll talk about how QR Codes do error correction while we&#8217;ll also dive into how the Reed-Solomon algorithm works.</p><h1>Conclusion</h1><p>In this article we had a general look at QR Codes, their main components, what unmasking is and how it works, and finally how to decode the QR Code message.</p><p><strong>Stay tuned, </strong>as in the upcoming articles we'll dive deeper into:</p><ol><li><p><strong>On Error Correction: From Hamming to Reed-Solomon</strong></p></li><li><p><strong>QR Code Models</strong></p></li></ol><p>See you soon <a href="https://emojipedia.org/waving-hand">&#128075;</a></p>]]></content:encoded></item><item><title><![CDATA[Least Frequently Used (LFU) Cache - Design and Implementation]]></title><description><![CDATA[On how LFU works and how to implement an optimal solution.]]></description><link>https://blog.diana-enache.com/p/least-frequently-used-lfu-cache-design</link><guid isPermaLink="false">https://blog.diana-enache.com/p/least-frequently-used-lfu-cache-design</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Thu, 21 Mar 2024 13:40:34 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b6d5ed26-a480-4ed8-88ec-8244f2c6b4dc_2815x1420.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3>What is LFU Cache?</h3><p>The basic principle of LFU caching is to remove the least frequently used items first when the cache reaches its capacity limit. This means that the cache keeps track of the frequency with which each item is accessed, and when it needs to make space for a new item, it removes the item that has been accessed the least frequently.</p><h3>LFU Cache Design</h3><p>To implement a <a href="https://en.wikipedia.org/wiki/Least_frequently_used">Least Frequently Used (LFU)</a> cache solution we need to design a data structure that would support the following operations:</p><ul><li><p><code>LFUCache(int capacity)</code> - initialises the object with the capacity of the data structure.</p></li><li><p><code>int get(int key)</code> - gets the value of the <code>key</code> if the <code>key</code> exists in the cache. Otherwise, returns <code>-1</code>.</p></li><li><p><code>void put(int key, int value)</code> - updates the value of the <code>key</code> if present, or inserts the <code>key</code> if not already present. When the cache reaches its <code>capacity</code>, it should invalidate and remove the <strong>least frequently used</strong> key before inserting a new item. For this problem, when there is a <strong>tie</strong> (i.e., two or more keys with the same frequency), the <strong>least recently used</strong> <code>key</code> would be invalidated.</p></li></ul><p>The frequency for a <code>key</code> is incremented whenever a <code>get</code> or a <code>put</code> operation is called on the element.</p><p>Let&#8217;s run through an example to better understand how this would work:</p><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/9X8cO/2/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2196645c-cf6c-49cb-9828-60dd9f51cfea_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:489,&quot;title&quot;:&quot;LFU Cache step-by-step operations&quot;,&quot;description&quot;:&quot;&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/9X8cO/2/" width="730" height="489" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><ol><li><p>Let&#8217;s say we have a cache with a capacity of 2 <code>LFU(2)</code> and we call <code>put(1,1)<br></code>&#8594; Our LFU will become <code>{1,1,1}</code> as we add the<code> key 1</code> with <code>value 1</code> which currently has a <code>frequency of 1</code></p></li><li><p>Then we call <code>put(2,2)</code> &#8594; Our LFU will become <code>{2,2,1},{1,1,1}</code>. We added a new element with <code>key 2 </code>and <code>value 2</code>  which currently has a <code>frequency of 1</code>. Since elements with key 1 and 2 have the same frequency of 1, we add the most recently used element (the one with key 2) to the front of the list. </p></li><li><p>Then we call <code>get(1)</code>. Our LFU will return 1 and the new LFU will be <code>{1,1,2}, {2,2,1}</code>. This is because we increase the frequency of element with key 2 to 2 (due to the read operation) and we move it to the front of the list because it has higher frequency than the other element.</p></li><li><p>Then we call <code>put(3, 3)</code>. Our LFU will become <code>{1,1,2}, {3,3,1}</code>. A new element comes in but our cache is at full capacity so we need to remove an element. We remove from the end of the list (as this is where the element with the least frequency and least recency is stored), so <code>{2,2,1}</code> gets removed. We then add the new element with a frequency of 1, <code>{3,3,1}</code>.</p></li><li><p>We call <code>get(2)</code>. We return <code>-1</code> as the element no longer exists in the cache.</p></li><li><p>We call <code>get(3)</code>. We return <code>3</code> and the LFU becomes <code>{3,3,2}, {1,1,2}</code>. We increase the frequency of 3 and move it before key 1 as even though they have the same frequency, key 3 has been accessed more recently.  </p></li><li><p>We call <code>put(4,4)</code>. The LFU becomes <code>{3,3,2}, {4,4,1}</code>. New element comes in, cache is full, we remove the last element with key 1 and add the new element with key 4.</p></li><li><p>We call <code>get(1)</code>. We return <code>-1</code> as element is no longer present in the cache.</p></li><li><p>We call <code>get(3)</code>. We return <code>3</code> and our LFU becomes <code>{3,3,3}, {4,4,1}</code>. The read operation increases the frequency of element at key 3 to 3.</p></li><li><p>We call <code>get(4)</code>. We return <code>4</code> and our LFU becomes <code>{3,3,3}, {4,4,2}</code>.</p></li></ol><p></p><h3>Possible Implementations</h3><ul><li><p>Using a <strong>brute force approach</strong>, we could have an array with the size of the cache. Each element in the array would store the item along with its frequency and last access time. When we get an element, we&#8217;d have to iterate through the array to find our element and update the item with the incremented frequency value. This means getting an element would be on average <strong>O(n)</strong>. To put an item when the cache is full, we&#8217;d have to look for an item to evict. So we&#8217;ll iterate through the whole array and look for the item with the least frequency and oldest access time. This would take <strong>O(n).</strong></p></li><li><p>Using a <strong>min-heap data structure</strong> would be an improvement - each node will hold the cached element along with its frequency. This makes it easy to remove an element when the cache is full as the least recently and frequently used element will be a the top. The problem comes when inserting or reading from the cache, as any operation will mean the min-heap will need to be rebalanced which will take <strong>O(logn)</strong>. </p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!y6y4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!y6y4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 424w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 848w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!y6y4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg" width="372" height="221.00274725274724" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:865,&quot;width&quot;:1456,&quot;resizeWidth&quot;:372,&quot;bytes&quot;:105120,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!y6y4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 424w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 848w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!y6y4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff58d03f3-e3c7-4cac-9983-92f399dad26f_2594x1541.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><h3>Solution in constant time</h3><p>To implement an LFU that uses constant time for pushing and getting items, we&#8217;re going to need 2 data structures: a key map and a frequency map.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WWtZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WWtZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 424w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 848w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WWtZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg" width="602" height="219.54807692307693" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:531,&quot;width&quot;:1456,&quot;resizeWidth&quot;:602,&quot;bytes&quot;:164010,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WWtZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 424w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 848w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!WWtZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65739174-1f37-4803-adf8-546ea99661c5_3311x1207.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><ul><li><p><strong>Key Map</strong> - stores the key of the item and the item node containing the key, value and frequency.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GvFO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GvFO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 424w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 848w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 1272w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GvFO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png" width="620" height="358.0281690140845" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:820,&quot;width&quot;:1420,&quot;resizeWidth&quot;:620,&quot;bytes&quot;:104722,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GvFO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 424w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 848w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 1272w, https://substackcdn.com/image/fetch/$s_!GvFO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2963e70-5e45-41be-9451-cb106ef2ee10_1420x820.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/fd3b7d62793eb150f827ce1a2639857c">gist</a></figcaption></figure></div><ul><li><p><strong>Frequency Map</strong> - stores the frequency as key and a doubly linked list of all the cached items having that frequency as value. The items in the doubly linked list are sorted by recency such that most recently accessed items will be at the head of the list and the least recently used at the tail. Due to the nature of the doubly linked list, removing, adding and updating a cached item will take O(1).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xMyI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xMyI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 424w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 848w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 1272w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xMyI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png" width="626" height="303.3826815642458" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1432,&quot;resizeWidth&quot;:626,&quot;bytes&quot;:114052,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xMyI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 424w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 848w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 1272w, https://substackcdn.com/image/fetch/$s_!xMyI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41e76fcc-f0ba-4302-94a6-b98ebdf25ee1_1432x694.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/edb527ea14cf62867341966704a9e227">gist</a></figcaption></figure></div><p>Given those two data structures, the cache operations would have the following logic:</p><ul><li><p><code>get</code> operation for an item X:</p><ul><li><p>check that X is in our key map. If it doesn&#8217;t exist return -1. Otherwise get the frequency from the value node.</p></li><li><p>for the frequency we&#8217;ve obtained above, get the doubly linked list from the frequency map. Delete the node from the doubly linked list. Create a new node with the incremented frequency and added it to the frequency map at the head of the doubly linked list.</p><p><br>Following the above instructions, the implementation for the <code>get</code> operation should look similar to this:</p></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!myLs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!myLs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 424w, https://substackcdn.com/image/fetch/$s_!myLs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 848w, https://substackcdn.com/image/fetch/$s_!myLs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 1272w, https://substackcdn.com/image/fetch/$s_!myLs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!myLs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png" width="698" height="703.9236209335219" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1426,&quot;width&quot;:1414,&quot;resizeWidth&quot;:698,&quot;bytes&quot;:258227,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!myLs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 424w, https://substackcdn.com/image/fetch/$s_!myLs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 848w, https://substackcdn.com/image/fetch/$s_!myLs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 1272w, https://substackcdn.com/image/fetch/$s_!myLs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F040e5341-9e77-4ac5-9746-819582e864b8_1414x1426.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/d0282a95407825fab8739e1c9b46ec3c.git">gist</a></figcaption></figure></div><ul><li><p><code>put</code> operation for an item X:</p><ul><li><p>if the item already exists in the cache, it returns the node and updates the frequency (hence, we call <code>getNode </code>which does all of these)</p></li><li><p>if the item doesn&#8217;t exist in the cache:</p><ul><li><p>if the cache is full - we remove the least recently and frequency used item, which should be the tail of the doubly linked list value in the frequency map with the minimum frequency.</p></li><li><p>then we add the item at the head of the doubly linked list with has the frequency of <code>1</code></p></li></ul><p><br>Considering the above, the implementation for the put operation would look like: </p></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fnz1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fnz1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 424w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 848w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 1272w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fnz1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png" width="666" height="714.9152542372881" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1520,&quot;width&quot;:1416,&quot;resizeWidth&quot;:666,&quot;bytes&quot;:313511,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fnz1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 424w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 848w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 1272w, https://substackcdn.com/image/fetch/$s_!fnz1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18d8d068-2ae8-4c66-b274-1c42c4c01047_1416x1520.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/810c91ff8a5caa9dcc498be6f727608a.git">gist</a></figcaption></figure></div><p>Putting everything together, the code should look like <a href="https://gist.github.com/b0367f776eed85e5a4c24dce48070314.git">this</a>. </p><h3>Conclusion</h3><p>In this article we had a look on how we would go about implementing an LFU. </p><p>To learn more about caching, have a look at the following articles:</p><ul><li><p><a href="https://thecraftydev.substack.com/p/mastering-cache-management-eviction">Mastering Cache Management: Eviction, Invalidation, and Optimal Data Access Strategies</a></p></li></ul><p>Or <strong>stay tuned </strong>as in the upcoming articles, we'll dive deeper into:</p><ul><li><p><strong>Types of Caches:</strong> Exploring different caching mechanisms like in-memory, distributed, and CDN caching.</p></li><li><p><strong>Deployment Strategies:</strong> Understanding how to integrate caching into your architecture.</p></li><li><p><strong>Metrics:</strong> How to monitor cache performance for optimal results.</p></li><li><p><strong>Implementation Solutions:</strong> Practical examples with popular caching tools and libraries.</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Mastering Cache Management: Eviction, Invalidation, and Optimal Data Access Strategies]]></title><description><![CDATA[On caching, what it is, when to use it, benefits, eviction policies, invalidation strategies and read/write flows]]></description><link>https://blog.diana-enache.com/p/mastering-cache-management-eviction</link><guid isPermaLink="false">https://blog.diana-enache.com/p/mastering-cache-management-eviction</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Wed, 06 Mar 2024 11:43:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A cache is a temporary storage area that improves performance by storing data closer to where it's needed, minimising trips to main storage or external systems. Think of a cache like your browser saving recently visited websites: instead of reloading everything from the internet, it provides a faster experience by using the local copy.</p><p><strong>When to use it</strong> :</p><ul><li><p>for <strong>frequently accessed data</strong> - e.g. website assets, session data, popular content</p></li><li><p>for <strong>expensive operations</strong> - database queries (if the same complex query is frequently executed), API calls (e.g. weather data, currency conversions), compute intensive calculations</p></li><li><p>for times <strong>when speed is paramount</strong> - high-traffic websites (reduce strain on the backend by serving frequent requests), search suggestion (autocomplete features often use a cache to display results instantly as the user types)</p></li></ul><p>When accessing data from a cache, there's a best-case and a fallback scenario. A <strong>cache hit</strong> means the data was found in the cache, leading to a speedy response. A <strong>cache miss</strong> means the data wasn't present, so the system will need to fetch it from the slower main storage.</p><p><strong>What to consider:</strong> </p><ul><li><p><strong>Amdahl&#8217;s Law</strong> - <em>&#8220;the overall performance improvement gained by optimizing a single part of a system is limited by the fraction of time that the improved part is actually used&#8221;</em></p><ul><li><p><strong>Diminishing Returns:</strong> Cache hit rates matter immensely. The initial gains from adding a cache are often dramatic. But, according to Amdahl's Law, as you reach 90, 95, 98% hit rates, the effort to squeeze more performance decreases in effectiveness.</p></li></ul></li><li><p><strong>Pareto Distribution</strong> - &#8220;also known as the &#8220;80-20 rule&#8221; stating that 80% of outcomes are due to 20% of causes&#8220;</p><ul><li><p><strong>Cache Misses Are </strong><em><strong>Not</strong></em><strong> Evenly Distributed:</strong> In many systems, some data is far more popular than others. Picture the top 10% of products on an e-commerce site getting 60% of the traffic. This skewed access pattern aligns with the Pareto principle. To get the caching wins, you need to first analyse your data to see if the access patterns truly exhibit Pareto-like characteristics.</p></li></ul></li></ul><h3>Cache Benefits</h3><ul><li><p><strong>Faster Access and</strong> <strong>Reduced Latency</strong> - serving data from in-memory stores vs. databases or external APIs offers massive latency gains.</p></li><li><p><strong>Bandwidth Optimisation</strong> - when external API calls or media assets are cached, you can potentially decrease bandwidth usage.</p></li><li><p><strong>Reduced Database Costs </strong>- databases are often a significant cost factor. Offloading queries to a cache can directly lower your database-related expenses.</p></li><li><p><strong>Improved Throughput</strong> - when a cache satisfies a request, your main database or origin servers are untouched. This allows your system to handle more concurrent users without overwhelming core resources.</p></li><li><p><strong>Handle Spikes</strong> - sudden surges that would cripple an uncached system can often be served smoothly when a cache absorbs the brunt of those requests.</p></li></ul><h3>Cache Eviction Policies</h3><p>A caching eviction policy determines how a cache handles the data eviction when the cache is full. When an item is requested from the cache, it may result in a miss, which means data needs to be fetched from the database and the cache needs to be updated. But before doing that, an item needs to be removed. The following section describes several strategies on how to choose which item to pick.  </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cbDV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cbDV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 424w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 848w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cbDV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg" width="638" height="295.7760989010989" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:675,&quot;width&quot;:1456,&quot;resizeWidth&quot;:638,&quot;bytes&quot;:137996,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cbDV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 424w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 848w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!cbDV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64517f5b-88c6-468d-8fc6-a5ab229ea3af_2936x1361.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Flow when a request results in a cache miss</figcaption></figure></div><ul><li><p><strong>Queue-Based Policies</strong> </p><ul><li><p><strong>FIFO</strong> (First-In-First-Out)</p><ul><li><p><strong>Process:</strong> The first in/oldest item gets removed to make space for the new item, which is then added to the end.</p></li><li><p><strong>Pros:</strong></p><ul><li><p><strong>Simplicity:</strong> Very easy to implement and understand.</p></li><li><p><strong>Fairness (Of Sorts):</strong> Prevents any item from 'hogging' the cache indefinitely if there's a steady stream of new data.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Inefficient for Many Workloads:</strong> Real-world data access patterns often aren't purely FIFO order. An item used constantly might get evicted simply because it was added a while ago, leading to repetitive refetches of the same thing.</p></li><li><p><strong>Ignores Popularity:</strong> A popular item accessed right after eviction will need to be fetched again, even though there may be other rarely-used data in the cache.</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1nL9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1nL9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 424w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 848w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1nL9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg" width="1456" height="583" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:583,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:81096,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1nL9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 424w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 848w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!1nL9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40be41c4-cd31-4e8b-8a8b-dd402f7ecbb7_3160x1265.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Data eviction following a FIFO approach</figcaption></figure></div><ul><li><p><strong>LIFO</strong> (Last-In-First-Out)</p><ul><li><p><strong>Process:</strong> The last in/newest item gets removed to make space for the new item, which is then added to the end.</p></li><li><p><strong>Pros:</strong> Same as FIFO</p></li><li><p><strong>Cons:</strong> Same as FIFO</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qVUg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qVUg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qVUg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg" width="586" height="319.1607142857143" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:793,&quot;width&quot;:1456,&quot;resizeWidth&quot;:586,&quot;bytes&quot;:83740,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qVUg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 424w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 848w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!qVUg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F64395809-0129-4f25-9215-a28098fbff77_2709x1476.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Data eviction following a LIFO approach</figcaption></figure></div><ul><li><p><strong>Recency-Based Policies</strong></p><ul><li><p><strong>LRU</strong> (Least Recently Used)</p><ul><li><p><strong>Process:</strong> The basic principle of LRU caching is to remove the least recently used items first when the cache reaches its capacity limit. This means that the cache keeps track of the order in which items are accessed, and when it needs to make space for a new item, it removes the least recently accessed item.</p></li><li><p><strong>Pros:</strong></p><ul><li><p><strong>Effective use of cache space:</strong> LRU caching maximizes the utilization of cache space by keeping frequently accessed items in memory.</p></li><li><p><strong>Good performance:</strong> LRU caching performs well in scenarios where there is a high degree of temporal locality, such as web servers and databases.</p></li><li><p><strong>Simple implementation:</strong> The basic LRU caching algorithm is relatively simple to implement and understand.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Overhead:</strong> LRU caching requires additional data structures to keep track of access history, which can introduce overhead in terms of memory and computational resources.</p></li><li><p><strong>Not suitable for all access patterns:</strong> LRU caching may not perform optimally in scenarios with non-temporal access patterns or where access frequencies vary widely.</p></li></ul></li><li><p><strong>Possible implementation:</strong></p><ul><li><p>An LRU cache is often implemented by pairing a doubly linked list with a hash map. The linked list is set up with the most-recently used item at the head of the list and the least-recently used item at the tail.</p></li><li><p>When we get a request, we first look up the item in our hash map:</p><ul><li><p>if it&#8217;s there,  it&#8217;s a cache hit:</p><ul><li><p>we use the hash map to quickly find the corresponding linked list node.</p></li><li><p>we then move the item's linked list node to the head of the linked list, since it's now the most recently used (so it shouldn't get evicted any time soon).</p></li></ul></li><li><p>if it&#8217;s not there, it&#8217;s a cache miss: </p><ul><li><p>if the cache is full - we evit the least recently used item which should be at the end of the queue.</p></li><li><p>we create a new linked list node and add it at the head of the queue and update the hashmap where the value points to the node we&#8217;ve just inserted. </p></li></ul></li></ul></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CmXF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CmXF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 424w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 848w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CmXF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg" width="1456" height="681" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:681,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141235,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CmXF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 424w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 848w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!CmXF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f6bcb53-cb6c-4bfb-ba97-99db1c178f2d_2923x1368.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Data eviction following a LRU approach</figcaption></figure></div><ul><li><p><strong>MRU</strong> (Most Recently Used)</p><ul><li><p><strong>Process:</strong> evicts the most recently accessed data item from the cache. When a new item needs to be added to the cache and it's already at full capacity, the cache algorithm looks at the usage history of the items and evicts the most recently used one. This makes room for the new item to be added. MRU can be useful in scenarios where a small subset of items is accessed frequently.</p></li><li><p><strong>Pros</strong>: Same as LRU</p></li><li><p><strong>Cons</strong>: Same as LRU</p></li></ul></li></ul></li><li><p><strong>Frequency-Based Policies</strong></p><ul><li><p><strong>LFU</strong> (Least Frequently Used)</p><ul><li><p><strong>Process:</strong> The basic principle of LFU caching is to remove the least frequently used items first when the cache reaches its capacity limit. This means that the cache keeps track of the frequency with which each item is accessed, and when it needs to make space for a new item, it removes the item that has been accessed the least frequently.</p></li><li><p><strong>Pros:</strong></p><ul><li><p><strong>Effective use of cache space</strong>: LFU caching maximizes the utilization of cache space by keeping frequently accessed items in memory.</p></li><li><p><strong>Good performance:</strong> LFU caching performs well in scenarios where there are consistent access patterns and certain items are accessed more frequently than others.</p></li><li><p><strong>Adaptive:</strong> LFU caching dynamically adjusts to changes in access patterns, ensuring that frequently accessed items remain in the cache.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Complexity:</strong> LFU caching algorithms can be more complex to implement and maintain compared to simpler caching strategies like LRU (Least Recently Used).</p></li><li><p><strong>Overhead:</strong> LFU caching requires additional data structures to track the access frequency of items, which can introduce overhead in terms of memory and computational resources.</p></li><li><p><strong>Sensitivity to outliers:</strong> LFU caching may not perform optimally in scenarios with highly variable access patterns or sudden spikes in access frequency. Outliers in access frequency can skew the effectiveness of the cache.</p></li></ul></li><li><p><strong>Possible Implementation:</strong></p><ul><li><p>We are going to need 2 data structures, a <strong>cache map</strong> HashMap&lt;item, Node&gt; which holds our item along with a Node object <br><br><code>Node(int val, int key) {</code></p><p><code>&#8195;this.val = val;</code></p><p><code>&#8195;this.key = key;</code></p><p><code>&#8195;this.frequency = 1;</code></p><p><code>}</code><br><br>and a <strong>frequency map</strong> HashMap&lt;count, DoublyLinkedList&lt;Node object&gt;&gt; which holds the count or frequency value a long with a list of all the items that have that given frequency.</p></li><li><p>When the cache is full, the next element that needs to be removed is the tail element of the doubly linked list with the least frequency.</p></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MLoc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MLoc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 424w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 848w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MLoc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg" width="1456" height="803" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:803,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:166860,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MLoc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 424w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 848w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!MLoc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F474370e1-9188-4bfe-be5b-bbe87865b775_2693x1485.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Data eviction following a LFU approach</figcaption></figure></div></li><li><p><strong>LFRU</strong> (Least Frequently Recently Used)</p><ul><li><p><strong>Process:</strong> based on a hybrid algorithm that combines aspects of both LFU (Least Frequently Used) and LRU (Least Recently Used) caching strategies. LFRU caching maintains both frequency and recency information for each item in the cache. It tracks how frequently an item is accessed (similar to LFU) and how recently it was accessed (similar to LRU). When a new item needs to be added to the cache and it's already at full capacity, the cache algorithm uses a combination of frequency and recency information to decide which item to evict.</p><ul><li><p>If an item has a low frequency of access but was accessed recently, it may be considered valuable and kept in the cache.</p></li><li><p>If an item has a high frequency of access but hasn't been accessed recently, it may be considered less valuable and evicted from the cache.</p></li></ul></li><li><p><strong>Pros:</strong></p><ul><li><p><strong>Balance</strong>: LFRU caching strikes a balance between considering both frequency and recency of access, leading to more nuanced cache management decisions.</p></li><li><p><strong>Adaptability</strong>: LFRU caching adapts well to varying access patterns and workload changes, ensuring efficient use of cache space.</p></li><li><p><strong>Performance</strong>: LFRU caching can provide good performance by retaining frequently accessed items while also accommodating recent access patterns.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Complexity</strong>: LFRU caching algorithms can be complex to implement and may require additional computational overhead compared to simpler caching strategies.</p></li><li><p><strong>Resource Consumption</strong>: Maintaining both frequency and recency information for each item in the cache may consume more memory and computational resources.</p></li><li><p><strong>Tuning</strong>: Tuning the parameters of an LFRU caching algorithm to optimize performance may require careful experimentation and analysis.</p></li></ul></li></ul></li></ul></li></ul><h3>Cache Invalidation</h3><p>Cache invalidation is the process of removing or marking as invalid data in a cache system. Implementing effective cache invalidation strategies is vital for preserving data integrity and preventing the delivery of stale or obsolete data.</p><ul><li><p><strong>Invalidating on Write</strong></p><ul><li><p>This cache invalidation happens upon modifications to the underlying data source. Whenever a modification operation, like an update or deletion, takes place, the cache is notified or marked to invalidate the associated cached data. Subsequent attempts to access the invalidated data result in cache misses, prompting the retrieval of the latest information from the data source. While this method reduces the risk of serving outdated data, it may introduce a slight delay due to cache misses during the invalidation process.</p></li></ul></li><li><p><strong>Invalidating on Read</strong></p><ul><li><p>Cache invalidation occurs during data access or retrieval. When a read request is received, the cache verifies whether the cached data is still valid or has become outdated. If the data has expired or is marked as invalid, the cache retrieves the latest information from the data source and updates its contents before fulfilling the request. While this method ensures the delivery of up-to-date data, it introduces overhead to each read operation as the cache must confirm the freshness of the data before responding.</p></li></ul></li><li><p><strong>Write-Through Invalidation</strong></p><ul><li><p>In systems where data is written directly to the cache and the source of truth simultaneously, cache entries are invalidated whenever data is written or updated in the source of truth.</p></li></ul></li><li><p><strong>Time Based Invalidation</strong></p><ul><li><p>Cached data is invalidated after a certain period of time has elapsed. For example, a web page cache may be configured to invalidate its content every hour to ensure that users see the most recent version of the page.</p></li></ul></li><li><p><strong>Time-to-Live (TTL)</strong></p><ul><li><p>Similar to time based invalidation, TTL associates a time duration with each cached item. When an item is stored in the cache, it is marked with a TTL value indicating how long the item is considered valid.</p></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BRP1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BRP1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BRP1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg" width="1456" height="1174" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1174,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:191514,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BRP1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 424w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 848w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!BRP1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34d84e33-911c-4670-9043-c2e20bb85b22_2227x1795.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Invalidation Strategies</figcaption></figure></div><h3>Caching Strategies</h3><ul><li><p><strong>Read Strategies</strong></p><ul><li><p><strong>Cache Aside </strong>- also known as lazy loading or lazy population, is a caching strategy where the application is responsible for managing the cache directly. When data is requested, the application first checks the cache. If the data is found, it is returned from the cache. If the data is not in the cache, the application retrieves it from the data source, stores it in the cache, and then returns it to the caller.</p><ul><li><p><strong>Pros</strong>:</p><ul><li><p><strong>Flexibility:</strong> Cache-aside allows applications to cache data selectively, based on their specific requirements. This flexibility enables developers to optimize caching for performance and efficiency.</p></li><li><p><strong>Control:</strong> Applications have full control over cache management, including when to populate, update, or evict cached data. This level of control allows for fine-tuning cache behavior to suit the application's needs.</p></li><li><p><strong>Reduced Load:</strong> By caching frequently accessed data, cache-aside can reduce the load on the primary data source, leading to improved overall system performance and scalability.</p></li></ul></li><li><p><strong>Cons</strong>: </p><ul><li><p><strong>Stale Data:</strong> Without proactive cache population or expiration mechanisms, cache-aside may lead to serving stale or outdated data to users, particularly if data changes frequently in the primary data source.</p></li><li><p><strong>Consistency Challenges:</strong> Ensuring consistency between the cache and the primary data source can be complex, particularly in scenarios involving distributed caching or multiple cache instances. Inconsistent data can lead to data integrity issues and user experience problems.</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tPbK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tPbK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tPbK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg" width="1456" height="479" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:479,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:171275,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tPbK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tPbK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa06879d4-2551-406e-9f72-f6b8df9c4a45_3486x1147.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Aside Flow</figcaption></figure></div><ul><li><p><strong>Read Through </strong>- caching strategy where the application reads data from the cache, and if the requested data is not found in the cache (cache miss), the cache automatically fetches the data from the primary data source (such as a database) and populates the cache with the retrieved data before returning it to the application.</p><ul><li><p><strong>Pros</strong>:</p><ul><li><p><strong>Automatic Population:</strong> Cache read-through automatically populates the cache with requested data, reducing the need for explicit cache management.</p></li><li><p><strong>Simplicity:</strong> Implementing cache read-through is relatively straightforward, as the caching logic is encapsulated within the cache itself, simplifying application code.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Complexity in Eviction:</strong> Managing cache eviction policies to make room for new data can be challenging, especially in scenarios with limited cache capacity or high data volatility.</p></li><li><p><strong>Resource Consumption:</strong> Cache read-through can consume additional system resources, such as memory and network bandwidth, particularly during cache population operations.</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OCx1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OCx1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 424w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 848w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OCx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg" width="1456" height="498" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:498,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:178991,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OCx1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 424w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 848w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!OCx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff54ea5b0-b2cf-4282-bc07-3d10e4737b51_3421x1169.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Read Through Flow</figcaption></figure></div><ul><li><p><strong>Refresh Ahead </strong>- also known as cache pre-loading or cache warm-up, is a caching strategy where the cache proactively fetches and stores data from the primary data source before it is requested by the application. This anticipatory approach aims to populate the cache with frequently accessed or critical data in advance, thereby reducing latency and improving overall system performance.</p><ul><li><p><strong>Pros:</strong></p><ul><li><p><strong>Reduced Latency:</strong> By pre-loading frequently accessed data into the cache, cache refresh-ahead reduces the latency associated with fetching data from the primary source on demand.</p></li><li><p><strong>Improved Performance:</strong> Proactively populating the cache with data that is likely to be accessed soon improves overall system performance by ensuring that frequently accessed data is readily available in memory.</p></li><li><p><strong>Consistency:</strong> By periodically refreshing pre-loaded data, cache refresh-ahead helps maintain data consistency between the cache and the primary data source, ensuring that cached data remains up-to-date.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Resource Overhead:</strong> Proactive data fetching and cache pre-loading consume additional system resources, such as memory, CPU, and network bandwidth, especially for large or frequently updated data sets.</p></li><li><p><strong>Over-Population:</strong> Inefficient cache pre-loading strategies may lead to over-population of the cache with unnecessary or infrequently accessed data, resulting in wasted resources and reduced cache effectiveness.</p></li></ul></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UgCU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UgCU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 424w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 848w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UgCU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg" width="1456" height="555" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:555,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:157637,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UgCU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 424w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 848w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!UgCU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F798e5043-1702-46ac-8f3f-3e6800c39f4f_3240x1234.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Refresh Ahead Flow</figcaption></figure></div><ul><li><p><strong>Write Strategies</strong></p><ul><li><p><strong>Write Through </strong>- caching strategy where data is written to both the cache and the underlying data store simultaneously whenever a write operation occurs.</p><ul><li><p><strong>Pros:</strong></p><ul><li><p><strong>Data Consistency:</strong> By writing data to both the cache and the underlying data store simultaneously, cache write-through ensures that the cache remains synchronized with the primary data source, minimizing the risk of data inconsistency.</p></li><li><p><strong>Simplicity:</strong> Cache write-through simplifies cache management by eliminating the need for separate cache invalidation or synchronization mechanisms, as data is always written to both the cache and the data store.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Write Latency:</strong> Write operations in cache write-through may experience increased latency due to the additional step of writing data to both the cache and the data store, which can impact application performance, especially for write-heavy workloads.</p></li><li><p><strong>Potential Bottleneck:</strong> In highly concurrent or distributed environments, cache write-through may introduce a potential bottleneck if multiple write operations contend for access to the cache and the data store simultaneously, leading to contention and performance degradation.</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TcjP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TcjP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TcjP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg" width="1456" height="544" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:544,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:112312,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TcjP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 424w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 848w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!TcjP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83147e32-a3be-477a-af6d-0c2e971e5396_3270x1222.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Write Through Flow</figcaption></figure></div><ul><li><p><strong>Write Around</strong> - caching strategy where write operations bypass the cache and are written directly to the underlying data store. Data is cached when it is read from the data store, allowing the cache to store frequently accessed data while avoiding unnecessary caching of infrequently accessed or transient data.</p><ul><li><p><strong>Pros:</strong></p><ul><li><p><strong>Reduced Cache Pollution:</strong> By bypassing the cache for write operations, cache write-around avoids caching infrequently accessed or transient data, reducing cache pollution</p></li><li><p><strong>Scalability:</strong> Cache write-around is well-suited for scenarios with write-heavy workloads or large datasets, as it minimizes the impact on cache performance and storage capacity by avoiding unnecessary caching of write operations.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Read Latency:</strong> The first read operation for data that has not been cached incurs additional latency, as the data must be fetched from the underlying data store instead of the cache, potentially impacting application performance.</p></li><li><p><strong>Stale Data:</strong> If data is frequently updated or modified in the underlying data store, cache write-around may result in cached data becoming stale, as the cache does not proactively invalidate or update cached data based on write operations.</p></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pCrm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pCrm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 424w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 848w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pCrm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg" width="1456" height="546" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:546,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:148237,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pCrm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 424w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 848w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!pCrm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F30926e64-76ec-4d38-8240-6c56256c6f16_3265x1224.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Around Write Flow</figcaption></figure></div><ul><li><p><strong>Write Back</strong> - caching strategy where write operations are initially written to the cache and later asynchronously propagated to the underlying data store.</p><ul><li><p><strong>Pros:</strong></p><ul><li><p><strong>Improved Write Performance:</strong> Cache write-back improves write performance by acknowledging write operations as soon as they are written to the cache, without waiting for them to be persisted to the data store. This reduces latency and improves throughput, especially for write-heavy workloads.</p></li><li><p><strong>Optimized Resource Utilization:</strong> Cache write-back optimizes resource utilization by buffering write operations in the cache, allowing the data store to handle write operations more efficiently and effectively, especially during peak load periods.</p></li></ul></li><li><p><strong>Cons:</strong></p><ul><li><p><strong>Increased Complexity:</strong> Implementing cache write-back requires additional mechanisms for asynchronous data propagation, periodic flushing, and handling of failure scenarios, increasing system complexity and maintenance overhead.</p></li><li><p><strong>Potential Data Loss:</strong> In the event of cache failure or system crash, cached data that has not been flushed to the data store may be lost, leading to potential data loss or corruption. Proper backup and recovery mechanisms are required to mitigate this risk.</p></li></ul></li></ul></li></ul></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dm9h!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dm9h!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dm9h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg" width="1456" height="524" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:524,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:130323,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dm9h!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dm9h!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87be0862-0a6c-4fd7-a741-6bd4d2718e3a_3334x1199.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Cache Write Back Flow</figcaption></figure></div><p>Each caching strategy has its own advantages and considerations, and the selection of an appropriate strategy depends on the specific requirements and characteristics of the system.&nbsp;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9cym!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9cym!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9cym!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9cym!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9cym!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9cym!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg" width="1456" height="1592" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1592,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:210364,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9cym!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9cym!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9cym!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9cym!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe06d517a-59c2-4bbe-a7e7-c83fc5d46dfa_1912x2091.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Caching Read/Write Strategies</figcaption></figure></div><h3>Summary</h3><p>In this article, we've explored the fundamentals of caching, covering the core definition of caching, scenarios where it offers significant advantages, the importance of eviction policies (like LRU or MRU) to manage cache size, and strategies for invalidation to ensure your cache aligns with the primary data source. We also touched on the flow of data between the cache and your database for both read and write operations. </p><p><strong>Stay Tuned!</strong> In upcoming articles, we'll dive deeper into:</p><ul><li><p><strong>Types of Caches:</strong> Exploring different caching mechanisms like in-memory, distributed, and CDN caching.</p></li><li><p><strong>Deployment Strategies:</strong> Understanding how to integrate caching into your architecture.</p></li><li><p><strong>Metrics:</strong> How to monitor cache performance for optimal results.</p></li><li><p><strong>Implementation Solutions:</strong> Practical examples with popular caching tools and libraries.</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Mentorship That Matters: Building Effective Knowledge-Sharing Systems]]></title><description><![CDATA[On why mentorship should be an organisational wide initiative, what works and common pitfalls]]></description><link>https://blog.diana-enache.com/p/mentorship-that-matters-building</link><guid isPermaLink="false">https://blog.diana-enache.com/p/mentorship-that-matters-building</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Thu, 15 Feb 2024 10:34:10 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/bf6bc89b-a085-49bf-b800-738eef18ba7a_2000x2000.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!61Hd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!61Hd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!61Hd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3200481,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!61Hd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!61Hd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b0a06c3-6f57-4b90-8193-90ee72c1eb94_13933x5627.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Illustration by <a href="https://storyset.com/">StorySet</a></figcaption></figure></div><p>In the whirlwind of software development, success too often depends not just on what you know, but on <em>who</em> you know. Picture this scene:</p><p>A promising new hire, Maya, joins your team with solid fundamentals but limited experience in your specific tech stack. She tackles an assigned task, diligently studying documentation and online resources. Days turn into a frustrating week as Maya gets stuck on an integration issue that seasoned team members know is notoriously finicky. Meanwhile, across the office, another developer, Alex, breezes through a similar task, muttering, &#8220;Oh, the usual quirks with that library...&#8221; under his breath. Maya doesn't know to connect with Alex, and Alex hasn't been prompted to share the workaround. What unfolds?</p><ul><li><p><strong>Wasted Effort:</strong> Maya's progress stalled, while precious time is spent reinventing the wheel.</p></li><li><p><strong>Missed Growth:</strong> A learning opportunity evaporates. This could have been a prime moment for Maya to master a tricky corner of the codebase <em>and</em> solidify a positive working relationship.</p></li><li><p><strong>Teamwide Impact:</strong> Frustration festers. Maya feels unsupported; Alex becomes the go-to person, soon overburdened with questions which could have been prevented by a better knowledge sharing accros teams. </p></li></ul><h2><strong>Real-Life Story: The Cost of &#8220;I'll Figure It Out&#8221;</strong></h2><p>Early in my career, I was hesitant to &#8220;bother&#8221; senior engineers, assuming struggling through was a rite of passage. This led to weeks of spinning my wheels and missed chances to understand design rationale. While I eventually brute-forced the solutions, more was lost than just my time. This mentality fostered an aversion to asking for help, harming my progress long-term.</p><p><strong>Beyond Missed Opportunities</strong></p><p>As seen so many times, these issues can easily scale throughout your organisation:</p><ul><li><p><strong>Knowledge Silos:</strong> Critical insights locked with individuals or teams impede agility and can jeopardize a project if someone leaves.</p></li><li><p><strong>Reinventing the Wheel:</strong> Lack of awareness of existing in-house solutions drains company resources and morale.</p></li><li><p><strong>Uneven Workload:</strong> A few &#8220;gurus&#8221; get swamped with basic questions, while the potential of less outspoken team members remains untapped.</p></li></ul><p>The problem isn't about a lack of talent or documentation&#8212;it's about failing to create the channels through which knowledge, wisdom, and experience can flow seamlessly.</p><p><strong>Why Traditional Approaches Fall Short</strong></p><p>While onboarding documents and internal wikis are valuable tools, they often fall short of providing the depth and personalisation critical to true mentorship. Here's why:</p><ul><li><p>Onboarding documentation or wikis represent a snapshot of knowledge at a particular point in time. Technology, teams, and best practices evolve rapidly, rendering them quickly outdated. Mentorship, in contrast, is a dynamic, two-way process that flexes with evolving needs. A mentor can pinpoint what needs updating and address the &#8220;why&#8221; behind processes, not just the &#8220;how.&#8221;</p></li><li><p>Boilerplate documentation lacks the nuanced, individualized context vital for deep understanding. Imagine a new engineer just out of school joining your heavily regulated industry. Onboarding docs tell them the company forms to fill out, but a mentor helps them grasp the underlying reasons for the strict compliance rules, fostering both adherence and innovation within the constraints.</p></li><li><p>Wikis present facts but lack the interpretive layer a mentor provides. A new hire wrestling with code architecture benefits infinitely more from a mentor patiently explaining design choices and tradeoffs than a page describing abstract patterns.</p></li></ul><p>My early days at a fast-growing startup were marked by frantic scrambling through the company wiki. There was <em>always</em> a page covering some technical process, but rarely addressing the edge cases I inevitably ran into. The breakthrough came when paired with an experienced developer. Turns out, half the wiki was outdated, filled with workarounds for since-fixed bugs! My mentor knew what to skip, the undocumented &#8220;tribal knowledge&#8221;, and crucially, was invested in helping me bridge those information gaps.</p><p><strong>Beyond Technical Knowledge</strong></p><p>It's crucial to stress that wikis and docs fall shortest in mentorship's &#8220;softer&#8221; aspects such as:</p><ul><li><p><strong>Confidence Building:</strong> The simple act of a senior engineer dedicating time boosts a mentee's sense that they belong.</p></li><li><p><strong>Company Culture Navigation:</strong> How and when to escalate issues, who is truly the expert on 'X' technology, informal communication norms &#8211; a good mentor accelerates this decoding process.</p></li><li><p><strong>Career Pathing:</strong> Wikis won't provide honest insights into growth opportunities or challenges within the organization.</p></li></ul><p>While documentation will always play a crucial role, the power of human connection in knowledge transfer and professional development shouldn&#8217;t be underestimated.</p><h2><strong>Key Elements of Effective Systems</strong></h2><p>Transforming good intentions into a true force for growth requires structure. What truly makes a mentorship program thrive? Let's dive into the key elements that separate well-meant initiatives from transformative programs:</p><ol><li><p><strong>&#9989; Finding the Right Match</strong></p><p>Skillset is just a start for finding a good match but what sometimes gets overlooked are the other aspects such as: ways of working, personality, common aspirations etc.</p><ul><li><p><strong>Communication Match:</strong> An introverted mentee may struggle with an overly extroverted mentor, regardless of technical skills. Similar communication styles streamline interaction and reduce misunderstandings. For example, both being naturally direct benefits efficiency, while an introvert/extrovert pairing may require adapting to each other's preferences.</p></li><li><p><strong>Personality Fit:</strong> Grumpy pessimist + eager optimist = potential frustration. Friction over clashing personality styles can dampen enthusiasm on both sides. A mentor who seems overly negative or dismissive can discourage the mentee, impacting their progress.</p></li><li><p><strong>Career Path Honesty:</strong> Is the mentor in a position to truly help with the mentee's aspirations? </p></li></ul></li><li><p><strong>&#9989; Setting SMART Goals and Keeping a Live Document</strong></p><p>SMART stands for Specific, Measurable, Achievable, Relevant, Time-bound. Don't just say &#8220;improve coding&#8221;, &#8220;get better at X&#8220; - break that down into smaller focused and actionable goals. Goals shouldn't be set and forgotten. Regular check-ins allow mentors/mentees to celebrate progress and refine goals as the relationship evolves.</p></li><li><p><strong>&#9989; Mentoring on Values</strong></p><ol><li><p><strong>The Hidden Culture Code:</strong> Every company has unspoken ways decisions get made, what truly signals being a team player, etc. This can be harder to navigate than the tech stack itself!</p></li><li><p><strong>Ethical Compass:</strong> This is especially significant in fast-paced, 'move fast, break things' environments. A mentor showing when to push back, uphold quality standards despite deadlines &#8211; that models values in action.</p></li></ol></li><li><p><strong>&#9989; Network Creation: Opening Doors and Breaking Invisible Barriers</strong></p><ol><li><p><strong>Advocate, Don&#8217;t Gatekeep:</strong> Simply knowing names gets a foot in the door, but true mentorship paves the way. (&#8220;May I intro you to Maya? Her work on X would be relevant to your team...&#8221;)</p></li><li><p><strong>Beyond Internal Networking:</strong> Mentors with broad industry connections become allies. Recommending the mentee for conference talks, open-source collaborations &#8211; this amplifies growth.</p></li><li><p><strong>&#8220;Paying it Forward&#8221; Loop:</strong> This isn't about indebted favors. Rather, the mentee, now armed with a wider network, is positioned to eventually open doors for others.</p></li></ol></li><li><p><strong>&#9989; Keeping it Structured, but Flexible</strong></p><p>Guidance and autonomy should balance. </p><ul><li><p><strong>The Power of Suggestion:</strong> Offer templates for goal setting, sample agendas for early meetings, etc. This eases pairs into the process without a rigid script.</p></li><li><p><strong>Mentorship Menu:</strong> Could have varying levels of engagement. Is there a 'quick question' channel AND an option for longer term pairing? </p></li><li><p><strong>Adaptability Test:</strong> Mentor is NOT a crutch. If a mentee fails to prepare for meetings, repeatedly disregards agreed-upon goals, that's feedback for the program coordinators.</p></li></ul></li><li><p><strong>&#9989; The Feedback Loop: Mentors Aren't Mind Readers</strong></p><ul><li><p><strong>Safe Space:</strong> Emphasize to mentees they WON'T hurt their mentor's feelings by being honest about what's not working. This protects from passive resentment.</p></li><li><p><strong>Org Level Feedback:</strong> How did this mentorship improve (or not) the mentee's onboarding? Their contributions?</p></li></ul></li><li><p><strong>&#9989; Celebrating Success &amp; Recognising Contributions</strong></p><ul><li><p><strong>Success Is Variable:</strong> Not all mentees become rockstars (that's okay!). Did they get unstuck on a blocker? First significant code contribution? Celebrate it to encourage the relationship.</p></li><li><p><strong>Make it Visible:</strong> Internal newsletter, 'thank you' board, etc. Showcases that the company is serious about this, not just paying lip service to mentorship.</p></li></ul></li><li><p><strong>&#9989; Fostering a Psychological Safe Environment</strong></p><ol><li><p><strong>Combatting Imposter Syndrome:</strong> Especially prevalent among new hires, the fear of appearing incompetent stifles questions. A mentor explicitly stating 'No question is stupid' is a start, but must be consistent in their response.</p></li><li><p><strong>Error as Opportunity:</strong> This takes more than words. Can a mentor show how they diagnose their OWN mistakes? This teaches more than perfect demos ever could.</p></li><li><p><strong>The Long Game:</strong> Psychological safety creates long-term loyalty. That sense of being 'seen' and supported can be the deciding factor on whether a mentee sticks with the company during hard times.</p></li></ol></li></ol><h1>Common Pitfalls </h1><p>If you've been involved in mentorship, chances are you've encountered a few stumbling blocks. In this section, we'll uncover the most prevalent pitfalls:</p><p><strong>&#10060; Pitfall 1: The 'Hero' Mentor</strong></p><p>Swooping in to solve problems instead of teaching problem-solving <em>strategies</em>. The mentee ends up reliant, the mentor burnt out. Mentoring is about giving a toolbox, not just building things for others. Empowering mentees is empowering for the long term.</p><p><strong>&#10060; Pitfall 2: Assuming Interest</strong></p><p>A 'passion project' mentorship match &#8211; brilliant engineer paired with someone just seeking stable paycheck. Mutual frustration ensues. Upfront discussion about goals and what excites the mentee prevents wasted time and potential resentment.</p><p><strong>&#10060; Pitfall 3: Rigid 'By-The-Book' Mentorship</strong></p><p>The program dictates all the focus areas, meeting frequency, etc., with no room for adaptation based on the pair's unique needs. Structure is good, rigidity is stifling. Allowing mentor/mentee input on format fosters both ownership and better matching in future iterations.</p><p><strong>&#10060; Pitfall 4: Neglecting Mentor Development</strong></p><p>We assume technical experts already possess all the skills needed to be effective mentors. An underdeveloped mentor may flounder when providing constructive feedback, navigating difficult conversations about the mentee's performance, etc. This directly impacts the mentee's growth and can even breed disillusionment with the concept of mentorship altogether. Beyond the tech side, mentors need to give feedback, motivate, and sometimes manage underperformance. This is skill, not inherent instinct.</p><h1><strong>Key Takeaways</strong></h1><p>Let's recap the essential lessons covered:</p><ol><li><p><strong>Be on the Lookout for Static Resources</strong> </p><ol><li><p>These risk leaving mentees feeling overwhelmed, misinformed, and frustrated.</p></li><li><p>&#8220;Information overload&#8221; hinders progress more than lack of access to facts. Mentors serve as filters and navigators.</p></li><li><p>Wikis can actually <em>impede</em> adaptation&#8212; they perpetuate legacy knowledge unless meticulously maintained.</p></li></ol></li><li><p><strong>Have Structured Mentorship Programs</strong></p><ul><li><p><strong>Pairing:</strong> Match new hires or less-experienced team members with seasoned developers for focused knowledge transfer and ongoing support. This program can be formal (designated time each week) or more flexible as needed.</p></li><li><p><strong>Goal Setting:</strong> Help mentor/mentee pairs define what success looks like for both parties, creating focus and increasing progress tracking.</p></li><li><p><strong>Company-Wide Mentorship Culture:</strong> Leaders must model this behavior by mentoring within their teams and recognizing those who dedicate time to develop others. This prevents mentorship from being viewed as a mere 'extra' duty.</p></li></ul></li><li><p><strong>Put in place Knowledge-Sharing Incentives and Tools</strong></p><ul><li><p><strong>Collaboration Hubs:</strong> Dedicated Slack channels, or other platforms designed for asking questions, offering tips, and recognizing team members for helping. </p></li><li><p><strong>Celebration of &#8220;Eureka&#8221; Moments:</strong> Encourage developers to share their hard-won solutions publicly, whether a brief internal knowledge article or a 'lunch and learn' presentation. Normalises both asking and sharing.</p></li><li><p><strong>Reward Structures (If Feasible):</strong> Public shout-outs for those actively spreading knowledge creates positive reinforcement.</p></li></ul></li><li><p><strong>Make Sure your Team&#8217;s Internal Documentation is always a &#8220;Living Document&#8221;:</strong> Set clear expectations for who keeps internal resources up-to-date. A version control system (like Git) allows seeing when modifications were made, aiding in trust of content.</p></li></ol>]]></content:encoded></item><item><title><![CDATA[Red Flags to Look for in Software Engineering Interviews]]></title><description><![CDATA[Part of the Software Engineer Interviews Series, this time we're looking at things to look out for during your interviews and some of the questions you might ask]]></description><link>https://blog.diana-enache.com/p/red-flags-to-look-for-in-software</link><guid isPermaLink="false">https://blog.diana-enache.com/p/red-flags-to-look-for-in-software</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 02 Feb 2024 14:23:01 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/39f11023-8c86-4a18-a917-c8fb5b23bc09_2000x2000.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!akqi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!akqi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 424w, https://substackcdn.com/image/fetch/$s_!akqi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 848w, https://substackcdn.com/image/fetch/$s_!akqi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 1272w, https://substackcdn.com/image/fetch/$s_!akqi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!akqi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png" width="1456" height="597" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:597,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2574822,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!akqi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 424w, https://substackcdn.com/image/fetch/$s_!akqi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 848w, https://substackcdn.com/image/fetch/$s_!akqi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 1272w, https://substackcdn.com/image/fetch/$s_!akqi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6efa7b8d-6f5d-4b0f-99e8-60713dd94b12_13933x5715.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Illustrations by <a href="https://storyset.com/">StorySet</a></figcaption></figure></div><p>Once you&#8217;ve been offered the opportunity to interview, you&#8217;d think all you need to do is perform well, right? In an ideal scenario interviews should go both ways, you&#8217;re trying to convince them to hire you but they should also do their best to convince you to come work for them. So this is your chance to test the grounds and get a feel of the culture and what working there would be like.</p><p>But what should you look for in your next company? 5 companies and +70 interviews later, here&#8217;s a list of red flags and points to consider I now go through to help set my expectations:</p><h1><strong>The Interview Process</strong></h1><ul><li><p><strong>You only get a technical interview</strong></p></li></ul><p>As an engineer, yes, you need to know how to do your job but that&#8217;s not the only thing you need in order to succeed. If a company is interested in having only the technical interview, it might suggest that they do not value all the other non-technical but essential skills: communication, collaboration, product knowledge etc. Which means that they&#8217;ve probably hired people whose skills in those areas have not been evaluated, so you might end up working with people who are all hard skills and no soft skills. In the end there&#8217;s more to a good engineer than his technical knowledge.</p><ul><li><p><strong>You do not get any technical interviews or the interviews lack any in-depth tech questions</strong></p></li></ul><p>They&#8217;re either hiring weak engineers or that is an entry level position.</p><ul><li><p><strong>Non-technical interviewer asking technical questions</strong></p></li></ul><p>If a people person starts asking you technical questions, they are probably reading off from a script and you probably need to say exactly what is written on it. It&#8217;s like playing engineering trivia.</p><ul><li><p><strong>Did you meet any of your co-workers during the interview process? If not, ask to before making a decision.</strong></p></li></ul><h1><strong>Company Culture</strong></h1><ul><li><p><strong>Non-existent vision, purpose, or reasonable goals</strong></p></li></ul><p>What is the point of working on a new feature if it doesn&#8217;t integrate with the wider picture and it doesn&#8217;t contribute to the product&#8217;s growth? That means you&#8217;ll probably be working on anything the customer asks and in this case your team plays more of a customer support role.</p><ul><li><p><strong>No of senior people in the team/Lack of diversity</strong></p></li></ul><p>You want to be part of an engineering squad that has a mix of junior/mid-level, and senior engineers. It gives you the opportunity to be surrounded by people you can learn from and to also strengthen your knowledge by mentoring others.</p><ul><li><p><strong>Staring at interviewers who look like stressed out zombies</strong></p></li></ul><p>Are engineers excited for the projects they&#8217;re working on or do they look stressed out and overworked?</p><ul><li><p><strong>Growth opportunities</strong></p></li></ul><p><em>&#8220;Is a learning mentality encouraged?&#8221; &#8220;Do they have 20% time?&#8221;</em> (time &#8212; usually a Friday off every now and then &#8212; when engineers are encouraged to learn/experiment with new things, prepare for exams etc) <em>&#8220;Do they have demos/brown-bags sessions?&#8221; &#8220;Is the sharing of knowledge encouraged?&#8221;</em></p><p><em>&#8220;Are there any personal development plans put into place?&#8221; &#8220;And is there a clear strategy/action points for you to progress/level up in your career?&#8221;</em></p><ul><li><p><strong>Your interviewer uses a lot of buzz words.</strong></p></li></ul><p><em>&#8220;We&#8217;re using AI, social networks, cloud computing, big data, IoT. Machine learning this, machine learning that.&#8221; </em>Buzz-words combined with the lack of explaining the tech behind it and how the company has implemented any of that, big red flag.</p><ul><li><p><strong>The work-life balance &#8212; Did they mention they look for an engineer with a &#8220;strong work ethic&#8221;?</strong></p></li></ul><blockquote><p><em>&#8220;We want you to work insane hours and weekends but we don&#8217;t want to compensate you accordingly. We want you to work those insane hours and weekends because <strong>you want to</strong>.&#8221;</em></p></blockquote><p>We&#8217;re all in this industry because we love what to do. Someone with a spark in their eyes will usually go the extra mile because they&#8217;re passionate about what they do and they care about the impact their work has. But everyone needs time to breath, to spend time with family, recharge, pursue other passions etc. Long hours are sometimes necessary but shouldn&#8217;t be the norm.</p><ul><li><p><strong>Do some &#8220;stalking&#8221; aka research the people you&#8217;re going to work with.</strong></p></li></ul><p>Are there any engineers who have been working at the company for some time now? If not, there might be a good reason they&#8217;ve left.</p><h1><strong>Day-to-day Work</strong></h1><ul><li><p><strong>Do they deal with a lot of tech debt?</strong></p></li></ul><p><em>&#8220;What state is the code base in?&#8221;</em> and <em>&#8220;How much documentation is there?&#8221;</em>. Devs will generally give honest answers to this kind of questions. <em>&#8220;How long is the onboarding expected to take? How long do you expect it will take for a new hire to become productive in your code base?&#8221; </em>This will give you a feel of how complex their systems are. Also, if they have lots of technical debt, and they think you will be instantly productive, they have ridiculously high expectations.</p><ul><li><p><strong>Support/On-Call Rota</strong></p></li></ul><p><em>&#8220;How often will you have to be on call?&#8221;</em> <em>&#8220;Will it cover your working hours or will you have to do 24h?&#8221;</em> <em>&#8220;How many tier 1 systems does your team support?&#8221; </em>Tier 1 meaning having to deal with systems that impact your customer the most if they&#8217;re down. (e.g. payment systems, authentication etc) <em>&#8220;How busy will on call be?&#8221; &#8220;How many issues per week does the team usually have to deal with?&#8221; &#8220;Does the team put emphasis on writing proper, in-depth run-books and postmortems?&#8221; &#8220;How are incidents dealt with? Is the root caused being investigated/ is there a list of actions the team is planning to work on?&#8221;</em></p><ul><li><p><strong>Do you need to own your feature from start &#8212; end? From gathering requirements, design/architecture proposal, infrastructure provisioning, implementation, testing, release, maintenance?</strong></p></li></ul><p>This might depend from company to company and it&#8217;s not really a red flag if you are or not involved in every step of the SDLC but it might be a good question to ask to set your expectations of what you will be asked to do.</p><ul><li><p><strong>How are things prioritised and how much of a voice can you have?</strong></p></li></ul><p><em>&#8220;Are incoming tasks discussed among the team before being assigned?&#8221;</em></p><p>Who gets to add tasks to the team&#8217;s backlog? Are customer requests always prioritised before anything else (bug fixes, dev experience improvements)?</p><p>Ask how and who decides on estimates and deadlines. If the developers do not participate at all in how tasks are prioritised and how long the completion should take, then it is likely the leadership makes promises to customers that are next to impossible to keep and pressure you to meet these deadlines while you&#8217;ll probably have to deal with a really slow and painful dev environment/deploy/release and who knows what else is in a bad state because no one had the time to fix it.</p><ul><li><p><strong>How does the dev experience look like?</strong></p></li></ul><p><em>&#8220;Do you have admin access on your work computer?&#8221; &#8220;Do applications need to go through an approval process to be granted the permission to be downloaded?&#8221; &#8220;Will you need to rise a request to download an approved application?&#8221;</em></p><p>I worked in a company where my request to have Notepad++ was declined on the premise I don&#8217;t need it for the job. However regulated the industry you&#8217;re working in needs to be, this is a sign of how much lack of trust the company has in its employees. Any security, regulatory or compliance checks should be run on the code you put into production as part of your build/deploy pipelines. As an engineer, how can I innovate/improve/grow if I can&#8217;t even try a postman replacement on the basis that I can already do my job with postman.</p><p><em>&#8220;How does the deployment and release work?&#8221; &#8220;Do they have a QA/review process?&#8221; &#8220;Can you release any time or do you need to wait for the end of the sprint?&#8221; &#8220;Will your team&#8217;s releases depend on other teams&#8217;?&#8221;</em></p><p>Having to wait for the end of the spring to release is not great.</p><p><em>&#8220;What&#8217;s the split percentage between improving the dev experience/working on legacy (e.g. automating any current manual work, smooth local running and testing of systems, easy straightforward deploys etc) and implementing new features?&#8221;</em></p><p>It should be a good balance between the two. If the only work the team is doing is working on new features it might be that the leadership team is a customer pleaser or you&#8217;ll be working in a brilliant team that doesn&#8217;t produce any tech debt. If the management is not able to say no to the business, it will have a major impact on your workload and how tasks are being prioritised.</p><ul><li><p><strong>Other things to double check &#8212; <a href="https://www.glassdoor.co.uk/member/home/index.htm">glassdor</a> reviews</strong></p></li></ul><p>That&#8217;s a good place to find the reasons former employees left the company.</p><h1><strong>In the end, you need to trust your gut</strong></h1><p>It&#8217;s unlikely that you&#8217;ll manage to keep track of all these red flags. The best thing you can do is trust your gut and try to find out if their working style will be a good fit for you.</p><div><hr></div><h1>Useful resources</h1><p>Here&#8217;s a list of questions I use when interviewing (what I ask changes depending of the company I&#8217;m interviewing with, who I&#8217;m talking to and what stage of the interviewing process I&#8217;m in):</p><p><strong>General/Processes</strong></p><ul><li><p><strong>What would you change around here if you could? Or is there anything you&#8217;d like to improve?</strong></p></li><li><p><strong>What do you enjoy the most about working here?</strong></p></li><li><p><strong>What does a typical day look like for you?</strong></p></li><li><p><strong>What is your policy on working from home/remotely?</strong></p></li></ul><p><strong>Culture</strong></p><ul><li><p><strong>How would you describe the culture at your company?</strong></p><ul><li><p>is there anything that you are particularly happy/excited about when it comes to the company culture? anything that stands out to you?</p></li></ul></li><li><p><strong>Are there any particular qualities that you are looking for in new joiners?</strong></p><ul><li><p>what are some of the areas in the org that you are looking to bolster?</p><ul><li><p>what about in the next 1-2 years?</p></li></ul></li></ul></li></ul><p><strong>Career Growth</strong></p><ul><li><p><strong>Are there any personal development plans put into place? Is there a clear strategy/action points for you to progress/level up in your career?</strong></p></li></ul><p><strong>Planning and Roadmaps</strong></p><ul><li><p><strong>What does the planning and goal setting process look like at your company?</strong></p><ul><li><p>Do you use use OKRs? Are they quarterly?</p></li><li><p>How do you keep the whole organisation aligned?</p></li><li><p>How do you ensure you&#8217;re building the right thing for the customers?</p><ul><li><p>How do you get feedback from the customers?</p></li></ul></li></ul></li><li><p><strong>Do you have a roadmap for the next 1-2 years?</strong></p></li><li><p><strong>What is a recent piece of feedback that you received from the customer?</strong></p></li><li><p><strong>What are some of the key performance metrics or KPIs that you measure and monitor?</strong></p></li><li><p><strong>What are some of the recent milestones at your company?</strong></p><ul><li><p>was there anything that could have been done better/faster? (in hindsight)</p></li><li><p>was there anything that you found particularly challenging while working on this?</p></li><li><p>how were the new features/products received by customers?</p></li></ul></li><li><p><strong>What are your highest priorities right now?</strong></p></li><li><p><strong>What are some areas of growth that your organisation is planning to focus on in the next few year?</strong></p><ul><li><p>Any particular areas of expertise you are looking to bring/grow?</p><ul><li><p>examples: data analytics, growth engineering, platform, etc.</p></li></ul></li></ul></li><li><p><strong>How do you prioritise internal/technical roadmap items?</strong></p><ul><li><p>Do you have a separate internal roadmap?</p></li><li><p>How do you choose where to allocate bandwidth?</p></li></ul></li></ul><p><strong>Technical/Engineering</strong></p><ul><li><p><strong>What are the engineering challenges that the company/team is facing?</strong></p></li><li><p><strong>What is the most fulfilling/exciting/technically complex project that you've worked on here so far?</strong></p></li><li><p><strong>How do you evaluate new technologies? Who makes the final decisions?</strong></p></li><li><p><strong>What is your stack? What is the rationale for/story behind this specific stack?</strong></p></li><li><p><strong>Do you tend to roll your own solutions or rely on third party tools? What's the rationale behind it?</strong></p></li><li><p><strong>How often have you moved teams? What made you join the team you're on right now? If you wanted to move teams, what would need to happen?</strong></p></li><li><p><strong>What would I work on if I joined this team and who would I work most closely with?</strong></p></li><li><p><strong>How long is the onboarding expected to take? How long do you expect it will take for a new hire to become productive in your code base?</strong></p></li><li><p><strong>Support/On-Call</strong></p><ul><li><p>How often will you have to be on call? Will it cover your working hours or will you have to do 24h? How many tier 1 systems does your team support?</p></li><li><p>How busy will on call be? How many issues per week does the team usually have to deal with?</p></li><li><p>Does the team put emphasis on writing proper, in-depth run-books and postmortems? How are incidents dealt with? Is the root caused being investigated/is there a list of actions the team is planning to work on?</p></li></ul></li><li><p><strong>Release</strong></p><ul><li><p>How does the deployment and release work?</p></li><li><p>Do you have a QA/review process? </p></li><li><p>Can you release any time or do you need to wait for the end of the sprint?</p></li><li><p>Will your team&#8217;s releases depend on other teams&#8217;?</p></li></ul></li><li><p><strong>Dealing with legacy</strong></p><ul><li><p>How does the engineering team balance the effort between feature requests and engineering maintenance/legacy work? </p></li><li><p>What&#8217;s the split percentage between improving the dev experience/working on legacy (e.g. automating any current manual work, smooth local running and testing of systems, easy straightforward deploys etc) and implementing new features?</p></li></ul></li></ul>]]></content:encoded></item><item><title><![CDATA[Refactoring legacy code: The Strangler Fig Migration Pattern]]></title><description><![CDATA[On one of the approaches for breaking the monolith into microservices]]></description><link>https://blog.diana-enache.com/p/refactoring-legacy-code-the-strangler</link><guid isPermaLink="false">https://blog.diana-enache.com/p/refactoring-legacy-code-the-strangler</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 05 Jan 2024 06:48:59 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/906d0708-27e3-46f3-a8f2-65de2ed2f77d_518x426.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tNaN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tNaN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tNaN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:4494687,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tNaN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!tNaN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5403a7a9-b2aa-48c4-83f5-d9a488352cb0_13933x5627.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Illustrations by <a href="https://storyset.com/">Storyset</a></figcaption></figure></div><p>Microservices have been getting more and more popular in recent years, and several companies are migrating their monolithic applications to microservices. But just a reminder that switching to microservices might not always be the good option. Martin Fowler wrote a nice article regarding the <a href="http://bit.ly/2jclLet">monolith-first approach</a>. Here are a couple of useful statements:</p><ul><li><p><em>Almost all the successful microservice stories have started with a monolith that grew too big and was broken up.</em></p></li><li><p><em>Almost all the cases I&#8217;ve heard of a system that was built as a microservice system from scratch, it has ended up in serious trouble.</em></p></li></ul><p>A migration technique that has been frequently used when doing system rewrites is called the Strangler Fig Application. This migration method is based on incrementally replacing existing functionalities of the old legacy system with new applications and services in a phased approach until the new application system eventually replaces all of the old legacy system&#8217;s features.</p><p>The name of this type of tree, the Strangler Fig, comes from their pattern of growth around the host tree, which often results in the host&#8217;s death. The roots grow down to the forest floor where they take root and begin to take nutrients from the soil. Gradually the roots wrap around the host tree, widen, and slowly form a lattice-work that surrounds the host&#8217;s trunk. The fig&#8217;s crown grows foliage which soon overshadows the tree. Eventually, the host tree dies leaving the fig with a hollow trunk.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8oeS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8oeS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 424w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 848w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 1272w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8oeS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png" width="1400" height="509" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:509,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8oeS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 424w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 848w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 1272w, https://substackcdn.com/image/fetch/$s_!8oeS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1a5889a-f447-4899-a9ec-a3b4e229d4fc_1400x509.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Overview of the growth of the strangler fig tree</figcaption></figure></div><p>Martin Fowler chose this as the name of the pattern as a metaphor of describing a way of doing rewrite of an important system. Our new system is initially supported by and wrapping the existing system. The old and the new can co coexist while the new system can grow and potentially replace the entire old system.</p><blockquote><p>&#8220;An alternative route is to gradually create a new system around the edges of the old, letting it grow slowly over several years until the old system is strangled.&#8221;</p><p>~ Martin Fowler</p></blockquote><h2><strong>Advantages</strong></h2><ul><li><p>It allows for incremental migration to a new system.</p></li><li><p>It allows to pause and even stop the migration while still taking advantage of the new system built so far.</p></li><li><p>Each step is reversible, reducing the risk of each incremental step.</p></li></ul><blockquote><p><em>&#8220;The most important reason to consider a strangler application over a cut-over rewrite is reduced risk. A strangler can give value steadily and the frequent releases allow you to monitor its progress more carefully. Many people still don&#8217;t consider a strangler since they think it will cost more &#8212; I&#8217;m not convinced about that. Since you can use shorter release cycles with a strangler you can avoid a lot of the unnecessary features that cut over rewrites often generate.<strong>&#8221;<br></strong>~ Martin Fowler</em></p></blockquote><h2><strong>When not to use it</strong></h2><ul><li><p>The pattern will not be suitable for small systems where the complexity and size are low.</p></li><li><p>The pattern cannot be used in systems where requests to the back-end system cannot be intercepted and routed.</p></li></ul><h2><strong>Issues</strong></h2><ul><li><p>Considerable effort is required for the legacy and new system integrations.</p></li><li><p>Considerable effort is required for the parallel run of the same functionality.</p></li><li><p>Big commitment. Lack of will and resources to finish the strangling job might lead you into a bigger mess where your system now has two ways of doing everything with an awkward interface between the two.</p></li></ul><h2><strong>How it works</strong></h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5stI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5stI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 424w, https://substackcdn.com/image/fetch/$s_!5stI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 848w, https://substackcdn.com/image/fetch/$s_!5stI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 1272w, https://substackcdn.com/image/fetch/$s_!5stI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5stI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png" width="1400" height="448" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:448,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5stI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 424w, https://substackcdn.com/image/fetch/$s_!5stI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 848w, https://substackcdn.com/image/fetch/$s_!5stI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 1272w, https://substackcdn.com/image/fetch/$s_!5stI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cae6b2d-c9ae-4ebd-9b3a-ab50521692c1_1400x448.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Overview of the strangler fig pattern</figcaption></figure></div><p>The strangler fig pattern allows you to move functionality over to your new service without having to touch or make changes to your existing system.</p><p>The pattern involves 4 steps:</p><ul><li><p><strong>Identify </strong>-&gt;<strong> </strong>identify parts of the existing system that you wish to migrate. (use Domain Driven Design to identify the various bounded contexts)</p></li><li><p><strong>Transform </strong>-&gt; implement this functionality in your new microservice.</p></li><li><p><strong>Co-exist</strong> -&gt; leave the existing module in the legacy application as-is. Incrementally reroute calls from the monolith over to the new microservice.</p></li><li><p><strong>Eliminate </strong>-&gt; once the traffic is completely redirected to the microservice, eliminate the legacy module.</p></li></ul><p>First, add a proxy (facade), which sits between the legacy application and the user. This can be a &#8220;bare bones&#8221; routing app or an API Gateway. Initially, this proxy doesn&#8217;t do anything and all the application traffic is routed to the legacy application. Once a new component is built, you can link it to the proxy and you can allow the proxy to serve traffic to that page. You can also test your new functionality in parallel against the existing monolithic code. Keep adding more services and link them to the proxy. Repeat until all required functionality is handled by the new stack. Both the monolith and the new built component need to be functional for a period of time. Sometimes the transitional phase can last for an extended duration. When the new component has been incrementally developed and tested, the legacy monolithic application can be removed.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TjVe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TjVe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 424w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 848w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 1272w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TjVe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png" width="1400" height="713" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:713,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!TjVe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 424w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 848w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 1272w, https://substackcdn.com/image/fetch/$s_!TjVe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa9771e6-8f74-4b8a-9a60-7c0a48ddfa3f_1400x713.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Conclusion</strong></h2><p>Moving from your existing legacy monolithic application into a microserviced architecture is a nice end goal to have but the journey is challenging and needs to be well planned. In this article, we have discussed about the Strangler Fig migration pattern which can help you in your journey. Keep in mind that strangling a monolith is not a quick process and may require some time.</p><h2><strong>Resources</strong></h2><ul><li><p>Martin Fowler <a href="https://www.martinfowler.com/bliki/StranglerApplication.html">Strangler Application</a></p></li><li><p>Michiel Rook <a href="https://www.michielrook.nl/2016/11/strangler-pattern-practice/">The Strangler pattern in practice</a></p></li><li><p>Azure Cloud Patterns <a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/strangler">Strangler pattern</a></p></li></ul>]]></content:encoded></item><item><title><![CDATA[Cracking the System Design Interview]]></title><description><![CDATA[On system design interview format and design components]]></description><link>https://blog.diana-enache.com/p/cracking-the-system-design-interview</link><guid isPermaLink="false">https://blog.diana-enache.com/p/cracking-the-system-design-interview</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 29 Dec 2023 07:36:04 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/592c4bf9-b219-43e1-91c9-1fabf8b19405_2000x2000.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jVs4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jVs4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jVs4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2903292,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jVs4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!jVs4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79385945-3948-44b0-a255-7e7652f4729b_13933x5627.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Illustrations by <a href="https://storyset.com/">Storyset</a></figcaption></figure></div><p>System design interviews are in general 45&#8211;60 minutes long where you are given a broad topic like &#8220;Design Twitter&#8221;. You&#8217;ll be expected to generate a high-level design, showing the different system components that will be required, how they&#8217;re connected, and any trade-offs in the approach you&#8217;ve taken. Time permitting, the interviewers will ask you to go deeper into how some of your system components would function and talk about how you would handle several scenarios and edge cases.</p><p>For a junior, mid-level engineer, this article aims to give you a fair good start at the System Design Interview and it&#8217;s not an exhausting list since system design is a vast topic. But from here, you can dig deeper with other resources.</p><p>Before starting with the theory basics, let&#8217;s briefly talk about how you should approach the interview.</p><ol><li><p><strong>Understand the goal and requirements.<br></strong>Never go head-first into the design, make sure you understand the problem and ask clarifying questions &#8212; What is the goal of the system? Who are the users of the system? What do they need it for? How are they going to use it? Clarify your assumptions.</p></li><li><p><strong>Establish the scope<br></strong>Go through all the features of the system that you can think of and write them down.</p></li><li><p><strong>Establish the scale<br></strong>What is the scale of the system? How many customers does the system need to serve per day? How many transactions? What are the performance requirements (e.g. latency, throughput, availability)? What is the expected read-to-write ratio? What&#8217;s the average expected response time?</p></li><li><p><strong>Design high-level. </strong>The first 20 min of the interview you should have a draft of your high level design. Make sure to discuss the trade offs and why you chose those types of technologies. What type of database would you use and why? What caching solutions are out there? Which would you choose and why?</p></li><li><p><strong>Drill-down</strong>. You should drill-down into more details, starting with the components that you are most familiar with. This is to ensure that you have time to go in-depth into the topics you have the most knowledge in.</p></li><li><p><strong>Wrap-up. </strong>Before the end of your interview, you should spend some time to summarize your solution and highlight important bottlenecks or improvement opportunities.</p><div><hr></div></li></ol><p>Now let&#8217;s touch onto the most encountered components you&#8217;ll probably have to talk about.</p><h1><strong>Distributed Systems</strong></h1><p><em>= group of computers working together as to appear as a single computer to the end-user. They have a shared state, operate concurrently and can fail independently without affecting the whole system&#8217;s uptime.</em></p><p><strong>Key Characteristics:</strong></p><ul><li><p><strong>Stability</strong> or <strong>Serviceability</strong> or <strong>Manageability</strong> = s<em>implicity and speed with which a system can be repaired or maintained.</em></p></li><li><p><strong>Reliability</strong> <em>= probability a system will fail in a given period</em>. A distributed system is considered reliable if it keeps delivering its services even when one or several of its software or hardware components fail.</p></li><li><p><strong>Scalability</strong> <em>=</em> <em>capability of a system, process, or a network to grow and manage increased demand.</em></p></li><li><p><strong>Efficiency/Performance</strong> = <em>response time</em> (or latency = delay to obtain the first item) + <em>throughput</em> (or bandwidth = the number of items delivered in a given time unit)</p></li><li><p><strong>Availability</strong> = <em>simple measure of the percentage of time that a system, service, or a machine remains operational under normal conditions.</em></p></li><li><p><strong>Fault tolerance</strong> and <strong>Catastrophe-preparedness</strong></p></li></ul><h1><strong>Load Balancer</strong></h1><p><em>= component that acts as a reverse proxy and distributes network or application traffic across a number of servers to improve responsiveness and availability of applications, websites or databases.</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ig8Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg" width="1400" height="296" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:296,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Ig8Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F40e06188-c0e3-4f8e-be29-93212fa17e23_1400x296.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><ul><li><p><strong>Benefits</strong>: faster, uninterrupted service, less downtime and higher throughput, predictive analytics that predict traffic bottlenecks (smart LB), fewer failed or stressed components.</p></li></ul><p><strong>Algorithms (used to balance the load):</strong></p><ul><li><p><strong>Least Connection Method</strong> (directs traffic to the server with the fewest active connections)</p></li><li><p><strong>Least Response Time Method</strong> (directs traffic to the server with the fewest active connections)</p></li><li><p><strong>Least Bandwidth Method</strong> (selects the server that is currently serving the least amount of traffic)</p></li><li><p><strong>Round Robin Method</strong> (cycles through a list of servers and sends each new request to the next server)</p></li><li><p><strong>Weighted Round Robin Method</strong> (servers with higher weights (= an integer value that indicates the processing capacity) receive new and more connections before those with less weights) &#8212; to handle servers with different characteristics (e.g. processing power, availability, load)</p></li><li><p><strong>IP Hash</strong> (a hash of the IP address of the client is calculated to redirect the request to a server)</p></li></ul><h1><strong>Caching</strong></h1><p><em>= temporary storage area that stores the result of expensive responses or frequently accessed data in memory so that subsequent requests are served more quickly.</em></p><p><strong>When to use it</strong> &#8212; when data is read frequently but modified infrequently</p><p><strong>Considerations</strong>:</p><ul><li><p>Set an <strong>expiration policy </strong>that is not too short (as it reloads data too fast from the database) and not too long (since data can become stale).</p></li><li><p>How to keep the cache and database <strong>consistent</strong> (see the <em>cache invalidation</em> section below)</p></li><li><p>The cache can easily become a<strong> single point of failure, </strong>you might need to use multiple caches</p></li><li><p>Choose an <strong>eviction policy </strong>(see the <em>cache eviction policies </em>section below)</p></li><li><p>Think how you handle <strong>concurrency </strong>(aka the reader-writer problem) &#8212; When multiple clients are trying to update the cache at the same time, there can be conflicts. A solution would be to use<strong> commit logs</strong>. To update the cache, we can store all the mutations into logs rather than update immediately. And then some background processes will execute all the logs asynchronously. This strategy is commonly adopted in database design.</p></li></ul><p><strong>Reading from cache:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FC2Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FC2Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 424w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 848w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FC2Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg" width="618" height="322.6842857142857" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:731,&quot;width&quot;:1400,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FC2Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 424w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 848w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!FC2Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99f8dec8-1855-49bf-8e57-0734425933db_1400x731.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Cache Invalidation </strong>= happens when data is modified in the database and becomes inconsistent with the cache.</p><p><strong>Updating the cache:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lhkK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lhkK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 424w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 848w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 1272w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lhkK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png" width="1456" height="371" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:371,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:242961,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lhkK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 424w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 848w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 1272w, https://substackcdn.com/image/fetch/$s_!lhkK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b077adc-8bf1-4a81-8e71-5b5feec8fb70_2364x602.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><ul><li><p><strong>Write-aside cache</strong> &#8212; data is written into the cache and the corresponding database at the same time (also known as <em>lazy loading</em>) &#8212; cons: higher latency</p></li><li><p><strong>Write-through cache </strong>&#8212; application adds or updates an entry in the cache. Cache synchronously writes entry to the data store. Cache returns the data to the application.</p></li><li><p><strong>Write-around cache</strong> &#8212; data is written directly to permanent storage, bypassing the cache &#8212; cons: read request for recently written data will create a &#8220;cache miss&#8221;.</p></li><li><p><strong>Write-back cache</strong> &#8212; data is written to cache alone and completion is immediately confirmed to the client and then data is <em>asynchronously </em>updated to the data store &#8212; pros: low latency and high throughput; cons: risk of data loss in case of a crash</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xdli!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xdli!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 424w, https://substackcdn.com/image/fetch/$s_!xdli!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 848w, https://substackcdn.com/image/fetch/$s_!xdli!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!xdli!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xdli!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg" width="668" height="276.2657142857143" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:579,&quot;width&quot;:1400,&quot;resizeWidth&quot;:668,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xdli!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 424w, https://substackcdn.com/image/fetch/$s_!xdli!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 848w, https://substackcdn.com/image/fetch/$s_!xdli!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!xdli!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8bdf576c-c8df-4a20-b725-29f050aaea1a_1400x579.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Cache eviction policies</strong></h2><ul><li><p><strong>First In First Out (FIFO)</strong> &#8212; evicts the first block accessed first</p></li><li><p><strong>Last In First Out (LIFO)</strong> &#8212; evicts the block accessed most recently</p></li><li><p><strong>Least Recently Used (LRU)</strong> &#8212; evicts the least recently used items first. When the client requests resource A, it happens as follow:<br>If A exists in the cache, we just return immediately.<br>If not and the cache has extra storage slots, we fetch resource A and return to the client. In addition, insert A into the cache.<br>If the cache is full, we kick out the resource that is least recently used and replace it with resource A.</p></li><li><p><strong>Most Recently Used (MRU)</strong> &#8212; evicts the most recently used items first</p></li><li><p><strong>Least Frequently Used (LFU)</strong> &#8212; counts how often an item is needed. Those that are used least often are discarded first.</p></li><li><p><strong>Random Replacement (RR)</strong> &#8212; randomly evicts a candidate item</p></li></ul><p><strong>Concurrency &#8212; reader-writer problem</strong> &#8212; When multiple clients are trying to update the cache at the same time, there can be conflicts. =&gt; use <strong>commit logs</strong>. To update the cache, we can store all the mutations into logs rather than update immediately. And then some background processes will execute all the logs asynchronously. This strategy is commonly adopted in database design.</p><h2><strong>Content Distribution Network (CDN)</strong></h2><p>= <em>system of distributed servers (network) that deliver webpages and other web content to a user based on the geographic locations of the user, the origin of the webpage and a content delivery server.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!956V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!956V!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 424w, https://substackcdn.com/image/fetch/$s_!956V!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 848w, https://substackcdn.com/image/fetch/$s_!956V!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!956V!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!956V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg" width="1400" height="353" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:353,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!956V!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 424w, https://substackcdn.com/image/fetch/$s_!956V!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 848w, https://substackcdn.com/image/fetch/$s_!956V!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!956V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdba41f3e-a09c-4cba-b594-50e736ef341d_1400x353.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>System flow</strong>: a request will first ask the CDN for a piece of static media; the CDN will serve that content if it has it locally available. If it isn&#8217;t available, the CDN will query the back-end servers for the file, cache it locally, and serve it to the requesting user.</p><h1><strong>Message Queues</strong></h1><p><em>= durable component, stored in memory, that supports asynchronous communication. Helps reduce request times for expensive operations that would otherwise be performed in-line. Receives, holds, and delivers messages.</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dwLy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dwLy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dwLy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg" width="650" height="227.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:490,&quot;width&quot;:1400,&quot;resizeWidth&quot;:650,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dwLy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dwLy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c9f14bf-5473-4a40-a9fb-30075bac19a0_1400x490.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>If an operation is too slow to perform inline, you can use a message queue with the following workflow:</p><ul><li><p>An application publishes a job to the queue, then notifies the user of job status.</p></li><li><p>A worker picks up the job from the queue, processes it, then signals the job is complete.</p></li></ul><p>e.g. RabbitMQ, Amazon SQS</p><h1><strong>Proxies</strong></h1><p><em>= piece of software or hardware that acts as an intermediary for requests from clients seeking resources from other servers</em>.</p><ul><li><p>used to filter requests, log requests, or sometimes transform requests. Its cache can serve a lot of requests (If multiple clients access a particular resource, the proxy server can cache it and serve it to all the clients without going to the remote server.)</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2fA0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2fA0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2fA0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg" width="594" height="271.54285714285714" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:640,&quot;width&quot;:1400,&quot;resizeWidth&quot;:594,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2fA0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 424w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 848w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!2fA0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5fea6f4a-2bc3-4a96-b45c-08dd0b3858a8_1400x640.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Types</strong>:</p><ul><li><p><strong>Open Proxy</strong> &#8212; accessible by any Internet user. Allows users within a network group to store and forward Internet services such as DNS or web pages to reduce and control the bandwidth used by the group.</p></li></ul><p><strong>Anonymous Proxy</strong> &#8212; reveals &#1110;ts &#1110;dent&#1110;ty &#1072;s &#1072; server but does not disclose the &#1110;n&#1110;t&#1110;&#1072;l IP address</p><p><strong>Tr&#1072;nsp&#1072;rent Proxy </strong>&#8212; &#1110;dent&#1110;f&#1110;es &#1110;tself, &#1072;nd w&#1110;th the support of HTTP he&#1072;ders, the f&#1110;rst IP &#1072;ddress c&#1072;n be v&#1110;ewed.</p><ul><li><p><strong>Reverse Proxy</strong> &#8212; retrieves resources on behalf of a client from one or more servers</p></li></ul><h1><strong>Databases</strong></h1><h2><strong>SQL vs NoSQL</strong></h2><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/RJVNG/2/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/17b80096-5d24-4809-8b7a-ba5d956c0961_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:992,&quot;title&quot;:&quot;SQL vs NoSQL&quot;,&quot;description&quot;:&quot;&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/RJVNG/2/" width="730" height="992" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><p></p><h2><strong>NoSQL Types</strong></h2><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/W3gm8/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53d3b357-c75e-45f3-a6fe-8f48a3b3f64e_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:743,&quot;title&quot;:&quot;NoSQL Types&quot;,&quot;description&quot;:&quot;&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/W3gm8/1/" width="730" height="743" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h2><strong>Sharding or Data Partitioning</strong></h2><p><em>= process of splitting up a DB/table across multiple machines to improve the manageability, performance, availability, and load balancing of an application</em></p><p><strong>Partitioning Methods:</strong></p><ul><li><p><strong>Horizontal Partitioning</strong> &#8212; put different rows into different tables &#8212; <strong>cons</strong>: unbalanced servers (if the value whose range is used for sharding isn&#8217;t chosen carefully)</p></li><li><p><strong>Vertical Partitioning</strong> divide our data to store tables related to a specific feature in their own server (instagram use case &#8212; user profile information stored on one DB server, friend lists on another) &#8212; <strong>cons</strong>: if our application experiences additional growth, then it may be necessary to further partition a feature specific DB across various servers</p></li><li><p><strong>Directory Based Partitioning</strong> &#8212; create a lookup service which knows your current partitioning scheme and abstracts it away from the DB access code.</p></li></ul><p><strong>Partitioning Criteria:</strong></p><ul><li><p><strong>Key or Hash-based partitioning</strong> &#8212; apply a hash function to some key attributes of the entity we are storing &#8212; <strong>cons</strong>: adding new servers means changing the hash function which would require redistribution of data and downtime for the service. (use Consistent Hashing)</p></li><li><p><strong>List partitioning</strong> &#8212; each partition is assigned a list of values, so whenever we want to insert a new record, we will see which partition contains our key and then store it there</p></li><li><p><strong>Round-robin partitioning</strong> &#8212; With &#8217;n&#8217; partitions, the &#8216;i&#8217; tuple is assigned to partition (i mod n).</p></li><li><p><strong>Composite partitioning</strong> &#8212; combine any of the above partitioning schemes to devise a new scheme</p></li></ul><p><strong>Complexities:</strong></p><p><strong>Joins and Denormalization</strong> &#8212; not feasible to perform joins that span database shards (denormalize the database so that queries that previously required joins can be performed from a single table)</p><p><strong>Referential Integrity</strong> &#8212; enforcing foreign keys in a sharded database can be extremely difficult. (not supported by most RDBMS &#8212; has to be enforced in application code.)</p><p><strong>Celebrity problem</strong> &#8212; how would you deal with hotspots? (one way would be to add a shard for each celebrity)</p><p><strong>Rebalancing/Resharding</strong> &#8212; data distribution is not uniform and there is a lot of load on a shard =&gt; have to create more DB shards or have to rebalance existing shards.</p><p><strong>Strategies</strong>:</p><ul><li><p><strong>Fixed number of partitions</strong> &#8212; Create many more partitions than there are nodes, and assign several partitions to each node</p></li><li><p><strong>Dynamic partitioning</strong> &#8212; when a partition grows to exceed a configured size, it is split into 2 partitions so that approximately half of the data ends up on each side of the split (no of partitions adapt to the total data volume)</p></li><li><p><strong>Partitioning proportionally to nodes</strong> &#8212; have a fixed no of partitions per node. The size of each partition grows proportionally to the dataset size while the no of nodes remains unchanged</p></li></ul><p>Examples of Real World Scenarios:</p><ul><li><p><em><strong><a href="https://medium.com/tinder-engineering/geosharded-recommendations-part-1-sharding-approach-d5d54e0ec77a">Read about Tinder&#8217;s Geosharded Recommendations Part 1: Sharding Approach</a> </strong>&#8212; </em>Based on Google&#8217;s S2 which uses the <strong>Hilbert curve </strong>(<em>=a space-filling curve that preserves spatial locality: two points that are close on the Hilbert curve are close in physical space. Each smallest Hilbert curve clone is a cell, and 4 adjacent cells form a bigger cell.</em>) Create the geoshards by enumerating all the possible container sizes, and calculate the standard deviation of each sharding configuration, the one with smallest standard deviation will be the most balanced geo sharding configuration we are looking for.</p></li><li><p><em><strong><a href="https://medium.com/@buckhx/unwinding-uber-s-most-efficient-service-406413c5871d">Read about Uber&#8217;s Unwinding Uber&#8217;s Most Efficient Service</a> </strong>&#8212; </em>using a QuadTree &#8212; Take a flat projection of your search space and divide it into quarters that we&#8217;ll call cells. You then divide each of those cells into quarters recursively until you hit a defined maximum depth which will be the leaves of the tree.</p></li><li><p><em><strong><a href="https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c">Read about Instragram&#8217;s Sharding &amp; IDs at Instagram</a></strong></em></p></li></ul><h1><strong>Replication</strong></h1><p><em>= keeping a copy of the same data on multiple machines that are connected via a network.</em></p><p><strong>Reasons</strong>:</p><ul><li><p><strong>Latency</strong> &#8212; Keep data geographically close to your users (reduce access latency)</p></li><li><p>High <strong>availability</strong> &#8212; Allow the system to continue working even if some parts of it have failed</p></li><li><p><strong>Scalability</strong> &#8212; Scale out the number of machines that can serve read queries</p></li></ul><p><strong>Leader-based replication</strong>:</p><ul><li><p>One of the replicas is designated as the <strong>leader</strong>. When clients want to write to DB, they send their requests to the leader, which writes the data to its local storage</p></li><li><p>The other replicas (<strong>followers</strong>) receive the data from the leader in a process called <strong>replication log</strong> or <strong>change stream</strong>.</p></li><li><p>When a client wants to read from the DB, it can query both leader/any of the followers.</p></li></ul><h1><strong>Indexes</strong></h1><p><em>= data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure.</em></p><p>Can be created using one or more columns of a database table.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1xDW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1xDW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 424w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 848w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1xDW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg" width="1400" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1xDW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 424w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 848w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!1xDW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99d4b71-e9e4-4b74-9c12-c59d7187f247_1400x694.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h1><strong>ACID</strong></h1><p><em>= set of properties of database transactions intended to guarantee validity even in the event of errors, power failures, etc</em>.</p><p>In the context of databases, a sequence of database operations that satisfies the ACID properties (and these can be perceived as a single logical operation on the data) is called a <strong>transaction</strong>. For example, a transfer of funds from one bank account to another, even involving multiple changes such as debiting one account and crediting another, is a single transaction.</p><p><strong>Atomicity</strong> &#8212; means that you guarantee that either all of the transaction succeeds or none of it does. You don&#8217;t get part of it succeeding and part of it not. If one part of the transaction fails, the whole transaction fails. With atomicity, it&#8217;s either &#8220;all or nothing&#8221;.</p><p><strong>Consistency</strong> &#8212; this ensures that you guarantee that all data will be consistent. All data will be valid according to all defined rules, including any constraints, cascades, and triggers that have been applied on the database.</p><p><strong>Isolation</strong> &#8212; guarantees that all transactions will occur in isolation. No transaction will be affected by any other transaction. So a transaction cannot read data from any other transaction that has not yet completed.</p><p><strong>Durability</strong> &#8212; means that, once a transaction is committed, it will remain in the system &#8212; even if there&#8217;s a system crash immediately following the transaction. Any changes from the transaction must be stored permanently. If the system tells the user that the transaction has succeeded, the transaction must have, in fact, succeeded.</p><h1><strong>CAP</strong></h1><p><em>impossible for a distributed software system to simultaneously provide more than two out of three of the following guarantees (CAP): Consistency, Availability, and Partition tolerance</em></p><ul><li><p><strong>Consistency</strong>: All nodes see the same data at the same time. Consistency is achieved by updating several nodes before allowing further reads.</p></li><li><p><strong>Availability</strong>: Every request gets a response on success/failure. Availability is achieved by replicating the data across different servers.</p></li><li><p><strong>Partition tolerance:</strong> The system continues to work despite message loss or partial failure. Means that the cluster continues to function even if there is a &#8220;partition&#8221; (communication break) between two nodes (both nodes are up, but can&#8217;t communicate).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Zsjc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Zsjc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Zsjc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg" width="1400" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Zsjc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Zsjc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7eb2e670-14f9-405f-91aa-aa7a47acd82e_1400x819.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>All the combinations available are:</p><ul><li><p><strong>CA</strong> &#8212; data is consistent between all nodes &#8212; as long as all nodes are online &#8212; and you can read/write from any node and be sure that the data is the same, but if you ever develop a partition between nodes, the data will be out of sync (and won&#8217;t re-sync once the partition is resolved).</p></li><li><p><strong>CP</strong> &#8212; data is consistent between all nodes, and maintains partition tolerance (preventing data desync) by becoming unavailable when a node goes down.</p></li><li><p><strong>AP</strong> &#8212; nodes remain online even if they can&#8217;t communicate with each other and will resync data once the partition is resolved, but you aren&#8217;t guaranteed that all nodes will have the same data (either during or after the partition)</p><div><hr></div></li></ul><h1><strong>Conclusion</strong></h1><p>I hope this article can be the first stepping stone on your journey to ace your system design interview. Good luck and reach out for any questions or feedback &#128515;</p>]]></content:encoded></item><item><title><![CDATA[Unbreaking the Loop: Go 1.22 fixes for loops]]></title><description><![CDATA[On go's #1 gotcha, its workaround and what the next go version brings]]></description><link>https://blog.diana-enache.com/p/unbreaking-the-loop-go-122-fixes</link><guid isPermaLink="false">https://blog.diana-enache.com/p/unbreaking-the-loop-go-122-fixes</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 22 Dec 2023 07:19:52 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/2fafa5cc-bed0-47e0-b23c-d3edecda61b6_1300x1392.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IKbM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IKbM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 424w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 848w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 1272w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IKbM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3859824,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IKbM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 424w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 848w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 1272w, https://substackcdn.com/image/fetch/$s_!IKbM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5ef3076a-de53-4e2c-bd71-d3555face29d_13933x5628.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As this year comes to an end, a new golang release approaches which promises to fix the existing issue of the <a href="https://go.dev/blog/loopvar-preview">reference loop values</a>.</p><p>But first, let&#8217;s start with understanding what the &#8216;issue&#8217; is by showing the difference between passing by value and by reference. The following code will print what you probably expect: the values 0 to 4 in some random order.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6m_6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6m_6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 424w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 848w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 1272w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6m_6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png" width="688" height="417.81767180925664" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:866,&quot;width&quot;:1426,&quot;resizeWidth&quot;:688,&quot;bytes&quot;:100933,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6m_6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 424w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 848w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 1272w, https://substackcdn.com/image/fetch/$s_!6m_6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc51833b5-0979-46f9-a52c-0ca98710491f_1426x866.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/540d4d55317e7b74c62fd8fd4c54d4bc">gist</a></figcaption></figure></div><p>Now, we&#8217;ve all probably made the error at some point of referencing a variable loop outside its loop scope. If we update the above code to pass by reference, the result will now be 5 5 5 5 5, probably not what we were expecting. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wU4q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wU4q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 424w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 848w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 1272w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wU4q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png" width="634" height="384.86478873239435" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:862,&quot;width&quot;:1420,&quot;resizeWidth&quot;:634,&quot;bytes&quot;:102421,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wU4q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 424w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 848w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 1272w, https://substackcdn.com/image/fetch/$s_!wU4q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3d4cee6-b1e7-4cf1-92eb-2fe315b2fda8_1420x862.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/4df0ffbdaf7e1c1ac3a84336e5fb4da4">gist</a></figcaption></figure></div><p>The issue we're encountering is related to the closure capturing the loop variable <code>i</code> by reference. By the time the goroutines are executed in the <code>main</code> function, they all refer to the final value of <code>i</code> (which is 5) because the loop has completed.</p><p>To fix this, you need to create a new variable inside the loop for each iteration, allowing each goroutine to capture a distinct variable. You can achieve this by introducing a new variable in the loop scope:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-ILL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-ILL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 424w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 848w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 1272w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-ILL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png" width="622" height="534.022630834512" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1214,&quot;width&quot;:1414,&quot;resizeWidth&quot;:622,&quot;bytes&quot;:150660,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-ILL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 424w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 848w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 1272w, https://substackcdn.com/image/fetch/$s_!-ILL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb0e4dec-516d-4cae-a5c4-ed87b61d2363_1414x1214.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/8f15daa7ee5eb28fd71308346cb8c5ce">gist</a></figcaption></figure></div><p>In the above, each goroutine captures a different variable <code>val</code>, and the <code>sync.WaitGroup</code> ensures that the <code>main</code> function waits for all goroutines to finish before exiting. The output should now be the values 0 to 4 as expected.</p><p>This problem was so common that <strong>Go 1.21</strong> contains a new experimental feature which changes the semantics so that each iteration would use a new variable. You can turn it on by exporting the flag <strong>export GOEXPERIMENT=loopvar</strong>. This behaviour will be <a href="https://go.dev/blog/loopvar-preview">enabled by default</a> in <strong>Go 1.22</strong>. Let&#8217;s say we have the following example: </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f8xR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f8xR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 424w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 848w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 1272w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!f8xR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png" width="620" height="511.6761363636364" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1162,&quot;width&quot;:1408,&quot;resizeWidth&quot;:620,&quot;bytes&quot;:165124,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!f8xR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 424w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 848w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 1272w, https://substackcdn.com/image/fetch/$s_!f8xR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feef19e65-a37b-4bac-8aa8-054787963ee5_1408x1162.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on gist</figcaption></figure></div><p>If we enable the flag, this time it will produce the following output: </p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2qWC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2qWC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 424w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 848w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 1272w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2qWC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png" width="670" height="234.4359464627151" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:366,&quot;width&quot;:1046,&quot;resizeWidth&quot;:670,&quot;bytes&quot;:62330,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2qWC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 424w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 848w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 1272w, https://substackcdn.com/image/fetch/$s_!2qWC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bd621b3-ef22-44a6-8b7e-10c545d7f10a_1046x366.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>With modifications like these, it's evident that Go is becoming increasingly welcoming to beginners. Tackling issues such as loop variable capture will significantly accelerate the onboarding process for new developers, enhancing the overall experience for gophers at any skill level.<br></p><p><strong>Resources</strong>:</p><ul><li><p><a href="https://go.dev/blog/loopvar-preview">Golang Blog Announcement</a> </p></li><li><p><a href="https://go.googlesource.com/proposal/+/master/design/60078-loopvar.md">Proposal of Per-Iteration Loop Variable</a></p></li><li><p><a href="https://github.com/golang/go/wiki/LoopvarExperiment">Loopvar Experiment Proposal</a></p></li></ul>]]></content:encoded></item><item><title><![CDATA[The Bear: How a show about a restaurant provides the best leadership takeaways]]></title><description><![CDATA[On the different dysfunctions of a team, conflict resolutions and leadership lessons]]></description><link>https://blog.diana-enache.com/p/the-bear-how-a-show-about-a-restaurant</link><guid isPermaLink="false">https://blog.diana-enache.com/p/the-bear-how-a-show-about-a-restaurant</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 15 Dec 2023 05:25:04 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4868c361-8de2-469d-b790-bfdb460aaeac_5695x3913.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1s51!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1s51!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 424w, https://substackcdn.com/image/fetch/$s_!1s51!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 848w, https://substackcdn.com/image/fetch/$s_!1s51!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 1272w, https://substackcdn.com/image/fetch/$s_!1s51!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1s51!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png" width="1456" height="587" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/af5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:587,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:19675005,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1s51!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 424w, https://substackcdn.com/image/fetch/$s_!1s51!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 848w, https://substackcdn.com/image/fetch/$s_!1s51!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 1272w, https://substackcdn.com/image/fetch/$s_!1s51!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5b0b44-1c57-42a0-ad1b-d896efb23be1_9708x3913.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Some time ago I finished watching the TV series The Bear, and some parts of it keep resurfacing in my brain. And yes, you&#8217;ve guessed it. In order to declutter my mind, I&#8217;ll obviously need to write about it.</p><p>As I watched the show, my mind couldn't help but dwell on the various aspects of it that I come across in my daily work. &#8220;Hey, I know a guy that behaves like that and I have the same issues working with him&#8220;, or &#8220;Hey, I&#8217;ve seen my manager do the same thing to resolve a similar conflict&#8220;. What I found really cool about the show is that each main character seemed to be represented as an accentuated archetype of the different people you might encounter in your work environment: the controller, the perfectionist, the slacker, the over-achiever, the challenger, and somehow (after a really long struggle) the team manages to make it all work. We&#8217;ll get into how in a second. I also found bits of myself in almost every character and made me realise the issues I might be dealing with myself.</p><p>So what I want from this article is to present a reflection of the different dysfunctions of a team, different conflict resolutions, and what leadership lessons we can get from the show.</p><p>But first things first, what is the show about? The Bear unfolds the story of Carmen (Carmy) a young chef who, following his brother's death, returns to his hometown of Chicago. He inherits not only his family restaurant but also his brother's co-manager and a team of disorderly cooks. With experience in some of the world's finest restaurants, he is confident in transforming his family establishment into a culinary landmark. Easy, right? But what Carmy&#8217;s journey will unravel is an endless road of blockers: tight deadlines, challenging budget, difficult stakeholders, endless milestones, the pain of legacy work (in this case his brother&#8217;s huge unpaid debts), a ton of dependencies, unknown risks, burn out, lack of alignment, no shared vision, others&#8217; pursue of self-interest over the well being of the team, personal commitments and so on. These are all real issues we&#8217;ve been dealing with which the show exposes in a really well-crafted manner.</p><div class="image-gallery-embed" data-attrs="{&quot;gallery&quot;:{&quot;images&quot;:[{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d5f1598c-74e2-4136-9da9-65bcda2dc3cc_480x270.gif&quot;},{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1e83f8a-7cfd-4593-8b6d-a3805afc824f_480x270.gif&quot;},{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/45068a08-75d0-4d51-b6cd-fedfc626acb6_480x270.gif&quot;}],&quot;caption&quot;:&quot;&quot;,&quot;alt&quot;:&quot;&quot;,&quot;staticGalleryImage&quot;:{&quot;type&quot;:&quot;image/png&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9f9a030-0e35-4b4f-94be-974aa14c4ecc_1456x474.png&quot;}},&quot;isEditorNode&quot;:true}"></div><h3>The Archetypes</h3><p>I couldn&#8217;t help but analyse how well the characters fitted in the Enneagram&#8217;s classic archetypes roles.</p><p>Firstly, we have Tina,&nbsp;<strong>the Challenger,</strong>&nbsp;who is resentful of change and of new-comers. She likes to be independent and refuses any type of help, even if it&#8217;s needed. When Sydney is appointed chef de cuisine, she constantly undermines Sydney's authority and disregards her advice. Her respect is earned through reason and competency, and not through age or status.</p><p>Sydney is&nbsp;<strong>The Achiever</strong>, the innovator, passionate about cooking, and very hard-working. She is always open and pushes for change. And sometimes pushing for too much at once, especially when the team is already dealing with a lot, leads to conflicts, as we&#8217;ll soon see.</p><p>Carmy is&nbsp;<strong>The Enthusiast</strong>, the observant, focused, and experienced person but with high anxiety, a problem of expressing himself, and the deep frustration that comes when he fails at that. Carmy loses his temper quickly. He doesn&#8217;t know how to communicate with his team. Routine and control calm him down.</p><p>Ritchie is&nbsp;<strong>The Giver</strong>&nbsp;(you doubt it but listen to me), the stubborn, lazy guy who takes it out on every one out of fear and anger at his own life. But under the angry hood, he is the loving and caring guy who cares strongly for his family even if he doesn't show it in words.</p><p>Marcus is&nbsp;<strong>The Peacemaker</strong>&nbsp;and&nbsp;<strong>The Perfectionist</strong>, an emphatic and thoughtful person but his dedication to his passion as a baker sometimes stands in the way of seeing the bigger picture as he fails to put the team&#8217;s wellbeing first before his own. This can be seen when he decides to work on his special doughnut rather than keep up with Carmy&#8217;s orders and his work at the restaurant during a busy and stressful time.</p><p>Getting to know your teammates&#8217; ways of working and needs is crucial to making the team work well together: develop a common language, identify strengths and weaknesses, and build an authentic culture that works for you and them.</p><div class="image-gallery-embed" data-attrs="{&quot;gallery&quot;:{&quot;images&quot;:[{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0477085c-40ab-4b55-ad16-4ddf98452bc6_480x270.gif&quot;},{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8c318aa0-ea13-422a-bf4e-20db6957a98b_480x270.gif&quot;},{&quot;type&quot;:&quot;image/webp&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/37f493ed-3733-4453-9b36-7836816e9708_200x200.webp&quot;}],&quot;caption&quot;:&quot;&quot;,&quot;alt&quot;:&quot;&quot;,&quot;staticGalleryImage&quot;:{&quot;type&quot;:&quot;image/png&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/75856f31-5b20-4d2e-8c4f-7e0c7021af78_1456x474.png&quot;}},&quot;isEditorNode&quot;:true}"></div><h3>Lessons</h3><p>I can&#8217;t share and analyse all the things I really appreciated about this show in the hope that I can still keep this article fairly short. So this is just a compressed list of lessons that came out from the different situations the team dealt with.</p><ol><li><p><strong>Grow your team; hire for passion and invest in skills</strong></p><p>Later in the show Tina and Ebra are sent to culinary school. Marcus is sent to Copenhagen to learn from a well-known baker and Natalie (Carmy&#8217;s sister) is brought in as a project manager because she might be a good fit. Even Richie is sent to a restaurant called Ever because Carmy &#8220;believes in him&#8220;. This completely changes the team&#8217;s dynamics as this constant investment in their skills makes them feel seen and appreciated. So they improve and contribute, and most importantly learn to care and show up.</p><p></p></li><li><p><strong>Don&#8217;t stop the learning</strong> <br>Another aspect I appreciated about the show was presenting how the team got unstuck when they were blocked (mentally). How they found inspiration and new creative ways to move forward. They would go outside and look at architecture, they would taste different foods, try out new restaurants, and travel to new places.<br>Now this sounds expensive, and the thing is growth is not only hard but it can also come with a price. This is where Carmy stepped in with his network. As a leader, you might not be aware of the different realities people come from. Not everyone has your network or resources. <br><br>During the show we can see Syd reading <em>&#8220;Leading with the Heart: Coach K&#8217;s Successful Strategies for Basketball, Business, and Life&#8221; </em>which emphasises the importance of leading with passion, empathy, and integrity. I haven&#8217;t read it but this approach to leadership will be emphasised in a lot of scenes during the show.<br></p></li><li><p><strong>Trust your team</strong></p><p>One of my <em>&#8216;but why&#8217;s&#8217;</em> moments was when Carmy sent Richie to a high-end, three-Michelin-star Chicago restaurant called Ever, to learn about how fine dining establishments are run. At this point, I was wondering how Richie fit in all of that. He&#8217;s the last person who would understand the importance of order and discipline. But what I wasn&#8217;t getting at that time is that Richie was battling some pretty big demons around his own ability and self-worth, like all of us out here, with our ever-growing imposter syndrome voice reminding us every day that we don&#8217;t belong and that we&#8217;ll never be good enough. And this is where Carmy steps in, and tells the crew over at Ever that he believes in Richie and his ability to work with people. Yes, Ritchie was supported by multiple people to be able to develop and thrive but without the trust from the others, maybe he wouldn&#8217;t have been able to step in and help Sydney when the tickets started to back up on opening night. Maybe he wouldn&#8217;t have been able to step up and become the &#8220;Expo&#8221; on that same night. <br></p></li><li><p><strong>Cultivate your shared rituals</strong><br>Do what works for you to solve the issues you&#8217;re dealing with. <br><br>Another aspect that made me <em>&#8220;I need this in my relationship with others. This is good advice.&#8221; </em>was Carmy and Sydney&#8217;s hand gesture. One of the things Carmy learned when he worked for another chef was that when tensions were high and there was no time to smooth things out, they would do this heart hand gesture to tell each other that they were sorry and that they could readdress the issue later. Sometimes too many emotions or having more high-priority issues on your mind don&#8217;t let you have the necessary bandwidth to deal with yet another issue. Having a shared ritual in your team for communicating the <em>&#8220;I&#8217;m sorry, we&#8217;re both well-intentioned, but this is not a good time so let&#8217;s come back to this later.&#8221;</em> can save your situation from overflowing into an out-of-control mess. Later on, Carmy and Sydney&#8217;s gesture ritual turns into a lack of communication and avoidance problem but we won&#8217;t talk about it now. What&#8217;s important is that while developing your rituals, you also need to adapt them or get rid of them accordingly when they don&#8217;t make sense for your team anymore. So, don&#8217;t continue doing something just because it used to work in the past.<br><em><br></em>Other communication rituals help them maintain safety, optimise their work and show each other respect, like <em><strong>&#8220;Behind!&#8221;</strong> -  I'm walking behind you, so please don't back up or turn quickly because one of us probably has a piping hot pot of something., <strong>&#8220;Corner!&#8221;</strong> - I'm coming around the corner, so be aware., </em><strong>&#8220;</strong><em><strong>Hands</strong></em><strong>&#8221;</strong>: <em>Someone, please come grab this food and deliver it to the customer while it's hot. <strong>&#8220;Thank you, Chef.&#8221;</strong> <br></em></p></li><li><p><strong>Show respect</strong></p><p>Talking about respect, I really enjoyed the <strong>&#8220;Yes, Chef&#8220;</strong> acknowledgment. Even if they didn&#8217;t agree with one another, they would say &#8220;Yes, Chef&#8220; as a type of <em>&#8220;No matter what you're saying or asking, I hear you&#8221; </em>and I find it a nice and thoughtful way of showing respect.<em><br></em></p></li><li><p><strong>Take Ownership</strong></p><p>Another important lesson is shown during Ritchie&#8217;s time at Ever. During a staff meeting, a member of the team asks who was responsible for a smudge on a plate during a past service. The problem was not the smudge but that no one owned it. No one stepped up. Haven&#8217;t we been in similar meetings at work? Realising that something as small as that detail shouldn't matter, but it does. It matters because it&#8217;s proof of the team&#8217;s culture - a lack of ownership where no one will try to improve a process they&#8217;re not responsible for.</p><p><br>&#8220;<strong>We're not children.</strong> <strong>It's okay to make mistakes.</strong> <strong>We can smudge things, but we need to own up to them with immediacy, integrity, and honesty.&#8221; (<a href="https://www.youtube.com/watch?v=FAwSLkx8iao&amp;ab_channel=FXNetworks">scene</a>)<br></strong></p></li><li><p><strong>Be customer-obsessed</strong> </p><p>Yet another interesting subject presented during Richie&#8217;s time at Ever is how much emphasis an established luxury restaurant puts on attention to detail. Some customers were chatting at their table about how disappointed they were to leave Chicago without trying the deep-dish pizza. The servers listened to the guests and carried the message to the kitchen, where they tracked down a real deep-dish pizza, which the chef remixed and sent it out to be served. Why is this important? The end product is not only the food but also the customer experience. Isn&#8217;t it the same in the tech space? The attention to detail, going above and beyond to understand your users and make their journey more enjoyable is what sets apart a successful company from the others.</p><p></p></li><li><p><strong>Experiment, build, and break into small iterations</strong></p><p>When Syd and Carmy were working together on setting up the menu, they would start small. Experiment. Fail. Try Again. Fail some more. Try something else. When something works, go from there. Iterate. <br></p></li><li><p><strong>Discover and embrace your leadership style</strong></p><p>Not everyone has the same talents or skills, so we all have different ways of conquering a problem. As so beautifully presented in the show, Richie has a way with people, Carmy brings with him his expertise, and Syd is the relentless innovator. While they all struggled for power at the beginning, they learned when it was time to take the lead and when it was time to take a step back and let the others do their thing.</p><div class="image-gallery-embed" data-attrs="{&quot;gallery&quot;:{&quot;images&quot;:[{&quot;type&quot;:&quot;image/webp&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb14c933-b47d-446c-bc40-cd5e213e08e5_480x480.webp&quot;},{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb05b09d-0e73-42cf-aed2-a2eaf2f52812_480x480.gif&quot;},{&quot;type&quot;:&quot;image/gif&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c8968aa1-13d5-4c9f-9ebe-d1e5f1cb0197_500x500.gif&quot;}],&quot;caption&quot;:&quot;&quot;,&quot;alt&quot;:&quot;&quot;,&quot;staticGalleryImage&quot;:{&quot;type&quot;:&quot;image/png&quot;,&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5ae54fb-42f6-445a-a4f5-6e8fac968c3b_1456x474.png&quot;}},&quot;isEditorNode&quot;:true}"></div></li></ol><div><hr></div><p>"The Bear" serves as a captivating reflection of the intricacies that make a team work together, their diverse personalities, and ways of working. What adds further to the show's brilliance is its portrayal of how the team manages to make it all work. It&#8217;s a journey full of frustration, high cortisol levels, and sometimes desperation but when everybody works towards the same goal, nice things start to happen.</p>]]></content:encoded></item><item><title><![CDATA[Beyond Goroutines: Exploring Advanced Concurrency Patterns in Go - Part 2]]></title><description><![CDATA[On Go's singleflight and errgroup packages, and other useful design patterns for concurrency]]></description><link>https://blog.diana-enache.com/p/beyond-goroutines-exploring-advanced</link><guid isPermaLink="false">https://blog.diana-enache.com/p/beyond-goroutines-exploring-advanced</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 08 Dec 2023 09:19:07 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/968e4d36-ce40-4202-89cc-552fdd6f2073_1300x1392.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VlkM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VlkM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 424w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 848w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 1272w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VlkM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3937092,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VlkM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 424w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 848w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 1272w, https://substackcdn.com/image/fetch/$s_!VlkM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0c743136-4b8c-40ff-8cb8-2b96fe17902e_13933x5628.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Last week we&#8217;ve delved into the basics of golang&#8217;s concurrency, how goroutines are scheduled and ways of synchronisation. This week we&#8217;ll take a closer look into some advanced patterns, how they work and when it&#8217;s best to use them.</p><h3>Package Singleflight </h3><ul><li><p>provides <strong>a duplicate function call suppression mechanism.</strong></p></li><li><p>executes and returns the results of the given function, <strong>making sure that only one execution is in-flight for a given key at a time</strong>. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.</p></li><li><p>especially useful in scenarios where multiple goroutines may request the same data simultaneously; makes sure that a specific piece of work is done only once, even if multiple goroutines request it simultaneously; suited for optimising your code by removing redundant processes that generate the same result.</p></li></ul><p>Let&#8217;s say we have the following example:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HHxh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HHxh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 424w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 848w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 1272w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HHxh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png" width="670" height="889.203413940256" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1866,&quot;width&quot;:1406,&quot;resizeWidth&quot;:670,&quot;bytes&quot;:278807,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HHxh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 424w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 848w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 1272w, https://substackcdn.com/image/fetch/$s_!HHxh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b6ae96-bd55-482a-b95e-30ddb92ead8a_1406x1866.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/9716396c68a05252afa1c35496110965">gist</a></figcaption></figure></div><p>The <code>fetchData</code> function simulates fetching data. The <code>singleflight.Group</code> is used to manage concurrent requests for the same key. Each goroutine calls <code>sf.Do</code> with a unique key. The function passed to <code>sf.Do</code> is executed only once for a given key, and subsequent goroutines with the same key will wait for the initial call to complete. The <code>sync.WaitGroup</code> is used to wait for all goroutines to finish.</p><p><strong>Use Cases:</strong></p><ul><li><p><strong>Cache Loading</strong> - to avoid redundant fetching or computation, making sure the operation is only done once.</p></li><li><p><strong>Resource Intensive Operations </strong>- for fetching data or performing an operation that is expensive and should be done only once even if requested by multiple goroutines simultaneously.</p></li><li><p><strong>Rate Limiting</strong> - for limiting the rate at which a specific function is executed to avoid exceeding certain limits.</p><p></p></li></ul><h3>Sync&#8217;s Once Function</h3><ul><li><p>provides a mechanism to ensure that a particular function is executed <strong>only once</strong>, regardless of the number of goroutines calling it.</p></li><li><p>useful for lazy initialisation, one-time setup, or any scenario where a specific action needs to be performed exactly once in a concurrent program.</p></li></ul><p><strong>Use Cases</strong>:</p><ul><li><p><strong>Lazy Initialisation</strong> - <code>sync.Once</code> can be used for lazy initialisation of resources that are expensive to create or set up. The initialisation function will be executed only when the resource is first accessed.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fdBS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fdBS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 424w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 848w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 1272w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fdBS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png" width="668" height="279.36273115220484" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1406,&quot;resizeWidth&quot;:668,&quot;bytes&quot;:95827,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fdBS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 424w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 848w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 1272w, https://substackcdn.com/image/fetch/$s_!fdBS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5069542-50bb-4179-ac3a-6a2f535a6e75_1406x588.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/4bf00fa2d071f037e763fef63f90a032">gist</a></figcaption></figure></div><ul><li><p><strong>Global Initialisation</strong> - When you have global variables or settings that need to be initialised before the application starts, <code>sync.Once</code> can ensure that the initialisation function is executed only once.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!s5on!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!s5on!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 424w, https://substackcdn.com/image/fetch/$s_!s5on!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 848w, https://substackcdn.com/image/fetch/$s_!s5on!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 1272w, https://substackcdn.com/image/fetch/$s_!s5on!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!s5on!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png" width="668" height="276.67517730496456" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:584,&quot;width&quot;:1410,&quot;resizeWidth&quot;:668,&quot;bytes&quot;:97956,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!s5on!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 424w, https://substackcdn.com/image/fetch/$s_!s5on!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 848w, https://substackcdn.com/image/fetch/$s_!s5on!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 1272w, https://substackcdn.com/image/fetch/$s_!s5on!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dcde5be-55c9-4218-a1e4-76855157110b_1410x584.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/da562cd81455ab6dd2af08470d876d13">gist</a></figcaption></figure></div><ul><li><p><strong>Singleton Pattern </strong>- <code>sync.Once</code> can be used to implement a simple form of the singleton pattern, ensuring that an instance of an object is created only once.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!40Ei!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!40Ei!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 424w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 848w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 1272w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!40Ei!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png" width="680" height="259.6011396011396" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:536,&quot;width&quot;:1404,&quot;resizeWidth&quot;:680,&quot;bytes&quot;:97113,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!40Ei!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 424w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 848w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 1272w, https://substackcdn.com/image/fetch/$s_!40Ei!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6067f940-78c3-4a79-b356-eff44eedf22e_1404x536.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/b7ff70f84d8b58cdb7a54871a6da20cc">gist</a></figcaption></figure></div><ul><li><p><strong>Resource Cleanup</strong> - <code>sync.Once</code> can be used to perform resource cleanup or teardown operations exactly once, ensuring that cleanup is not duplicated.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1Y_9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1Y_9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 424w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 848w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 1272w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1Y_9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png" width="682" height="305.25779036827197" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:632,&quot;width&quot;:1412,&quot;resizeWidth&quot;:682,&quot;bytes&quot;:98008,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1Y_9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 424w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 848w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 1272w, https://substackcdn.com/image/fetch/$s_!1Y_9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0c1696-39ba-4948-94fd-9108e5da6b59_1412x632.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/5f5ad3459d3903d1cb97fe024ee19321">gist</a></figcaption></figure></div><p><code>Singleflight</code> and <code>sync.Once </code>might look like they both achieve the same purpose but that&#8217;s not the case. </p><ul><li><p><code>singleflight</code><strong> :</strong></p><ul><li><p>You want to limit the concurrency of a specific function to avoid redundant work.</p></li><li><p>You are dealing with scenarios where multiple goroutines may request the same data or perform similar work concurrently.</p></li></ul></li></ul><ul><li><p><code>sync.Once</code><strong>:</strong></p><ul><li><p>You want to ensure that a particular function is executed exactly once, regardless of the number of goroutines calling it.</p></li><li><p>You are dealing with scenarios like lazy initialization, global setup, or singleton pattern implementation.</p></li></ul><p></p></li></ul><h3>Errgroup</h3><ul><li><p>similar to <code>sync.WaitGroup</code> but where the tasks return errors that are propagated back to the waiter.</p></li><li><p>useful when you have <strong>multiple operations that you want to wait for, but you also want to determine if they all completed successfully</strong>.</p></li><li><p><code>errgroup</code> is designed to manage a group of goroutines. You can add goroutines to the group, and the group will keep track of their execution. If any goroutine in the group returns an error, it cancels the entire group, causing all other goroutines to be signaled to stop. This helps to prevent goroutines from continuing to run when an error has occurred. The <code>Wait</code> method of <code>errgroup.Group</code> can be used to block until all goroutines in the group have completed their execution. The <code>Wait</code> method returns an error if any of the goroutines in the group returned an error. This allows you to handle errors centrally.</p></li></ul><p><strong>Use cases:</strong></p><ul><li><p><strong>Fan-Out Concurrency - </strong>When you need to perform multiple independent tasks concurrently (fan-out), and you want to wait for all of them to complete or return on the first error.</p></li><li><p><strong>Error Handling - </strong>When you want to handle errors collectively instead of checking errors from each goroutine individually.</p></li><li><p><strong>Context Cancellation - </strong><code>errgroup</code> integrates well with the Go context package, allowing you to use context cancellation to stop the entire group if needed.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!H6Al!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!H6Al!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 424w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 848w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!H6Al!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png" width="1414" height="1080" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1080,&quot;width&quot;:1414,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:177881,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!H6Al!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 424w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 848w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!H6Al!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9d984760-f40c-4c7d-b938-3f43ef7c657c_1414x1080.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/30a7750559ec7323b38b4def048e5479">gist</a></figcaption></figure></div><h3>Bounded concurrency via buffered channels</h3><ul><li><p>pattern where you use channels with a specified capacity (buffered channels) to control the level of concurrency.</p></li><li><p>can be used to limit the number of goroutines actively working on a task, preventing unbounded resource consumption.</p></li><li><p>useful in situations where you want to limit the number of simultaneous network requests, file I/O operations, or database queries to avoid overwhelming external resources.</p></li></ul><p><strong>Use cases</strong>:</p><ul><li><p><strong>Rate Limiting - </strong>When you want to limit the rate at which certain operations are performed, such as API requests, file reads/writes, or database queries.</p></li><li><p><strong>Resource Constraints - </strong>When you need to prevent unbounded resource consumption by limiting the number of concurrent operations.</p></li></ul><p>Let&#8217;s say we want to control the rate of our API requests by allowing only two requests to be processed concurrently. In the following example, the <code>processRequest</code> function simulates the processing of each request and we limit the concurrency to 2 using a buffered channel (<code>ch</code>) with a capacity of 2.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GSwB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GSwB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 424w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 848w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 1272w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GSwB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png" width="1420" height="1612" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1612,&quot;width&quot;:1420,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:244609,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GSwB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 424w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 848w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 1272w, https://substackcdn.com/image/fetch/$s_!GSwB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe90ccb8e-960d-41eb-b2be-4541bfb79b87_1420x1612.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/66751b9c1bb3426b035c7b1e57082cc5">gist</a></figcaption></figure></div><h3>Weighted bounded concurrency</h3><ul><li><p>in some cases, not all tasks are equally expensive. So instead of reasoning about the number of tasks we want to run concurrently, we come up with a "cost" for every task and acquire and release that cost from a semaphore.</p></li><li><p>mechanism where tasks are assigned different weights, and the system ensures that the sum of weights of concurrently executing tasks does not exceed a certain limit.</p></li><li><p>allows for fine-grained control over the level of concurrency based on the computational or resource intensity of each task.</p></li></ul><p><strong>Use cases</strong>:</p><ul><li><p><strong>Resource Management - </strong>When tasks have varying resource requirements, and you want to ensure that resource-intensive tasks do not overwhelm the system.</p></li><li><p><strong>Fine-Grained Control - </strong>When you need fine-grained control over the level of concurrency based on the specific characteristics of each task.</p></li><li><p><strong>Adaptive Scaling - </strong>When the system needs to adaptively adjust the concurrency limit based on factors such as system load, resource availability, or external conditions.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-GAB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-GAB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 424w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 848w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 1272w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-GAB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png" width="1436" height="1562" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/be18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1562,&quot;width&quot;:1436,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:207804,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-GAB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 424w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 848w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 1272w, https://substackcdn.com/image/fetch/$s_!-GAB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe18fd82-9b6d-4ab8-89e4-07278d809e83_1436x1562.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wk0Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wk0Z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 424w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 848w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 1272w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wk0Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png" width="1440" height="764" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:764,&quot;width&quot;:1440,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:155894,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wk0Z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 424w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 848w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 1272w, https://substackcdn.com/image/fetch/$s_!wk0Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F05c311d1-562e-4d42-bb79-20f0d27d0227_1440x764.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/2dd1bbd0e6137e38447183a567cc1500">gist</a></figcaption></figure></div><p>In the above example, tasks are assigned weights based on their IDs (for illustration purposes). The <code>worker</code> goroutines read tasks from the <code>tasks</code> channel and process them, taking into account their weights. The <code>concurrencyLimit</code> ensures that the sum of weights of concurrently executing tasks does not exceed the specified limit.</p><div><hr></div><p>Those concurrency mechanisms provide a way to allow you to build highly efficient and responsive systems. Whether handling complex distributed scenarios or managing parallel execution of tasks, Golang's concurrency features contribute to the development of robust and scalable applications in a concurrent world.</p>]]></content:encoded></item><item><title><![CDATA[Book Review: "The Worlds I See: Curiosity, Exploration, and Discovery at the Dawn of AI", by Dr. Fei-Fei Li]]></title><description><![CDATA[On the story of a woman in STEM following her dream of becoming a scientist, the history and birth of computer vision and the implications of AI]]></description><link>https://blog.diana-enache.com/p/book-review-the-worlds-i-see-curiosity</link><guid isPermaLink="false">https://blog.diana-enache.com/p/book-review-the-worlds-i-see-curiosity</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Sun, 03 Dec 2023 09:21:26 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/486d9bec-3c7a-4f36-82cc-6956cfb14835_5059x3703.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!j1hm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!j1hm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 424w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 848w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 1272w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!j1hm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7857281,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!j1hm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 424w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 848w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 1272w, https://substackcdn.com/image/fetch/$s_!j1hm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d8d20f-7aec-4306-8657-fa1581954bb7_9689x3913.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I have been following Dr Fei Fei Li&#8217;s research since my master's degree days and when I saw the news that she has just published her memoir, couldn&#8217;t be more excited to find out more about her days as a researcher.</p><p>Dr. Li is a renowned computer scientist who played a crucial role in shaping the evolution of contemporary artificial intelligence. Her new memoir presents her journey from her childhood years in China, navigating the hurdles of relocating to the United States during high school, to her eventual ascent as a prominent researcher and professor at Stanford, at the forefront of technological innovation.</p><p>In &#8220;The Worlds I See&#8221;, she presents her experiences and challenges as an immigrant, a woman in STEM, a teacher and a scientist, emphasising her profound passion for the work that has solidified her standing as a well-known thought leader in the field of AI.</p><p>Born in Chengdu, China, she moved to Parsippany, New Jersey in the early 1990s where she met Mr Sabella, her math teacher and her first American friend. She majored in physics and then went on to study at Princeton on a full scholarship, followed by a PhD at Caltech. Dr. Li completed her dissertation, titled "<em>Visual Recognition: Computational Models and Human Psychophysics"</em>, under the primary supervision of Pietro Perona and secondary supervision of Christof Koch, where she put together ImageNet, a fourteen million-image dataset that enabled rapid advances in computer vision in the 2010s.</p><h3>The birth of ImageNet</h3><p>During an era when the predominant focus of AI research was on models and algorithms, Dr Li aimed to broaden and enhance the dataset available for training AI algorithms. She admits this was not an easy challenge as she recounts her struggles and efforts in encouraging other academics&#8217; use of the dataset in the early years:</p><p><em>&#8220;We&#8217;d dedicated years of our lives to a data set that was orders of magnitude beyond anything that has ever existed, orchestrated an international competition to explore its capabilities, and, for all that, accomplished little more than simply reifying the status quo. If ImageNet was a bet, it was time to start wondering if we&#8217;d simply lost.&#8220;</em></p><p>ImageNet contains more than 20,000 categories where a typical category consists of several hundred images. Images have been annotated to indicate what objects are pictured and in at least one million of the images, bounding boxes are also provided.</p><p>As her husband Silvio mentioned&nbsp;<em>&#8220;That&#8217;s another thing about hypotheses - sometimes they take time to win everyone over.&#8220;</em>, and he was right. During one of the annual <strong>ImageNet Large Scale Visual Recognition Challenge</strong> (<strong>ILSVRC</strong>) where programs compete to correctly classify and detect objects and scenes, one of the contestants&#8217; algorithms had the surprising results of an <strong>85%</strong> accuracy. They had used a neural network, a concept talked about only decades earlier but considered &#8220;<em>a dusty artifact, encased in glass and protected by velvet ropes.</em>&#8220; More precisely, the algorithm was based on a&nbsp;<strong>CNN</strong>&nbsp;(<strong>convolutional neural network</strong>), where each of its layers integrates further details into higher and higher levels of awareness until a real-world object comes fully into view.</p><p>A CNN behaves like a retina,&nbsp;<em>&#8220;its outermost layer applies thousands of receptive fields to the pixels of a photograph, each turned to a unique, tiny pattern and activating when it encounters it. [&#8230;] At this level of awareness, these filters could be reacting to anything [&#8230;] Rather than arbitrarily deciding in advance which feature to look for, the authors allowed each of its hundreds of thousands of neurons to learn their own sensitivities from the training data, without manual intervention. [&#8230;] Signals from those thousands of receptive fields travel deep into the network, merging and clustering into larger, clearer hints. Each new layer, operating at a more sophisticated level of perception than the last, responds when sensing something familiar - something it had been trained to recognize.&#8220;</em></p><p>The team that published the entry was a three-man team, made of Alex Krizhevsky (hence the name of the algorithm, AlexNet), Ilya Sutskever, and Geoffrey E. Hinton (the same person that has developed the backpropagation technique in the mid-80s).</p><h3>The intersection of computer vision with other domains</h3><p>A second-year grad student under Dr Li&#8217;s supervision, Andrej Karpathy&#8217;s thesis focused on the intersection of natural language processing with computer vision, investigating deep learning models suited for this task (<a href="https://searchworks.stanford.edu/view/11849345">Connecting images and natural language, 2016</a>). The exploration started with the idea of having an algorithm that could describe an entire scene and not just being able to label whichever object appears in the background. </p><p>This posed a couple of challenges. The fundamental unit of a spoken language is a word and unlike a pixel, words convey distinct meaning, in isolation or as part of group of words. (e.g. &#8220;rock bottom&#8220;, &#8220;rock fragments&#8220;, &#8220;rock music&#8220; etc)</p><p>Karpathy&#8217;s idea to solve the challenge would be to pair a CNN with an RNN. Another neural network, the <strong>RNN</strong> (<strong>recurrent neural network</strong>), born a couple of decades ago as well would start to gain attention. The model is a bi-directional artificial neural network, meaning that it allows the output from some nodes to affect subsequent input to the same nodes, and hence being able to infer basic properties of text. </p><p><em>&#8220;One to encode visual information and pair it with words, and the other to generate language. We&#8217;ll train our model on pairs of images and human-written description. [&#8230;] the RNN generates each new word in the description conditionally, based on the words already in the sentence.&#8220;</em></p><h3>And the birth of deep learning</h3><p>A sense of eagerness gripped the field as the demand for more intensified: more deeper neural networks, more layers, more compute power to speed up the training process, more data, more of &#8230; <em><strong>everything</strong></em>. It wasn't merely the era of machine learning, but as with more and more layered networks being experimented with across labs, it was the era of <em><strong>deep learning</strong></em>. </p><p>In 2015, a retrospective was published on the ILSVRC competition, including the results conducted by Andrej estimating the human error rate when labelling a thousand images at around <strong>5.1%</strong>. In 2014, GoogLeNet, Google&#8217;s neural network classifier, achieved an all-time-low error rate of just <strong>6.67%</strong>, coming close to surpassing humans at the top.</p><p>Later that year, <strong>Deep Residual Network</strong> (<strong>ResNet</strong>) led by Kaiming He, a network with 152 layers presented a unique innovation: some of the layers could be bypassed during the training phase, allowing different images to direct their influence toward smaller subregions in the network.</p><p>Google later acquired DeepMind with the mission of working on <strong>AGI</strong> (<strong>artificial general intelligence</strong>), a form of AI that rather than just performing tasks like classification or translation, it can replicate all human cognitive capabilities.</p><p>In 2018, at the Google Brain conference, one of the topics was &#8220;<strong>neural architecture research</strong>&#8220;, a domain looking into the optimisation of the neural network&#8217;s architecture. Models are defined by a set of parameters which govern the trade-off between speed vs accuracy, memory vs efficiency etc, and finding a way to balance this trade-off is a complex task. One method to solve this would be to automate it, have a machine learning model design other machine learning models. But from a computational side, this would be a really expensive task.</p><p>More innovations followed, such as LLMs, transformers, &#8220;multimodal&#8220; networks, which allowed models to provide solutions to more complex tasks. </p><h3>Future challenges concerning AI policies</h3><p>Another project Dr Li&#8217;s lab was working on was in the healthcare domain and its goal was to have an automated technique for ensuring that caregivers wash their hands consistently and thoroughly throughout hospital spaces. The aim was to have some type of technology that would blend into the background discreetly, keeping a silent watch and speaking up only when it sensed danger (what Dr Li called&nbsp;<strong>ambient intelligence</strong>). But the unexpected that followed was not only the complexity of the sheer technical task but more the implications of it from a human perspective. The technology was intended for patient safety, but it raised concerns when it came to performance reviews. This was Dr Li&#8217;s first encounter with an aspect of AI that would soon haunt the public&#8217;s mind: its capacity for&nbsp;<em><strong>surveillance</strong></em>.</p><p>Another concern towards AI would soon be raised by the emerging threat known as &#8220;<strong>adversarial attacks</strong>&#8220;, in which input is designed for the sole purpose of confusing an ML algorithm to counterintuitive or destructive ends. For instance, an input photo can be modified with subtle fluctuations in the colours of individual pixels in such a way that would trigger a cascade of failures with the NN. This would have enormous consequences in the self-driving field where for example, a stop sign could be misclassified, putting at risk our own&nbsp;<em><strong>safety</strong></em>.</p><p>In 2016, ProPublica investigations documented the widespread use of&nbsp;<em><strong>biased AI</strong></em>&nbsp;in helping lenders process loan applications and even in assisting judges in making decisions about parole. Similar reports discovered other questionable uses of technology in domains like pre-screening job applicants before being called for interviews.</p><p><em>&#8220;As scary as each of these issues was in isolation, they pointed toward a future that would be characterised by oversight, more inequality, and, in the wrong hands, possibly even a kind of looming, digital authoritarianism.&#8220;</em></p><p>Providing concrete examples of the initial processes and challenges in AI up to the present, Dr. Li contemplates the principles that should underpin AI in both academic and private sector contexts. Lopsided data sets, susceptibility to bias, adversarial influence, fairness, and privacy are all concerns that need to be addressed. This is another challenge Dr Li tries to solve as a Co-Director of the Stanford Institute for Human-Centered Artificial Intelligence.</p><h3>A memoir of one woman&#8217;s curiosity and courage </h3><p>In one of her talks, she mentions her friend, John Etchemendy, reading her first manuscript saying it&#8217;s no good and convincing her she needs to write a memoir which she was reluctant at first.</p><p><em>&#8220;Many AI scientists and technologists can write this book, but there are so many young women, young people, immigrants, people of all walks of life out there, who need to identify with the voice that is not a typical AI voice, and you have a journey that you need to share with these people.&#8220;</em></p><p>The challenges of being an immigrant and woman in STEM paired with the demands of having a career, a marriage and a motherhood are some of the hardships Dr. Li describes so heartedly. As an immigrant and woman in engineering, I&#8217;m really happy she listened to her friend&#8217;s advice as her book has become a cosy journey of reminiscing of my own struggles and the spark and passion that made me move forward. </p><p>Her book&#8217;s final chapter contains probably the most fitting message for all the immigrants scientists out there, <em>&#8220;No matter who you are, or where you came from - you belong here&#8220;</em> so that we can build the future together.</p><div><hr></div><p>The Worlds I See is an inspiring story of one woman&#8217;s curiosity and courage following her passion to become a scientist. Today, Dr. Li is a computer science professor and director of the Stanford Human-Centered AI which she founded while she also plays a role as a consultant to the Senate and House sub-committees and the Executive branch on matters concerning AI security and policies. </p>]]></content:encoded></item><item><title><![CDATA[Cracking the Go Concurrency Basics - Part 1]]></title><description><![CDATA[Part of the Go-In-Depth-Series, this time on concurrency, parallelism, goroutines and data synchronisation]]></description><link>https://blog.diana-enache.com/p/cracking-the-go-concurrency-basics</link><guid isPermaLink="false">https://blog.diana-enache.com/p/cracking-the-go-concurrency-basics</guid><dc:creator><![CDATA[Diana Darie]]></dc:creator><pubDate>Fri, 01 Dec 2023 08:47:29 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/3e453318-fa19-42b2-b06a-457fbb8b0565_1300x1392.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!StH-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!StH-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!StH-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!StH-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!StH-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!StH-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png" width="1456" height="588" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:588,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5941440,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!StH-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 424w, https://substackcdn.com/image/fetch/$s_!StH-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 848w, https://substackcdn.com/image/fetch/$s_!StH-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 1272w, https://substackcdn.com/image/fetch/$s_!StH-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5cf68c1-52ab-4193-b305-617092ac8873_13933x5627.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This week&#8217;s newsletter aims to approach the basics of Go concurrency by going through a couple of concepts:</p><ul><li><p>the difference between concurrency and parallelism</p></li><li><p>how different concepts are done in the JVM (Java) vs Go</p></li><li><p>how goroutines are scheduled</p></li><li><p>ways of declaring goroutines</p></li><li><p>ways of synchronising goroutines by going through the different sync package primitives</p></li></ul><h3>Concurrency vs Parallelism</h3><p><em>&#8220;Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.&#8221;</em>&#8202;&#8212;&#8202;Rob Pike</p><p>Concurrency is a property of the code; parallelism is a property of the running program.</p><blockquote><p>Concurrency is a <em><strong>semantic property of a program or system</strong></em>. Concurrency is when multiple tasks are in progress for overlapping periods of time. Concurrency is a conceptual property of a program or a system, it&#8217;s more about how the program or system has been designed. Long story short, concurrency happens when you have context switching between sequential tasks.</p></blockquote><p>Using the same example as Kirill Bobrov uses in <a href="https://www.manning.com/books/grokking-concurrency">Grokking Concurrency</a>, imagine that one cook is chopping salad while occasionally stirring the soup on the stove. He has to stop chopping, check the stove top, and then start chopping again, and repeat this process until everything is done.</p><p>As you can see, we only have one processing resource here, the chef, and his concurrency is mostly related to logistics; without concurrency, the chef has to wait until the soup on the stove is ready to chop the salad.</p><blockquote><p>Parallelism is an <em><strong>implementation property</strong></em>. It resides on the hardware layer.</p><p>Parallelism is about multiple tasks or subtasks of the same task that literally run at the same time on a hardware with multiple computing resources like multi-core processor.</p></blockquote><p>Back in the kitchen, now we have two chefs, one who can do stirring and one who can chop the salad. We&#8217;ve divided the work by having another processing resource, another chef.</p><blockquote><p>Concurrency can be parallelised but concurrency does not imply parallelism.&nbsp;<br>e.g. In a single-core CPU, you can have concurrency but not parallelism.</p></blockquote><p>=&gt; We don&#8217;t write parallel code, only concurrent code that we hope might be ran in parallel.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kUWh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kUWh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 424w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 848w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kUWh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg" width="1456" height="558" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:558,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kUWh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 424w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 848w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!kUWh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9847ac1-8480-44ac-9d42-650105a18792_1600x613.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>What are GoRoutines?</h3><p>GoRoutines are light-weight execution threads integrated into Go&#8217;s runtime that run independently, along with the initialised functions.</p><h3><strong>Concurrency in Go vs Java</strong></h3><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/GgofG/4/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65c34d67-aef4-45f8-bcf1-7d6cdb14e8c8_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:716,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/GgofG/4/" width="730" height="716" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h3><strong>Scheduling</strong></h3><p>Go&#8217;s mechanism for hosting goroutines is an implementation of what&#8217;s called an <strong>M:N scheduler</strong>: which states that <strong>M</strong> number of goroutines can be distributed over <strong>N</strong> number of OS threads.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!M9-9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!M9-9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 424w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 848w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 1272w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!M9-9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png" width="540" height="201.2167300380228" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:392,&quot;width&quot;:1052,&quot;resizeWidth&quot;:540,&quot;bytes&quot;:72274,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!M9-9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 424w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 848w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 1272w, https://substackcdn.com/image/fetch/$s_!M9-9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e783b3f-8872-4e16-8456-4874c7f37e30_1052x392.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>When a Go program starts =&gt; it is given a logical processor P for every virtual core =&gt; Every P is assigned an OS thread M =&gt; Every Go program is also given an initial G which is the path of execution for a Go program. OS threads are context-switched on and off a core, goroutines are context-switched on and off a M.</p><p>There are two run queues in the Go scheduler.</p><ul><li><p><strong>Global Run Queue (GRQ)</strong></p></li><li><p><strong>Local Run Queue (LRQ)</strong></p></li></ul><p>Each P is given given a LRQ that manages the goroutines assigned to be executed within the context of P. These goroutines take turn being context-switched on and off the M assigned to that P. GRQ is for goroutines that have not been assigned to a P yet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ilLI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ilLI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 424w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 848w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ilLI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png" width="476" height="364.93333333333334" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1104,&quot;width&quot;:1440,&quot;resizeWidth&quot;:476,&quot;bytes&quot;:193984,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ilLI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 424w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 848w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!ilLI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49abd8e0-2c89-4d77-aeeb-db70cb0a48d9_1440x1104.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When a goroutine is performing an asynchronous system call, P can swap the G off M and put in a different G for execution. However, when a goroutine is performing a synchronous system call, the OS thread is effectively blocked. Go scheduler will create a new thread to continue servicing the existing goroutines in the LRQ.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Fk-e!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Fk-e!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 424w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 848w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 1272w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Fk-e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png" width="540" height="300.4120879120879" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:810,&quot;width&quot;:1456,&quot;resizeWidth&quot;:540,&quot;bytes&quot;:242881,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Fk-e!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 424w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 848w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 1272w, https://substackcdn.com/image/fetch/$s_!Fk-e!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa51d4104-cd03-445f-ab03-bf60831abdb3_1916x1066.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Go follows a model of concurrency called the <strong>fork-join model</strong>:</p><ul><li><p><strong>fork</strong> &#8212; at any point in the program, a <em><strong>child</strong></em> branch of execution can be split off and run concurrently with its <em><strong>parent</strong></em></p></li><li><p><strong>join</strong> &#8212; at some point in the future, the concurrent branches of execution will join back together</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bpRg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bpRg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 424w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 848w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 1272w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bpRg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png" width="356" height="303.52601156069363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:590,&quot;width&quot;:692,&quot;resizeWidth&quot;:356,&quot;bytes&quot;:72205,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bpRg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 424w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 848w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 1272w, https://substackcdn.com/image/fetch/$s_!bpRg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F855cf9ed-2fe1-413e-8f5d-d2a01801fde3_692x590.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Ways of declaring goroutines</strong></h3><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eCXg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eCXg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 424w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 848w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 1272w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eCXg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png" width="660" height="195.2112676056338" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:420,&quot;width&quot;:1420,&quot;resizeWidth&quot;:660,&quot;bytes&quot;:56018,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eCXg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 424w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 848w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 1272w, https://substackcdn.com/image/fetch/$s_!eCXg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbce832c8-a1d6-421a-a91c-6accba7dddb1_1420x420.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t6t_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t6t_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 424w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 848w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 1272w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t6t_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png" width="664" height="115.96619718309859" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:248,&quot;width&quot;:1420,&quot;resizeWidth&quot;:664,&quot;bytes&quot;:49633,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t6t_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 424w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 848w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 1272w, https://substackcdn.com/image/fetch/$s_!t6t_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2e8ba74-912f-4265-8d51-a86b19bb361b_1420x248.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I47m!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I47m!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 424w, https://substackcdn.com/image/fetch/$s_!I47m!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 848w, https://substackcdn.com/image/fetch/$s_!I47m!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 1272w, https://substackcdn.com/image/fetch/$s_!I47m!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I47m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png" width="662" height="136.3215796897038" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:292,&quot;width&quot;:1418,&quot;resizeWidth&quot;:662,&quot;bytes&quot;:46510,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I47m!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 424w, https://substackcdn.com/image/fetch/$s_!I47m!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 848w, https://substackcdn.com/image/fetch/$s_!I47m!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 1272w, https://substackcdn.com/image/fetch/$s_!I47m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb1f7568e-3207-45cf-bc3e-f466f2ee5fa7_1418x292.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3><strong>Synchronizing goroutines &#8212; via sync package</strong></h3><p>To make sure your goroutines execute before the main goroutine we need <strong>join points</strong>. These can be created via:</p><p><strong>The WaitGroup primitive</strong></p><p><em>used for waiting for a set of concurrent operations to complete when you either don&#8217;t care about the result of the concurrent operation, or you have other means of collecting their results.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-qrt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-qrt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 424w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 848w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 1272w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-qrt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png" width="1428" height="508" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:508,&quot;width&quot;:1428,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:103706,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-qrt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 424w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 848w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 1272w, https://substackcdn.com/image/fetch/$s_!-qrt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F580feb4f-e41f-42c1-8d07-65023893942b_1428x508.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/650ac349c1949d81d07b8ba605ae2700">gist</a></figcaption></figure></div><p><strong>Closures</strong> = <em>a function value that references variables from outside its body.</em></p><p>With closures, we&#8217;d have <strong>to pass a copy of the variable </strong>into the closure so by the time a goroutine is run, it will be operating on the data from its iteration of the loop.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qUUO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qUUO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 424w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 848w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 1272w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qUUO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png" width="1430" height="750" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:750,&quot;width&quot;:1430,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:111926,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qUUO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 424w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 848w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 1272w, https://substackcdn.com/image/fetch/$s_!qUUO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70fbd94e-1086-4112-ba4a-c9e6a5fd4bb5_1430x750.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/e6f9818ca581cc17820239cdda43dd33">gist</a></figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RDZG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RDZG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 424w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 848w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 1272w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RDZG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png" width="1432" height="822" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:822,&quot;width&quot;:1432,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:154857,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!RDZG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 424w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 848w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 1272w, https://substackcdn.com/image/fetch/$s_!RDZG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f402049-259f-4e8d-bc83-b979d39e39b9_1432x822.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/5f8d44e1c00f10bc71d5e830018816c7">gist</a></figcaption></figure></div><h2><strong>The Mutex primitive</strong></h2><p><em>provides a concurrent-safe way to express exclusive access to these shared resources.</em></p><p>How to: <code>sync.Mutex</code> interface with <code>Lock()</code> and <code>Unlock()</code> methods</p><blockquote><p><em><strong>Shares memory by creating a convention developers must follow to synchronise access to the memory.</strong></em></p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6iod!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6iod!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 424w, https://substackcdn.com/image/fetch/$s_!6iod!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 848w, https://substackcdn.com/image/fetch/$s_!6iod!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 1272w, https://substackcdn.com/image/fetch/$s_!6iod!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6iod!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png" width="1426" height="1478" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1478,&quot;width&quot;:1426,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:204023,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6iod!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 424w, https://substackcdn.com/image/fetch/$s_!6iod!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 848w, https://substackcdn.com/image/fetch/$s_!6iod!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 1272w, https://substackcdn.com/image/fetch/$s_!6iod!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd177ea6c-8ac9-42dc-8bcc-7d001da68ad0_1426x1478.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/8841a76ff900697cd33c381048004028">gist</a></figcaption></figure></div><h2><strong>The RWMutex primitive</strong></h2><p><em>same as Mutex but it provides a read/write lock. We can have a multiple number of readers holding a reader lock as long as nobody is holding a writer lock.</em></p><p>How to: <code>sync.RWMutex</code> interface with <code>RLock()</code> and <code>RUnlock()</code> methods</p><p><strong>RWMutex can only be held by n readers at a time, or by a single writer</strong></p><p>A way of choosing which primitive to use is emphasised in the following diagram:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I1-t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I1-t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 424w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 848w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 1272w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I1-t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png" width="378" height="447.0576923076923" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1722,&quot;width&quot;:1456,&quot;resizeWidth&quot;:378,&quot;bytes&quot;:457314,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!I1-t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 424w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 848w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 1272w, https://substackcdn.com/image/fetch/$s_!I1-t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9859682-4c3c-4d9b-a06c-bc13f5971e4d_4073x4817.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3><strong>Other basic primtives</strong></h3><p><strong>The Cond Primitive</strong></p><p><em>a rendezvous point for goroutines waiting for or announcing the occurrence of an event (=signal between 2 or more goroutines, has no info other than it happened).</em></p><p>How to: <code>sync.NewCond(&amp;sync.Mutex{})</code> with 2 methods:</p><ol><li><p><strong>Signal</strong> - notifies goroutines (runtime picks the one that has been waiting the longest) blocked on a <code>Wait</code> call that the condition has been triggered</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SVmL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SVmL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 424w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 848w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 1272w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SVmL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png" width="1416" height="462" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:462,&quot;width&quot;:1416,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97718,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SVmL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 424w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 848w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 1272w, https://substackcdn.com/image/fetch/$s_!SVmL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff1061f50-129d-44fa-a129-be4996dfa9ed_1416x462.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/98396056f55153d1af9b2db75ad76071">gist</a></figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Hi67!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Hi67!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 424w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 848w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 1272w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Hi67!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png" width="1406" height="982" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:982,&quot;width&quot;:1406,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:167381,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Hi67!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 424w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 848w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 1272w, https://substackcdn.com/image/fetch/$s_!Hi67!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdfcb936c-7976-4546-9ea6-582a3e19325b_1406x982.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/95a3653df9ee3ebe2d3afbe6ad20d4b4">gist</a></figcaption></figure></div><ol start="2"><li><p><strong>Brodcast</strong> - sends signal to all waiting goroutines</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fo-D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fo-D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 424w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 848w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 1272w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fo-D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png" width="1414" height="1694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1694,&quot;width&quot;:1414,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:304635,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fo-D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 424w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 848w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 1272w, https://substackcdn.com/image/fetch/$s_!fo-D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ae4244b-7304-4339-99e9-c09c8854d16d_1414x1694.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/1cd45c53815e8145745ba1b12cdd4066">gist</a></figcaption></figure></div><p><strong>Once</strong></p><p>ensures that only <strong>one call to </strong><code>Do</code> ever calls the function passed in</p><blockquote><p><em>counts the <strong>no of times </strong></em><code>Do</code> is called<strong>, not how many unique functions passed into </strong><code>Do</code> are called</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Sb-Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 424w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 848w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 1272w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png" width="1416" height="502" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:502,&quot;width&quot;:1416,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:79959,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 424w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 848w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 1272w, https://substackcdn.com/image/fetch/$s_!Sb-Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2b17e92-cfa4-4053-8505-a406c3e33928_1416x502.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/7f0b4ed3c7f7992c87e7d3d1ca068148">gist</a></figcaption></figure></div><p><strong>Pool</strong></p><p><em>= concurrent-safe implementation of the object pool pattern.</em></p><p>How to:</p><ol><li><p><code>Get</code> interface - checks wether the are any available instances within the pool to return to the caller, and if not, call its <code>New</code> member variable to create one.</p></li><li><p><code>Put</code> interface - to put the instance they were working with back in the pool</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BA2W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BA2W!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 424w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 848w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 1272w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BA2W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png" width="1410" height="718" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:718,&quot;width&quot;:1410,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:138372,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!BA2W!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 424w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 848w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 1272w, https://substackcdn.com/image/fetch/$s_!BA2W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1f604fd-301f-4964-b59d-efb0c58e8028_1410x718.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/74ad143ea927dab8b926bb473ac75dca">gist</a></figcaption></figure></div><p>Uses cases:</p><ul><li><p><strong>memory optimisations</strong> as instantiated objects are garbage collected.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!98rQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!98rQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 424w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 848w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 1272w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!98rQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png" width="1424" height="1432" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1432,&quot;width&quot;:1424,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:236773,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!98rQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 424w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 848w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 1272w, https://substackcdn.com/image/fetch/$s_!98rQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14002a16-bcbc-4e17-ba12-a5c338ddf1de_1424x1432.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/aeeb07273f92b5595c143bb1078f5b1d">gist</a></figcaption></figure></div><ul><li><p><strong>warming up a cache of pre-allocated objects </strong>for operations that must run as quickly as possible. (by trying to guard the host machine&#8217;s memory front-loading the time it takes to get a reference to another object)</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cgzx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cgzx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 424w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 848w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 1272w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cgzx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png" width="1430" height="1788" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1788,&quot;width&quot;:1430,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:270377,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cgzx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 424w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 848w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 1272w, https://substackcdn.com/image/fetch/$s_!cgzx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffd5c6031-726d-4bf8-bb83-5dccd48b1f73_1430x1788.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on gist</figcaption></figure></div><h3><strong>Channels</strong></h3><p>Can be used to synchronise access of the memory and to communicate information between goroutines.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!t2qr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!t2qr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 424w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 848w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 1272w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!t2qr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png" width="1408" height="192" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:192,&quot;width&quot;:1408,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:44320,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!t2qr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 424w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 848w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 1272w, https://substackcdn.com/image/fetch/$s_!t2qr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4261fcde-bb3f-4575-acf1-721853bff470_1408x192.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/675b6c32d8a36ab9e166a6055167f36a">gist</a></figcaption></figure></div><p>They support <strong>unidirectional</strong> flow of data:</p><ul><li><p>channel that can only <strong>read</strong></p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N8-n!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N8-n!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 424w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 848w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 1272w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N8-n!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png" width="1422" height="202" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/48804c5b-305c-42f6-8672-2265accd189d_1422x202.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:202,&quot;width&quot;:1422,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45573,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N8-n!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 424w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 848w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 1272w, https://substackcdn.com/image/fetch/$s_!N8-n!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48804c5b-305c-42f6-8672-2265accd189d_1422x202.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/8a01bed3a580d2361569286b84c85ac7">gist</a></figcaption></figure></div><ul><li><p>channel that can only <strong>send</strong></p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ozA0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ozA0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 424w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 848w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 1272w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ozA0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png" width="1422" height="202" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:202,&quot;width&quot;:1422,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45132,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ozA0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 424w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 848w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 1272w, https://substackcdn.com/image/fetch/$s_!ozA0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7dce4ecd-b847-47bd-9a61-acc9c0e0680c_1422x202.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/32395093c799596a002f7c56aeddd144">gist</a></figcaption></figure></div><p><strong>Sending to/Receiving from a channel</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Dh9g!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dh9g!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 424w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 848w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 1272w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dh9g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png" width="1418" height="332" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:332,&quot;width&quot;:1418,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:73574,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Dh9g!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 424w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 848w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 1272w, https://substackcdn.com/image/fetch/$s_!Dh9g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2b3f3163-4fad-442c-90e5-14ac92b357ee_1418x332.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/2b9be6835531cf50c18d91c1606da56b">gist</a></figcaption></figure></div><p><em>Channels are <strong>blocking</strong>.</em></p><p><em>Any goroutine that attempts to write to a channel that is full will wait until the channel has been emptied.</em></p><p><em>Any goroutine that attempts to read from a channel that is empty will wait until at least one item is placed on it.</em></p><p><strong>Closing a channel</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yd-U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yd-U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 424w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 848w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 1272w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yd-U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png" width="1434" height="196" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:196,&quot;width&quot;:1434,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:42165,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yd-U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 424w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 848w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 1272w, https://substackcdn.com/image/fetch/$s_!yd-U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccb67350-fb17-41bb-aac2-a9d3e894ca59_1434x196.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/047f8c63a2bbc241b9f7bf6ae9f6ceeb">gist</a></figcaption></figure></div><p><strong>Ranging over a channel</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eK9N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eK9N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 424w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 848w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 1272w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eK9N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png" width="1414" height="688" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:688,&quot;width&quot;:1414,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94340,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eK9N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 424w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 848w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 1272w, https://substackcdn.com/image/fetch/$s_!eK9N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6394bcda-91fc-42b3-ba98-da394008eb2a_1414x688.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/face2bd9569eda2e03c9b026ebf1d4e3">gist</a></figcaption></figure></div><p><strong>Closing a channel signals to multiple goroutines</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4ayh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4ayh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 424w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 848w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 1272w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4ayh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png" width="1406" height="720" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:1406,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114590,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4ayh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 424w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 848w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 1272w, https://substackcdn.com/image/fetch/$s_!4ayh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363f3902-1db3-4691-a29f-96a6453cd0b2_1406x720.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/83f9b0823e3d02f3638636f356588a23">gist</a></figcaption></figure></div><h3><strong>Buffered Channels</strong></h3><p><em>= channels that are given a capacity when they&#8217;re instantiated.</em></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vWZ_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vWZ_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 424w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 848w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 1272w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vWZ_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png" width="1410" height="202" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:202,&quot;width&quot;:1410,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:45636,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vWZ_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 424w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 848w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 1272w, https://substackcdn.com/image/fetch/$s_!vWZ_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e889426-4a6d-44c0-a4db-315d704180a3_1410x202.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Code available on <a href="https://gist.github.com/yumed15/7104b0b55574b88163a9abfaba692855">gist</a></figcaption></figure></div><p>Buffered channels are in-memory FIFO queue for concurrent processes to communicate over.</p><div><hr></div><p>In the next newsletter, we&#8217;ll dive deeper into the result of channel operation given a channel&#8217;s state, how to design your code into channel owners and consumers and some other go concurrency patterns.</p>]]></content:encoded></item></channel></rss>