<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="https://muhammadahmed-01.github.io/feed_style.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <tabi:metadata xmlns:tabi="https://github.com/welpo/tabi">
        <tabi:base_url>https:&#x2F;&#x2F;muhammadahmed-01.github.io&#x2F;</tabi:base_url>
        <tabi:separator>
            •
        </tabi:separator>
        <tabi:about_feeds>This is a web feed, also known as an Atom feed. Subscribe by copying the URL from the address bar into your newsreader. Visit About Feeds to learn more and get started. It&#x27;s free.</tabi:about_feeds>
        <tabi:visit_the_site>Visit website</tabi:visit_the_site>
        <tabi:recent_posts>Recent posts</tabi:recent_posts>
        <tabi:last_updated_on>Updated on $DATE</tabi:last_updated_on>
        <tabi:default_theme></tabi:default_theme>
        <tabi:post_listing_date>date</tabi:post_listing_date>
        <tabi:current_section>Systems &amp; Stories</tabi:current_section>
    </tabi:metadata><title>Systems & Stories</title>
        <subtitle>Backend systems, production lessons, and engineering case studies.</subtitle>
    <link href="https://muhammadahmed-01.github.io/atom.xml" rel="self" type="application/atom+xml"/>
    <link href="https://muhammadahmed-01.github.io" rel="alternate" type="text/html"/>
    <generator uri="https://www.getzola.org/">Zola</generator><updated>2026-06-16T00:00:00+00:00</updated><id>https://muhammadahmed-01.github.io/atom.xml</id><entry xml:lang="en">
        <title>AWS Cost Hygiene: S3 Lifecycle Savings at Careem</title>
        <published>2026-06-16T00:00:00+00:00</published>
        <updated>2026-06-16T00:00:00+00:00</updated>
        <author>
            <name>Muhammad Ahmed</name>
        </author>
        <link rel="alternate" href="https://muhammadahmed-01.github.io/projects/aws-cost-infra-hygiene/" type="text/html"/>
        <id>https://muhammadahmed-01.github.io/projects/aws-cost-infra-hygiene/</id>
        
            <content type="html">&lt;p&gt;&lt;strong&gt;Case study repo:&lt;&#x2F;strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;aws-cost-infra-case-study&quot;&gt;aws-cost-infra-case-study&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Cloud spend often hides in defaults: log buckets that never expire, idle compute nobody owns, Terraform changes that merge without a cost diff. This page summarizes two real workstreams: &lt;strong&gt;S3 lifecycle savings at Careem&lt;&#x2F;strong&gt; and &lt;strong&gt;SageMaker pricing in Infracost&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;careem-s3-log-storage&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#careem-s3-log-storage&quot; aria-label=&quot;Anchor link for: careem-s3-log-storage&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Careem: S3 log storage&lt;&#x2F;h3&gt;
&lt;p&gt;Application and service logs accumulated in S3 Standard storage without tiering or expiration. After lifecycle policies (transition to IA&#x2F;Glacier, expire beyond retention, noncurrent version cleanup):&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;&#x2F;th&gt;&lt;th&gt;Result&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Storage volume&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;83% to 88% reduction&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Monthly savings&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;~$1k&lt;&#x2F;strong&gt; (measured)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Reference Terraform pattern: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;aws-cost-infra-case-study&#x2F;blob&#x2F;main&#x2F;examples&#x2F;terraform&#x2F;s3-log-lifecycle.tf&quot;&gt;s3-log-lifecycle.tf&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Full checklist (EC2&#x2F;EKS right-sizing, idle resources, tagging): &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;aws-cost-infra-case-study&#x2F;blob&#x2F;main&#x2F;docs&#x2F;INFRA-HYGIENE-CHECKLIST.md&quot;&gt;INFRA-HYGIENE-CHECKLIST.md&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;infracost-sagemaker-endpoint-configuration&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#infracost-sagemaker-endpoint-configuration&quot; aria-label=&quot;Anchor link for: infracost-sagemaker-endpoint-configuration&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Infracost: SageMaker endpoint configuration&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;GitHub PR:&lt;&#x2F;strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;infracost&#x2F;infracost&#x2F;pull&#x2F;3567&quot;&gt;infracost&#x2F;infracost#3567&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Three billing paths mapped for &lt;code&gt;aws_sagemaker_endpoint_configuration&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Provisioned instances:&lt;&#x2F;strong&gt; &lt;code&gt;instance_type&lt;&#x2F;code&gt; and &lt;code&gt;initial_instance_count&lt;&#x2F;code&gt;, plus optional hosting storage&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Serverless inference:&lt;&#x2F;strong&gt; GB-second compute and provisioned concurrency when configured&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Shadow variants:&lt;&#x2F;strong&gt; Parallel shadow production variants costed separately from live traffic&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Longer write-up: &lt;a href=&quot;&#x2F;projects&#x2F;infracost-sagemaker&#x2F;&quot;&gt;Shift-Left FinOps: Adding SageMaker Support to Infracost&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sample-audit-artifact&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#sample-audit-artifact&quot; aria-label=&quot;Anchor link for: sample-audit-artifact&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Sample audit artifact&lt;&#x2F;h3&gt;
&lt;p&gt;Illustrative monthly waste table (format only, not Careem billing): &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;aws-cost-infra-case-study&#x2F;blob&#x2F;main&#x2F;docs&#x2F;sample-waste-audit.md&quot;&gt;sample-waste-audit.md&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        </entry><entry xml:lang="en">
        <title>What I Learned Building a FinOps Control Plane for an AI Gateway</title>
        <published>2026-06-09T00:00:00+00:00</published>
        <updated>2026-06-09T00:00:00+00:00</updated>
        <author>
            <name>Muhammad Ahmed</name>
        </author>
        <link rel="alternate" href="https://muhammadahmed-01.github.io/projects/finops-rag-gateway/" type="text/html"/>
        <id>https://muhammadahmed-01.github.io/projects/finops-rag-gateway/</id>
        
            <content type="html">&lt;p&gt;Most teams add a chatbot and discover two problems later: &lt;strong&gt;token spend has no owner&lt;&#x2F;strong&gt;, and &lt;strong&gt;retrieval changes are impossible to compare&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I built &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;FinOps-AI-Gateway&quot;&gt;FinOps AI Gateway&lt;&#x2F;a&gt; as a learning lab to tackle both. It is not a production deployment at Careem. It is a demo with real instrumentation: hybrid RAG, tiered model routing, LangSmith traces, and Grafana dashboards.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-problem-i-wanted-to-explore&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#the-problem-i-wanted-to-explore&quot; aria-label=&quot;Anchor link for: the-problem-i-wanted-to-explore&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
The problem I wanted to explore&lt;&#x2F;h2&gt;
&lt;p&gt;Shipping RAG in production usually fails quietly:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Cost visibility&lt;&#x2F;strong&gt;: every query hits the same model tier. Nobody can answer “what did last Tuesday’s support bot traffic cost?”&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Retrieval quality&lt;&#x2F;strong&gt;: you change BM25 weights or add a reranker and hope answers feel better. There is no before&#x2F;after score.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;I wanted a gateway that treats those as engineering problems, not product guesses.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-the-gateway-does&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#what-the-gateway-does&quot; aria-label=&quot;Anchor link for: what-the-gateway-does&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
What the gateway does&lt;&#x2F;h2&gt;
&lt;p&gt;One query flows through:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Classify&lt;&#x2F;strong&gt; query complexity (Groq)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Retrieve&lt;&#x2F;strong&gt; with hybrid RAG (pgvector + BM25 + rerank)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Route&lt;&#x2F;strong&gt; to cheap vs expensive tiers (local Ollama vs Groq)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Export&lt;&#x2F;strong&gt; routing labels, latency breakdown, modeled cost, and eval scores to &lt;strong&gt;Grafana + LangSmith&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Stack: Python, PostgreSQL&#x2F;pgvector, Prometheus, Pushgateway, Grafana, LangSmith, Docker Compose.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;measured-results-from-committed-benchmarks&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#measured-results-from-committed-benchmarks&quot; aria-label=&quot;Anchor link for: measured-results-from-committed-benchmarks&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Measured results (from committed benchmarks)&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;routing-load-test-n-12&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#routing-load-test-n-12&quot; aria-label=&quot;Anchor link for: routing-load-test-n-12&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Routing (load test, n=12)&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Tier&lt;&#x2F;th&gt;&lt;th&gt;Share&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Simple (local Ollama)&lt;&#x2F;td&gt;&lt;td&gt;7&#x2F;12 (58%)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Medium&lt;&#x2F;td&gt;&lt;td&gt;3&#x2F;12&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Complex&lt;&#x2F;td&gt;&lt;td&gt;2&#x2F;12&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h3 id=&quot;latency&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#latency&quot; aria-label=&quot;Anchor link for: latency&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Latency&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Component&lt;&#x2F;th&gt;&lt;th&gt;p50 (approx.)&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;End-to-end&lt;&#x2F;td&gt;&lt;td&gt;~276s (concurrency=2, local Ollama)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Retrieval&lt;&#x2F;td&gt;&lt;td&gt;~2.3s&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Generation (simple tier)&lt;&#x2F;td&gt;&lt;td&gt;~356s (local Ollama dominates wall time)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Generation (medium&#x2F;complex)&lt;&#x2F;td&gt;&lt;td&gt;~1–4s (Groq in demo mode)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Retrieval is fast. Local generation is slow and free. That tradeoff is visible in the Grafana &lt;strong&gt;Latency Breakdown p50&lt;&#x2F;strong&gt; panel.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;cost-simulated-finops-model-not-actual-api-spend&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#cost-simulated-finops-model-not-actual-api-spend&quot; aria-label=&quot;Anchor link for: cost-simulated-finops-model-not-actual-api-spend&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Cost (simulated FinOps model, not actual API spend)&lt;&#x2F;h3&gt;
&lt;p&gt;Demo mode uses Groq + Ollama ($0 actual spend). Grafana applies Claude list rates × tokens for medium&#x2F;complex tiers as a &lt;strong&gt;counterfactual model&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;&#x2F;th&gt;&lt;th&gt;Value&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Simulated spend (tier routing)&lt;&#x2F;td&gt;&lt;td&gt;$0.0143 (12 queries)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Simulated all-complex counterfactual&lt;&#x2F;td&gt;&lt;td&gt;$0.0609&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Modeled savings vs counterfactual&lt;&#x2F;td&gt;&lt;td&gt;~77% (simulation only)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Actual API spend (demo mode)&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;$0&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Label simulated numbers clearly. Do not present them as production FinOps proof.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;rag-eval-ragas-pilot-n-8&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#rag-eval-ragas-pilot-n-8&quot; aria-label=&quot;Anchor link for: rag-eval-ragas-pilot-n-8&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
RAG eval (RAGAS pilot, n=8)&lt;&#x2F;h3&gt;
&lt;p&gt;Eight Q&amp;amp;A pairs is enough to compare pipelines directionally. It is not statistically powered for strong faithfulness claims.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;&#x2F;th&gt;&lt;th&gt;Baseline (cosine)&lt;&#x2F;th&gt;&lt;th&gt;Hybrid+rerank&lt;&#x2F;th&gt;&lt;th&gt;Delta&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;context_precision&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;0.6042&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;0.7917&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;+0.1875&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;faithfulness&lt;&#x2F;td&gt;&lt;td&gt;0.7690&lt;&#x2F;td&gt;&lt;td&gt;1.0000&lt;&#x2F;td&gt;&lt;td&gt;+0.2310 &lt;em&gt;(noisy at n=8)&lt;&#x2F;em&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;answer_relevancy&lt;&#x2F;td&gt;&lt;td&gt;0.7716&lt;&#x2F;td&gt;&lt;td&gt;0.7947&lt;&#x2F;td&gt;&lt;td&gt;+0.0231&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;&lt;strong&gt;Lead metric:&lt;&#x2F;strong&gt; context_precision +0.19 on the pilot set. Do not over-claim faithfulness at n=8.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-i-would-do-differently-for-production&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#what-i-would-do-differently-for-production&quot; aria-label=&quot;Anchor link for: what-i-would-do-differently-for-production&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
What I would do differently for production&lt;&#x2F;h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Scale the eval set&lt;&#x2F;strong&gt; to n=50+ before claiming retrieval wins to stakeholders.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Separate measured spend from modeled spend&lt;&#x2F;strong&gt; in every dashboard panel (this repo does; many demos do not).&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Add circuit breakers&lt;&#x2F;strong&gt; when cheap tiers fail open to expensive models.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Wire real billing exports&lt;&#x2F;strong&gt; (AWS Cost Explorer, OpenAI usage API) instead of list-price simulation.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;what-this-lab-is-good-for&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#what-this-lab-is-good-for&quot; aria-label=&quot;Anchor link for: what-this-lab-is-good-for&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
What this lab is good for&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Showing you can instrument an LLM gateway like any other backend service&lt;&#x2F;li&gt;
&lt;li&gt;Comparing retrieval pipelines with structured eval, not vibes&lt;&#x2F;li&gt;
&lt;li&gt;Explaining tiered routing tradeoffs with numbers attached&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;what-it-is-not&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#what-it-is-not&quot; aria-label=&quot;Anchor link for: what-it-is-not&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
What it is not&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;A replacement for production PostgreSQL&#x2F;API performance work (my day job at Careem)&lt;&#x2F;li&gt;
&lt;li&gt;Proof that hybrid RAG is “production ready” at n=8 eval&lt;&#x2F;li&gt;
&lt;li&gt;A $3,500 Upwork audit deliverable by itself&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;try-it-locally&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#try-it-locally&quot; aria-label=&quot;Anchor link for: try-it-locally&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Try it locally&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;git clone https:&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support z-function&quot;&gt;github.com&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;muhammadahmed&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;01&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;FinOps&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;AI&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;Gateway&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;copy .env.example .env   &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# GROQ_API_KEY + LANGCHAIN_API_KEY&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;.\scripts\bootstrap_demo.ps1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Grafana: http:&#x2F;&#x2F;localhost:3001 → dashboard &lt;strong&gt;FinOps AI Gateway&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;em&gt;Muhammad Ahmed · Backend engineer @ Careem · &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&quot;&gt;GitHub&lt;&#x2F;a&gt; · &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.linkedin.com&#x2F;in&#x2F;muhammadahmed19&#x2F;&quot;&gt;LinkedIn&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</content>
        <summary type="html">Hybrid RAG, cost routing, LangSmith traces, and Grafana metrics on a local AI gateway lab.</summary>
        </entry><entry xml:lang="en">
        <title>Shift-Left FinOps: Adding SageMaker Support to Infracost</title>
        <published>2026-05-10T00:00:00+00:00</published>
        <updated>2026-05-10T00:00:00+00:00</updated>
        <author>
            <name>Muhammad Ahmed</name>
        </author>
        <link rel="alternate" href="https://muhammadahmed-01.github.io/projects/infracost-sagemaker/" type="text/html"/>
        <id>https://muhammadahmed-01.github.io/projects/infracost-sagemaker/</id>
        
            <content type="html">&lt;p&gt;&lt;strong&gt;GitHub PR:&lt;&#x2F;strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;infracost&#x2F;infracost&#x2F;pull&#x2F;3567&quot;&gt;infracost&#x2F;infracost#3567&lt;&#x2F;a&gt; (open, awaiting upstream review)&lt;&#x2F;p&gt;
&lt;p&gt;“Shift-Left FinOps” is about moving cost visibility as far forward in the development lifecycle as possible. This project involved contributing to &lt;strong&gt;Infracost&lt;&#x2F;strong&gt;, a tool that sits in the CI&#x2F;CD pipeline to show engineers the “price tag” of their Terraform changes before they hit production.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-technical-challenge&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#the-technical-challenge&quot; aria-label=&quot;Anchor link for: the-technical-challenge&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
The Technical Challenge&lt;&#x2F;h3&gt;
&lt;p&gt;AWS SageMaker pricing is complex because it involves multiple billing dimensions that are often opaque in Terraform code. My contribution adds pricing support for &lt;code&gt;aws_sagemaker_endpoint_configuration&lt;&#x2F;code&gt;, covering:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inference Variants:&lt;&#x2F;strong&gt; Support for both provisioned instance-based hosting and serverless configurations.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Shadow Production Variants:&lt;&#x2F;strong&gt; Mapping costs for test models running in parallel with production traffic—a common source of “bill shock.”&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Async &amp;amp; Data Capture:&lt;&#x2F;strong&gt; Accounting for the overhead of asynchronous inference configurations and data capture fees for model monitoring.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;implementation-details&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#implementation-details&quot; aria-label=&quot;Anchor link for: implementation-details&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Implementation Details&lt;&#x2F;h3&gt;
&lt;p&gt;Working in the &lt;strong&gt;Go&lt;&#x2F;strong&gt; codebase of Infracost, I had to map complex Terraform resource attributes to the AWS Price List API. This required:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Resource Mapping:&lt;&#x2F;strong&gt; Translating nested configuration blocks into cost components.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Logic for Serverless vs. Provisioned:&lt;&#x2F;strong&gt; Handling the conditional logic where pricing changes from “per hour” to “per GB&#x2F;second” based on the &lt;code&gt;serverless_config&lt;&#x2F;code&gt; block.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Unit Testing:&lt;&#x2F;strong&gt; Ensuring that the pricing engine accurately calculates the monthly estimate across various edge cases (e.g., provisioned concurrency).&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;key-learnings&quot;&gt;&lt;a class=&quot;header-anchor no-hover-padding&quot; href=&quot;#key-learnings&quot; aria-label=&quot;Anchor link for: key-learnings&quot;&gt;&lt;span class=&quot;link-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;a&gt;
Key Learnings&lt;&#x2F;h3&gt;
&lt;p&gt;Contributing to a FinOps tool is a masterclass in the “financial underbelly” of the cloud. It forces you to realize that as a software engineer, you are effectively a “financial architect.” Every line of infrastructure code is a spending decision. By building visibility into the PR level, we empower engineers to be proactive rather than reactive.&lt;&#x2F;p&gt;
</content>
        <summary type="html">Infracost Go PR for SageMaker endpoints, serverless inference, shadow variants, and async Terraform pricing.</summary>
        </entry><entry xml:lang="en">
        <title>💥 The &quot;Money Vanishing&quot; Story: Concurrency &amp; Data Integrity!</title>
        <published>2026-05-08T00:00:00+00:00</published>
        <updated>2026-05-08T00:00:00+00:00</updated>
        <author>
            <name>Muhammad Ahmed</name>
        </author>
        <link rel="alternate" href="https://muhammadahmed-01.github.io/case-studies/concurrency-analysis/" type="text/html"/>
        <id>https://muhammadahmed-01.github.io/case-studies/concurrency-analysis/</id>
        
            <content type="html">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;GitHub (reproduce locally):&lt;&#x2F;strong&gt; https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;Go-Concurrency-Ledger-Integrity · &lt;strong&gt;Measured results:&lt;&#x2F;strong&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;muhammadahmed-01&#x2F;Go-Concurrency-Ledger-Integrity&#x2F;blob&#x2F;master&#x2F;k6&#x2F;K6_RESULTS.md&quot;&gt;k6&#x2F;K6_RESULTS.md&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Maps directly to &lt;strong&gt;DDIA Chapter 7 (Transactions)&lt;&#x2F;strong&gt; and &lt;strong&gt;Chapter 8 (Distributed Systems)&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Hardware:&lt;&#x2F;strong&gt; Intel i5-12500H · 16GB RAM · Windows 11 (battery; absolute latencies would be lower on AC&#x2F;server hardware; relative comparisons hold)
&lt;strong&gt;Stack:&lt;&#x2F;strong&gt; Go · PostgreSQL · Prometheus · Grafana · k6
&lt;strong&gt;Load:&lt;&#x2F;strong&gt; 800 VUs ramped over 50s (200 → 500 → 800), starting balance: &lt;strong&gt;$1,000,000&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;1-the-problem&quot;&gt;1. The Problem&lt;&#x2F;h2&gt;
&lt;p&gt;Two goroutines simultaneously deduct $10 from the same account. After both finish, only $10 was deducted — not $20. &lt;strong&gt;Money vanished.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Account balance: 1,000,000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Goroutine A:  READ  balance → 1,000,000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Goroutine B:  READ  balance → 1,000,000   ← same stale value, before A writes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Goroutine A:  WRITE balance = 999,990&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Goroutine B:  WRITE balance = 999,990     ← overwrites A. One deduction lost.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Final: 999,990  (should be 999,980)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the &lt;strong&gt;Lost Update Problem&lt;&#x2F;strong&gt; — one of the most common bugs in concurrent systems, and the most dangerous because it produces &lt;strong&gt;no errors&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;2-root-cause&quot;&gt;2. Root Cause&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;Read-Modify-Write without synchronization.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; ❌ BUGGY — classic lost update&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;balance&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; :=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; SELECT balance FROM accounts WHERE id&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  &#x2F;&#x2F; read&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;&#x2F;&#x2F; another goroutine reads the same value here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;UPDATE accounts SET balance&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;10&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; WHERE id&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;  &#x2F;&#x2F; overwrite, not decrement&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The check and write are not atomic. Between reading and writing, another goroutine reads the same stale value. Both write back a balance reflecting only &lt;em&gt;their&lt;&#x2F;em&gt; deduction.&lt;&#x2F;p&gt;
&lt;p&gt;Maps to &lt;strong&gt;DDIA §7.1 — Dirty Reads and Lost Updates&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;3-three-strategies-tested&quot;&gt;3. Three Strategies Tested&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;fix-1-pessimistic-locking&quot;&gt;Fix #1 — Pessimistic Locking&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Principle:&lt;&#x2F;strong&gt; Assume conflict will happen. Lock the resource before reading.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;BEGIN&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;FROM&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; accounts &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;WHERE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; FOR UPDATE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;  &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- acquires exclusive row lock&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- other goroutines trying FOR UPDATE BLOCK here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;UPDATE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; accounts &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;SET&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; WHERE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;COMMIT&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;  &lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- lock released&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;SELECT FOR UPDATE&lt;&#x2F;code&gt; acquires an exclusive row-level lock. Any other transaction attempting to read or write the same row blocks until the first commits or rolls back. Guarantees serialization — no two goroutines inside the critical section simultaneously.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;✅ Pros&lt;&#x2F;th&gt;&lt;th&gt;❌ Cons&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Correctness guaranteed&lt;&#x2F;td&gt;&lt;td&gt;Serializes all deductions → lower throughput&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Simple mental model&lt;&#x2F;td&gt;&lt;td&gt;Lock contention grows with concurrency&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Database enforces it&lt;&#x2F;td&gt;&lt;td&gt;Deadlock risk with multiple rows&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;fix-2-optimistic-concurrency-control-occ&quot;&gt;Fix #2 — Optimistic Concurrency Control (OCC)&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;Principle:&lt;&#x2F;strong&gt; Assume conflict is &lt;em&gt;unlikely&lt;&#x2F;em&gt;. Don’t lock — detect conflicts on write and retry.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- Read without any lock&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;SELECT&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance, &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;version FROM&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; accounts &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;WHERE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- balance=999,990, version=42&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- Write only if version hasn&amp;#39;t changed (Compare-And-Swap)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;UPDATE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; accounts&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;   SET&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; balance &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;, &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;version&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; version&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt; WHERE&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; id &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; AND version&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 42&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;-- If another goroutine already incremented version → rows_affected = 0 → RETRY&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;WHERE version = $current_version&lt;&#x2F;code&gt; guard is a CAS operation. If &lt;code&gt;rows_affected == 0&lt;&#x2F;code&gt;, a conflict occurred — retry from the read. The database never blocks; goroutines spin instead of waiting.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;✅ Pros&lt;&#x2F;th&gt;&lt;th&gt;❌ Cons&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Higher throughput when contention is low&lt;&#x2F;td&gt;&lt;td&gt;CPU burns on retries under high contention&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;No deadlock risk&lt;&#x2F;td&gt;&lt;td&gt;Retry logic adds code complexity&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;Scales horizontally&lt;&#x2F;td&gt;&lt;td&gt;Fairness not guaranteed — goroutines can starve&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;4-real-benchmark-results&quot;&gt;4. Real Benchmark Results&lt;&#x2F;h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key insight:&lt;&#x2F;strong&gt; Completed iterations differ significantly per mode — this is a finding, not noise.
Buggy has no overhead so all requests complete. Pessimistic serializes. Optimistic hits retry limits.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Metric&lt;&#x2F;th&gt;&lt;th&gt;Buggy&lt;&#x2F;th&gt;&lt;th&gt;Optimistic&lt;&#x2F;th&gt;&lt;th&gt;Pessimistic&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Completed Iterations&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;160,207&lt;&#x2F;td&gt;&lt;td&gt;21,798&lt;&#x2F;td&gt;&lt;td&gt;51,914&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Effective Deductions&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;964&lt;&#x2F;strong&gt; ❌&lt;&#x2F;td&gt;&lt;td&gt;12,768 ✅&lt;&#x2F;td&gt;&lt;td&gt;51,293 ✅&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Final Balance&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;990,360 ❌&lt;&#x2F;td&gt;&lt;td&gt;872,320 ✅&lt;&#x2F;td&gt;&lt;td&gt;487,070 ✅&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Money Lost&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;$9,640 vanished&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;$0&lt;&#x2F;td&gt;&lt;td&gt;$0&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;p95 Latency (peak)&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;~600ms&lt;&#x2F;td&gt;&lt;td&gt;~4,250ms 🔴&lt;&#x2F;td&gt;&lt;td&gt;~2,200ms 🔴&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Error Rate&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;&lt;strong&gt;0%&lt;&#x2F;strong&gt; ✅&lt;&#x2F;td&gt;&lt;td&gt;~5–8% (409&#x2F;500)&lt;&#x2F;td&gt;&lt;td&gt;~15–28% (503)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;k6 SLA Thresholds&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;✅ Passed&lt;&#x2F;td&gt;&lt;td&gt;❌ Failed&lt;&#x2F;td&gt;&lt;td&gt;✅ Passed&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;DB Version at End&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;1 (never incremented)&lt;&#x2F;td&gt;&lt;td&gt;12,769 ✅&lt;&#x2F;td&gt;&lt;td&gt;N&#x2F;A&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Data Integrity&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;❌ CORRUPTED&lt;&#x2F;td&gt;&lt;td&gt;✅ Correct&lt;&#x2F;td&gt;&lt;td&gt;✅ Correct&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;5-detailed-findings&quot;&gt;5. Detailed Findings&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;rotating-light-finding-1-the-invisible-failure-buggy-mode&quot;&gt;🚨 Finding 1 — The Invisible Failure (Buggy Mode)&lt;&#x2F;h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The most dangerous failure mode in production.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;✅ HTTP 200 on every request, 0% error rate, lowest latency, highest throughput&lt;&#x2F;li&gt;
&lt;li&gt;❌ &lt;strong&gt;Only 964 effective deductions out of 160,207 attempts. $9,640 vanished silently.&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Version column stayed at &lt;code&gt;1&lt;&#x2F;code&gt; — every goroutine overwrote the same cycle&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;⚡ The Key Insight:&lt;&#x2F;strong&gt; Your monitoring shows all green while silently destroying data. This is why data integrity bugs are more dangerous than crashes. &lt;strong&gt;Crashes are obvious. Silent corruption is not.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;rotating-light-finding-2-the-retry-storm-optimistic-mode&quot;&gt;🚨 Finding 2 — The Retry Storm (Optimistic Mode)&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;Postgres logs confirmed two distinct failure modes: &lt;code&gt;409&lt;&#x2F;code&gt; responses where the version CAS guard rejected the write after retries were exhausted, and &lt;code&gt;500&lt;&#x2F;code&gt; responses where the Go context timeout cancelled the in-flight Postgres query mid-execution — visible in DB logs as &lt;code&gt;canceling statement due to user request&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Only &lt;strong&gt;21,798 iterations completed&lt;&#x2F;strong&gt; — 7× less useful work than buggy mode&lt;&#x2F;li&gt;
&lt;li&gt;p95 climbed to &lt;strong&gt;~4,250ms&lt;&#x2F;strong&gt; — highest of all three&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;version=12,769&lt;&#x2F;code&gt; matched &lt;code&gt;12,768 effective deductions&lt;&#x2F;code&gt; exactly — correctness maintained, at enormous cost&lt;&#x2F;li&gt;
&lt;li&gt;Postgres hit &lt;strong&gt;49.6% CPU&lt;&#x2F;strong&gt; just managing retry overhead&lt;&#x2F;li&gt;
&lt;li&gt;System continued processing in-flight retries &lt;strong&gt;after k6 stopped&lt;&#x2F;strong&gt; — retry storms have a latency tail that outlasts the load&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;The version column is the smoking gun:&lt;&#x2F;strong&gt; proves OCC was always correct — but correctness required ~580,000 retries.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;rotating-light-finding-3-the-availability-cliff-pessimistic-mode&quot;&gt;🚨 Finding 3 — The Availability Cliff (Pessimistic Mode)&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;~155 failed requests&#x2F;second&lt;&#x2F;strong&gt; at peak (503)&lt;&#x2F;li&gt;
&lt;li&gt;p95 reached &lt;strong&gt;~2,200ms&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Only &lt;strong&gt;51,914 iterations&lt;&#x2F;strong&gt; — 3× fewer than buggy, but nearly all correct&lt;&#x2F;li&gt;
&lt;li&gt;Without the 2s context timeout, goroutines would pile up until OOM. &lt;strong&gt;503 is the right production behavior.&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;6-broader-concepts-this-experiment-demonstrates&quot;&gt;6. Broader Concepts This Experiment Demonstrates&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;acid-guarantees&quot;&gt;ACID Guarantees&lt;&#x2F;h3&gt;
&lt;p&gt;The buggy endpoint violates &lt;strong&gt;Isolation&lt;&#x2F;strong&gt; — two transactions observe each other’s in-progress state. Pessimistic locking restores it. Maps to &lt;strong&gt;DDIA §7.2&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;isolation-levels&quot;&gt;Isolation Levels&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Read Committed&lt;&#x2F;strong&gt; (PostgreSQL default): prevents dirty reads, but &lt;strong&gt;not&lt;&#x2F;strong&gt; lost updates&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Repeatable Read &#x2F; Serializable&lt;&#x2F;strong&gt;: prevents lost updates, higher overhead&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SELECT FOR UPDATE&lt;&#x2F;code&gt; at Read Committed gives Serializable-like safety for specific rows without changing the whole transaction’s isolation level — surgical and efficient&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;lost-updates-six-prevention-strategies-ddia-ss7-4&quot;&gt;Lost Updates — Six Prevention Strategies (DDIA §7.4)&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;Atomic writes (&lt;code&gt;UPDATE ... SET balance = balance - 10&lt;&#x2F;code&gt; — safe if no read in between)&lt;&#x2F;li&gt;
&lt;li&gt;Explicit locking (&lt;code&gt;SELECT FOR UPDATE&lt;&#x2F;code&gt;) — what we tested&lt;&#x2F;li&gt;
&lt;li&gt;Compare-and-set (OCC version column) — what we tested&lt;&#x2F;li&gt;
&lt;li&gt;Conflict detection (database detects and aborts)&lt;&#x2F;li&gt;
&lt;li&gt;Application-level locking (Redis distributed lock)&lt;&#x2F;li&gt;
&lt;li&gt;Serializable isolation (nuclear option — correct but expensive)&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;throughput-vs-consistency&quot;&gt;Throughput vs Consistency&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;Buggy: max throughput, zero consistency&lt;&#x2F;li&gt;
&lt;li&gt;Pessimistic: min throughput, max consistency&lt;&#x2F;li&gt;
&lt;li&gt;Optimistic: good throughput &lt;em&gt;when contention is low&lt;&#x2F;em&gt;, catastrophic under heavy conflict&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is the fundamental tension in distributed systems — the tradeoff DDIA explores across Chapters 7–9.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;retries-and-idempotency&quot;&gt;Retries and Idempotency&lt;&#x2F;h3&gt;
&lt;p&gt;OCC &lt;em&gt;requires&lt;&#x2F;em&gt; retries. If your operation is not idempotent, retrying causes double-charges, double-sends, double-emails. &lt;strong&gt;Always pair retry logic with idempotency keys.&lt;&#x2F;strong&gt; Maps to &lt;strong&gt;DDIA §11.5&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-this-looks-like-at-stripe-netflix-scale&quot;&gt;What This Looks Like at Stripe&#x2F;Netflix Scale&lt;&#x2F;h3&gt;
&lt;p&gt;At millions of requests&#x2F;second on the same row, even OCC breaks down. Escalation path:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Shard the account&lt;&#x2F;strong&gt; — split one row into N shards, sum on read (reduces hot-row contention by N×)&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Event sourcing&lt;&#x2F;strong&gt; — append-only log, no in-place updates, replay to get balance&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;CRDT-based balance&lt;&#x2F;strong&gt; — conflict-free replicated data types, mathematically merge concurrent updates without coordination&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;7-the-contention-spectrum&quot;&gt;7. The Contention Spectrum&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    SINGLE HOT ROW (extreme contention)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                               │&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Buggy ─────────────────────┼────────── Pessimistic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   (fast, silent               │           (correct, serialized,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    corruption)                │            503s under load)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                               │&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                          Optimistic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                       (correct but retry storm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        at high contention,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        excellent at low contention)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                               │&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    MANY INDEPENDENT ROWS (low contention)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                       (OCC wins — conflicts rare,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                        no blocking, horizontally scalable)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;8-real-world-architectural-solutions-at-scale&quot;&gt;8. Real-World Architectural Solutions at Scale&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;option-a-lmax-sequencer-right-answer-for-high-frequency-deductions&quot;&gt;Option A — LMAX Sequencer (Right Answer for High-Frequency Deductions)&lt;&#x2F;h3&gt;
&lt;p&gt;Remove the database from the hot path entirely. Route all deductions through a single-threaded processor fed by an in-memory ring buffer. DB write is async — the processor never waits for disk.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[800 VUs] → [Ring Buffer] → [Single Worker Thread] → [Async DB write]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                (sequential, zero contention)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Why this beats pessimistic + semaphore:&lt;&#x2F;strong&gt; A semaphore reduces how many goroutines fight over the row — the fight still exists. LMAX removes the fight entirely. Processes at memory speed: no locks, no retries, no conflicts. LMAX-based systems handle millions of ops&#x2F;sec per core.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Cons:&lt;&#x2F;strong&gt; High implementation complexity, fully async I&#x2F;O required, hard to scale horizontally.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Use when:&lt;&#x2F;strong&gt; Payment processors, matching engines, HFT — anywhere hot row contention is unavoidable and throughput targets can’t be met with locking.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;option-b-pessimistic-locking-go-semaphore-pragmatic-middle-ground&quot;&gt;Option B — Pessimistic Locking + Go Semaphore (Pragmatic Middle Ground)&lt;&#x2F;h3&gt;
&lt;p&gt;Cap concurrent DB writers so excess requests queue cheaply in Go, not expensively in Postgres:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;var&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; sem&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; make&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;chan struct&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;{},&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 25&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;func&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; deductHandler&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt;w&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt; http&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;ResponseWriter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter&quot;&gt; r&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;http&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-type&quot;&gt;Request&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-source&quot;&gt;    sem&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; struct&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;{}{}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    defer func&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;() {&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;sem&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; }()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;    &#x2F;&#x2F; Now 25 goroutines compete, not 800&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;    runPessimisticDeduction&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt;r&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt;Context&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span class=&quot;z-source&quot;&gt; db&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Still DB-row-bound, but eliminates the goroutine flood causing 503 storms. Right choice when simplicity matters more than maximum throughput.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;option-c-sharding-eventual-consistency-event-sourcing&quot;&gt;Option C — Sharding, Eventual Consistency, Event Sourcing&lt;&#x2F;h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Approach&lt;&#x2F;th&gt;&lt;th&gt;Use When&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Sharding&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Many independent accounts, low per-account contention&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Async Queue (SQS&#x2F;Kafka)&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Can tolerate stale reads, non-critical aggregates&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Event Sourcing&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Full audit trail required, append-only semantics&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;9-decision-framework&quot;&gt;9. Decision Framework&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Is contention on a single hot row unavoidable?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;├── No (many independent rows) → OCC (Optimistic Locking)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;└── Yes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ├── Maximum throughput required → LMAX Sequencer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    └── Simplicity &amp;gt; max throughput&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ├── Low-medium traffic → Pessimistic + Semaphore&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        └── Can tolerate stale reads → Eventual Consistency&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        <summary type="html">Go concurrency lab: 800 VUs, ledger integrity, OCC vs pessimistic locking.</summary>
        </entry><entry xml:lang="en">
        <title>The SAA Blueprint: Learning to &#x27;Think&#x27; in AWS</title>
        <published>2026-04-15T00:00:00+00:00</published>
        <updated>2026-04-15T00:00:00+00:00</updated>
        <author>
            <name>Muhammad Ahmed</name>
        </author>
        <link rel="alternate" href="https://muhammadahmed-01.github.io/learnings/saa-blueprint/" type="text/html"/>
        <id>https://muhammadahmed-01.github.io/learnings/saa-blueprint/</id>
        
            <content type="html">&lt;p&gt;Passing the AWS Solutions Architect – Associate (SAA-C03) with a score of &lt;strong&gt;861&#x2F;1000&lt;&#x2F;strong&gt; was less about the badge and more about a fundamental shift in how I approach backend engineering.&lt;&#x2F;p&gt;
&lt;p&gt;In a high-scale environment like Careem, “making it work” is the bare minimum. The real challenge is making it resilient, cost-effective, and observable. Here are the core principles I carried away from the certification process.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;depth-over-trivia&quot;&gt;Depth over Trivia&lt;&#x2F;h3&gt;
&lt;p&gt;The SAA syllabus is vast, but the real engineering value lies in the &lt;strong&gt;Core Pillars&lt;&#x2F;strong&gt;: Resilience, Performance, Security, and Cost. I chose to ignore the “trivia” (maximum limits of obscure services) and focused on the integration logic of the core stack: RDS, SQS, S3, and VPC networking.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-differential-learning-framework&quot;&gt;The “Differential Learning” Framework&lt;&#x2F;h3&gt;
&lt;p&gt;When you can’t easily lab “Enterprise” features like &lt;strong&gt;Control Tower&lt;&#x2F;strong&gt; or &lt;strong&gt;AWS Organizations&lt;&#x2F;strong&gt; in a personal account, you have to use a different approach. I used LLMs as an investigative tool to stress-test my understanding of subtle differences between similar services.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of reading a syllabus, I asked: &lt;em&gt;“If I have a multi-account strategy with strict compliance requirements, why would I use Control Tower over a manual Organizations setup?”&lt;&#x2F;em&gt; This turned abstract concepts into concrete architectural decisions.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;from-how-to-code-to-how-it-fails&quot;&gt;From “How to Code” to “How it Fails”&lt;&#x2F;h3&gt;
&lt;p&gt;The most permanent change is the lens through which I view every system design decision. I no longer just see a Go service; I see a component that must handle:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Exponential Backoff &amp;amp; Jitter:&lt;&#x2F;strong&gt; Ensuring a network blip doesn’t trigger a retry storm.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Decoupling:&lt;&#x2F;strong&gt; Using SQS not just for async work, but as a buffer to protect downstream databases.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Cost as a First-Class Citizen:&lt;&#x2F;strong&gt; Realizing that a sub-optimal S3 storage class is effectively a technical debt on the company’s bottom line.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;If you are a backend engineer, don’t study for the paper. Study for the mental map that allows you to reason through tradeoffs when there is no single “right” answer.&lt;&#x2F;p&gt;
</content>
        </entry>
</feed>
