<?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[Alagesan Palani]]></title><description><![CDATA[Advanced Go (Golang), practical patterns, practices and solutions in software design and architecture, Microservices, Cloud, Kubernetes and DevOps.]]></description><link>https://www.alagesanpalani.com</link><image><url>https://substackcdn.com/image/fetch/$s_!bplc!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e36bd2f-66e7-49a4-9de7-935038e1ee51_1024x1024.png</url><title>Alagesan Palani</title><link>https://www.alagesanpalani.com</link></image><generator>Substack</generator><lastBuildDate>Tue, 05 May 2026 11:38:44 GMT</lastBuildDate><atom:link href="https://www.alagesanpalani.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Alagesan Palani]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[alagesann@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[alagesann@substack.com]]></itunes:email><itunes:name><![CDATA[Alagesan Palani]]></itunes:name></itunes:owner><itunes:author><![CDATA[Alagesan Palani]]></itunes:author><googleplay:owner><![CDATA[alagesann@substack.com]]></googleplay:owner><googleplay:email><![CDATA[alagesann@substack.com]]></googleplay:email><googleplay:author><![CDATA[Alagesan Palani]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[PProf debug session to uncover performance problems in production - Profiling solution]]></title><description><![CDATA[Profiling Go Applications with pprof]]></description><link>https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance-5c3</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance-5c3</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 07 Feb 2026 07:40:41 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!pEEU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a href="https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance">Part 1, Description of the go application and its performance issues</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_!pEEU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pEEU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 424w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 848w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 1272w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pEEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png" width="715" height="325" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:325,&quot;width&quot;:715,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41483,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pEEU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 424w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 848w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.png 1272w, https://substackcdn.com/image/fetch/$s_!pEEU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3bb7b131-80cd-494f-bcdc-12bf090e205b_715x325.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></p><h4>Lets start Profiling:</h4><p>Run the application in one terminal:</p><pre><code>&#10140;  pprof go run pprof_demo.go
2026/02/07 12:54:19 Starting server on :8080</code></pre><p>In another terminal, run below bash script, wait for to it complete and load pprof prompt.</p><pre><code># bash script to generate load
for i in {1..100}; do
  curl "http://localhost:8080/process"
  echo "Request $i done"
done

# Now let's capture a CPU profile
go tool pprof http://localhost:8080/debug/pprof/profile</code></pre><p>You will see something like below for running all 100 requests and loading pprof prompt.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-aLJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-aLJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 424w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 848w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 1272w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-aLJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png" width="1330" height="820" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:820,&quot;width&quot;:1330,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:156732,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-aLJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 424w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 848w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.png 1272w, https://substackcdn.com/image/fetch/$s_!-aLJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c62c0a0-8245-4aa3-a547-1cd9c02ea60e_1330x820.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><h4><strong>Understanding the CPU Profile</strong></h4><p>Once you have the profile loaded, try these commands:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!i2TC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!i2TC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 424w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 848w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 1272w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!i2TC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png" width="1072" height="518" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:518,&quot;width&quot;:1072,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:102269,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!i2TC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 424w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 848w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.png 1272w, https://substackcdn.com/image/fetch/$s_!i2TC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F339bbe5b-36fd-4b16-9f9c-6ec8eb5cf80a_1072x518.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, It is spending almost all sampled time <strong>waiting, parking, and waking goroutines</strong>.</p><ul><li><p>Goroutines are runnable only briefly</p></li><li><p>They block almost immediately</p></li><li><p>The scheduler is doing most of the work</p></li></ul><p>This is <strong>coordination overhead dominating execution</strong>.</p><p><strong>Blocker #1: Massive Lock Contention</strong></p><pre><code>runtime.NumCPU() * 10

------

p.mu.Lock()
p.processed[key]++
p.aggregates[key] += val
p.records = append(p.records, &amp;record)
p.mu.Unlock()</code></pre><p>You now have:</p><ul><li><p>many goroutines</p></li><li><p>fighting for one mutex</p></li><li><p>waking &#8594; blocking &#8594; parking &#8594; waking again</p></li></ul><p><strong>Blocker #2: Buffered Channel == Burst Scheduling</strong></p><pre><code>recordCh := make(chan Record, len(records))</code></pre><p>This causes:</p><ul><li><p>instant enqueue of all work</p></li><li><p>workers wake simultaneously</p></li><li><p>mutex contention storm</p></li><li><p>scheduler churn</p></li></ul><p>Critical fixes:</p><ul><li><p>reduce goroutine count equal to core count</p></li><li><p>introduce context cancellation/done </p></li><li><p>use select case to deterministically end the for loop</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_!wg5D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wg5D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 424w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 848w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 1272w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wg5D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png" width="1310" height="1608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1608,&quot;width&quot;:1310,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:253219,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wg5D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 424w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 848w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.png 1272w, https://substackcdn.com/image/fetch/$s_!wg5D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99edd118-b534-4ca5-adcc-578606fa85cb_1310x1608.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>Rerun the profile and see results improved on goroutine contention and locking.</p><p>Now lets identify memory issues:</p><pre><code>go tool pprof http://localhost:6060/debug/pprof/allocs</code></pre><p>will result into:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UzBE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UzBE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 424w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 848w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 1272w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UzBE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png" width="1314" height="530" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:530,&quot;width&quot;:1314,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:234983,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UzBE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 424w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 848w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.png 1272w, https://substackcdn.com/image/fetch/$s_!UzBE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11222b-c82e-4077-9c46-e4003dbf2087_1314x530.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>major allocations happening in generateRecords, createKey, processRecord and sortTags.</p><p>when you run &#8220;list createKey&#8221; you will see createKey has impact on string concatenation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QzFI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QzFI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 424w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 848w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 1272w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QzFI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png" width="1456" height="440" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:440,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:182568,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QzFI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 424w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 848w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.png 1272w, https://substackcdn.com/image/fetch/$s_!QzFI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1a33bb8-6a42-433d-aafa-cdec9539204b_1726x522.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>So, Lets improve createKey by introducing stringBuilder</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MkND!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MkND!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 424w, https://substackcdn.com/image/fetch/$s_!MkND!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 848w, https://substackcdn.com/image/fetch/$s_!MkND!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 1272w, https://substackcdn.com/image/fetch/$s_!MkND!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MkND!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png" width="1060" height="530" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:530,&quot;width&quot;:1060,&quot;resizeWidth&quot;:null,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MkND!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 424w, https://substackcdn.com/image/fetch/$s_!MkND!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 848w, https://substackcdn.com/image/fetch/$s_!MkND!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.png 1272w, https://substackcdn.com/image/fetch/$s_!MkND!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faef9600c-ce21-4a70-9b55-737ec8ce00bd_1060x530.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>and improve sorting by sorting while generating test data itself so that we completely remove the sortTags func.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!E838!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!E838!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 424w, https://substackcdn.com/image/fetch/$s_!E838!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 848w, https://substackcdn.com/image/fetch/$s_!E838!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 1272w, https://substackcdn.com/image/fetch/$s_!E838!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!E838!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png" width="1402" height="788" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:788,&quot;width&quot;:1402,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:192760,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!E838!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 424w, https://substackcdn.com/image/fetch/$s_!E838!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 848w, https://substackcdn.com/image/fetch/$s_!E838!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.png 1272w, https://substackcdn.com/image/fetch/$s_!E838!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd45777b9-8db2-4e63-8ce9-6b5cadba0468_1402x788.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>Now, when you run pprof allocs, you will see a lot improvement:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9q89!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9q89!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 424w, https://substackcdn.com/image/fetch/$s_!9q89!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 848w, https://substackcdn.com/image/fetch/$s_!9q89!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 1272w, https://substackcdn.com/image/fetch/$s_!9q89!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9q89!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png" width="1316" height="458" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:458,&quot;width&quot;:1316,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:158259,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9q89!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 424w, https://substackcdn.com/image/fetch/$s_!9q89!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 848w, https://substackcdn.com/image/fetch/$s_!9q89!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.png 1272w, https://substackcdn.com/image/fetch/$s_!9q89!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F544f3476-3c9f-44ee-8140-51976a30f0d5_1316x458.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>Pls note, now allocs not flagging createKey, sortTags, generateRecords.</p><p>Sameway you can try profiling many other pprof options such as:</p><pre><code>go tool pprof http://localhost:8080/debug/pprof/goroutine
go tool pprof http://localhost:8080/debug/pprof/block
go tool pprof http://localhost:8080/debug/pprof/mutex</code></pre><h2><strong>Visualizing the Problem</strong></h2><p>You can visualize the pprof result visually on browser.</p><p>As a prerequisite you need to install graphviz in your mac.</p><pre><code>brew install graphviz</code></pre><p>then run:</p><pre><code>go tool pprof -http=:8081 http://localhost:8080/debug/pprof/profile</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!X2R1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!X2R1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 424w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 848w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 1272w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!X2R1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png" width="1456" height="1692" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1692,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:239807,&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;:&quot;https://www.alagesanpalani.com/i/187176309?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!X2R1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 424w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 848w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.png 1272w, https://substackcdn.com/image/fetch/$s_!X2R1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbc209dcf-645c-4757-82f1-25ea47ef84c8_1496x1738.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><pre><code># Generate CPU flame graph
go tool pprof -http=:8081 http://localhost:8080/debug/pprof/profile

# Generate heap flame graph  
go tool pprof -http=:8082 http://localhost:8080/debug/pprof/heap

# Generate goroutine graph
go tool pprof -http=:8083 http://localhost:8080/debug/pprof/goroutine</code></pre><p>there are many other options to explore. enjoy.</p><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance-5c3/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance-5c3/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[PProf debug session to uncover performance problems in production]]></title><description><![CDATA[Profiling Go Applications with pprof]]></description><link>https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 07 Feb 2026 07:39:57 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SeaD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Performance issues in Go rarely come from &#8220;slow Go.&#8221;<br>They come from <strong>unseen allocation patterns, lock contention, goroutine explosions, and architectural coupling</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_!SeaD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SeaD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 424w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 848w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 1272w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SeaD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png" width="715" height="325" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:325,&quot;width&quot;:715,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:41483,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SeaD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 424w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 848w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.png 1272w, https://substackcdn.com/image/fetch/$s_!SeaD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ac7303-8ac4-44fc-8b56-c3bbef1c309b_715x325.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></p><p>I am going to walk you through a typical pprof debug session in production with a real life go example and identify RCA for the performance problems and fix them for good.</p><p>we will:</p><ul><li><p>Capture CPU &amp; memory profiles</p></li><li><p>Identify hot paths and contention</p></li><li><p>Refactor the code</p></li><li><p>Re-run pprof to verify improvements</p></li></ul><p>sounds good? lets go.</p><h4><strong>Our Mission: The &#8220;FastEnough&#8221; Data Processor</strong></h4><p>Let&#8217;s build a data processing service that:</p><ol><li><p>Reads JSON records</p></li><li><p>Processes them through multiple transformation stages</p></li><li><p>Writes results to a simulated database</p></li><li><p>Serves metrics via HTTP</p></li></ol><p><strong>But here&#8217;s the catch:</strong> Our initial implementation has hidden performance issues that only show up under load - not during regular load.</p><h4><strong>Lets see the Problematic Implementation</strong></h4><p>First, lets define data struct to represent data model, We will define Record and Processor structs as 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_!xX1C!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xX1C!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 424w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 848w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xX1C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png" width="982" height="1024" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:982,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:202100,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xX1C!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 424w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 848w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!xX1C!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07e3ba72-1c42-4d90-9fa0-e826c59b0b10_982x1024.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>In the code snippet above a Record represent a data record that we are processing with simple fields.  Processor is where our pipeline logic will live to process this record using goroutines and a NewProcessor func to create and return an object pointer for the same.</p><p>We create first func generateRecords and attach it to Processor struct. func generateRecords below helps us to generate test data as the input source for the processing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rPUT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rPUT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 424w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 848w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 1272w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rPUT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png" width="1402" height="646" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:646,&quot;width&quot;:1402,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:151631,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rPUT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 424w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 848w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.png 1272w, https://substackcdn.com/image/fetch/$s_!rPUT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58667b56-173f-4294-8887-5eeb41ec459f_1402x646.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>Rest of the funcs we create below are more important as they each demonstrate an individual performance problem which we will solve using pprof. </p><h4>Problem 1: Inefficient string concatenation:</h4><p>Note func createKey below does inefficient string concatenation inside a loop, everytime new string is created which puts stress on both memory and cpu.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zfKx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zfKx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 424w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 848w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 1272w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zfKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png" width="1330" height="490" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:490,&quot;width&quot;:1330,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87967,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zfKx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 424w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 848w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.png 1272w, https://substackcdn.com/image/fetch/$s_!zfKx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff28cde40-e021-4da0-9425-3e24f34bbbb0_1330x490.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><h4>Problem 2: Unnecessary sorting on every call:</h4><p>func sortTags below sorts tags on every call irrespective of whether sorting is needed or not - we will understand this better at the time of calling this func.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_TT4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_TT4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 424w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 848w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 1272w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_TT4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png" width="1006" height="562" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:562,&quot;width&quot;:1006,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:102563,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_TT4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 424w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 848w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.png 1272w, https://substackcdn.com/image/fetch/$s_!_TT4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ed9f0d8-5a01-4b58-b220-4dcd6897734f_1006x562.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><h4>Problem 3: Holding locks too long:</h4><p>following processRecord func is doing the real work, processing record. However, to avoid race condition, it uses mutex to hold lock so that only one thread can process the whole process at a time. Problem with the locking implemented below is we locked whole func logic including the logic thats more time consuming and heavy duty. So, locking is held for longer during creating contention.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_t28!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_t28!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 424w, https://substackcdn.com/image/fetch/$s_!_t28!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 848w, https://substackcdn.com/image/fetch/$s_!_t28!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 1272w, https://substackcdn.com/image/fetch/$s_!_t28!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_t28!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png" width="1094" height="1046" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1046,&quot;width&quot;:1094,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:191210,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_t28!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 424w, https://substackcdn.com/image/fetch/$s_!_t28!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 848w, https://substackcdn.com/image/fetch/$s_!_t28!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.png 1272w, https://substackcdn.com/image/fetch/$s_!_t28!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2bc8601-0391-4429-bacd-6c9aabf1bea7_1094x1046.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><h4>Problem 4: Memory leak via goroutine:</h4><p>This issue needs bit careful analysis.  Here there is no memory leak due to any pointer lose but instead due to <strong>&#8220;goroutine retention&#8221;</strong> - which will be revealed by pprof not your go compiler.</p><p>First issue is goroutine oversubscription along with it stack retention for each goroutine. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lIGa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lIGa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 424w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 848w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 1272w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lIGa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png" width="1214" height="1274" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1274,&quot;width&quot;:1214,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:237800,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lIGa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 424w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 848w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.png 1272w, https://substackcdn.com/image/fetch/$s_!lIGa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fad7443c8-8875-4cb0-8c27-bdd156496432_1214x1274.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 create runtime.NumCPU()*10 goroutines. </p><p>On an 8-core machine:</p><ul><li><p>80 goroutines</p></li><li><p>each with a stack (starts small, grows dynamically)</p></li><li><p>scheduler metadata</p></li><li><p>references to captured variables</p></li></ul><h3>Why this matters</h3><p>Go <strong>cannot reclaim a goroutine&#8217;s stack</strong> until:</p><p>the goroutine exits, the function returns and the references are released.</p><p>If workers block, workers wait on channels and workers stall on locks</p><p>&#8220;their stacks stay allocated&#8221;</p><p>This looks like a <strong>memory leak</strong> in heap profiles. we will analyse and solve this as part of pprof results.</p><p>The below metricsHandler is the first REST service endpoint handler to display records processed statistics at the point of calling this endpoint.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tIPO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tIPO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 424w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 848w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 1272w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tIPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png" width="1396" height="710" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16343b9b-40c6-492f-8752-d106978443c0_1396x710.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:710,&quot;width&quot;:1396,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:162674,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tIPO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 424w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 848w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.png 1272w, https://substackcdn.com/image/fetch/$s_!tIPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16343b9b-40c6-492f-8752-d106978443c0_1396x710.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 below leakHandler is another service REST endpoint we use to cause a memory growth and leak so that we can monitor and resolve with pprof.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xvYS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xvYS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 424w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 848w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 1272w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xvYS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png" width="1312" height="564" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:564,&quot;width&quot;:1312,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:120446,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!xvYS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 424w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 848w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.png 1272w, https://substackcdn.com/image/fetch/$s_!xvYS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc1c9ae94-fcbc-4a55-be96-6a5eff38d17c_1312x564.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>processHandler below is a main REST service endpoint handler to trigger the whole processing by generating records test data and starts gorutines to process the records so that we can observe the processing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y799!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y799!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 424w, https://substackcdn.com/image/fetch/$s_!Y799!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 848w, https://substackcdn.com/image/fetch/$s_!Y799!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 1272w, https://substackcdn.com/image/fetch/$s_!Y799!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y799!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png" width="1364" height="484" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:484,&quot;width&quot;:1364,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y799!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 424w, https://substackcdn.com/image/fetch/$s_!Y799!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 848w, https://substackcdn.com/image/fetch/$s_!Y799!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.png 1272w, https://substackcdn.com/image/fetch/$s_!Y799!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e796a09-274b-48ee-b51d-22c73f54655f_1364x484.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>Last but not the least - main func.  Creates new processor and hooks all the REST api handlers to a rest endpoint path on the server started on port 8080.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7qfP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7qfP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 424w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 848w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 1272w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7qfP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png" width="1044" height="706" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:706,&quot;width&quot;:1044,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:128946,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7qfP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 424w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 848w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.png 1272w, https://substackcdn.com/image/fetch/$s_!7qfP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91e80ce5-7b2b-432b-93a6-7ae6a0c40cfe_1044x706.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><h4>PProf setup:</h4><p>Now, where is the pprof setup in main func and how pprof will work automatically, you ask! thats where a simple go magic helps setup this whole pprof profiling.</p><p>we have following imports at the top of the golang code. here</p><p>_ &#8220;net/http/pprof&#8221; import is our point of interest. This _ import tells go that we are not going to make use of any funcs from the import but instead just import them and while importing the package the &#8220;init&#8221; func from the pprof package automatically executes - just like any other go package. The init func from the package has the logic to automatically setup all the necessary endpoints for profiling at the root http server.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JzNo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JzNo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 424w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 848w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 1272w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JzNo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png" width="454" height="560" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0996610c-6764-49da-bba1-ee9058c66870_454x560.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:560,&quot;width&quot;:454,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:40292,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JzNo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 424w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 848w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.png 1272w, https://substackcdn.com/image/fetch/$s_!JzNo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0996610c-6764-49da-bba1-ee9058c66870_454x560.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><blockquote><p>Pls note: In production system, your pprof server and business logic server must run in its own servers for security reasons. But, in this example analysis, we are setting up in the same server for simplicity.</p></blockquote><p>So, once pprof endpoints are setup in the server, then run the application and check the pprof endpoints are live:</p><pre><code>&#10140;  pprof go run pprof_demo.go
2026/02/07 12:54:19 Starting server on :8080</code></pre><p>then visit at: http://localhost:8080/debug/pprof/</p><p>you will see following page with all the supported pprof endpoints:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qQ-O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qQ-O!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 424w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 848w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 1272w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qQ-O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png" width="1456" height="725" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:725,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:401103,&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;:&quot;https://www.alagesanpalani.com/i/187089034?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qQ-O!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 424w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 848w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.png 1272w, https://substackcdn.com/image/fetch/$s_!qQ-O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F736155fc-b199-48f6-9d96-bc07d3451820_2688x1338.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>In the following blog continuation, we will do pprof profiling, identify and solve all the issues we discussed.</p><p></p><p><a href="https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance-5c3">Part2: Pprof profiling solution</a></p><p></p><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/pprof-debug-session-to-uncover-performance/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Demystifying: "Do not communicate by sharing memory; instead, share memory by communicating." Golang idiom.]]></title><description><![CDATA[Golang's approach to effectively addressing locking and race condition]]></description><link>https://www.alagesanpalani.com/p/demystifying-do-not-communicate-by</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/demystifying-do-not-communicate-by</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Tue, 03 Feb 2026 16:56:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!gziY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We are going to understand the statement with a simple example that contrasts two approaches to concurrency.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gziY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gziY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 424w, https://substackcdn.com/image/fetch/$s_!gziY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 848w, https://substackcdn.com/image/fetch/$s_!gziY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 1272w, https://substackcdn.com/image/fetch/$s_!gziY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gziY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png" width="860" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:860,&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;: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_!gziY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 424w, https://substackcdn.com/image/fetch/$s_!gziY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 848w, https://substackcdn.com/image/fetch/$s_!gziY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.png 1272w, https://substackcdn.com/image/fetch/$s_!gziY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4647a8bb-2dd0-4c4d-9732-ad17cb40d491_860x514.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>The statement is about the preferred way to handle concurrency in Go.</p><ol><li><p>&#8220;Do not communicate by sharing memory&#8221; -&gt; This is the traditional way where multiple threads/goroutines access the same shared memory and use locks to protect access. This is error-prone and can lead to race conditions and deadlocks.</p></li><li><p>&#8220;Instead, share memory by communicating&#8221; -&gt; This is the Go way. Instead of having multiple goroutines accessing the same memory, we use channels to pass data between goroutines. Only one goroutine has access to the data at any given time. This is inspired by the Communicating Sequential Processes (CSP) model.</p></li></ol><p>Let&#8217;s see an example of both.</p><p>Example: We want to increment a counter from multiple goroutines.</p><p>Approach 1: Sharing memory (with a mutex) - This is what we are advised against.</p><p>Approach 2: Using channels (sharing by communicating) - This is the Go way.</p><p>We&#8217;ll write two programs that increment a counter 1000 times using 10 goroutines.</p><h1><strong>Simple Explanation: Don&#8217;t Share, Pass Messages Instead</strong></h1><p>Think of it like <strong>talking vs. fighting over a toy</strong>:</p><h2><strong>Example 1: The Wrong Way (Sharing Memory)</strong></h2><pre><code>package main

import (
    "fmt"
    "time"
)

// BAD: Two kids fighting over the same toy
func main() {
    toy := "Teddy Bear"  // One toy in the middle
    
    // Kid 1 wants the toy
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(100 * time.Millisecond)
            fmt.Println("Kid 1 playing with:", toy)  // Direct access
        }
    }()
    
    // Kid 2 also wants the toy
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(150 * time.Millisecond)
            fmt.Println("Kid 2 playing with:", toy)  // Direct access
        }
    }()
    
    time.Sleep(1 * time.Second)
}</code></pre><p><strong>Problem:</strong> Both goroutines are grabbing the same <code>toy</code> variable. This can cause:</p><ul><li><p>Race conditions (they might play at the same time)</p></li><li><p>Unexpected behavior</p></li><li><p>Need for locks (making it complicated)</p></li></ul><h2><strong>Example 2: The Right Way (Communicating)</strong></h2><pre><code>package main

import (
    "fmt"
)

// GOOD: One toy, passed between kids properly
func main() {
    // Channel acts as a "toy passing system"
    toyChannel := make(chan string)
    
    // Kid 1: Waits for toy, plays, then passes it back
    go func() {
        for i := 0; i &lt; 3; i++ {
            toy := &lt;-toyChannel  // Wait to receive toy
            fmt.Println("Kid 1 playing with:", toy)
            toyChannel &lt;- toy  // Pass toy back
        }
    }()
    
    // Kid 2: Same behavior
    go func() {
        for i := 0; i &lt; 3; i++ {
            toy := &lt;-toyChannel
            fmt.Println("Kid 2 playing with:", toy)
            toyChannel &lt;- toy
        }
    }()
    
    // Start by putting toy in the channel
    toyChannel &lt;- "Teddy Bear"
    
    // Let them play
    time.Sleep(1 * time.Second)
}</code></pre><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p></p><h2><strong>Even Simpler: Counter Example</strong></h2><h3><strong>Bad: Everyone touches the counter</strong></h3><pre><code>var counter = 0  // Shared memory everyone touches

func main() {
    // 5 people trying to increment same counter
    for i := 0; i &lt; 5; i++ {
        go func() {
            counter++  // DANGER: Race condition!
        }()
    }
}</code></pre><h3><strong>Good: One counter owner, others send requests</strong></h3><pre><code>func main() {
    // Channel for increment requests
    increment := make(chan int)
    
    // ONE counter owner
    go func() {
        counter := 0  // Private, not shared
        for amount := range increment {
            counter += amount
            fmt.Println("Counter:", counter)
        }
    }()
    
    // Send requests through channel
    for i := 0; i &lt; 5; i++ {
        increment &lt;- 1  // "Please add 1 to counter"
    }
    
    time.Sleep(100 * time.Millisecond)
}</code></pre><h2><strong>Real-Life Analogy</strong></h2><h3><strong>Shared Memory Approach (Bad)</strong></h3><p>Imagine a <strong>shared whiteboard</strong> in an office:</p><ul><li><p>Everyone writes on it simultaneously</p></li><li><p>People erase each other&#8217;s work</p></li><li><p>Need rules like &#8220;Raise hand before writing&#8221;</p></li><li><p>Chaos ensues</p></li></ul><h3><strong>Communicating Approach (Good)</strong></h3><p>Imagine a <strong>whiteboard with one marker</strong>:</p><ul><li><p>Only one person has the marker at a time</p></li><li><p>Others request the marker when they need it</p></li><li><p>Clear who has control</p></li><li><p>No conflicts</p></li></ul><h2><strong>Complete Simple Program</strong></h2><pre><code>package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("=== SHARING MEMORY (BAD) ===")
    badWay()
    
    time.Sleep(100 * time.Millisecond)
    
    fmt.Println("\n=== COMMUNICATING (GOOD) ===")
    goodWay()
}

func badWay() {
    sharedNumber := 0
    
    // Two goroutines fighting over sharedNumber
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(50 * time.Millisecond)
            sharedNumber++  // Direct access - dangerous!
            fmt.Println("Goroutine 1 set to:", sharedNumber)
        }
    }()
    
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(75 * time.Millisecond)
            sharedNumber = sharedNumber + 2  // Direct access - dangerous!
            fmt.Println("Goroutine 2 set to:", sharedNumber)
        }
    }()
    
    time.Sleep(300 * time.Millisecond)
    fmt.Println("Final (unpredictable):", sharedNumber)
}

func goodWay() {
    // Channel for sending number updates
    updates := make(chan int)
    
    // ONE goroutine owns the number
    go func() {
        privateNumber := 0  // Private, safe
        for change := range updates {
            privateNumber += change
            fmt.Println("Number is now:", privateNumber)
        }
    }()
    
    // Other goroutines send requests
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(50 * time.Millisecond)
            updates &lt;- 1  // "Add 1 please"
        }
    }()
    
    go func() {
        for i := 0; i &lt; 3; i++ {
            time.Sleep(75 * time.Millisecond)
            updates &lt;- 2  // "Add 2 please"
        }
    }()
    
    time.Sleep(300 * time.Millisecond)
    close(updates)
}</code></pre><h2><strong>Bottomline:</strong></h2><p><strong>Old way (C#/Java style):</strong></p><ul><li><p>&#8220;Here&#8217;s a shared variable, everyone can use it&#8221;</p></li><li><p>&#8220;Be careful! Use locks when you touch it&#8221;</p></li><li><p>&#8220;Hope no one forgets the locks!&#8221;</p></li></ul><p><strong>Go way:</strong></p><ul><li><p>&#8220;Here&#8217;s a message box (channel)&#8221;</p></li><li><p>&#8220;If you want to change the data, send a message&#8221;</p></li><li><p>&#8220;One dedicated worker will handle all messages&#8221;</p></li><li><p>&#8220;No fighting, no locks needed&#8221;</p></li></ul><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/demystifying-do-not-communicate-by/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/demystifying-do-not-communicate-by/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[The Architecture Pattern That Actually Works in Golang (Hint: It’s Not Clean Architecture)]]></title><description><![CDATA[Go scales through clarity, not framework-style layering. Domain-centric packages beat onion diagrams every time.]]></description><link>https://www.alagesanpalani.com/p/the-architecture-pattern-that-actually</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/the-architecture-pattern-that-actually</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Fri, 30 Jan 2026 13:31:32 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!gGBK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Clean Architecture is supposed to be the holy grail of software design.</p><p>If you read tech Twitter, watch conference talks, or join a new enterprise team, you&#8217;ve probably seen this diagram:</p><ul><li><p>Controllers</p></li><li><p>Use cases</p></li><li><p>Repositories</p></li><li><p>Entities</p></li><li><p>Ports &amp; Adapters</p></li><li><p>Dependencies flowing inward</p></li></ul><p>It sounds perfect.</p><p>It promises:</p><ul><li><p>separation of concerns</p></li><li><p>testability</p></li><li><p>maintainability</p></li><li><p>framework independence</p></li></ul><p>So when engineers move into Go &#8212; especially from Java/Spring/C# &#8212; they naturally ask:</p><blockquote><p>&#8220;How do we implement Clean Architecture in Go?&#8221;</p></blockquote><p>And at first&#8230;</p><p>It feels right.</p><p>Until one day you open your Go service&#8230;</p><p>&#8230;and you can&#8217;t find where the actual logic lives anymore.</p><p>Let&#8217;s talk about why Clean Architecture often makes Go worse.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gGBK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gGBK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gGBK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1378879,&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;:&quot;https://www.alagesanpalani.com/i/186276475?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gGBK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!gGBK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcfd9f97d-1ec2-4d9a-a7e6-c133a038ccce_1536x1024.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><h4>The Problem Isn&#8217;t Clean Architecture</h4><p>Let&#8217;s be clear:</p><p>Clean Architecture isn&#8217;t evil.</p><p>The problem is this:</p><blockquote><p>Clean Architecture was born in ecosystems that <em>need</em> heavy abstraction.</p></blockquote><p>Go was born in ecosystems that <em>reject</em> it.</p><p>Go doesn&#8217;t scale through frameworks.</p><p>Go scales through clarity.</p><p>And when you import architecture patterns designed for Java/C# into Go&#8230;</p><p>You often end up writing Java/C#&#8230;</p><p>in Go syntax.</p><h4>The First Symptom: Layers Without Meaning</h4><p>In Clean Architecture, a simple operation becomes a relay race.</p><h5>Example: &#8220;Get User by ID&#8221;</h5><p>In a typical layered Go service:</p><pre><code>HTTP Handler
   &#8595;
Service Layer
   &#8595;
UseCase Interface
   &#8595;
Repository Interface
   &#8595;
Repository Implementation
   &#8595;
Database</code></pre><p>That&#8217;s not architecture.</p><p>That&#8217;s indirection.</p><p>What It Looks Like in Code</p><p>Step 1: Repository Interface</p><pre><code>type UserRepository interface {
    FindByID(ctx context.Context, id string) (*User, error)
}</code></pre><p>Step 2: Repository Implementation</p><pre><code>type PostgresUserRepository struct {
    db *sql.DB
}

func (r *PostgresUserRepository) FindByID(ctx context.Context, id string) (*User, error) {
    var user User
    err := r.db.QueryRowContext(ctx,
        "SELECT id, name FROM users WHERE id=$1", id,
    ).Scan(&amp;user.ID, &amp;user.Name)

    return &amp;user, err
}</code></pre><p>Step 3: Service Layer</p><pre><code>type UserService struct {
    repo UserRepository
}

func (s *UserService) GetUser(ctx context.Context, id string) (*User, error) {
    return s.repo.FindByID(ctx, id)
}</code></pre><p>Step 4: Handler Layer</p><pre><code>func (h *Handler) GetUser(c *fiber.Ctx) error {
    id := c.Params("id")

    user, err := h.service.GetUser(c.Context(), id)
    if err != nil {
        return err
    }

    return c.JSON(user)
}</code></pre><p>Now pause.</p><p>Ask yourself honestly:</p><h4>What value did the service layer add?</h4><p>Nothing.</p><p>It forwarded the call.</p><p>This is what happens in most Clean Architecture Go projects:</p><blockquote><p>Layers exist because the architecture demands them&#8230;<br>not because the system needs them.</p></blockquote><h3>The Architecture Tax</h3><p>Every feature now requires:</p><ul><li><p>handler</p></li><li><p>DTO</p></li><li><p>usecase interface</p></li><li><p>implementation</p></li><li><p>repository interface</p></li><li><p>repository implementation</p></li><li><p>mocks</p></li><li><p>wiring</p></li></ul><p>A one-line SQL change becomes a 6-file diff.</p><p>This is not maintainability.</p><p>This is ceremony.</p><h4>Interfaces Everywhere = Unidiomatic Go</h4><p>Clean Architecture pushes interface-first design:</p><pre><code>type UserUseCase interface {
    Execute(ctx context.Context, req Request) (*Response, error)
}</code></pre><p>But Go&#8217;s philosophy is different:</p><blockquote><p>Accept interfaces, return structs.</p></blockquote><p>In Go, interfaces are meant to be:</p><ul><li><p>small</p></li><li><p>consumer-defined</p></li><li><p>introduced only when needed</p></li></ul><p>Not global contracts imposed upfront.</p><p>When you define interfaces for everything, you get:</p><ul><li><p>mock-heavy testing</p></li><li><p>abstraction without purpose</p></li><li><p>code nobody wants to touch</p></li></ul><p>You recreate Spring patterns&#8230;</p><p>without Spring.</p><h4>Testing Becomes Worse, Not Better</h4><p>Clean Architecture claims to improve testability.</p><p>In Go, it often does the opposite.</p><p>Instead of testing real code, teams test mocks of mocks:</p><pre><code>mockRepo.On("FindByID").Return(&amp;User{}, nil)</code></pre><p>Now your test verifies:</p><ul><li><p>the mock was called</p></li><li><p>not the system works</p></li></ul><p>Go&#8217;s testing culture is simpler:</p><ul><li><p>fakes</p></li><li><p>table-driven tests</p></li><li><p>minimal mocking</p></li></ul><p>Clean Architecture often pulls you away from that.</p><h4>The Repository Pattern: The Biggest Java/C# Carryover Mistake</h4><p>Repositories exist in Java/C# because:</p><ul><li><p>ORMs are complex</p></li><li><p>mocking is expected</p></li><li><p>interfaces are explicit contracts</p></li></ul><p>In Go?</p><p>Most repositories are just SQL wrappers.</p><p>They add no abstraction.</p><p>Just boilerplate.</p><p>Go doesn&#8217;t need:</p><pre><code>UserRepository
UserRepositoryImpl
UserRepositoryMock
UserRepositoryFactory</code></pre><p>It needs:</p><pre><code>store.GetUserByID()</code></pre><h4>The Go Alternative: Domain-Centric Architecture</h4><p>Here&#8217;s what actually works in Go:</p><blockquote><p>Organize by domain capability, not technical layers.</p></blockquote><p>Instead of:</p><pre><code>handler/
service/
repository/</code></pre><p>Do:</p><pre><code>internal/user/
internal/billing/
internal/campaign/</code></pre><p>Each domain owns:</p><ul><li><p>its data</p></li><li><p>its persistence</p></li><li><p>its logic</p></li><li><p>its API boundary</p></li></ul><h4>What That Looks Like</h4><h4>Folder Structure</h4><pre><code>internal/user/
   handler.go
   service.go
   store.go
   model.go</code></pre><p>Now everything related to &#8220;user&#8221; lives together.</p><p>No jumping across folders.</p><p>No scattered ownership.</p><h4>Idiomatic Go Example</h4><h4>user/store.go</h4><pre><code>package user

type Store struct {
    db *sql.DB
}

func (s *Store) GetByID(ctx context.Context, id string) (*User, error) {
    var u User

    err := s.db.QueryRowContext(ctx,
        "SELECT id, name FROM users WHERE id=$1", id,
    ).Scan(&amp;u.ID, &amp;u.Name)

    return &amp;u, err
}</code></pre><p>user/service.go</p><pre><code>package user

func GetProfile(ctx context.Context, store *Store, id string) (*User, error) {
    return store.GetByID(ctx, id)
}</code></pre><p>user/handler.go</p><pre><code>package user

type Handler struct {
    store *Store
}

func (h *Handler) GetUser(c *fiber.Ctx) error {
    id := c.Params("id")

    u, err := GetProfile(c.Context(), h.store, id)
    if err != nil {
        return err
    }

    return c.JSON(u)
}</code></pre><p></p><p>No repository interface.</p><p>No forwarding layers.</p><p>Just Go.</p><h4>Interfaces Still Exist &#8212; But Only Where Needed</h4><p>When substitution is real, Go does interfaces beautifully.</p><p>Consumer defines:</p><pre><code>type UserFetcher interface {
    GetByID(ctx context.Context, id string) (*User, error)
}</code></pre><p><strong>You can use this at service layer or in test fake.</strong></p><p>Test fake:</p><pre><code>type FakeStore struct{}

func (f *FakeStore) GetByID(ctx context.Context, id string) (*User, error) {
    return &amp;User{ID: id, Name: "Test"}, nil
}</code></pre><p>No mocking frameworks.</p><p>No ceremony.</p><p>Just behavior.</p><h4>The Real Go Architecture Pattern</h4><p>If you remember one thing:</p><blockquote><p>Go architecture is package boundaries + consumer-defined interfaces.</p></blockquote><p>Not onion diagrams.</p><p>Not ports/adapters folders.</p><p>Just:</p><ul><li><p>cohesive packages</p></li><li><p>explicit dependencies</p></li><li><p>minimal abstraction</p></li><li><p>concrete-first design</p></li></ul><p>That&#8217;s how Kubernetes is built.</p><p>That&#8217;s how Go survives at scale.</p><h4>When Clean Architecture <em>Does</em> Make Sense</h4><p>To be fair, Clean Architecture helps when:</p><ul><li><p>domain is extremely complex</p></li><li><p>multiple backends must be swapped</p></li><li><p>you&#8217;re building a reusable platform library</p></li></ul><p>But for most Go microservices?</p><p>It&#8217;s overkill.</p><h4>Bottom Line</h4><p>Clean Architecture isn&#8217;t wrong &#8212; it&#8217;s just often misapplied.</p><p>In ecosystems like Java/C# and Spring, heavy layering protects you from frameworks, inheritance, and enterprise sprawl.</p><p>But Go doesn&#8217;t have that problem.</p><p>Go&#8217;s strength is precision:</p><ul><li><p>packages with clear ownership</p></li><li><p>interfaces that emerge from consumers</p></li><li><p>concrete code before abstraction</p></li><li><p>domain logic that stays close to where it matters</p></li></ul><p>When you force Go into onion layers, you don&#8217;t get cleanliness.</p><p>You get distance.</p><p>You get ceremony.</p><p>You get a codebase where the business logic is always hiding behind one more interface.</p><p>The best Go architecture isn&#8217;t a diagram.</p><p>It&#8217;s a system that stays readable at 10K lines&#8230; and still readable at 500K.</p><h4>The real lesson:</h4><blockquote><p>Don&#8217;t build architecture.<br>Build software.<br>Let structure emerge.</p></blockquote><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/the-architecture-pattern-that-actually/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/the-architecture-pattern-that-actually/comments"><span>Leave a comment</span></a></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Go’s Most Underrated Performance Feature: Escape Analysis in Production]]></title><description><![CDATA[Why your variables &#8220;escape&#8221; to the heap, how that triggers GC pressure, and what every senior Go engineer should understand before optimizing performance]]></description><link>https://www.alagesanpalani.com/p/gos-most-underrated-performance-feature</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/gos-most-underrated-performance-feature</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Thu, 29 Jan 2026 02:56:52 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vWHR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg" 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_!vWHR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vWHR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vWHR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg" width="1200" height="675" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:675,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32244,&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;:&quot;https://www.alagesanpalani.com/i/186084204?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vWHR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 424w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 848w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!vWHR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea99d7a7-d41a-44a4-a6a6-27252a26bcce_1200x675.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></figure></div><p>Most Go developers think performance problems come from:</p><ul><li><p>slow SQL queries</p></li><li><p>inefficient algorithms</p></li><li><p>too many goroutines</p></li></ul><p>But in real production systems, the biggest performance cost is often much quieter:</p><h3><strong>your variables are escaping to the heap without you realizing it.</strong></h3><p>Every time that happens, Go does something expensive:</p><ul><li><p>allocates memory</p></li><li><p>increases garbage collector workload</p></li><li><p>adds latency under load</p></li></ul><p>The scary part?</p><p>You can write perfectly &#8220;clean&#8221; Go code&#8230;<br>and still trigger thousands of heap allocations per second.</p><p>That&#8217;s why we need to understand one concept that changes how we write services:</p><blockquote><p><strong>Escape Analysis</strong></p></blockquote><p>In this post, we&#8217;ll go from beginner intuition to expert-level understanding &#8212; and connect escape analysis directly to what matters most:</p><h3>production throughput, GC pressure</h3><p>Let&#8217;s begin.</p><h2>1. Stack vs Heap (The Only Starting Point You Need)</h2><p>When Go creates a variable, it must live somewhere in memory.</p><p>There are two places:</p><h3>Stack (fast)</h3><ul><li><p>local to a function</p></li><li><p>allocation is basically free</p></li><li><p>cleaned up automatically when the function returns</p></li></ul><h3>Heap (expensive)</h3><ul><li><p>used for values that outlive a function</p></li><li><p>managed by the garbage collector</p></li><li><p>increases memory churn</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_!Whv0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Whv0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Whv0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg" width="579" height="619" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:619,&quot;width&quot;:579,&quot;resizeWidth&quot;:579,&quot;bytes&quot;:66527,&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;:&quot;https://www.alagesanpalani.com/i/186084204?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Whv0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Whv0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe25efc2b-ba7e-40cb-8e55-301b6123ff1d_579x619.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></p><p>So the key question becomes:</p><blockquote><p>How does Go decide where a variable goes?</p></blockquote><h3>2. Escape Analysis: The Compiler&#8217;s Decision Engine</h3><p>Go decides stack vs heap at <strong>compile time</strong> using:</p><blockquote><p><strong>Escape Analysis</strong></p></blockquote><p>Escape analysis answers:</p><h3>&#8220;Will this value be used after the function returns?&#8221;</h3><ul><li><p>If <strong>no</strong> &#8594; stays on the stack</p></li><li><p>If <strong>yes</strong> &#8594; moves to the heap</p></li></ul><p>That&#8217;s it.</p><h3>3. The Classic Escape Example: Returning a Pointer</h3><pre><code>func NewUser() *User {
    u := User{Name: "Alice"}
    return &amp;u
}</code></pre><p>At first glance, <code>u</code> looks local.</p><p>But you return its address.</p><p>So Go knows:</p><ul><li><p>caller will still use <code>u</code></p></li><li><p>stack frame will disappear</p></li><li><p>value must survive</p></li></ul><p>So:</p><blockquote><p><code>u</code> escapes &#8594; heap allocation</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_!af41!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!af41!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 424w, https://substackcdn.com/image/fetch/$s_!af41!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 848w, https://substackcdn.com/image/fetch/$s_!af41!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!af41!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!af41!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg" width="392" height="548" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:548,&quot;width&quot;:392,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:39722,&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;:&quot;https://www.alagesanpalani.com/i/186084204?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!af41!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 424w, https://substackcdn.com/image/fetch/$s_!af41!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 848w, https://substackcdn.com/image/fetch/$s_!af41!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!af41!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b17e6dd-5a93-4468-acbc-62fa3f8e034d_392x548.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>4. Seeing Escape Analysis in Real Life</h3><p>Go can tell you exactly what escapes.</p><p>Run:</p><blockquote><p>go build -gcflags=&#8221;-m&#8221;</p></blockquote><p>Example output:</p><blockquote><p>u escapes to heap</p></blockquote><p>This is one of the most valuable tools for performance debugging.</p><h3>5. Escapes You Don&#8217;t Expect (But Happen Constantly)</h3><p>The pointer-return example is obvious.</p><p>The dangerous ones are subtle.</p><h4>5.1 Interfaces Cause Hidden Escapes</h4><pre><code>func Print(v interface{}) {
    fmt.Println(v)
}

func main() {
    x := 42
    Print(x)
}</code></pre><p>Here, <code>x</code> is boxed into an interface:</p><ul><li><p>type metadata stored</p></li><li><p>value stored indirectly</p></li><li><p>may escape to heap</p></li></ul><p>In hot paths, interface boxing is a major allocation source.</p><h4>5.2 Closures Capture Variables</h4><pre><code>func Counter() func() int {
    x := 0
    return func() int {
        x++
        return x
    }
}</code></pre><p><code>x</code> must survive after the function returns.</p><p>So:</p><blockquote><p><code>x</code> escapes &#8594; heap</p></blockquote><h4>5.3 Goroutines Force Escapes</h4><pre><code>func Start() {
    msg := "hello"

    go func() {
        fmt.Println(msg)
    }()
}</code></pre><p>Because goroutines run asynchronously:</p><ul><li><p>function returns immediately</p></li><li><p>goroutine still needs <code>msg</code></p></li></ul><p>So:</p><p>msg escapes &#8594; heap</p><h3>6. Why Heap Escapes Matter in Production</h3><p>This is where expert-level understanding needed.</p><p>Stack allocations are cheap.</p><p>Heap allocations are not.</p><p>Heap allocations mean:</p><ul><li><p>more garbage</p></li><li><p>more GC cycles</p></li><li><p>more CPU overhead</p></li><li><p>worse tail latency</p></li></ul><div><hr></div><h3>Production Reality</h3><p>Imagine a service doing:</p><ul><li><p>50k requests/sec</p></li><li><p>2 KB heap allocation per request</p></li></ul><p>That&#8217;s:</p><blockquote><p>100 MB/sec allocations</p></blockquote><p>The garbage collector will run constantly.</p><h5>Symptoms You See in Production</h5><ul><li><p>memory growth</p></li><li><p>CPU spent inside GC</p></li><li><p>latency spikes at p95/p99</p></li><li><p>unpredictable performance under load</p></li></ul><h3>7. pprof: How This Shows Up in Real Systems</h3><p>When heap escapes become a problem, profiling reveals it.</p><p>Run:</p><blockquote><p>go tool pprof http://localhost:6060/debug/pprof/heap</p></blockquote><p></p><p>Sample output:</p><blockquote><p>runtime.mallocgc</p><p>encoding/json.Unmarshal</p><p>fmt.Sprintf</p><p>interface conversions</p></blockquote><p>If you see:</p><ul><li><p><code>runtime.mallocgc</code> dominating</p></li><li><p>GC workers consuming CPU</p></li></ul><p>You don&#8217;t have an algorithm problem.</p><p>You have an allocation problem.</p><h3>8. Escape Analysis Traps in Services</h3><p>This is where it gets real.</p><p>Trap 1: Missing Deadlines &#8594; Heap + Goroutine Leaks</p><pre><code>client.Call(ctx, req)</code></pre><p>Without timeouts:</p><ul><li><p>requests hang</p></li><li><p>goroutines pile up</p></li><li><p>heap grows</p></li></ul><p>Always do:</p><pre><code>ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()</code></pre><p>Trap 2: Logging in Hot Paths</p><pre><code>log.Infof("req=%v", req)</code></pre><p>This causes:</p><ul><li><p>interface boxing</p></li><li><p>reflection</p></li><li><p>allocations</p></li></ul><p>Better:</p><pre><code>log.Infof("userID=%s", req.UserId)</code></pre><p>Trap 3: JSON into map[string]interface{}</p><pre><code>var data map[string]interface{}
json.Unmarshal(body, &amp;data)</code></pre><p>This allocates heavily.</p><p>Use concrete structs instead.</p><p>Trap 4: Returning Pointers Everywhere</p><pre><code>func GetValue() *int</code></pre><p>Go prefers values unless mutation is required.</p><p>Pointers often escape unnecessarily.</p><h3>9. Expert-Level Optimization Philosophy</h3><p>Escape analysis is powerful.</p><p>But don&#8217;t turn it into premature micro-optimization.</p><p>Rule:</p><blockquote><p>Optimize only hot paths.<br>Measure first.<br>Fix allocation hotspots, not random structs.</p></blockquote><p>More importantly focus on:</p><ul><li><p>p99 latency</p></li><li><p>GC churn</p></li><li><p>throughput under load</p></li></ul><p>Not shaving nanoseconds.</p><h2>Bottomline</h2><p>Escape analysis is not a niche compiler detail.</p><p>It explains:</p><ul><li><p>why allocations happen</p></li><li><p>why GC spikes occur</p></li><li><p>why latency worsens under load</p></li><li><p>why some Go services scale beautifully and others don&#8217;t</p></li></ul><p>Once you understand escape analysis:</p><p>You stop guessing about performance.</p><p>You start engineering for production.</p><h3>If you enjoyed this deep dive&#8230;</h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/gos-most-underrated-performance-feature/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/gos-most-underrated-performance-feature/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Accept Interfaces, Return Structs - Idiomatic Go]]></title><description><![CDATA[golang's important design principle for writing Idiomatic Go]]></description><link>https://www.alagesanpalani.com/p/accept-interfaces-return-structs</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/accept-interfaces-return-structs</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Fri, 23 Jan 2026 04:28:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Dbx4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.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_!Dbx4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dbx4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dbx4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:870470,&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;:&quot;https://www.alagesanpalani.com/i/185498189?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Dbx4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!Dbx4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b3c2014-a135-4ea4-a101-8fce69044a3e_1536x1024.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></p><p>If you write Go, you will hear this advice again and again:</p><blockquote><p><strong>&#8220;Accept interfaces, return structs.&#8221;</strong></p></blockquote><p>At first, it sounds confusing.<br>Let&#8217;s break it down <strong>slowly and simply</strong>&#8212;with real examples.</p><h2>First: What Does It Mean?</h2><h3>&#9989; Accept interfaces</h3><p>&#10145;&#65039; Functions should <strong>take interfaces as input</strong></p><pre><code>type Store interface {
    Save(data string) error
}

func ProcessData(s Store) error {
    return s.Save("hello")
}</code></pre><h3>&#9989; Return structs</h3><p>&#10145;&#65039; Functions should <strong>return concrete types (structs)</strong></p><pre><code>type PostgresStore struct {}

func NewPostgresStore() *PostgresStore {
    return &amp;PostgresStore{} // return struct here not interfaces
}

func (p *PostgresStore) Save(data string) error {
    fmt.Println("saving to postgres:", data)
    return nil
}
</code></pre><p>This keeps your code:</p><ul><li><p>Simple</p></li><li><p>Flexible</p></li><li><p>Easy to test</p></li><li><p>Easy to understand</p><p></p></li></ul><h2>Why Not Return Interfaces?</h2><p>Many beginners write code like this:</p><p>&#10060; <strong>Not recommended</strong></p><pre><code>func NewStore() Store {
    return &amp;PostgresStore{}
}</code></pre><p>Why is this bad?</p><p>Because:</p><ul><li><p>The caller doesn&#8217;t know <strong>what they actually got</strong></p></li><li><p>You hide useful methods</p></li><li><p>Debugging becomes harder</p></li><li><p>You add abstraction <strong>too early</strong></p></li></ul><p>Go prefers <strong>explicitness</strong>.</p><h2>Correct Way: Return Structs</h2><p>&#9989; <strong>Recommended</strong></p><pre><code>func NewPostgresStore() *PostgresStore {
    return &amp;PostgresStore{}
}</code></pre><p>Now the caller:</p><ul><li><p>Knows exactly what type it is</p></li><li><p>Can use all methods</p></li><li><p>Can still treat it as an interface later</p></li></ul><h2>Where Do Interfaces Fit Then?</h2><p>Interfaces are best used <strong>at the point of usage</strong>, not creation.</p><h3>Example: Accept an Interface</h3><pre><code>type Store interface {
    Save(data string) error
}

func ProcessData(s Store) error {
    return s.Save("hello")
}</code></pre><p>This function:</p><ul><li><p>Doesn&#8217;t care <em>how</em> data is saved</p></li><li><p>Works with <strong>any Store</strong></p></li><li><p>Is easy to test</p></li></ul><p>Full Example (Real-World Style)</p><p>Step 1: Define a Struct</p><pre><code>type PostgresStore struct {}

func (p *PostgresStore) Save(data string) error {
    fmt.Println("saving to postgres:", data)
    return nil
}</code></pre><p>Step 2: Return the Struct</p><pre><code>func NewPostgresStore() *PostgresStore {
    return &amp;PostgresStore{}
}</code></pre><p>Step 3: Accept Interface Where Needed</p><pre><code>func ProcessData(store Store) error {
    return store.Save("important data")
}</code></pre><p>Step 4: Use Them Together</p><pre><code>store := NewPostgresStore()
ProcessData(store)</code></pre><h2>Why This Is Idiomatic Go</h2><p>This pattern:</p><ul><li><p>Avoids unnecessary abstraction</p></li><li><p>Makes testing easy</p></li><li><p>Keeps code readable</p></li><li><p>Matches how Go&#8217;s standard library works</p></li></ul><p>Example from Go itself:</p><pre><code>func NewReader(r io.Reader) *bufio.Reader</code></pre><p>Go <strong>returns structs</strong> and <strong>accepts interfaces</strong> everywhere.</p><h2>Testing Becomes Super Easy</h2><p>Because you accept interfaces, you can use mocks easily:</p><pre><code>type MockStore struct {}

func (m *MockStore) Save(data string) error {
    return nil
}</code></pre><pre><code>func TestProcessData(t *testing.T) {
    mock := &amp;MockStore{}
    err := ProcessData(mock)
    if err != nil {
        t.Fail()
    }
}</code></pre><p>No frameworks. No magic. Just Go.</p><h2>Common Mistakes to Avoid</h2><p>&#10060; Returning interfaces from constructors<br>&#10060; Creating large interfaces<br>&#10060; Forcing interfaces everywhere<br>&#10060; Writing Java-style abstractions</p><h2>In summary</h2><p>Go is about <strong>clarity over cleverness</strong>.</p><p>If someone can read your function and instantly understand:</p><ul><li><p>What it returns</p></li><li><p>What it needs</p></li></ul><p>Then you&#8217;re writing <strong>good Go</strong>.</p><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/accept-interfaces-return-structs/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/accept-interfaces-return-structs/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Common Anti-Patterns When Java Devs Write Go]]></title><description><![CDATA[Its totally upside down thinking for java devs in designing interface. &#128516;]]></description><link>https://www.alagesanpalani.com/p/common-anti-patterns-when-java-devs</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/common-anti-patterns-when-java-devs</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 17 Jan 2026 13:34:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ThgU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is where things usually go wrong &#128516;<br>I&#8217;ll show the anti-pattern, why it&#8217;s bad, and the Go-native fix.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ThgU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ThgU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ThgU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1860868,&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;:&quot;https://www.alagesanpalani.com/i/184863873?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ThgU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ThgU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1dfb05b-539e-4b2e-8627-94d3fde7d9e0_1536x1024.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></p><h5>Anti-Pattern 1: Interface-First Design</h5><p>Java habit</p><blockquote><p>&#8220;Define interfaces before writing code&#8221;</p></blockquote><pre><code>type UserService interface {
&#9;CreateUser(...)
&#9;UpdateUser(...)
&#9;DeleteUser(...)
&#9;GetUser(...)
&#9;ListUsers(...)
}</code></pre><h5>Why it&#8217;s bad in Go</h5><ul><li><p>speculative abstraction</p></li><li><p>fat interfaces</p></li><li><p>hard to implement and test</p></li></ul><h5>Go way</h5><p>Start with concrete code.<br>Extract interfaces <em>only when needed</em>.</p><pre><code>type UserCreator interface {
&#9;CreateUser(ctx context.Context, u User) error
}</code></pre><p>Small. Focused. Intentional.</p><div><hr></div><h5>Anti-Pattern 2: One Interface Per Struct</h5><pre><code>type UserService interface {
&#9;Serve()
}

type userService struct{}</code></pre><p>This adds <strong>zero value</strong>.</p><h5>Why Java devs do this</h5><ul><li><p>inheritance mindset</p></li><li><p>framework expectations</p></li></ul><p>Go way</p><p>If there&#8217;s only one implementation:</p><ul><li><p>don&#8217;t use an interface</p></li><li><p>use the concrete type</p></li></ul><p>Interfaces are for <strong>variation</strong>, not formality.</p><div><hr></div><h5>Anti-Pattern 3: God Interfaces (a.k.a. &#8220;Service Interfaces&#8221;)</h5><pre><code>type OrderService interface {
&#9;Create()
&#9;Cancel()
&#9;Refund()
&#9;Get()
&#9;List()
&#9;Validate()
&#9;Notify()
}</code></pre><h5>Problems</h5><ul><li><p>violates interface segregation</p></li><li><p>painful to mock</p></li><li><p>impossible to evolve safely</p></li></ul><h5>Go way</h5><p>Split by behavior:</p><pre><code>type OrderCreator interface {
    Create(ctx context.Context, o Order) error
}

type OrderReader interface {
    Get(ctx context.Context, id string) (*Order, error)
}</code></pre><p>Consumers depend only on what they need.</p><div><hr></div><h5>Anti-Pattern 4: Sharing Go Interfaces Across Services</h5><pre><code>// shared/contracts/payment.go
type PaymentService interface {
&#9;Charge(...)
}</code></pre><h5>Why this is dangerous</h5><ul><li><p>compile-time coupling across services</p></li><li><p>forces lockstep deployments</p></li><li><p>breaks microservice autonomy</p></li></ul><h5>Go way</h5><ul><li><p>Share protobuf / OpenAPI</p></li><li><p>Generate clients</p></li><li><p>Define interfaces locally in the consumer</p></li></ul><div><hr></div><h5>Anti-Pattern 5: Mocking Frameworks Everywhere</h5><p>Java dev instinct:</p><blockquote><p>&#8220;Where&#8217;s the mocking library?&#8221;</p></blockquote><p>In Go, mocks are often overkill.</p><h5>Bad Go code</h5><ul><li><p>code generation for trivial tests</p></li><li><p>brittle expectations</p></li></ul><h5>Good Go code</h5><pre><code>type FakeClock struct{}

func (f *FakeClock) Now() time.Time {
&#9;return fixedTime
}</code></pre><p>Simple structs beat magic.</p><div><hr></div><p>Anti-Pattern 6: Inheritance Thinking</p><p>Java mindset:</p><pre><code>BaseService
  &#9500;&#9472; UserService
  &#9500;&#9472; OrderService</code></pre><p>Go has <strong>no inheritance</strong> for a reason.</p><h5>Go way: composition</h5><pre><code>type Service struct {
&#9;logger Logger
&#9;store  Store
}</code></pre><p>Behavior is composed, not inherited.</p><h5>Final Mental Model (Keep This)</h5><p><strong>Java / C#:</strong></p><blockquote><p>Interfaces describe what a service <em>is</em></p></blockquote><p><strong>Go:</strong></p><blockquote><p>Interfaces describe what my code <em>needs</em></p></blockquote><p>This one shift:</p><ul><li><p>shrinks abstractions</p></li><li><p>hardens boundaries</p></li><li><p>improves service autonomy</p></li><li><p>simplifies testing</p></li></ul><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/common-anti-patterns-when-java-devs/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/common-anti-patterns-when-java-devs/comments"><span>Leave a comment</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item><item><title><![CDATA[Go Broke OOP on Purpose: Why Interfaces Belong to Consumers, Not Producers]]></title><description><![CDATA[Go&#8217;s Interfaces Feel Wrong to Java Devs&#8230; Until They Don&#8217;t]]></description><link>https://www.alagesanpalani.com/p/go-broke-oop-on-purpose-why-interfaces</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/go-broke-oop-on-purpose-why-interfaces</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 17 Jan 2026 13:08:17 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!P7Li!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you come from <strong>Java or C#</strong>, Go&#8217;s interfaces can feel&#8230; backwards.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P7Li!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P7Li!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P7Li!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1860868,&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;:&quot;https://www.alagesanpalani.com/i/184861807?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P7Li!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!P7Li!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42685d90-0312-4db9-af3c-9871700cb715_1536x1024.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>No <code>implements</code>.<br>No explicit contracts.<br>No giant interface files shared across teams.</p><p>Yet somehow, Go code ends up <strong>more decoupled, more testable, and easier to evolve</strong>.</p><p>This is not accidental.</p><p>Go follows a philosophy called <strong>consumer-defined interfaces</strong> &#8212; and once you understand it, you&#8217;ll never design APIs the same way again.</p><div><hr></div><h5>What &#8220;Consumer-Defined Interface&#8221; Means</h5><blockquote><p>In Go, the code that <em>uses</em> a dependency defines the interface &#8212; not the code that <em>provides</em> it.</p></blockquote><p>This is the opposite of classical OOP.</p><h5>Traditional OOP (Java / C#)</h5><ul><li><p>Producer defines the interface</p></li><li><p>Consumer must conform to it</p></li><li><p>Interface is a <em>contract imposed from above</em></p></li></ul><h5>Go</h5><ul><li><p>Consumer defines <strong>exactly what it needs</strong></p></li><li><p>Producer doesn&#8217;t even know the interface exists</p></li><li><p>Interface is a <em>local expectation</em>, not a global contract</p></li></ul><div><hr></div><h5>The Classic Java / C# Model (Producer-Defined Contract)</h5><p>Let&#8217;s start with what most of us learned first.</p><h6>Java Example</h6><pre><code>// Producer defines the contract
public interface PaymentService {
    void charge(String userId, double amount);
    void refund(String transactionId);
    PaymentStatus getStatus(String transactionId);
}</code></pre><p>Now every consumer must depend on <strong>everything</strong>, even if they only need one method.</p><pre><code>public class OrderService {
    private final PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void placeOrder() {
        paymentService.charge("u123", 100.0);
    }
}
</code></pre><h5>Problems</h5><ol><li><p><strong>Fat interfaces</strong></p></li><li><p><strong>Ripple effects</strong> when methods change</p></li><li><p><strong>Mocking complexity</strong></p></li><li><p>Interfaces become <em>organizational artifacts</em>, not design tools</p></li></ol><div><hr></div><h5>Go&#8217;s Approach: Start With the Consumer</h5><p>Now let&#8217;s do the same thing in Go &#8212; properly.</p><h5>Consumer-Defined Interface in Go (The Right Way)</h5><p>Step 1: Write the consumer first</p><pre><code>type PaymentProcessor interface {
    Charge(userID string, amount float64) error
}</code></pre><p>That&#8217;s it.</p><ul><li><p>No refund</p></li><li><p>No status</p></li><li><p>Only what this consumer actually needs</p></li></ul><p>Step 2: Use the interface</p><pre><code>type OrderService struct {
    payments PaymentProcessor
}

func (s *OrderService) PlaceOrder(userID string, amount float64) error {
    return s.payments.Charge(userID, amount)
}
</code></pre><p>Step 3: Any type that matches <em>implicitly</em> works</p><pre><code>type StripeClient struct{}

func (s *StripeClient) Charge(userID string, amount float64) error {
&#9;// call Stripe API
     return nil
}
</code></pre><p>No <code>implements</code>.<br>No registration.<br>No inheritance.</p><p>It just works.</p><div><hr></div><h5>The Key Rule (Memorize This)</h5><blockquote><p>If a type has the methods, it satisfies the interface.</p></blockquote><p>This is called <strong>structural typing</strong>.</p><p>Java/C# use <strong>nominal typing</strong><br>Go uses <strong>structural typing</strong></p><h5>Why This Is So Powerful</h5><p><strong>Interfaces Stay Small (Automatically)</strong></p><p>In Go, interfaces tend to look like this:</p><pre><code>type Reader interface {
     Read(p []byte) (n int, err error)
}</code></pre><p>Not this:</p><pre><code>interface Reade
r {
    int read(byte[] b);
    int read(byte[] b, int off, int len);
    void close();
    boolean ready();
}</code></pre><p>Because consumers define them, interfaces naturally become:</p><ul><li><p>minimal</p></li><li><p>focused</p></li><li><p>easy to satisfy</p></li></ul><div><hr></div><p><strong>No Up-Front Design Tax</strong></p><p>In Java/C# you often hear:</p><blockquote><p>&#8220;Let&#8217;s define interfaces first&#8221;</p></blockquote><p>In Go, you hear:</p><blockquote><p>&#8220;Let&#8217;s write real code first &#8212; interfaces will appear naturally&#8221;</p></blockquote><p>You don&#8217;t design abstractions <em>ahead of need</em>.</p><div><hr></div><p><strong>Zero Coupling Between Packages</strong></p><p>The producer does <strong>not</strong> import the consumer.</p><pre><code>consumer &#8594; defines interface
producer &#8594; unaware it implements anything</code></pre><p>This avoids:</p><ul><li><p>dependency cycles</p></li><li><p>shared &#8220;contracts&#8221; packages</p></li><li><p>versioning nightmares</p></li></ul><div><hr></div><p><strong>Testing Becomes Trivial</strong></p><p>Go Test Example</p><pre><code>type FakePayment struct{}

func (f *FakePayment) Charge(userID string, amount float64) error {
&#9;return nil
}</code></pre><p>Use it instantly:</p><pre><code>func TestPlaceOrder(t *testing.T) {
&#9;service := OrderService{
&#9;&#9;payments: &amp;FakePayment{},
&#9;}

&#9;err := service.PlaceOrder("u1", 50)
&#9;if err != nil {
&#9;&#9;t.Fatal(err)
&#9;}
}</code></pre><p>No mocking frameworks.<br>No annotations.<br>No magic.</p><p>When NOT to Use Interfaces in Go</p><p>Yes &#8212; interfaces can be overused.</p><p>Avoid interfaces when:</p><ul><li><p>there is only one implementation</p></li><li><p>no testing or substitution is needed</p></li><li><p>the abstraction adds no clarity</p></li></ul><p>Go proverb:</p><blockquote><p>&#8220;Accept interfaces, return structs.&#8221;</p></blockquote><div><hr></div><p>Real-World Go Interfaces You Already Use</p><p>You&#8217;ve seen this pattern everywhere:</p><pre><code>io.Reader
io.Writer
http.Handler
context.Context</code></pre><p>None of these were designed as giant frameworks.</p><p>They are:</p><ul><li><p>tiny</p></li><li><p>composable</p></li><li><p>consumer-driven</p></li></ul><h2>Bottomline - take away</h2><p>Go&#8217;s <strong>consumer-defined interface principle</strong> is not just syntax &#8212; it&#8217;s a philosophy:</p><ul><li><p>Design from usage, not speculation</p></li><li><p>Let behavior define contracts</p></li><li><p>Keep abstractions small and local</p></li><li><p>Decouple without ceremony</p></li></ul><p>Once this clicks, Go stops feeling &#8220;minimal&#8221;<br>and starts feeling <strong>precise</strong>.</p><h3><strong>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/go-broke-oop-on-purpose-why-interfaces/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/go-broke-oop-on-purpose-why-interfaces/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Kubernetes Ingress and Ingress controller: Complete Guide with microservice]]></title><description><![CDATA[Exposing Golang microservice]]></description><link>https://www.alagesanpalani.com/p/kubernetes-ingress-and-ingress-controller</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/kubernetes-ingress-and-ingress-controller</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sun, 07 Sep 2025 07:24:11 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!4AqO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.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_!4AqO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4AqO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 424w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 848w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 1272w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4AqO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png" width="1456" height="760" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:760,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94681,&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;:&quot;https://www.alagesanpalani.com/i/172959073?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4AqO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 424w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 848w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.png 1272w, https://substackcdn.com/image/fetch/$s_!4AqO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47a3eb91-a1f2-478b-a330-13f88c0b6fa0_1606x838.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><h2><strong>What is Ingress?</strong></h2><ul><li><p><strong>Ingress</strong> is an API object in Kubernetes that manages external access to services inside your cluster.</p></li><li><p>Normally, Kubernetes services (<code>ClusterIP</code>, <code>NodePort</code>, <code>LoadBalancer</code>) expose pods, but they are limited:</p><ul><li><p><code>ClusterIP</code> &#8594; accessible only inside the cluster.</p></li><li><p><code>NodePort</code> &#8594; exposes service on each node&#8217;s IP with a fixed port (not very flexible).</p></li><li><p><code>LoadBalancer</code> &#8594; creates a cloud provider load balancer per service (can be costly).</p></li></ul></li></ul><p>Instead, <strong>Ingress provides a smarter way</strong>:</p><ul><li><p>It acts like a <strong>reverse proxy</strong> and defines <strong>rules</strong> (based on hostnames or paths) for routing external traffic to internal services.</p></li><li><p>Example:</p><ul><li><p><code>myapp.com/api</code> &#8594; forwards to <code>api-service</code></p></li><li><p><code>myapp.com/web</code> &#8594; forwards to <code>frontend-service</code></p></li></ul></li></ul><div><hr></div><h3><strong>What is an Ingress Controller?</strong></h3><ul><li><p>The <strong>Ingress object</strong> itself is just a set of rules (YAML config).</p></li><li><p>To actually enforce those rules, you need an <strong>Ingress Controller</strong>, which is a piece of software running in the cluster.</p></li><li><p>It watches Ingress resources and configures a reverse proxy (like <strong>NGINX</strong>, <strong>HAProxy</strong>, <strong>Traefik</strong>, etc.) to handle incoming requests accordingly.</p></li></ul><p>&#128073;  Without an <strong>Ingress Controller</strong>, the Ingress object does nothing.</p><div><hr></div><h3><strong>Why do we need Ingress + Ingress Controller?</strong></h3><ul><li><p>To <strong>expose multiple services</strong> under the same IP/domain using rules.</p></li><li><p>To <strong>avoid creating a separate LoadBalancer per service</strong> (cost &amp; complexity).</p></li><li><p>To handle <strong>TLS/SSL termination</strong> in one place.</p></li><li><p>To support advanced features:</p><ul><li><p>Path-based routing (<code>/api</code> &#8594; one service, <code>/app</code> &#8594; another)</p></li><li><p>Host-based routing (<code>foo.example.com</code>, <code>bar.example.com</code>)</p></li><li><p>Load balancing and sticky sessions</p></li><li><p>Rate limiting, authentication, etc. (depending on controller).</p></li></ul></li></ul><h2><strong>Installing an Ingress Controller</strong></h2><p>Lets install first a famous ingress controller, nginx ingress controller - using public helm charts. As installed below, we need to install the ingress controller with the nodeport service type so that we can run in local cluster.</p><pre><code><strong>helm repo add nginx-stable https://helm.nginx.com/stable</strong>
"nginx-stable" has been added to your repositories
controlplane:~$ <strong>helm repo update</strong>
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "nginx-stable" chart repository
Update Complete. &#9096;Happy Helming!&#9096;
controlplane:~$ 

# Install NGINX Ingress Controller
<strong>helm install nginx-ingress nginx-stable/nginx-ingress \
  --create-namespace \
  --namespace nginx-ingress \
  --set controller.service.type=NodePort \
  --set controller.service.httpPort.nodePort=30080 \
  --set controller.service.httpsPort.nodePort=30443</strong>
NAME: nginx-ingress
LAST DEPLOYED: Sat Sep  6 15:54:24 2025
NAMESPACE: nginx-ingress
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
NGINX Ingress Controller 5.1.1 has been installed.

# Verify installation

<strong>kubectl get pods -n nginx-ingress</strong>
NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-654b4578d7-tlhjz   1/1     Running   0          85s
<strong>kubectl get svc -n nginx-ingress</strong>
NAME                       TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
nginx-ingress-controller   NodePort   10.102.185.119   &lt;none&gt;        80:30080/TCP,443:30443/TCP   2m15s
controlplane:~$ 
</code></pre><p>In addition to the above resources, the helm chart also installs required ClusterRole, ClusterRolebinding and most importantly an ingress class name called &#8220;nginx&#8221;. This ingress class name is important while setting up ingress resource rules as mentioned below.</p><h2><strong>Creating an Microservice Deployment</strong></h2><p>This is the same deployment built and deployed in previous blog: <a href="https://www.alagesanpalani.com/p/containerize-golang-microservice">Containerized microservice</a> . Refer this on how its built.</p><pre><code># dep.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-helloworld
  labels:
    app: go-helloworld
spec:
  replicas: 2
  selector:
    matchLabels:
      app: go-helloworld
  template:
    metadata:
      labels:
        app: go-helloworld
    spec:
      containers:
      - name: go-helloworld
        image: alagesann/go-helloworld:1.0.0
        ports:
        - containerPort: 80
</code></pre><p>Verify</p><p>&#10140;  ~ kubectl apply -f dep.yaml</p><p>deployment.apps/go-helloworld created</p><p>&#10140;  ~ kubectl get deploy</p><p>NAME            READY   UP-TO-DATE   AVAILABLE   AGE</p><p>go-helloworld   2/2     2            2           26s</p><h2><strong>Create ClusterIP Service, svc.yaml</strong></h2><p>Lets create a service of type clusterip to expose the above deployment. Also ingress controller will redirect external service call to internal clusterip service and then to deployments.</p><pre><code>apiVersion: v1
kind: Service
metadata:
  name: go-helloworld-service
  labels:
    app: go-helloworld
spec:
  selector:
    app: go-helloworld
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  type: ClusterIP</code></pre><p>kubectl apply -f svc.yaml</p><p>&#10140;  ~ k get deploy,svc</p><p>NAME                            READY   UP-TO-DATE   AVAILABLE   AGE</p><p>deployment.apps/go-helloworld   2/2     2            2           5m35s</p><p>NAME                            TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE</p><p>service/go-helloworld-service   ClusterIP   10.43.85.59   &lt;none&gt;        80/TCP    4d</p><h2><strong>Test Service is reachable inside the cluster</strong></h2><p>&#10140;  ~ kubectl run -it --image=curlimages/curl curly -- /bin/sh</p><p>If you don't see a command prompt, try pressing enter.</p><p>~ $ curl http://go-helloworld-service/</p><p>Hello, World!</p><p>Welcome to our Go microservice!</p><h2><strong>Exposing the Service via Ingress</strong></h2><p>Create below ingress resource to expose clusterip service to external access. IngressClassName is important to autoconfigure this ingress resource with the nginx ingress controller installed in the earlier step.  This ingress resource now will expose the service via a common domain called &#8220;example.com&#8221;. </p><pre><code># go-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: "example.com"
    http:
      paths:
      - pathType: Prefix
        path: "/go"
        backend:
          service:
            name: go-helloworld-service
            port:
              number: 80</code></pre><p>kubectl apply -f go-ingress.yaml</p><p>ingress.networking.k8s.io/go-ingress created</p><p>&#10140;   kubectl get ingress</p><p>NAME         CLASS   HOSTS         ADDRESS        PORTS   AGE</p><p>go-ingress   nginx   example.com   192.168.5.15   80      13h</p><p>Now for the DNS resolution properly work on the domain name &#8220;example.com&#8221;, create following entry in the /etc/hosts file with the nginx-ingress service&#8217;s internal_ip</p><pre><code># /etc/hosts

10.102.185.119 example.com</code></pre><p><code>now hit the service :</code></p><p>curl http://example.com/go</p><p>Hello, World!</p><p>Welcome to our Go microservice!</p><p>Congratulations, you have exposed a Go microservice on an ingress controller successfully.</p>]]></content:encoded></item><item><title><![CDATA[Containerize Golang microservice with Kubernetes]]></title><description><![CDATA[Golang microservice on kubernetes]]></description><link>https://www.alagesanpalani.com/p/containerize-golang-microservice</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/containerize-golang-microservice</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Tue, 02 Sep 2025 16:52:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!cyZF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Lets containerize a sample golang microservice, create a docker image, upload to dockerhub, deploy the microservice in kubernetes cluster.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cyZF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cyZF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 424w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 848w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 1272w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cyZF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png" width="1079" height="655" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:655,&quot;width&quot;:1079,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;Deploying a Golang Microservice to Kubernetes&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="Deploying a Golang Microservice to Kubernetes" title="Deploying a Golang Microservice to Kubernetes" srcset="https://substackcdn.com/image/fetch/$s_!cyZF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 424w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 848w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.png 1272w, https://substackcdn.com/image/fetch/$s_!cyZF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7d0b54a-848e-45cb-9fa3-bb34492b9884_1079x655.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>Lets create a simple golang microservice, no fancy, just a basic one, when hit with curl, simply returns string &#8220;hello world&#8221; and a health endpoint. cool?</p><pre><code>mkdir go-helloworld
cd go-helloworld</code></pre><p>No explanation needed for the above two lines, is n&#8217;t it?</p><p>Inside the above directory, create a main.go file just to host our simple hello world application as given below:</p><h2><strong>Create a golang microservice</strong></h2><pre><code>package main

import (
&#9;"fmt"
&#9;"log"
&#9;"net/http"
&#9;"os"
)

func main() {
&#9;http.HandleFunc("/", helloHandler)
&#9;http.HandleFunc("/health", healthHandler)
&#9;
&#9;port := os.Getenv("PORT")
&#9;if port == "" {
&#9;&#9;port = "8080"
&#9;}
&#9;
&#9;log.Printf("Server starting on port %s", port)
&#9;log.Fatal(http.ListenAndServe(":"+port, nil))
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
&#9;name := r.URL.Query().Get("name")
&#9;if name == "" {
&#9;&#9;name = "World"
&#9;}
&#9;
&#9;fmt.Fprintf(w, "Hello, %s!\n", name)
&#9;fmt.Fprintf(w, "Welcome to our Go microservice!\n")
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
&#9;w.Header().Set("Content-Type", "application/json")
&#9;fmt.Fprint(w, `{"status": "healthy", "service": "helloworld"}`)
}</code></pre><p>The above code does n&#8217;t use any fancy library, just native http library is used to expose a REST endpoint:</p><p>GET / &#8212; returns hello world</p><p>GET /health - returns health of the service</p><p>Create a go.mod file as below:</p><pre><code>module github.com/alagesann/go-helloworld

go 1.23.3

require ()</code></pre><h2><strong>Create Dockerfile</strong></h2><pre><code># Build stage
FROM golang:1.23.3-alpine AS builder

WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o helloworld .

# Final stage
FROM alpine:3.18
WORKDIR /app
COPY --from=builder /app/helloworld .

EXPOSE 8080
USER nobody:nobody

CMD ["./helloworld"]</code></pre><p>Build and test the service locally:</p><pre><code># Build the Go application
go build -o helloworld .

# Test locally
./helloworld 
curl http://localhost:8080/
curl http://localhost:8080/?name=Raj
curl http://localhost:8080/health
</code></pre><p>Observe app works fine and returns result from the microservice.</p><h2><strong>Build and Push Docker Image</strong></h2><pre><code># Build the Docker image -- here instead of my name, use your docker hub username.

docker build -t alagesann/go-helloworld:1.0.0 .

# Test the Docker image
docker run -d -p 8080:8080 --name helloworld alagesann/go-helloworld:1.0.0

curl http://localhost:8080/
docker stop helloworld
docker rm helloworld

# Login to Docker Hub
docker login

# Push to Docker Hub
docker push alagesann/go-helloworld:1.0.0</code></pre><h2><strong>Create Kubernetes Deployment</strong></h2><p>Create a kubernetes deployment yaml file to deploy the microservice on the local kubernetes cluster. dep.yaml</p><pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-helloworld
  labels:
    app: go-helloworld
spec:
  replicas: 2
  selector:
    matchLabels:
      app: go-helloworld
  template:
    metadata:
      labels:
        app: go-helloworld
    spec:
      containers:
      - name: go-helloworld
        image: alagesann/go-helloworld:1.0.0
        ports:
        - containerPort: 8080 </code></pre><h2><strong>Create ClusterIP Service, svc.yaml</strong></h2><pre><code>apiVersion: v1
kind: Service
metadata:
  name: go-helloworld-service
  labels:
    app: go-helloworld
spec:
  selector:
    app: go-helloworld
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  type: ClusterIP</code></pre><h2><strong>Deploy to Kubernetes</strong></h2><pre><code># Apply the deployment and service
kubectl apply -f dep.yaml
kubectl apply -f svc.yaml

# Verify deployment
kubectl get deployments
kubectl get pods
kubectl get svc

# Check pod logs
kubectl logs -l app=go-helloworld --tail=10</code></pre><h2><strong>Test the microservice</strong></h2><pre><code>&#10140;  go-helloworld k get svc

NAME                    TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE

go-helloworld-service   ClusterIP   10.43.85.59   &lt;none&gt;        80/TCP    47m


Now test the service using a temporary pod.

&#10140;  kubectl run -it --image=curlimages/curl curly -- /bin/sh

~ $ curl http://go-helloworld-service/

Hello, World!

Welcome to our Go microservice!

~ $ curl http://go-helloworld-service?name=Raj

Hello, Raj!

Welcome to our Go microservice!

~ $</code></pre><p>Congratulations you have done the following:</p><ol><li><p>&#9989; Created a simple Go microservice with HTTP endpoints</p></li><li><p>&#9989; Containerized it using a multi-stage Docker build</p></li><li><p>&#9989; Published the image to Docker Hub</p></li><li><p>&#9989; Deployed it to Kubernetes using a Deployment</p></li><li><p>&#9989; Exposed it internally using a ClusterIP service</p></li><li><p>&#9989; Verified the service is working correctly</p></li></ol>]]></content:encoded></item><item><title><![CDATA[Microservice Architecture]]></title><description><![CDATA[Independent, smaller services, enabling agility, scalability, and easier maintenance]]></description><link>https://www.alagesanpalani.com/p/microservice-architecture</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/microservice-architecture</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sun, 19 Jan 2025 02:25:21 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!z88_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h4>Why Microservices?</h4><p>As businesses grow, their applications need to handle increasing complexity, traffic, and evolving user needs. Traditional monolithic architectures, where all components are tightly coupled, become bottlenecks due to poor scalability, deployment delays, and difficulty in maintaining large codebases. Microservice architecture solves these issues by breaking applications into independent, smaller services, enabling agility, scalability, and easier maintenance.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!z88_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!z88_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 424w, https://substackcdn.com/image/fetch/$s_!z88_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 848w, https://substackcdn.com/image/fetch/$s_!z88_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 1272w, https://substackcdn.com/image/fetch/$s_!z88_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!z88_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif" width="1392" height="1601" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1601,&quot;width&quot;:1392,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:434793,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&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_!z88_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 424w, https://substackcdn.com/image/fetch/$s_!z88_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 848w, https://substackcdn.com/image/fetch/$s_!z88_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 1272w, https://substackcdn.com/image/fetch/$s_!z88_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d16732-a689-4737-a709-65f01227ce37_1392x1601.gif 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><h4>What is Microservice?</h4><p>Microservices are an architectural style where applications are composed of small, autonomous services, each responsible for a specific business function. These services communicate over APIs, making them independently deployable and scalable.</p><h4>Key Components and Patterns</h4><p>&#128073; <strong>Load Balancer </strong>: Distributes incoming traffic across multiple instances of a service to ensure high availability and reliability.</p><p>Example: AWS Elastic Load Balancer (ELB), Nginx.</p><p>&#128073; <strong>API Gateway</strong> : Acts as a single entry point for clients, handling requests, rate limiting, authentication, routing, and protocol translation.</p><p>Example: Kong, AWS API Gateway.</p><p>&#128073; <strong>Service Discovery</strong>: Helps services locate each other dynamically, especially in containerized environments where IPs and instances change frequently.</p><p>Example: Consul, Eureka.</p><p>&#128073; <strong>Service Coordination</strong> : Coordinates distributed systems by managing configurations, leader elections, and metadata.</p><p>Example: Zookeeper for managing distributed locks or configurations.</p><p>&#128073; <strong>Sidecar Pattern</strong> : Attaches auxiliary tasks (like monitoring, logging, or communication) to a primary service as a separate container or process.</p><p>Example: Envoy for network proxy, logging, and telemetry.</p><p>&#128073; <strong>Database per Service</strong> : Each microservice has its own dedicated database to maintain data autonomy and avoid dependency between services.</p><p>&#128073; <strong>Message Queue-Based Communication</strong> : Enables asynchronous communication between services for decoupled and reliable data exchange.</p><p>Example: RabbitMQ, Apache Kafka.</p><p>&#128073; <strong>Log Visualization with ELK Stack</strong>: Centralized logging for troubleshooting, monitoring, and identifying bottlenecks.</p><p>Logstash: Collects, processes, and forwards logs.</p><p>Elasticsearch: Stores and indexes logs for quick search.</p><p>Kibana: Visualizes logs and provides dashboards for insights.</p><p>&#128073; <strong>Metrics Visualization</strong> : Monitors service health, performance, and usage patterns.</p><p>Prometheus: Collects and stores real-time metrics (e.g., CPU usage, request latency).</p><p>Grafana: Visualizes metrics with rich dashboards.</p><p></p><h4>&#128073; <strong>Cloud Deployment Strategy with Kubernetes</strong></h4><p>Autoscaling: Kubernetes adjusts the number of service instances based on traffic or resource usage (Horizontal Pod Autoscaler).</p><p>Load Balancing: Manages traffic distribution among pods using services or ingress.</p><p>Self-Healing: Automatically restarts failed containers.</p><p></p><h4>&#128293; Advantages of Microservice Architecture:</h4><p>Scalability: Scale individual services independently.</p><p>Agility: Faster development and deployment cycles.</p><p>Resilience: Failure in one service doesn't crash the entire application.</p><p>Technology Freedom: Use different tech stacks for different services.</p><p>Team Autonomy: Teams can work independently on separate services.</p><p></p><h4>&#128559; Complexity of Microservice Architecture :</h4><p>Increased Overhead: Managing multiple services, databases, and deployments.</p><p>Communication Challenges: Requires robust inter-service communication mechanisms.</p><p>Monitoring: More effort needed for centralized logging, metrics, and tracing.</p><p>Deployment Complexity: Continuous Integration/Delivery pipelines are essential.</p><p>Data Consistency: Managing distributed data transactions across services is challenging.</p><p><strong>#microservice</strong> <strong>#architecture</strong></p>]]></content:encoded></item><item><title><![CDATA[Coding: The Ultimate Brain Gym ]]></title><description><![CDATA[No Dumbbells Required!]]></description><link>https://www.alagesanpalani.com/p/coding-the-ultimate-brain-gym</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/coding-the-ultimate-brain-gym</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 04 Jan 2025 06:37:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!KO1N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We all know that coding is a must-have skill in today&#8217;s tech-savvy world. But here&#8217;s something you might not have considered: coding is also like a mental CrossFit session for your brain! Yep, that&#8217;s right. Move over Sudoku and crosswords&#8202;&#8212;&#8202;coding is the new &#8220;mental workout&#8221; in town, keeping your brain fresh, sharp, and as agile as a ninja on caffeine.</p><p>Let&#8217;s dive into why cracking those coding problems is not only good for your r&#233;sum&#233; but also keeps your brain from turning into mush as you age.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KO1N!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KO1N!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 424w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 848w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 1272w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KO1N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png" width="1456" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:109775,&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_!KO1N!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 424w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 848w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.png 1272w, https://substackcdn.com/image/fetch/$s_!KO1N!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e481112-de08-4d86-9d9f-06bd19d4394e_1732x952.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><h4>1. Coding: Building Brain Muscles You Didn&#8217;t Know You Had</h4><p>Ever feel like your brain is flexing when you solve a tricky coding problem? Well, that&#8217;s because it literally is. Each time you troubleshoot a bug, you&#8217;re building new neural pathways&#8202;&#8212;&#8202;think of it as adding extra lanes to the brain&#8217;s superhighway. The more you practice, the smoother the traffic flows. No road rage here, just pure cognitive efficiency.</p><p>A study from the University of Passau found that regular coders activate the prefrontal cortex, AKA the brain&#8217;s project manager, making you quicker at problem-solving. So next time you&#8217;re stuck on a tough bug, just remember: your brain&#8217;s getting ripped!</p><h4>2. Fight Off Brain Aging&#8202;&#8212;&#8202;One Line of Code at a Time</h4><p>Sure, everyone&#8217;s concerned about wrinkles and gray hair, but what about your brain getting a little&#8230; rusty? Practicing coding is like Botox for your brain cells. As we age, our cognitive abilities start to decline. But coding is the ultimate anti-aging cream for the mind&#8202;&#8212;&#8202;it keeps those neurons firing on all cylinders.</p><p>The Alzheimer&#8217;s Association even says that challenging your brain can lower the risk of cognitive diseases like Alzheimer&#8217;s by up to 30%. So basically, coding is like the fountain of youth, except instead of water, it&#8217;s made of algorithms and semicolons. How cool is that?</p><h4>3. Neuroplasticity: The Brain&#8217;s DIY Renovation Program</h4><p>Coding is like a home makeover show for your brain. Every time you face a new problem, debug a complex issue, or simply write a neat function, your brain rewires itself. This process is called neuroplasticity (fancy word for your brain saying, &#8220;I&#8217;ve got this!&#8221;). The more you code, the better your brain gets at adapting to new challenges.</p><p>According to the National Institutes of Health (NIH), coding can spark the production of new neurons in the hippocampus&#8202;&#8212;&#8202;the part of your brain responsible for memory. It&#8217;s like your brain is constantly downloading new updates, keeping it young and running smoothly&#8202;&#8212;&#8202;no reboot required.</p><h4>4. Coding Makes You a Memory Master (Goodbye, Lost Keys!)</h4><p>Coding requires holding multiple things in your head at once&#8202;&#8212;&#8202;variables, loops, algorithms, and the endless mystery of why it works on your colleague&#8217;s machine but not yours. But hey, all that mental juggling helps improve both your short-term and long-term memory.</p><p>A study from the University of Michigan shows that mental exercises like coding can boost your memory and focus. So, not only will you remember how to solve that coding problem from two months ago, but maybe you&#8217;ll finally stop losing your car keys. Maybe.</p><h4>5. Debugging Your Way to Mental Resilience</h4><p>Let&#8217;s be honest: coding comes with its fair share of headaches. You&#8217;ll face bugs, errors, and the occasional &#8220;I have no idea why this isn&#8217;t working&#8221; moment. But that&#8217;s the beauty of it. Each time you overcome a challenge, your brain gets tougher, more resilient, and ready to take on the next problem. It&#8217;s like the mental version of toughening up in the gym. Debugging is your brain&#8217;s version of burpees. Annoying? Yes. Beneficial? Absolutely.</p><p>Think of it this way: coding doesn&#8217;t just help you avoid burnout; it helps you turn into the mental Hulk. Bring on the bugs!</p><h4>Fun Facts and Stats to Blow Your Mind (Literally):</h4><ul><li><p>The Alzheimer&#8217;s Society says keeping your brain active can reduce your risk of dementia by up to 30%. So coding is basically a superpower.</p></li><li><p>Stanford University found that people who keep their brains busy with tasks like coding are 40% more likely to retain their cognitive superpowers into old age. That means you&#8217;ll be the sharpest grandparent at the bingo table!</p></li><li><p>NIH studies show that coding can literally grow new neurons. You&#8217;re not just coding; you&#8217;re building a brain empire.</p></li></ul><h3>So, Coding Is Your Brain&#8217;s Personal Trainer</h3><p>So, next time you&#8217;re hunched over your computer, staring at a chunk of code that just won&#8217;t cooperate, remember this: you&#8217;re not just coding, you&#8217;re giving your brain a top-notch workout. Solving coding problems keeps your brain sharp, fresh, and strong, while also helping to prevent age-related brain decline. Who knew debugging could be your ticket to a healthier mind?</p><p>In short, coding isn&#8217;t just good for your career&#8202;&#8212;&#8202;it&#8217;s your brain&#8217;s secret weapon to stay smart, agile, and ready to tackle whatever life (or that next coding challenge) throws at you. Happy coding, and don&#8217;t forget to flex those mental muscles!</p>]]></content:encoded></item><item><title><![CDATA[Five Go Concurrency Patterns, every Go Developer must know - Pipeline]]></title><description><![CDATA[With an example implementation of E-Commerce Order Processing]]></description><link>https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Wed, 01 Jan 2025 11:05:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!y2qx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is a part of a multi-part series on Go Concurrency Patterns:</p><ol><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool">Go Concurrency Patterns - Worker Pool</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan">Go Concurrency Patterns - Fan-In Fan-Out</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation">Go Concurrency Patterns - Context Cancellation</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline">Go Concurrency Patterns - Pipeline</a></p></li></ol><p></p><h4>Go Concurrency Patterns - Pipeline</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!y2qx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!y2qx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 424w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 848w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!y2qx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg" width="1200" height="628" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:628,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:148529,&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_!y2qx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 424w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 848w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!y2qx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc42ccc59-38f7-4f55-80c6-b2a66a73d8d3_1200x628.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></figure></div><p>The <strong>Pipeline</strong> concurrency pattern is a design paradigm where:</p><ol><li><p>Data flows through multiple stages, each performing a specific task.</p></li><li><p>Each stage processes input, transforms it, and passes the result to the next stage.</p></li><li><p>Stages run concurrently, improving throughput.</p></li></ol><h4>Scenario: E-Commerce Order Processing</h4><p>In an <strong>e-commerce platform</strong>, consider the following workflow for processing customer orders:</p><ol><li><p><strong>Order Validation</strong>: Ensure all necessary details are presentk and are valid.</p></li><li><p><strong>Inventory Check</strong>: Verify the stock availability of the ordered products.</p></li><li><p><strong>Payment Processing</strong>: Process the payment securely with a payment gateway</p></li><li><p><strong>Shipment Preparation</strong>: Prepare the items for shipping.</p></li></ol><h4><strong>Problem</strong></h4><ul><li><p>Processing above tasks sequentially can be slow, especially during peak times.</p></li><li><p>High concurrency with multiple independent goroutines can make error handling and task management complex.</p></li></ul><h4><strong>Solution</strong></h4><p>So, we use the <strong>Pipeline</strong> concurrency pattern:</p><ol><li><p>Each stage is responsible for a specific step in the order processing workflow.</p></li><li><p>Data (orders) flows from one stage to the next via channels.</p></li><li><p>Each stage operates concurrently, improving efficiency and scalability.</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_!syfd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!syfd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 424w, https://substackcdn.com/image/fetch/$s_!syfd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 848w, https://substackcdn.com/image/fetch/$s_!syfd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 1272w, https://substackcdn.com/image/fetch/$s_!syfd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!syfd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png" width="1456" height="981" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:981,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:260270,&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_!syfd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 424w, https://substackcdn.com/image/fetch/$s_!syfd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 848w, https://substackcdn.com/image/fetch/$s_!syfd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.png 1272w, https://substackcdn.com/image/fetch/$s_!syfd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ea965d5-abc7-47c6-a80e-06fd00e8451b_2068x1393.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>Image explaining four stages and each stage manages separate input and output channel. There are four stages and five channels</p><blockquote><p>Keypoint:  Each stage subroutine processes an order independently/concurrently, while the rest of the stages work on its own functionality on its own order concurrently. Hence increasing throughput. However, due to sequential and queuing nature of channel ensures each order goes through the stages in the predefined order always, ie. stage1 &#8594;stage2&#8594;stage3&#8594;stage4. for example: when stage1 validates order4, stage3 performs payment for order3  and stage4 process shipping on order1 - all at once concurrently. </p><p>Important: Each stage runs in its own goroutine, allowing multiple orders to be processed simultaneously at different stages</p><p>Channels pass orders between stages, ensuring thread-safe communication.</p></blockquote><p></p><h4>Implementation:</h4><p><strong>Order</strong> : This object is a subset of full Order object as it does n&#8217;t have complete order detail such as order qty and price etc. this just has some order status to represent the status of the current processing stage such as Valid, Stocked, Paid, Shipped.</p><p><strong>validateOrder</strong> : Its the first stage of the pipeline and performs:  a). Takes in an input channel to read Order object from main function  b). Spinup a new goroutine to run validation in a separate goroutine. c). Performs validation and marks order status as order.Valid=true or false d). publishes validated Order to an output channel (which will be sent to the next stage in the process).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oL0f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oL0f!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 424w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 848w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 1272w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oL0f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png" width="1456" height="940" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:940,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:243179,&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_!oL0f!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 424w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 848w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.png 1272w, https://substackcdn.com/image/fetch/$s_!oL0f!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20195857-3e87-4230-8b01-e4725fd259cf_2090x1350.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>checkInventory</strong> : Its the second stage of the pipeline and performs:  a). Takes in an input channel (passed from previous stage) to read Order object from previous stage  b). Spinup a new goroutine to run inventory checking in a separate goroutine. c). Performs stock inventory availability check and marks order status as order.Stocked=true or false d). publishes inventory checked Order to an output channel (which will be sent to the next stage in the process).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0Dwm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0Dwm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 424w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 848w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 1272w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0Dwm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png" width="1456" height="1045" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1045,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:350270,&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_!0Dwm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 424w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 848w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.png 1272w, https://substackcdn.com/image/fetch/$s_!0Dwm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff56ce5f7-0294-42e4-9cd2-8ad1e7d35ded_2154x1546.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>processPayment</strong> : Its the third stage of the pipeline and performs:  a). Takes in an input channel (passed from previous stage) to read Order object from previous stage  b). Spinup a new goroutine to process payment in a separate goroutine. c). Process payment perhaps via a separate payment gateway and marks order status as order.Paid=true or false d). publishes Payment done Order to an output channel (which will be sent to the next stage in the process).</p><p><strong>prepareShipment</strong> : Its the fourth and final stage of the pipeline and performs:  a). Takes in an input channel (passed from previous stage) to read Order object from previous stage  b). Spinup a new goroutine to prepare for shipment in a separate goroutine. c). performs shipment and marks order status as order.Shipped=true or false d). publishes shipped Order to an output channel (which will be sent to the main function again to display the result).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eFGd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eFGd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 424w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 848w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 1272w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eFGd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png" width="1456" height="571" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:571,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:177523,&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_!eFGd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 424w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 848w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.png 1272w, https://substackcdn.com/image/fetch/$s_!eFGd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ea1651-a2a7-4fa7-bb9f-c773011721d5_2188x858.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>main: main function mostly works as the orchestrator of the order processing. For now, it just dynamically generates five order objects and publishes on the `orders` channel which will be passed on to the first stage of the pipeline. Its handled in a separate goroutine so that it will happen on a separate goroutine without blocking main function flow.</p><p>In the next step it composes order processing pipeline stages.</p><blockquote><p>Order pipeline is completely loosely coupled, should there be a need for another stage, it can be easily built and added at the pipeline without impacting any other stages or processing.</p></blockquote><p>Each stage takes in a input channel from previous stage and creates an output channel and returns it only to pass on to next stage. That way whole pipeline is built.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5OIV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5OIV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 424w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 848w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 1272w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5OIV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png" width="1456" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:319541,&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_!5OIV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 424w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 848w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.png 1272w, https://substackcdn.com/image/fetch/$s_!5OIV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1e484b5d-4a83-4597-9f54-794c08e3672a_2390x1314.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>Processes the result from the channel from final stage and displays the final order status to the user.</p><blockquote><p>We use unbuffered channel throught the solution so that writing to and reading from the channel is not blocked improving more throughput while maintaining the order of `order` processing.</p></blockquote><h4>Sample output:</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LP2S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LP2S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 424w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 848w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 1272w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LP2S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png" width="1456" height="759" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:759,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:237892,&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_!LP2S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 424w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 848w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.png 1272w, https://substackcdn.com/image/fetch/$s_!LP2S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb85ccb-fc04-4a63-9bc2-21d358761722_1942x1012.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>If you enjoyed this deep dive&#8230;</strong></h3><p>I write weekly about:</p><ul><li><p>Go performance and runtime behavior</p></li><li><p>Kubernetes-native service design</p></li><li><p>Expert-level engineering lessons from real systems</p></li></ul><p><strong>Subscribe if you want more posts like this.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline/comments"><span>Leave a comment</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Five Go Concurrency Patterns, every Go Developer must know - Context Cancellation]]></title><description><![CDATA[with a hotel room booking simulation]]></description><link>https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Mon, 30 Dec 2024 07:49:17 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!FdA2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is a part of a multi-part series on Go Concurrency Patterns:</p><ol><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool">Go Concurrency Patterns - Worker Pool</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan">Go Concurrency Patterns - Fan-In Fan-Out</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation">Go Concurrency Patterns - Context Cancellation</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline">Go Concurrency Patterns - Pipeline</a></p></li></ol><p></p><h4>Go Concurrency Pattern - Context Cancellation</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FdA2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FdA2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 424w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 848w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 1272w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FdA2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp" width="800" height="645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:645,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24640,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&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_!FdA2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 424w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 848w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 1272w, https://substackcdn.com/image/fetch/$s_!FdA2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F827adfb1-55c4-4742-9ec6-39c261fdfe8f_800x645.webp 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>The <strong>Context Cancellation</strong> pattern in Go is a way to manage the lifecycle of goroutines, enabling:</p><ol><li><p><strong>Propagation of cancellation signals</strong> across multiple goroutines.</p></li><li><p><strong>Timeouts and deadlines</strong> to control the execution duration of tasks.</p></li><li><p><strong>Resource cleanup</strong> when operations are no longer needed.</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_!Ws77!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ws77!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 424w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 848w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 1272w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ws77!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png" width="1456" height="996" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:996,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:143193,&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_!Ws77!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 424w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 848w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.png 1272w, https://substackcdn.com/image/fetch/$s_!Ws77!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0faddd28-8ada-4278-8854-67f7ed84802d_1543x1056.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></figure></div></li></ol><p></p><h4>Scenario :  Hotel room booking with timeout cancellation</h4><h4>In a <strong>hotel booking system</strong>, a customer initiates a booking process:</h4><ol><li><p>The system reserves a room.</p></li><li><p>The payment gateway processes the transaction.</p></li><li><p>The confirmation is sent to the user.</p></li></ol><h4><strong>Problem</strong></h4><ul><li><p>If <strong>payment processing exceeds a time limit</strong> (e.g., due to gateway issues), the room reservation should be canceled to avoid blocking inventory.</p></li><li><p><strong>We will </strong>Use the <strong>Context Cancellation</strong> pattern to ensure:</p><ul><li><p>Timeout for payment processing.</p></li><li><p>Propagation of cancellation to the room reservation goroutine if payment fails or exceeds the deadline.</p></li></ul></li></ul><h4>Solution:</h4><ol><li><p>Setup business process functions passing in a handle to the context reference with the required timeout settings. </p></li><li><p>Perform business process regularly</p></li><li><p>When the whole process takes more than the required timelimit, then context timeout automatically happens, cancelling all the outstanding processes</p></li><li><p>In the normal situation, when all the processes completes within timelimit, then we explicitly call context cancellation to cleanup all the resources </p></li></ol><p><em><strong>reserveRoom</strong></em>: reserveRoom function simulates serverving a room using Select..Case. Select..Case is a blocking wait till anyone of the case is activated.  When a case is activated, it executes logic under that case and return the function, after marking wb.Done(). First case simulates reserving a room and second case monitor for context cancellation signal. Reserve room returns success message 80% of the time through rand function. For 20% of time, it returns error again simulating an error in room reservation. Second case writes error when the context cancellation signal is received.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yhmv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yhmv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 424w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 848w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 1272w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yhmv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png" width="1456" height="1004" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1004,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:395774,&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_!yhmv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 424w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 848w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.png 1272w, https://substackcdn.com/image/fetch/$s_!yhmv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F501d0be3-ebd5-43b1-9bac-7c68fae22132_2324x1602.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><em><strong>processPayment</strong></em>: Similar to above reserveRoom, processPayment simulates payment processing using Select..Case. First case, simulates payment processing and second case monitor for context cancellation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5l1i!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5l1i!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 424w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 848w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 1272w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5l1i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png" width="1456" height="528" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9759b028-bcef-4ba2-b993-73465066723e_2296x832.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:528,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:200121,&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_!5l1i!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 424w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 848w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.png 1272w, https://substackcdn.com/image/fetch/$s_!5l1i!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9759b028-bcef-4ba2-b993-73465066723e_2296x832.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><em><strong>sendConfirmation</strong></em>: Similar to above reserveRoom/processPayment, sendConfirmation simulates sending room reservation status (confirmation/error) using Select..Case. First case, simulates sending confirmation and second case monitor for context cancellation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sM2L!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sM2L!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 424w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 848w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 1272w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sM2L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png" width="1456" height="888" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:888,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:247615,&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_!sM2L!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 424w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 848w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.png 1272w, https://substackcdn.com/image/fetch/$s_!sM2L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8c55fee-3121-4769-ae9e-924dfa54af4f_2172x1324.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><em><strong>bookHotel</strong></em> : bookHotel is the main orchestrator of room reservation process steps. It make use of Go&#8217;s waitGroup, channel and goroutines. Channel is used for tracking errors from all the goroutines. We spin up all the goroutines concurrently and wait for all them to close in the separate goroutine. Meanwhile we collect error messages from error channel as and when they occur. When the error channel is closed, for range terminates and all the errors collected if any are returned to main function.</p><p>Pls note, all the goroutines are triggered parallelly which means they can run from any order which can produce misleading behavior as give below in the output result.  Say Sending cofirmation can happen even before payment is done or room is reserved.  This issue can be addressed using Saga pattern or sequencing above steps.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!i501!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!i501!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 424w, https://substackcdn.com/image/fetch/$s_!i501!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 848w, https://substackcdn.com/image/fetch/$s_!i501!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 1272w, https://substackcdn.com/image/fetch/$s_!i501!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!i501!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png" width="1456" height="443" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:443,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:152488,&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_!i501!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 424w, https://substackcdn.com/image/fetch/$s_!i501!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 848w, https://substackcdn.com/image/fetch/$s_!i501!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.png 1272w, https://substackcdn.com/image/fetch/$s_!i501!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3709c51f-d2c0-4884-b1d0-8768e1aa7233_2288x696.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><em><strong>main</strong> </em>: main function starts with creating a context with a timeout of 10 seconds. This is the key point in this pattern. meaning the whole process must completes within 10 seconds otherwise for any reason, context will be auto cancelled which might cancel all the outstanding running goroutine automatically.  Also note that even after a successful completion, we still call the cancel function (due to defer cancel()) as a best practice which will ensure cleaning up of all the active outstanding resources if any</p><h4>Sample Output:</h4><p>Below is the result of multiple running of the program with error and successful scenario.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IAAT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IAAT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 424w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 848w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 1272w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IAAT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png" width="1456" height="682" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:682,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:177202,&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_!IAAT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 424w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 848w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.png 1272w, https://substackcdn.com/image/fetch/$s_!IAAT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5867b85f-d981-49f2-b576-ca9b62013d31_1746x818.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><h4>Conclusion:</h4><p>As a best practice</p><ul><li><p><strong>Set Reasonable Timeouts</strong>: Choose timeout values that balance user experience and system performance.</p></li><li><p><strong>Always Use </strong><code>defer cancel()</code>: Ensure that resources tied to the context are released properly.</p></li><li><p><strong>Pass Context Downstream</strong>: Use the same context across all functions involved in an operation to propagate cancellation effectively.</p></li><li><p><strong>Check </strong><code>ctx.Done()</code>:  Use <code>ctx.Done()</code> to identify whether a timeout or explicit cancellation occurred.</p></li><li><p><strong>Avoid Leaking Goroutines</strong>: Use context cancellation to terminate long-running or unused goroutines.</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Five Go Concurrency Patterns, every Go Developer must know - Fan-Out, Fan-In]]></title><description><![CDATA[Multiple goroutines for high throughput and collect result.]]></description><link>https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 28 Dec 2024 08:36:20 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!oViK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is a part of a multi-part series on Go Concurrency Patterns:</p><ol><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool">Go Concurrency Patterns - Worker Pool</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan">Go Concurrency Patterns - Fan-In Fan-Out</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation">Go Concurrency Patterns - Context Cancellation</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline">Go Concurrency Patterns - Pipeline</a></p><p></p></li></ol><h4>Go Concurrency Patterns - Fan-Out, Fan-In</h4><p>The <strong>Fan-Out, Fan-In</strong> pattern in Go is a concurrency model where:</p><ol><li><p><strong>Fan-Out</strong>: A single producer (goroutine) delegates work to multiple worker goroutines that process tasks concurrently. In other words, Fan-out is the practice of starting multiple goroutines to process data coming from a single channel. It involves distributing the work across multiple workers to improve parallelism and throughput.</p></li><li><p><strong>Fan-In</strong>: Results from multiple workers are aggregated into a single output channel for further processing. In other words, Combine outputs from multiple sources (channels) into one. The combined output can be an aggregation of results, or it can simply be a merged stream.</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_!oViK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oViK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 424w, https://substackcdn.com/image/fetch/$s_!oViK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 848w, https://substackcdn.com/image/fetch/$s_!oViK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 1272w, https://substackcdn.com/image/fetch/$s_!oViK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oViK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png" width="1456" height="479" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png&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;:198234,&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_!oViK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 424w, https://substackcdn.com/image/fetch/$s_!oViK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 848w, https://substackcdn.com/image/fetch/$s_!oViK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.png 1272w, https://substackcdn.com/image/fetch/$s_!oViK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F47cfbe7f-e02d-425d-b0b9-ca02252d359a_2963x975.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><h4>Scenario: </h4><p>In an e-commerce platform, consider the task of synchronizing product inventory data from multiple warehouses. Warehouses have APIs providing product stock updates. These APIs can be slow or rate-limited, and the data must be processed concurrently to minimize delays.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>So, We want to:</p><ul><li><p>Fetch inventory data from multiple warehouse APIs concurrently (<strong>Fan-Out</strong>).</p></li><li><p>Aggregate the stock updates into a central database (<strong>Fan-In</strong>).</p></li></ul><h4>Solution:</h4><p><strong>fetchInventory</strong>: The below `fetchInventory` method simulates the functionality of fetching inventory from external warehouse.  Currently this only simulates a dynamic delay and returns three stock inventory with varying random quantities. This will usually be implemented with a service client or a queue client to get inventory updates in real implementation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0D7F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0D7F!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 424w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 848w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 1272w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0D7F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png" width="1456" height="779" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:779,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:292446,&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_!0D7F!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 424w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 848w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.png 1272w, https://substackcdn.com/image/fetch/$s_!0D7F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c2d61c5-b364-4b57-9bf3-f5c58e483efe_2078x1112.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>worker:</strong> Worker is the Fan-out goroutines to concurrently process multiple warehouses identified by warehouseID. Once Inventory is received, it updates/writes the inventory to the same output channel `out`. From the main function below, we spinup 5 workers, so 5 concurrent workers will run reading from each warehouse at the same time - and result of all the workers are written to the same output channel.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!c_py!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!c_py!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 424w, https://substackcdn.com/image/fetch/$s_!c_py!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 848w, https://substackcdn.com/image/fetch/$s_!c_py!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 1272w, https://substackcdn.com/image/fetch/$s_!c_py!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!c_py!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:286749,&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_!c_py!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 424w, https://substackcdn.com/image/fetch/$s_!c_py!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 848w, https://substackcdn.com/image/fetch/$s_!c_py!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.png 1272w, https://substackcdn.com/image/fetch/$s_!c_py!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53b11d18-fda3-4611-8eeb-fb483970f71c_1998x1498.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>main function</strong>: we declared a slice with 5 warehouseIds and spinup 5 separate goroutines (Fan-out) to process all the warehouses concurrently. We setup a waitgroup to wait for the completion of all the 5 goroutines and closeout the `out` channel.</p><p>At the end, we are continuously read inventory from the `out` channel (Fan-In) whenever each worker write an inventory. we collect the inventory and aggregate the quantity in a separate centralInventory map. When the waitgroup completes in the above goroutine, then it closes the `out` channel - leading to terminating the below for range loop on the `out` channel. Finally we display the aggregated inventory result. </p><h4>Sample output:</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eMq6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eMq6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 424w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 848w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 1272w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eMq6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png" width="1456" height="427" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:427,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:131909,&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_!eMq6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 424w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 848w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.png 1272w, https://substackcdn.com/image/fetch/$s_!eMq6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58dbfa68-2a5c-4c55-89a1-f5fa1952f43e_1820x534.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><h4><strong>Best Practices</strong></h4><ol><li><p><strong>Channel Buffers</strong>: Use buffered channels if the producer and consumer speeds differ significantly to prevent blocking.</p></li><li><p><strong>Worker Pool</strong>:  when number of warehouses increases, consider implementing worker pool pattern to restrict the number of workers to avoid resource exhaustion.</p></li><li><p><strong>Error Handling</strong>: Enhance the worker function to handle and report errors from API calls.</p></li><li><p><strong>Timeouts</strong>: Use <code>context.WithTimeout</code> to ensure no goroutine hangs indefinitely due to unresponsive APIs.</p></li><li><p><strong>Rate Limiting</strong>: Implement rate limiting if warehouse APIs have request caps to prevent overloading.</p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Five Go Concurrency Patterns, every Go Developer must know - Worker Pool]]></title><description><![CDATA[Parallel processing of tasks with efficient system resource utilization]]></description><link>https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Fri, 27 Dec 2024 15:23:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!oRaA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this three part series, we will be mastering `Go Concurrency` by exploring top Go Concurrency patterns that are frequently used in solving well known concurrency problems efficiently.</p><p>Series:</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><ol><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-worker-pool">Go Concurrency Patterns - Worker Pool</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-fan-out-fan">Go Concurrency Patterns - Fan-In Fan-Out</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-pattern-context-cancellation">Go Concurrency Patterns - Context Cancellation</a></p></li><li><p><a href="https://www.alagesanpalani.com/p/go-concurrency-patterns-pipeline">Go Concurrency Patterns - Pipeline</a></p></li></ol><p></p><h4>Worker Pool</h4><p>In the current part, we will explore in depth on `Worker Pool Pattern`  by applying it in the real world industry examples.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oRaA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oRaA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 424w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 848w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 1272w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oRaA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png" width="1270" height="686" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:686,&quot;width&quot;:1270,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:586733,&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_!oRaA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 424w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 848w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.png 1272w, https://substackcdn.com/image/fetch/$s_!oRaA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2b8417a-8cbb-485c-b412-5604f763abde_1270x686.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>The Worker Pool pattern effectively leverages goroutines and channels to distribute tasks or jobs to a pool of worker goroutines. The main goals are to limit the number of concurrent goroutines and efficiently manage task execution, avoiding the overhead or risk of running an excessive number of goroutines concurrently.</p><p>It is particularly useful in situations where tasks need to be processed concurrently but system resources are limited, such as CPU, memory, or network bandwidth.</p><p>We will apply this pattern to handle a `Banking transactions processing` scenario.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rrtp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rrtp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 424w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 848w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rrtp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png" width="1456" height="980" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:980,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:132217,&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_!rrtp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 424w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 848w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!rrtp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F734b9abe-fe96-4e09-a8be-64995c4a2c8d_1729x1164.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><h4><strong>Scenario</strong></h4><p>In the banking industry, consider the need to process thousands of financial transactions. Each transaction involves say:</p><ul><li><p>Reading data from a database.</p></li><li><p>Performing validations (e.g., fraud checks, balance validation).</p></li><li><p>Updating account balances.</p></li></ul><p>Creating a new goroutine for each transaction may lead to resource exhaustion (e.g., too many open connections or high memory usage). so, we will apply the <strong>Worker Pool</strong> pattern to process transactions concurrently but with a controlled number of workers.</p><h4>Solution:</h4><p>We setup three main constructs to solve the problem.</p><ol><li><p>Define a minimilistic `Transaction` struct</p></li><li><p>Define a `ProcessTransaction function` that simulates processing a transaction and return if there is any error</p></li><li><p>Define a `worker function` that processes the list of tasks retrieved from a channel and sends out the result</p></li><li><p>Define a `main function` to orchestrate the whole process</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_!oHW_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oHW_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 424w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 848w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 1272w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oHW_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png" width="1456" height="1038" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1038,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:388774,&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_!oHW_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 424w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 848w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.png 1272w, https://substackcdn.com/image/fetch/$s_!oHW_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F334289c6-77fb-4c76-9ad2-08ea29929d22_2182x1556.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>ProcessTransaction :  receives a transaction instance and simulate processing it by introducing a random delay. At a random time, it also returns a error simulation. Finally prints as Transaction completed.</p><p>Worker: this constitues the worker goroutine in the pool. It ranges transaction from the tasks channel (behaves as task queue) and sends it for processing by calling `ProcessTransaction`. Writes error to the results channel when a error is returned from `ProcessTransaction`.  WaitGroup is important to make the wait function to wait till all workers completes its processing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!f2Nm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!f2Nm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 424w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 848w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 1272w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!f2Nm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png" width="1456" height="1322" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1322,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:266638,&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_!f2Nm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 424w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 848w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.png 1272w, https://substackcdn.com/image/fetch/$s_!f2Nm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65347419-102f-4a96-87f4-4f1afb351523_1722x1564.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_!EFbx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EFbx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 424w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 848w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 1272w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EFbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png" width="1456" height="736" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:736,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:115120,&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_!EFbx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 424w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 848w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.png 1272w, https://substackcdn.com/image/fetch/$s_!EFbx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F465e2913-a1e4-4fcf-8c47-1ad66c82bcf0_1860x940.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>Main funtion performs following steps:</p><ol><li><p>Defines a set of sample Transactions.</p></li><li><p>Define a variable `numWorkers` and initializes to 3 - which means we want to restrict spinning up only 3 worker goroutines in the pool.</p></li><li><p>We start 3 worker goroutines and pass transactions via channel to those workers</p></li><li><p>We range transactions slice and write transaction one by one on the tasks channel. Meanwhile each worker started in the above step is blocked and waiting to receive a task from the task channel (as the channel is a unbuffered channel). When a task is available in the channel any random worker picks up the task and goes for processing. Once all the transactions are written on channel, then we close the tasks channel. </p></li><li><p>We wait on waitgroup again on a separate goroutine, when waitgroup over, then we close result channel</p></li><li><p>We read error result from the result channel and displays when any error occured.</p></li><li><p>There are multiple goroutine dynamics in this program. Main function starts the process and spinup all the separate goroutines and run till it gets blocked on result channel in the last block of code. Everything else runs on random unpredictable order, in parallel. When all the worker completes its work, then waitgroup completes and then closes the result channel. Closing of result channel is the trigger to complete reading result from the result channel, then terminates for range. </p></li></ol><h4>Sample Output:</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oVsP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oVsP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 424w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 848w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 1272w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oVsP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png" width="1456" height="578" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:578,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:189204,&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_!oVsP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 424w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 848w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.png 1272w, https://substackcdn.com/image/fetch/$s_!oVsP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdf03db3-fca0-4488-af9b-940d5cdd336c_1818x722.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 above output result is one sample run, but the order of execution can drastically varry from one run to another due to parallel nature of running goroutines (when multiple cpu  core available). But the main program always waits to complete all the worker goroutines.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Dependency injection pattern in Go]]></title><description><![CDATA[with an easy implementation and unit/mock testing example]]></description><link>https://www.alagesanpalani.com/p/dependency-injection-pattern-in-go</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/dependency-injection-pattern-in-go</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Thu, 26 Dec 2024 00:54:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SHp1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Dependency Injection (DI) is a crucial design pattern in modern software development that promotes loose coupling and increased modularity. This blog will introduce you to the DI pattern using Go and explain why implementing it in Go is incredibly straightforward and powerful. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SHp1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SHp1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 424w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 848w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 1272w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SHp1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png" width="1200" height="700" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:700,&quot;width&quot;:1200,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:117446,&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_!SHp1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 424w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 848w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.png 1272w, https://substackcdn.com/image/fetch/$s_!SHp1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7607e17-9fd9-4a1e-bf72-11125a54578e_1200x700.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></p><h4><strong>What Is Dependency Injection?</strong></h4><p>Dependency Injection is a design pattern where an object's dependencies are provided externally rather than being hard-coded within the object itself. This approach allows for greater flexibility and easier unit testing, as external dependencies can be swapped with mocks or stubs during testing.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani Newsletter! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h4><strong>DI in Go: </strong></h4><p>Go, with its simplicity and structural approach, facilitates DI in a seamless manner. Unlike languages that require special frameworks or complex configurations to manage dependencies, Go leverages its interface-based design to make DI implementation straightforward. </p><p>Let's use the following Go program as an example to see how DI works.</p><p>The below Go program manages product data and demonstrates dependency injection through its various layers: data storage, business logic, and the controller.</p><p>We define a `DataStore` interface for fetching product data and implement this interface in the `ProductDataStore` structure. `Product` is a simple DTO to represent products.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hwyA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hwyA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 424w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 848w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 1272w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hwyA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png" width="1456" height="871" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e903be94-28c8-4019-b212-758ef1428013_1518x908.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:871,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:169097,&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_!hwyA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 424w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 848w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.png 1272w, https://substackcdn.com/image/fetch/$s_!hwyA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe903be94-28c8-4019-b212-758ef1428013_1518x908.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>`ProductDataStore` struct is considered as implementing the interface `DataStore` because it implements two methods defined in the interface with the same name and signature.  Unlike other languages such as Java where a `class` considered implementing an `interface` only by explictly declaring it as `implements`  and implementing all the methods of the interface, Go requires only to implement the methods and is implicitly considered as implementing the interface. </p><blockquote><p>Power of Go is both interface and struct are completely oblivious of each other&#8217;s existence and not explicitly knowing one is using another, so each can evolve independently in a loosely coupled way.</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_!B92S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B92S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 424w, https://substackcdn.com/image/fetch/$s_!B92S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 848w, https://substackcdn.com/image/fetch/$s_!B92S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 1272w, https://substackcdn.com/image/fetch/$s_!B92S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B92S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png" width="1456" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:114579,&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_!B92S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 424w, https://substackcdn.com/image/fetch/$s_!B92S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 848w, https://substackcdn.com/image/fetch/$s_!B92S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.png 1272w, https://substackcdn.com/image/fetch/$s_!B92S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2deede96-d2c8-4087-8fd1-e3760218922e_1626x510.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>Methods `GetProductById` and `GetProductByName` are self-explanatory by getting product by either Id or Name respectively.</p><h4>Logger Adapter and Interface</h4><p>We define a `Logger` interface capable of logging messages. The `LoggerAdapter` is a function type that implements this interface, showcasing Go's flexibility in treating functions as first-class citizens.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Tk3I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Tk3I!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 424w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 848w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 1272w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Tk3I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png" width="1456" height="465" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:465,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:87061,&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_!Tk3I!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 424w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 848w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.png 1272w, https://substackcdn.com/image/fetch/$s_!Tk3I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe1046df-9e11-4e81-b7a9-459a403a1c36_1604x512.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>Just like a struct, a `func` type can also be considered as implementing an interface when it has a method (Log) with same name and signature as the the method defined in that interface (Log).  As a different style, LoggerAdapter can also be declared as an empty struct{} type producing same result. Either way, it follows interface and implementing type principle so that we can apply DI in a loosely couple way.  </p><h4>ProductLogic Layer</h4><p>The `ProductLogic` structure uses DI by accepting a `Logger` and `DataStore` as dependencies. This decoupling means that both logging and data operations can be easily modified without altering the logic layer.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Pu8W!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Pu8W!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 424w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 848w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 1272w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Pu8W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png" width="1456" height="538" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:538,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:104820,&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_!Pu8W!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 424w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 848w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.png 1272w, https://substackcdn.com/image/fetch/$s_!Pu8W!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F253f404a-3a4c-4248-9b6d-eccaf0a57371_1668x616.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>Note that `ProductLogic` is depending only on interface not concret type. So, it can be later injected with the required concret types.  `Logic` interface is also declared and `ProductLogic` implementing its methods (GetProductsById and GetProductByName) below.</p><p>Factory function `NewProductLogic` is creating an instance of `ProductLogic` by injecting its dependencies. For this we must always follow an important Go idiom given below:</p><blockquote><p>Accept Interfaces, Return concret types </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_!YwJC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YwJC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 424w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 848w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 1272w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YwJC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png" width="1456" height="535" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:535,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:195338,&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_!YwJC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 424w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 848w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.png 1272w, https://substackcdn.com/image/fetch/$s_!YwJC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac65ec56-e0af-4727-ba43-24d963d35519_1802x662.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 above two methods are again self-explanatory on getting products.</p><h4>Controller Layer</h4><p>Finally, the `Controller` leverages DI to manage HTTP requests, ensuring that core functionalities are abstracted through interfaces.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_Wx1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_Wx1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 424w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 848w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 1272w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_Wx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png" width="1456" height="386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:386,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:60783,&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_!_Wx1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 424w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 848w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.png 1272w, https://substackcdn.com/image/fetch/$s_!_Wx1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F277e6b5b-7aa3-43a0-b8b6-d0490d3724c7_1692x448.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>Note again that `Controller` struct only relies on interfaces and factory function `NewController` accepts interfaces and return concret type.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-RXw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-RXw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 424w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 848w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 1272w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-RXw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png" width="1456" height="766" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:766,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:217988,&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_!-RXw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 424w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 848w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.png 1272w, https://substackcdn.com/image/fetch/$s_!-RXw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c7ad643-8633-4a3e-a735-c585868aeb89_1734x912.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>Above method encapsulates controller level concern by taking in a query string parameter from REST api and gets the product by either Id or Name.</p><h4>Main Function</h4><p>At the end, the main function wires up all dependencies using the New* constructor functions, thus achieving dependency injection without additional frameworks.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!u15j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!u15j!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 424w, https://substackcdn.com/image/fetch/$s_!u15j!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 848w, https://substackcdn.com/image/fetch/$s_!u15j!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 1272w, https://substackcdn.com/image/fetch/$s_!u15j!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!u15j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png" width="1456" height="325" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.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;:77153,&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_!u15j!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 424w, https://substackcdn.com/image/fetch/$s_!u15j!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 848w, https://substackcdn.com/image/fetch/$s_!u15j!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 1272w, https://substackcdn.com/image/fetch/$s_!u15j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F08b38563-1695-4b5e-99d1-62b34b91e0b6_1584x354.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Keypoint to understand from above dependency wiring up is again following the Go Idiom `Accept interfaces, return concrete types`.  Each statement is a factory function that create and return a concrete type instances and passed over to the next level factory function as dependencies, while all the parameter types receiving/accepting are interfaces.  </p><p>As DI simply follows `Open-Close` from `SOLID Principles`, we can just extend this pattern for easy and effective unit testing as given below</p><h4>How DI helps in unit testing</h4><p>Unit testing is all about testing in isolation of one unit at a time. While one unit is being tested for its logic correctness, any dependent logic must be mocked and injected with correct expectations. In the above example, each higher level layer depends on next lower level entities. </p><p>Controller depends on business logic, Business logic depends on DataStore.</p><p>While testing controller logic, its dependent usiness instances is mocked and injected. While testing Business logic, Its dependent datastore is mocked and injected. Since each layer: Data, business, and controller accepts interfaces in its factory functions, we can simply implement mocked version of the dependent instances and inject for unit testing.</p><h4>Mocking dependencies:</h4><p>Following is the implementation of DataStore mocking to be used in business logic unit testing. Note that the mock implements `DataStore` interface.</p><blockquote><p>For mocking, we leverage in built support from Go itself,  while its preferable (avoiding any third party libraries), you can still try other thirdparty libraries like GoMock, Testability etc for mocking.</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_!ZMvt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZMvt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 424w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 848w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 1272w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZMvt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png" width="1456" height="591" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:591,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:135416,&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_!ZMvt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 424w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 848w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.png 1272w, https://substackcdn.com/image/fetch/$s_!ZMvt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88b6d69e-d1f3-4b92-89fe-646f2d183e9c_1636x664.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>Following is the mock implementation of Logger interface.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HCA8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HCA8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 424w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 848w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 1272w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HCA8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png" width="1456" height="261" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/da5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:261,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:54560,&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_!HCA8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 424w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 848w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 1272w, https://substackcdn.com/image/fetch/$s_!HCA8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fda5c3f3a-f718-4c7f-b4eb-7e1764173a39_1730x310.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h4>Unit testing Product business logic:</h4><p>Following code is the unit test case for `GetProductById`. First mock values are setup as expected and injected to business factory functions. Business logic is tested with mocked dependencies.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T4SP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T4SP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 424w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 848w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 1272w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T4SP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png" width="1456" height="918" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:918,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:280129,&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_!T4SP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 424w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 848w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.png 1272w, https://substackcdn.com/image/fetch/$s_!T4SP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75f3020a-eca9-46b9-97d9-9ab2094447ce_1942x1224.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>Following code is the unit test case for `GetProductByName`. First mock values are setup as expected and injected to business factory functions. Business logic is tested with mocked dependencies.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P8im!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P8im!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 424w, https://substackcdn.com/image/fetch/$s_!P8im!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 848w, https://substackcdn.com/image/fetch/$s_!P8im!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 1272w, https://substackcdn.com/image/fetch/$s_!P8im!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P8im!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png" width="1456" height="937" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:937,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:284545,&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_!P8im!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 424w, https://substackcdn.com/image/fetch/$s_!P8im!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 848w, https://substackcdn.com/image/fetch/$s_!P8im!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.png 1272w, https://substackcdn.com/image/fetch/$s_!P8im!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F409ee0f4-a808-4351-b345-8f74b0688acc_1912x1230.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><h4>Conclusion</h4><p>Dependency Injection is a powerful design pattern that enhances the modularity, testability, and extensibility of your design. When working with Go, you benefit from the language's simplicity and built-in capabilities to implement DI effectively without third-party dependencies. Use DI in your Go projects to keep your code clean, maintainable, and adaptable to changes.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Alagesan Palani Newsletter! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Coming soon]]></title><description><![CDATA[This is Alagesan Palani.]]></description><link>https://www.alagesanpalani.com/p/coming-soon</link><guid isPermaLink="false">https://www.alagesanpalani.com/p/coming-soon</guid><dc:creator><![CDATA[Alagesan Palani]]></dc:creator><pubDate>Sat, 07 Dec 2024 06:06:41 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!bplc!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e36bd2f-66e7-49a4-9de7-935038e1ee51_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is Alagesan Palani.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.alagesanpalani.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.alagesanpalani.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item></channel></rss>