<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chaitty on Medium]]></title>
        <description><![CDATA[Stories by Chaitty on Medium]]></description>
        <link>https://medium.com/@chsharma23051998?source=rss-4f0e08f85624------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*V29g2PaBnBMsmIKj</url>
            <title>Stories by Chaitty on Medium</title>
            <link>https://medium.com/@chsharma23051998?source=rss-4f0e08f85624------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:54:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chsharma23051998/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Caching Strategies: A Deep Dive Into the Patterns That Run the Internet]]></title>
            <link>https://medium.com/@chsharma23051998/caching-strategies-a-deep-dive-into-the-patterns-that-run-the-internet-8cadfafdd378?source=rss-4f0e08f85624------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cadfafdd378</guid>
            <category><![CDATA[caching-strategies]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Chaitty]]></dc:creator>
            <pubDate>Mon, 06 Jul 2026 08:24:32 GMT</pubDate>
            <atom:updated>2026-07-06T08:24:32.273Z</atom:updated>
            <content:encoded><![CDATA[<p>I use Redis in my RAG Gateway project, and until recently I’d have told you I knew caching well. I’d wired it up, tuned TTLs, seen it work in production. Then I asked Claude to run me through an architect-level mock interview on caching — and watched my confident answers fall apart one follow-up at a time. I had write-through backwards. I reached for a cache-layer lock to solve what was actually a database concurrency problem. I said “smaller TTL” as if it were a strategy rather than a number I couldn’t justify.</p><p>What I’d actually known was <em>how to use</em> a cache. What the interview exposed was that I’d never systematically understood <em>how reads and writes should flow</em> between application, cache, and source of truth — which is what a caching strategy really defines. This article is the deep dive I wished I’d done earlier, written after filling those gaps properly.</p><p>Ask most engineers to describe caching and you’ll get “store frequently accessed data in memory so you don’t hit the database.” That’s true, but it skips the hard part entirely. The interesting decisions aren’t about <em>whether</em> to cache — they’re about that read/write flow, and getting it wrong is how systems end up serving fictional data, melting databases during traffic spikes, or losing writes entirely.</p><p>What follows are the six caching strategies that matter in practice — how each one works mechanically, what it costs, where it breaks, and where industry actually uses it.</p><p>One framing to hold throughout: <strong>every strategy is answering two questions — who populates the cache on a read miss, and who keeps the cache correct on a write?</strong> The six strategies are just different combinations of answers.</p><h3>1. Cache-Aside (Lazy Loading)</h3><p><strong>The most widely used caching pattern in the industry, and the default you should assume unless something forces you elsewhere.</strong></p><h3>How it works</h3><p>The application owns all the logic. The cache is a passive store that knows nothing about your database.</p><p><strong>Read path:</strong></p><ol><li>Application checks the cache for the key.</li><li><strong>Hit:</strong> return the cached value. Done.</li><li><strong>Miss:</strong> application queries the database, writes the result into the cache (with a TTL), and returns it.</li></ol><p><strong>Write path:</strong></p><ol><li>Application writes to the database.</li><li>Application <strong>deletes</strong> (invalidates) the corresponding cache key — it does <em>not</em> update it.</li></ol><pre>READ (cache-aside)                    WRITE (cache-aside)</pre><pre>App ──(1)──&gt; Cache : GET key          App ──(1)──&gt; DB    : UPDATE row<br>    &lt;──────        : miss             App ──(2)──&gt; Cache : DEL key<br>App ──(2)──&gt; DB    : SELECT row<br>App ──(3)──&gt; Cache : SET key, TTL     (next reader repopulates)<br>    return value</pre><p>That last point is deliberate and frequently misunderstood, so it’s worth pausing on. Why delete instead of update? Because updating the cache on write opens a race: two concurrent writes can update the database in order A→B but reach the cache in order B→A, leaving the cache permanently holding the older value. Deletion sidesteps this — the next read repopulates from the database, which is guaranteed to have the final value. You trade one extra cache miss for a much simpler correctness story.</p><h3>Example</h3><p>A user-profile service. GET /users/42 checks Redis for user:42. On a miss, it queries Postgres, stores the serialized profile with a 1-hour TTL, and returns it. When user 42 updates their bio, the app writes to Postgres and issues DEL user:42. The next reader takes the miss and repopulates.</p><h3>Pros</h3><ul><li><strong>Only requested data gets cached.</strong> No memory wasted on rows nobody reads. With a catalog of 50M products where 1M get 95% of traffic, this matters enormously.</li><li><strong>Cache failure is survivable.</strong> If Redis dies, every request degrades to a database read. Slower, but functional — the cache is an optimization, not a dependency.</li><li><strong>The cache and database are fully decoupled.</strong> No special cache-database integration required; works with any cache and any store.</li><li><strong>Simple mental model for correctness:</strong> database is always the truth, cache is a best-effort accelerator.</li></ul><h3>Cons</h3><ul><li><strong>Cache miss penalty is paid by a real user.</strong> The first request for any key (or the first after invalidation/expiry) eats three hops: cache miss → DB query → cache write. On cold starts or after mass eviction, this compounds.</li><li><strong>Staleness window exists by design.</strong> Between a DB write and the cache delete completing — or if the delete fails — readers see old data. There’s also a subtler race: a reader misses the cache, reads the <em>old</em> value from the DB, and before it writes that value to the cache, a writer updates the DB and deletes the key; the reader then writes the stale value into the now-empty cache, where it sits wrongly until the next invalidation or TTL. The interleaving is easier to see on a timeline:</li></ul><pre>time ──────────────────────────────────────────────────►</pre><pre>  Reader R              Writer W             Cache     DB<br>  ────────              ────────             ─────     ──<br>  GET key → miss                                       v1<br>  SELECT → reads v1<br>                        UPDATE → v2                    v2<br>                        DEL key              (empty)   v2<br>  SET key = v1  ─────────────────────────►   v1 ✗      v2<br>                                             STALE</pre><p>Real systems mitigate with a TTL as a safety net (bounding how long that stale entry can live) or a delayed double-delete.</p><ul><li><strong>Every service that writes must remember to invalidate.</strong> The invalidation logic lives in application code, so a new code path (a batch job, a migration script) that forgets to delete keys silently introduces staleness.</li><li><strong>Thundering herd on hot-key invalidation.</strong> Delete a key that’s being read 10,000 times per second, and all 10,000 readers stampede the database simultaneously. Requires mitigation (request coalescing, brief locks) at scale.</li></ul><h3>Industry usage</h3><p>This is the bread-and-butter pattern behind most Redis and Memcached deployments. Facebook’s Memcached architecture — described in their well-known “Scaling Memcache at Facebook” paper — is fundamentally cache-aside with delete-on-write, plus a fleet of additional machinery (leases, regional invalidation pipelines) built specifically to patch the race conditions and stampedes described above. That’s a telling data point: even at the largest scale, the industry chose cache-aside and engineered around its weaknesses rather than switching strategies.</p><h3>2. Read-Through</h3><h3>How it works</h3><p>Structurally identical to cache-aside with one relocation: <strong>the cache itself (or a caching library/layer) owns the fetch-on-miss logic</strong>, not your application.</p><p><strong>Read path:</strong></p><ol><li>Application asks the cache for the key — and <em>only</em> the cache. It never talks to the database for reads.</li><li><strong>Hit:</strong> cache returns the value.</li><li><strong>Miss:</strong> the <em>cache layer</em> queries the database (via a configured loader), stores the result, and returns it. The application never knows a miss happened.</li></ol><h3>Example</h3><p>Using a JVM caching library like Caffeine or a distributed cache with loader support:</p><p>java</p><pre>LoadingCache&lt;Long, User&gt; users = Caffeine.newBuilder()<br>    .expireAfterWrite(Duration.ofHours(1))<br>    .build(userId -&gt; userRepository.findById(userId)); // the loader</pre><pre>User u = users.get(42L); // hit or miss, caller doesn&#39;t care</pre><p>The application calls get() and gets a user. Whether that involved a database round trip is invisible.</p><h3>Pros</h3><ul><li><strong>Application code gets dramatically simpler.</strong> No check-miss-fetch-populate boilerplate scattered across every read path; the pattern lives in one place.</li><li><strong>Miss-handling logic is centralized</strong>, which makes stampede protection much easier to implement well — many read-through libraries provide built-in request coalescing (Caffeine does exactly this: concurrent get() calls for the same missing key trigger one loader invocation, and everyone shares the result).</li><li><strong>Consistent behavior across teams.</strong> When the cache layer owns loading, individual developers can’t implement the read pattern subtly wrong.</li></ul><h3>Cons</h3><ul><li><strong>The cache becomes a harder dependency.</strong> Since the app only talks to the cache for reads, the cache layer’s availability and the loader’s correctness are now on the critical path in a way they aren’t with cache-aside.</li><li><strong>Loader inflexibility.</strong> The loader typically maps one key to one query. Reads that need joins, batch fetches, or request-specific context fit awkwardly.</li><li><strong>Same staleness characteristics as cache-aside</strong> — read-through says nothing about writes, so it’s almost always paired with one of the write strategies below or with delete-on-write invalidation. It’s a read-side pattern, not a complete answer.</li><li><strong>First-request latency penalty still exists</strong> — the miss cost didn’t disappear, it just moved out of your code.</li></ul><h3>Industry usage</h3><p>Ubiquitous at the <em>library</em> level (Caffeine, Guava’s LoadingCache, Spring’s @Cacheable abstraction) and available in managed offerings (DAX — DynamoDB Accelerator — is effectively a read-through/write-through cache that sits transparently in front of DynamoDB). Less common as a hand-rolled pattern against raw Redis, where cache-aside dominates because Redis itself has no native loader concept — the &quot;read-through&quot; you build on Redis is just cache-aside wearing a wrapper function.</p><h3>3. Write-Through</h3><h3>How it works</h3><p>The write path is where this strategy lives: <strong>every write goes to the database first (source of truth), and the cache is updated as part of the same synchronous operation.</strong> The write is only acknowledged to the caller after both succeed.</p><p><strong>Write path:</strong></p><ol><li>Application writes to the database.</li><li>Application (or the cache layer, if paired with read-through) writes the new value into the cache.</li><li>Only then is the write acknowledged.</li></ol><p><strong>Order matters, and it’s not a style preference.</strong> Database first, always. If the cache write fails after the DB write succeeded, you’re left with a stale or missing cache entry — recoverable, since the next read repopulates from the truth. If you invert the order and the DB write fails after the cache write, the cache now serves a value that <em>never existed</em> in the source of truth. That’s not staleness — it’s fiction, and it persists until something overwrites it.</p><h3>Example</h3><p>An inventory service where availability must be fresh: an order comes in, the service decrements stock in Postgres inside a transaction, and upon commit immediately writes the new count to Redis (SET stock:sku123:zip10001 4 EX 300 — the TTL is the safety net for the crash-between-writes case described in the cons below). Readers always find a value in cache, and that value reflects the last committed write.</p><h3>Pros</h3><ul><li><strong>Readers never see a missing key for written data</strong> — no post-write miss window, which eliminates the invalidation-triggered thundering herd entirely. This is the strategy’s killer feature for hot, frequently written, frequently read keys.</li><li><strong>Cache is as fresh as the last write.</strong> The staleness window shrinks to the milliseconds between DB commit and cache write — for most purposes, effectively zero.</li><li><strong>Predictable read latency</strong>, since reads of recently written data are always hits.</li></ul><h3>Cons</h3><ul><li><strong>Every write pays a double cost</strong> — DB write plus cache write, synchronously, in the caller’s latency budget. For write-heavy workloads, this adds up.</li><li><strong>Wasted work on write-heavy, read-light data.</strong> If a value is written 100 times and read once, you did 99 pointless cache writes. Write-through assumes what you write will be read soon — when that assumption fails, it’s pure overhead.</li><li><strong>Cache fills with written-but-never-read data</strong>, competing for memory with genuinely hot entries (mitigated by pairing with a TTL and a sane eviction policy, but it’s working against you).</li><li><strong>The two-system atomicity problem doesn’t vanish.</strong> DB commit succeeds, then the process crashes before the cache write: cache is now stale with no invalidation coming. Robust implementations back this with a TTL safety net or a change-data-capture (CDC) pipeline that repairs the cache asynchronously from the database’s replication log.</li></ul><h3>Industry usage</h3><p>Common wherever read-your-own-writes freshness matters on hot keys: inventory and availability systems, account balances, session state, feature-flag distribution. DAX implements write-through natively for DynamoDB. It’s also the standard prescription for fixing invalidation-stampede problems: replace “delete on write” with “write on write” and the empty-key window disappears.</p><h3>4. Write-Behind (Write-Back)</h3><h3>How it works</h3><p>The write path inverts completely: <strong>the application writes to the cache only, acknowledges immediately, and a background process asynchronously flushes accumulated writes to the database</strong> — batched, coalesced, on an interval or a queue.</p><p><strong>Write path:</strong></p><ol><li>Application writes to the cache. Acknowledged. Done, from the caller’s perspective.</li><li>Asynchronously — milliseconds to seconds later — a flusher drains the pending writes to the database, often batching many into one statement and coalescing repeated writes to the same key into just the final value.</li></ol><h3>Example</h3><p>A view-counter for articles. Every page view does INCR views:article:789 in Redis — microseconds, no DB involvement. Every 10 seconds, a background job reads the accumulated deltas and applies them to Postgres in one batched UPDATE. Ten thousand views become one database write. The counter in Postgres runs a few seconds behind reality, and nobody cares.</p><h3>Pros</h3><ul><li><strong>Write latency drops to cache speed</strong> — sub-millisecond acknowledgment, no matter how slow the database is.</li><li><strong>Massive write compression.</strong> Batching turns N writes into one round trip; coalescing turns N updates to the same key into one. For high-frequency counters, metrics, and rapidly-updated state, this can reduce database write load by orders of magnitude.</li><li><strong>Absorbs write spikes</strong> the database couldn’t handle directly — the cache acts as a shock absorber, and the flusher drains at a sustainable pace.</li></ul><h3>Cons</h3><ul><li><strong>You can lose acknowledged writes.</strong> This is the defining con and it’s not subtle: if the cache node dies between acknowledgment and flush, every unflushed write is gone — and you <em>told the caller it succeeded</em>. This is categorically different from every other strategy’s failure modes, which produce staleness; this one produces data loss.</li><li><strong>The database is now behind the cache</strong>, so anything reading the DB directly — analytics, other services, backups — sees a lagging view. The cache has quietly become the real source of truth for recent writes, which inverts the usual architecture and everything that assumes it.</li><li><strong>Complexity is substantial:</strong> you now own a flusher, its failure handling, retry logic, ordering guarantees, and what happens when the DB rejects a write that the caller was told succeeded seconds ago (there’s no caller left to return the error to).</li><li><strong>Mitigations exist but change the architecture:</strong> Redis persistence (AOF) reduces the loss window but doesn’t eliminate it; putting a durable queue (Kafka) between cache and DB gives real durability but at that point you’ve built a different system.</li></ul><h3>Industry usage</h3><p>Reserved for cases where write volume is extreme and individual-write loss is tolerable or recoverable: page-view and like counters, metrics aggregation, leaderboard updates, IoT telemetry buffering. It is deliberately <em>avoided</em> for anything transactional — orders, payments, inventory — where “we lost some acknowledged writes” is an incident, not a footnote. Notably, this pattern is standard <em>inside</em> databases themselves (every serious DB acknowledges writes from a memory buffer backed by a write-ahead log, flushing pages to disk lazily) — the difference is they pair it with a durable log, which is exactly the component most application-level write-behind setups skip.</p><h3>5. Write-Around</h3><h3>How it works</h3><p>The simplest write strategy: <strong>writes go to the database only, bypassing the cache entirely.</strong> The cache is populated exclusively by the read path (cache-aside or read-through). If the written key already exists in cache, you must still invalidate it — otherwise readers see the old value until TTL.</p><p><strong>Write path:</strong></p><ol><li>Application writes to the database.</li><li>Optionally (necessarily, if the key might be cached): delete the cache key.</li><li>The cache learns the new value only if and when someone reads it.</li></ol><h3>Example</h3><p>A logging or audit-trail service: events are written constantly, but any individual event is read rarely — maybe during an investigation weeks later. Writing each event through to the cache would flood it with data that will almost never be read. Instead, writes go straight to the store, and the cache fills only with the events investigators actually pull up.</p><h3>Pros</h3><ul><li><strong>No cache pollution from write traffic.</strong> Cache memory is spent only on data with demonstrated read demand.</li><li><strong>Cheapest possible write path</strong> — one system touched instead of two.</li><li><strong>Pairs naturally with cache-aside</strong>, and in fact “cache-aside with delete-on-write” and “write-around plus invalidation” are describing nearly the same system from two angles.</li></ul><h3>Cons</h3><ul><li><strong>Recently written data is always a cache miss.</strong> If your workload reads what it just wrote (a user edits their profile and immediately views it), every such read pays the miss penalty — the exact opposite of write-through’s guarantee.</li><li><strong>Invalidation is still your problem.</strong> Write-around doesn’t exempt you from deleting stale keys; forgetting that is the classic source of “I updated it but I still see the old value” bugs.</li></ul><h3>Industry usage</h3><p>The default for write-heavy, read-light data: logs, audit trails, archival records, bulk imports. Also the implicit strategy of most cache-aside deployments — many teams run write-around without ever naming it, because “write to the DB and delete the cache key” is just what cache-aside prescribes.</p><h3>6. Refresh-Ahead</h3><h3>How it works</h3><p>A read-side optimization layered on top of the others: <strong>the cache proactively refreshes entries before they expire</strong>, based on access patterns, so hot keys never expire in the face of active traffic.</p><p><strong>Mechanics:</strong> if a key with a 60-second TTL is accessed after, say, 75% of its lifetime has elapsed (t &gt; 45s), the cache returns the current value immediately <em>and</em> triggers an asynchronous background reload from the database. The TTL resets on completion. Frequently accessed keys therefore stay perpetually warm; keys nobody reads are allowed to expire naturally.</p><p>A probabilistic variant — each request near expiry has a small, increasing chance of triggering the refresh — additionally desynchronizes refreshes across keys and servers, which is the standard defense against the <em>cache avalanche</em> (mass simultaneous expiry of keys warmed together with identical TTLs).</p><h3>Example</h3><p>A homepage that displays trending products, backed by an expensive 800ms aggregation query, cached for 60 seconds. Without refresh-ahead, once per minute at least one unlucky request eats the 800ms miss — and without stampede protection, every concurrent request in that window does. With refresh-ahead, a request at t=50s gets the cached value instantly and kicks off the recomputation in the background; by expiry the fresh value is already in place. No user ever waits on the aggregation.</p><pre>TTL = 60s, refresh threshold = 45s (75% of lifetime)</pre><pre>0s ────────────────────── 45s ──── 50s ──────────── 60s ──►<br>SET key (fresh)            │        │                 │<br>                       threshold    read arrives      old expiry<br>                                    ├─ serve cached value (instant)<br>                                    └─ trigger async reload (800ms)<br>                                             │<br>                                       reload done → value replaced,<br>                                                     TTL reset<br>User-facing latency: always a cache hit.<br>The 800ms recompute runs where no user is waiting.</pre><h3>Pros</h3><ul><li><strong>Eliminates miss latency for hot keys</strong> — the miss penalty is paid by a background job, never by a user request.</li><li><strong>Smooths database load</strong>: refreshes are spread over time instead of clustering in expiry-triggered bursts.</li><li><strong>Directly counters the avalanche failure mode</strong> when jittered.</li></ul><h3>Cons</h3><ul><li><strong>Wasted refreshes on mispredicted keys.</strong> A key accessed once at t=46s and never again still triggers a full recomputation. If access patterns are erratic, you burn database capacity refreshing data nobody comes back for.</li><li><strong>Only helps expiry-driven misses.</strong> Event-driven invalidation (inventory hits zero, key deleted <em>now</em>) happens outside any TTL schedule; refresh-ahead has nothing to say about it.</li><li><strong>Adds moving parts:</strong> background refresh scheduling, concurrency limits on refresh jobs, and handling refresh failures (serve the stale value? for how long?).</li></ul><h3>Industry usage</h3><p>Common for expensive-to-compute, frequently read, tolerably stale data: homepage aggregations, recommendation lists, exchange rates, configuration blobs. Built into several caching products natively (Oracle Coherence popularized the term; Caffeine offers it as refreshAfterWrite). Also standard practice at CDN layer — stale-while-revalidate in HTTP caching is precisely refresh-ahead: serve the stale response now, revalidate in the background.</p><h3>Choosing: The Two-Question Framework</h3><p>Before the framework, the three write strategies side by side — the differences are entirely about <em>what is acknowledged when</em>, and <em>who carries the risk</em>:</p><pre>WRITE-THROUGH             WRITE-BEHIND                WRITE-AROUND</pre><pre>App                       App                         App<br> │ (1) write DB   ✓        │ (1) write Cache  ✓        │ (1) write DB  ✓<br> │ (2) write Cache         │     ack NOW               │ (2) DEL cache key<br> │ (3) ack                 ▼                           │ (3) ack<br> ▼                        (async, later)               ▼<br>DB is truth;              Flusher batches →  DB       Cache learns value<br>cache mirrors it                                      only on next read</pre><pre>ack after:  DB + cache    ack after:  cache only      ack after:  DB only<br>staleness:  ~0            DB lags behind cache        fresh write = miss<br>extra risk: none          LOSS of acked writes        extra risk: none<br>                          if cache dies pre-flush</pre><p>Strip away the terminology and the decision comes down to the two questions from the top, applied to <em>each category of data</em> (not to your system as a whole):</p><p><strong>Question 1 — On a read miss, who fetches?</strong></p><ul><li>Application code → cache-aside</li><li>The cache layer → read-through</li><li>Nobody, because we refreshed before expiry → refresh-ahead (layered on either)</li></ul><p><strong>Question 2 — On a write, what happens to the cache?</strong></p><ul><li>Key gets deleted; next read repopulates → cache-aside invalidation / write-around</li><li>Key gets updated synchronously, DB first → write-through</li><li>Cache takes the write; DB catches up later → write-behind</li></ul><p>Then stress-test the choice against the properties of the data:</p><p>PropertyPushes you towardRead-heavy, staleness cheapCache-aside + TTL (simplest thing that works)Read-heavy, staleness expensive, hot keysWrite-through (no empty-key window, no stampede)Write-heavy, read-lightWrite-around (don’t pollute the cache)Write volume extreme, loss tolerableWrite-behind (batch and absorb)Expensive computation, predictable accessRefresh-ahead on top</p><h3>What Industry Actually Standardizes On</h3><p>Here’s the honest summary, because “it depends” is true but unsatisfying:</p><p><strong>The industry default is cache-aside with delete-on-write invalidation and a TTL safety net.</strong> It’s what the overwhelming majority of Redis and Memcached deployments run, it’s what the major engineering-blog architectures describe, and it’s the pattern Facebook scaled to billions of users — choosing to engineer around its known races (with leases and invalidation pipelines) rather than abandon it. Its dominance comes from three properties that matter more in production than elegance: the database remains unambiguously the source of truth, cache failure degrades to slowness rather than outage, and the pattern requires zero special integration between cache and store.</p><p>Around that default, the industry applies the other strategies <em>surgically</em>, per data category, within the same system:</p><ul><li><strong>Write-through</strong> for the specific keys where post-write freshness and stampede-avoidance justify the write cost — inventory, balances, sessions.</li><li><strong>Write-around</strong> implicitly for everything write-heavy and read-light — which is most log-shaped data.</li><li><strong>Write-behind</strong> narrowly, for counter/metric-shaped workloads where loss is tolerable — and increasingly with a durable queue underneath, at which point it’s really a streaming pipeline.</li><li><strong>Refresh-ahead / stale-while-revalidate</strong> at the edges — CDNs, expensive aggregations — where it’s cheap insurance against latency spikes.</li></ul><p>The mature architecture isn’t one strategy; it’s cache-aside as the backbone with deliberate, documented exceptions. If there’s a single takeaway worth carrying into a design discussion, it’s this: don’t ask “which caching strategy should our system use?” Ask “for <em>this</em> data, what happens if the cached value is wrong, and who should pay the cost of keeping it right — the reader, the writer, or a background process?” The strategy falls out of the answer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cadfafdd378" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Spent Years on Python and Java.]]></title>
            <link>https://medium.com/@chsharma23051998/i-spent-years-on-python-and-java-b2d34235fde9?source=rss-4f0e08f85624------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2d34235fde9</guid>
            <category><![CDATA[gateway]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[fastify]]></category>
            <category><![CDATA[rags]]></category>
            <dc:creator><![CDATA[Chaitty]]></dc:creator>
            <pubDate>Tue, 30 Jun 2026 16:29:04 GMT</pubDate>
            <atom:updated>2026-06-30T16:31:48.837Z</atom:updated>
            <content:encoded><![CDATA[<h3>I Spent Years on Python and Java. A RAG Gateway Made Me Use Fastify — And My Architecture Reading Made Me Confident About It.</h3><blockquote>I’d never seriously touched the Node ecosystem. Python and Java covered everything I built. Then I started building a gateway for a RAG system and ended up fully on <em>Fastify</em>. The switch itself isn’t the interesting part — what’s interesting is that I’d recently spent months deliberately studying software architecture concepts, somewhat academically, and this was the first time I watched that reading actually do something instead of just sitting there as theory. I want to walk through the decision the way I’d want a more experienced architect to walk me through one: not just “what I picked,” but which concept did the work at each step.</blockquote><h3>Step one: name the quality attribute before naming the technology</h3><p>A habit I picked up from that reading was: before reaching for any technology, write down what quality attribute actually matters for this component. Not “what’s fast” or “what’s good” in the abstract — name the specific -ility you’re optimising, because different attributes pull you toward different architectures.</p><p>For the gateway, the attribute was throughput under concurrent I/O wait, not computational speed. The gateway barely computes anything — it takes a request, fans it out to an embedding service, a vector store, a reranker, and an LLM endpoint, waits on all of them, and stitches the response together. Almost the entire request lifecycle is the system doing nothing but waiting on a network call to return.</p><p>That’s a different problem than “make this code run fast,” and naming it precisely is what made the next step possible.</p><h3>Step two: match the concurrency model to the actual bottleneck</h3><p>Python and Java handle concurrency with thread-per-request (or process-per-request). Each request holds a thread for its entire lifetime, including all the time it spends waiting. That’s a fine model when requests are doing real work. It’s a bad fit when requests are mostly idle, waiting on something else to respond — you’re paying thread overhead (memory, context-switching) for time spent doing nothing.</p><p>Node’s event loop is built for exactly the bottleneck I’d named: I/O wait, not computation. A waiting request doesn’t block the loop; the loop just services other pending work and comes back when the response lands. This is a textbook case of a known architectural trade-off — thread-per-request vs. event-loop concurrency — and once I’d correctly named the quality attribute in step one, the concurrency model basically picked itself. That’s the part I’d have gotten wrong by instinct alone a year ago: I’d have defaulted to “Python is what I know” instead of asking what the bottleneck actually was first.</p><h3>Step three: treat the framework choice as an ADR, not a default</h3><p>Once Node made sense, the lazy move was Express — most tutorials, most Stack Overflow answers, the path of least resistance. Writing actual Architecture Decision Records for a while changed how I approached this. An ADR forces you to write down the alternatives you rejected and <em>why</em>, not just the choice you made. So instead of “Express because it’s popular,” I made myself answer: what does this gateway specifically need that a generic default doesn’t give me?</p><p>Two things came out of that exercise:</p><p><strong>Schema-enforced contracts at the boundary.</strong> This gateway sits between a frontend and several backend services — every request and response crossing that line needed a contract, not just a convention everyone hopefully follows. Fastify lets every route declare a JSON Schema for its request and response, compiled ahead of time into a validator and serializer. That’s the closest thing Node has to Pydantic models or Java’s Bean Validation, and it meant the contract was enforced by the framework, not maintained by discipline.</p><p><strong>Encapsulation over a global default.</strong> Express middleware is global by default — register something and it applies everywhere unless you’re careful, which tends to decay into a pile of middleware nobody fully understands a few months in. Fastify’s plugin system gives each plugin its own scope, closer to how a well-bounded module stays contained than to Express’s flatter middleware stack. That’s a small thing, but it’s the same instinct DDD’s bounded-context thinking gave me elsewhere: keep things from leaking into each other by default, not by convention.</p><p>Writing this out as if it were an ADR — alternatives considered, trade-off, decision — is what made me confident in the call instead of just going with a hunch.</p><h3>Step four: draw the boundary, don’t dissolve it</h3><p>The thing I’m proudest of isn’t “I moved to Fastify.” It’s where I stopped. The actual RAG logic — embeddings, retrieval scoring, anything compute-heavy — has no business sitting on a single-threaded event loop, and I didn’t try to drag it there. Fastify owns the gateway: the layer that’s mostly traffic. Python still owns the layer that’s mostly computation.</p><p>That’s a bounded context, in the literal DDD sense — a clear line around what belongs on each side, justified by what each side is actually optimized for, not by “let’s use one language everywhere” or “let’s use what I already know everywhere.” Drawing that line deliberately, instead of letting the codebase sprawl into whichever language was nearest, is the actual skill I was practicing. Fastify was just the artifact it produced.</p><h3>The honest takeaway</h3><p>I didn’t switch frameworks because I read a few books on architecture and Fastify happened to be the answer. I switched because the habits from that reading — name the quality attribute first, treat framework choice as a decision worth writing down, draw boundaries on purpose — gave me a process that led somewhere specific instead of somewhere familiar. The technology is incidental. The process is the part worth keeping for the next decision, whatever it turns out to be.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2d34235fde9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Skipped LangChain and Built an AI Agent From Scratch First. Here’s Why That Was the Right Call.]]></title>
            <link>https://medium.com/@chsharma23051998/i-skipped-langchain-and-built-an-ai-agent-from-scratch-first-heres-why-that-was-the-right-call-7f395136d920?source=rss-4f0e08f85624------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f395136d920</guid>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[learn]]></category>
            <dc:creator><![CDATA[Chaitty]]></dc:creator>
            <pubDate>Mon, 29 Jun 2026 09:33:43 GMT</pubDate>
            <atom:updated>2026-06-29T09:33:43.393Z</atom:updated>
            <content:encoded><![CDATA[<p>A few weeks ago I sat down to actually learn agentic AI — not skim a blog post about it, but build something real and understand what’s happening underneath.</p><p>I’d been putting this off for a while, honestly out of a quiet sense of being left behind. Every other week there was a new tool, a new framework, a new “AI agent built this in 5 minutes” video, and the gap between knowing the buzzwords and actually being able to build anything felt like it was getting wider, not narrower. So I did what most people do: I went looking for a course. I queued up tutorials, bookmarked YouTube channels, signed up for things. For a few days I’d watch someone explain transformers or walk through a LangChain demo, nod along, feel like I understood it — and then open my laptop and realise I couldn’t actually build anything myself. The knowledge wasn’t sticking because I wasn’t doing anything with it.</p><p>That’s where I dropped off the first time. Watching without building turned into watching less, then not at all, then feeling guilty about the backlog of unwatched videos, which made the whole topic feel heavier than it needed to be. A familiar trap.</p><p>The thing that got me unstuck wasn’t a better video. It was deciding to stop consuming and just write code, even bad code, from the very first session. The obvious move at that point was still to install LangChain, follow a quickstart, and have a “working agent” in twenty minutes.</p><p>I didn’t do that either. I wrote raw Python, made raw HTTP calls to a local model, and built the agent loop myself — tool definitions, tool selection, the works — before touching a single framework. Here’s why, and what I’d tell anyone considering the same trade.</p><h3>The trap of “working” code</h3><p>Frameworks are optimized to get you to a demo fast. That’s their entire value proposition, and it’s a real one — for shipping. But when the goal is <em>understanding</em>, fast is exactly the wrong axis to optimize for.</p><p>If I’d started with LangChain, I would have had an agent answering questions in under an hour. I also would have had almost no idea why it worked. The framework hides the mechanism on purpose — that’s what abstraction means. You get the result without the model.</p><p>For someone already comfortable writing production code, that’s a bad trade. I don’t need help getting <em>something</em> working. I need to know what breaks, and why, when it inevitably does.</p><h3>What “from scratch” actually taught me</h3><p>The single most useful thing I learned wasn’t a library or an API — it was understanding that <strong>the messages list is the agent.</strong> Not a log of what happened. Not a side effect of the conversation. It is the entire state. If a fact isn’t in that list, it does not exist for the model, full stop. The model has no memory beyond it.</p><p>That sounds obvious written down. It was not obvious to me until I’d built the loop by hand: send messages, model decides to call a tool, you run the tool yourself, you append the result back into the list, you send it again. Doing that wiring manually — three tools, one simple agent loop, watching it choose the wrong tool occasionally and silently hallucinate after a tool error — taught me more about failure modes in twenty lines of raw code than any framework tutorial would have, because the framework would have caught and smoothed over exactly the failures I needed to see.</p><p>Then I added persistent memory — just writing that messages list to disk and reloading it — and immediately ran into the next real problem: that file grows forever. Every conversation makes it bigger, until it eats your context window and the model’s behavior degrades. That’s not a bug. That’s a fundamental constraint of how these systems work, and frameworks deal with it for you via retrieval and chunking strategies you’ll use eventually anyway. But now I know <em>why</em> those strategies exist, instead of just knowing they’re “best practice.”</p><h3>Where I’d push back on myself</h3><p>This isn’t a framework-bashing post, and I want to be honest about the limits of the build-it-raw approach.</p><p>Raw Python doesn’t scale to a real system. The moment you need multi-agent coordination, retries, streaming, or observability across a dozen tool calls, you are reinventing wheels that LangGraph or similar tools already built correctly, with edge cases you haven’t thought of yet. I’m not planning to hand-roll a production system. The point was never “never use a framework.” The point was: understand the thing the framework is abstracting <em>before</em> you let it abstract it for you.</p><p>There’s also a real cost in time. Building raw is slower, by design. If your job pressure is “ship an agent by Friday,” this approach is the wrong call and you should reach for the framework. I had the luxury of treating this as deliberate skill-building rather than a deliverable, and that’s what made the trade worth it.</p><h3>Why this matters beyond just “getting it working”</h3><p>It’s easy to get away with knowing how to use a tool without knowing how it works. Most of the time that’s fine. But the moment something breaks in a way the framework doesn’t explain — or you need to judge whether a new tool is actually solving your problem or just hiding it — “I know how to call the library” stops being enough. You need to know what’s happening when the abstraction leaks, and abstractions always leak eventually.</p><p>The people I respect most when it comes to using new technology well aren’t the ones who’ve memorised the most tools. They’re the ones who understand the primitives well enough to know when a tool is the right call and when it’s quietly creating a problem they’ll inherit later. That’s the bar I’m building toward, one deliberately slow week at a time.</p><p>If you’re weighing whether to learn agentic AI through a framework or from the ground up: it depends entirely on what you’re optimising for. If you need output fast, use the framework. If you want real judgement, build it raw first — even just once.</p><p>And if you’re sitting where I was a few weeks ago — bookmarking videos, feeling like everyone else already gets it, not quite starting — the fastest way out isn’t a better course. It’s closing the tabs and writing the first ugly version of the thing yourself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f395136d920" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>