<?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 Rahul Jindal on Medium]]></title>
        <description><![CDATA[Stories by Rahul Jindal on Medium]]></description>
        <link>https://medium.com/@rahul.jindal57?source=rss-2b5295e46a98------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vmaOM1KH-8h5EnETaOVEQQ.jpeg</url>
            <title>Stories by Rahul Jindal on Medium</title>
            <link>https://medium.com/@rahul.jindal57?source=rss-2b5295e46a98------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:09:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahul.jindal57/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[Stop Guessing How Your Recursion Works — Watch It]]></title>
            <link>https://medium.com/@rahul.jindal57/stop-guessing-how-your-recursion-works-watch-it-afca55218eaa?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/afca55218eaa</guid>
            <category><![CDATA[competitive-programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[beginner]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sat, 18 Jul 2026 10:28:10 GMT</pubDate>
            <atom:updated>2026-07-23T20:06:51.157Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Introducing Code Visualiser, a tool that turns JavaScript execution into something you can actually see.</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_KhDkyDSTA3Ks6EksRVcyg.png" /><figcaption><a href="https://code-visualiser-vert.vercel.app/">https://code-visualiser-vert.vercel.app/</a></figcaption></figure><p>Every developer hits this wall. You write a recursive function, it returns the wrong answer, and you’re left staring at the code adding console.log after console.log, trying to reconstruct in your head the tangle of calls the machine ran in milliseconds. Recursion and dynamic programming are hard not because the ideas are complicated, but because the <em>execution </em>is invisible.</p><p><a href="https://code-visualiser-vert.vercel.app/"><strong>Code Visualiser</strong></a><strong> makes it visible.</strong></p><h3>See how your code actually runs</h3><p>Paste in a JavaScript function, hit run, and watch the call tree unfold node by node. Every recursive call becomes a branch. Every return value flows back up. Instead of imagining the stack, you <em>see</em> it — the exact shape of the computation, laid out in front of you.</p><p>Then step through it, call by call:</p><p><strong>Walk the execution</strong> forward and back, at your own pace</p><p><strong>Inspect variables</strong> at every step, so you know exactly what state the program was in</p><p><strong>Set breakpoints</strong> and jump straight to the moment things get interesting</p><p>It’s the mental model you’ve always built by hand — the boxes, the arrows, the “okay so <em>this</em> call returns 3, which means…” — except the tool draws it for you, correctly, every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TjG7U_OeykbOC9sXtBAi1g.png" /><figcaption><a href="https://code-visualiser-vert.vercel.app/visualiser">https://code-visualiser-vert.vercel.app/visualiser</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kl36Uu4KfdEruxVVmj2Lg.png" /><figcaption><a href="https://code-visualiser-vert.vercel.app/visualize">https://code-visualiser-vert.vercel.app/visualize</a></figcaption></figure><h3>Perfect for learning (and teaching)</h3><p>If you’re wrestling with recursion, backtracking, or DP for the first time, there’s no faster way to build intuition than watching a real call tree grow. And if you’re teaching, it beats a whiteboard: your diagram is guaranteed to match what the code truly does.</p><h4>Runs entirely in your browser — nothing uploaded</h4><p>Your code runs in a sandboxed Web Worker right on your machine. Nothing is sent to a server, nothing is stored, nothing leaves the tab.</p><p>There’s even a built-in <strong>sketch whiteboard</strong> for the diagrams you’d otherwise scribble on paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FFBIVoYgHOvEcbEy0p1NNA.png" /><figcaption><a href="https://code-visualiser-vert.vercel.app/scritch">https://code-visualiser-vert.vercel.app/scritch</a></figcaption></figure><h3>Try it</h3><p>No sign-up, no install, no upload. Just open it and paste some code. Provide your valuable feedback to improve it:</p><p>👉 <a href="https://code-visualiser-vert.vercel.app/"><strong>https://code-visualiser-vert.vercel.app/</strong></a></p><p>Stop tracing recursion in your head. Watch it run.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afca55218eaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AI-Assisted Coding: My Real Experience After the Initial Hype]]></title>
            <link>https://medium.com/@rahul.jindal57/ai-assisted-coding-my-real-experience-after-the-initial-hype-2840e18c4dce?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/2840e18c4dce</guid>
            <category><![CDATA[ai-assistant]]></category>
            <category><![CDATA[experience]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Mon, 18 May 2026 05:37:31 GMT</pubDate>
            <atom:updated>2026-05-18T05:37:31.682Z</atom:updated>
            <content:encoded><![CDATA[<h4>The tradeoff between development speed, system ownership, and long-term maintainability</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*49Znu9xofsi6uYHR" /><figcaption>Photo by <a href="https://unsplash.com/@steve_j?utm_source=medium&amp;utm_medium=referral">Steve A Johnson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The Initial Excitement Felt Unreal</h3><p>Over the past few months, I have been using AI very actively in my development workflow. Not just for experiments or toy projects, but for actual day-to-day engineering work involving feature development, debugging, testing, scripts, refactors, and architectural exploration.</p><p>In real production systems though, my usage stayed fairly controlled. I was careful about where AI-generated code was allowed, how much I trusted it, and what parts still required manual implementation or deeper review.</p><p>But outside production work, especially in personal and hobby projects, I started experimenting much more aggressively. That is where I really began trying to maximize acceleration.</p><p>Like most developers, my initial reaction was pure excitement.</p><p>The first few weeks honestly felt unreal. Tasks that previously required hours of repetitive effort suddenly became almost frictionless. I could scaffold APIs quickly, generate tests in seconds, create utility scripts instantly, and remove large amounts of boilerplate with very little effort.</p><p>For some time, it genuinely felt like development itself had changed permanently.</p><p>I wanted to see how far this could go. How much faster could software development become if implementation itself stopped being the bottleneck?</p><p>But after sustained usage on real projects, I slowly started noticing a tradeoff that completely changed how I think about AI-assisted development.</p><h3>AI Increased Speed, But Reduced System Ownership</h3><p>The biggest shift I noticed was not related to code quality alone. It was related to ownership.</p><p>Earlier, engineering had a natural rhythm. We designed, implemented, debugged, and gradually built familiarity with the system through the process of writing the code ourselves. That process naturally created architectural memory.</p><p>AI changes that rhythm completely.</p><p>Entire flows, abstractions, and helper layers can appear instantly before we fully process their long-term implications. The code may still work correctly, but understanding does not always scale at the same speed as generation.</p><p>Externally, everything still looked productive. Internally though, architectural clarity slowly started degrading.</p><p>Over time, the codebase started accumulating:</p><ul><li>Oversized files</li><li>Duplicate helpers</li><li>Inconsistent patterns</li><li>Partially migrated logic</li><li>Unclear boundaries</li><li>Unnecessary abstractions</li></ul><p>Some abstractions technically worked, but they existed because AI generated them, not because they were intentionally designed.</p><p>I also realized that reviewing large amounts of generated code is often more cognitively exhausting than manually writing smaller amounts myself. Writing naturally creates ownership, context, and memory formation. Reviewing does not always create the same depth of understanding.</p><p>At one point, I noticed something uncomfortable: <em>I no longer felt like the primary author of the system.</em></p><p>I felt more like an auditor reviewing generated output.</p><h3>I Realized I Did Not Actually Want Maximum Speed</h3><p>After pushing AI-assisted development quite aggressively, I eventually realized that maximum speed was not actually the goal.</p><p>In some of the personal projects I built heavily with AI assistance, I slowly lost the sense of intimacy with the codebase. I could still navigate the project and make changes, but the strong mental connection with the implementation was no longer there in the same way as before.</p><p>And surprisingly, that also started affecting my long-term interest in maintaining some of those projects.</p><p>Because large portions of the implementation appeared extremely quickly, parts of the system gradually started feeling less personal and less intellectually connected to me. The project worked, but the engineering journey behind it felt compressed.</p><p>That realization also made me think more carefully about sustainability.</p><p>In real production systems, long-term maintainability matters far more than raw implementation speed. Engineering is not just about producing working code quickly. It is also about preserving clarity, predictability, and long-term ownership across a system that may evolve for years.</p><p>That is when I stopped optimizing for maximum acceleration.</p><p>What feels significantly more practical to me now is achieving moderate acceleration while still preserving deep understanding of the system. Even a 20–30% productivity improvement is already enormously valuable if it removes repetitive work without disconnecting developers from the architecture they are building.</p><p>That balance feels far more sustainable than fully autonomous generation.</p><h3>AI Is Incredible For Some Things</h3><p>Despite all these concerns, there are areas where AI genuinely feels transformative.</p><p>And interestingly, these are mostly areas where the work is bounded, repetitive, or locally scoped.</p><p>Scripts are probably one of the highest ROI use cases for AI coding. Historically, many engineers avoid writing small automation scripts because the setup effort feels larger than the actual benefit. AI completely changes that equation. Tiny automations suddenly become practical because the implementation cost becomes negligible.</p><p>Testing is another area where AI genuinely shines. Repetitive test setup, mocks, flaky test fixes, edge case generation, and boilerplate-heavy testing workflows become significantly easier without sacrificing architectural understanding.</p><p>Debugging also becomes extremely powerful when I originally authored the underlying system myself. Because I already understand the architectural intent, domain assumptions, and historical tradeoffs, AI starts behaving more like an intelligent debugging partner rather than an autonomous problem solver.</p><p>That distinction is important.</p><p>I found AI most useful when it amplifies existing understanding rather than replacing it.</p><h3>My Workflow Eventually Changed Completely</h3><p>Over time, I gradually converged toward a workflow that feels much healthier and more sustainable.</p><p>Today, I keep architecture, planning, decomposition, and system reasoning fully human-owned.</p><p>Before touching AI, I think carefully about the feature itself:</p><ul><li>What problem are we solving?</li><li>What boundaries should exist?</li><li>What should remain simple?</li><li>What are the long-term maintenance implications?</li><li>Where should abstractions exist?</li><li>What should explicitly not be abstracted?</li></ul><p>Only after that do I bring AI into the process.</p><p>And even then, I mostly use it for acceleration rather than replacement:</p><ul><li>Repetitive implementation</li><li>Tests</li><li>Utility generation</li><li>Small scoped tasks</li><li>Refactors with clearly defined boundaries</li></ul><p>I also review generated changes extremely carefully.</p><p>Every abstraction, helper, side effect, and utility gets read line by line because I realized that the moment developers stop deeply reviewing generated code, they slowly lose authorship over the system itself.</p><p>And once authorship disappears, maintainability starts degrading surprisingly quickly.</p><p>Interestingly, I also noticed that writing a meaningful portion of the system manually helps preserve familiarity with the architecture. Today I still prefer implementing core logic and important flows myself while letting AI assist around those areas.</p><p>That balance feels significantly healthier than treating AI as an autonomous software generator.</p><h3>Final Thoughts</h3><p>I no longer think the future of engineering is about maximizing generation speed at all costs.</p><p>The more I used AI aggressively, the more I realized that good engineering still depends heavily on human judgment:</p><ul><li>Understanding tradeoffs</li><li>Maintaining architectural clarity</li><li>Preserving system ownership</li><li>Knowing when simplicity matters more than abstraction</li><li>Recognizing long-term consequences beyond immediate output</li></ul><p>AI is genuinely one of the most powerful productivity tools I have used in software development. But I also think there is a meaningful difference between accelerating engineering and replacing the engineering process itself.</p><p>Right now, the most sustainable approach for me is not fully autonomous generation.</p><p>It is intentional acceleration.</p><p>Using AI to remove low-value repetitive effort while deliberately preserving the parts of software development that create deep understanding, system intuition, and long-term ownership.</p><p>Because in the end, writing software is not only about producing working code quickly.</p><p>It is also about building systems that we can confidently understand, evolve, and maintain years later.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2840e18c4dce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System Design Interview — Replication Part 3: Replication Lag and Read Consistency]]></title>
            <link>https://medium.com/@rahul.jindal57/system-design-interview-replication-part-3-replication-lag-and-read-consistency-7913a818bd31?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/7913a818bd31</guid>
            <category><![CDATA[consistency]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[backend-engineering]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sat, 07 Mar 2026 06:50:27 GMT</pubDate>
            <atom:updated>2026-03-07T06:50:27.883Z</atom:updated>
            <content:encoded><![CDATA[<h4>How replication lag affects reads and why consistency guarantees matter to users</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8IcGao8kcNNRah1t" /><figcaption>Photo by <a href="https://unsplash.com/@alinnnaaaa?utm_source=medium&amp;utm_medium=referral">Alina Grubnyak</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This blog is part of a replication series. If you haven’t read the previous article on operating leader-based replication, start there first.</p><p><a href="https://medium.com/@rahul.jindal57/system-design-interview-replication-part-2-operating-leader-based-replication-ff085681e7fb">System Design Interview: Replication Part 2 — Operating Leader-Based Replication</a></p><p>In previous articles, we explored leader-based replication and how followers stay synchronized using replication logs. In practice, replicas rarely stay perfectly in sync with the leader. Followers usually apply updates slightly later, creating a delay known as replication lag.</p><p>This delay is known as <strong>replication lag</strong>, and while it is usually measured in milliseconds or seconds, it can create confusing behaviors for users if not handled properly.</p><p>In this article, we will explore the problems replication lag introduces and the practical consistency guarantees that distributed systems implement to mitigate them.</p><h3>Table of Contents</h3><ol><li>Why Replication Lag Happens</li><li>Reading Your Own Writes</li><li>Monotonic Reads</li><li>Consistent Prefix Reads</li><li>Solutions for Replication Lag</li><li>References</li></ol><h3>1. Why Replication Lag Happens</h3><p>In leader-based replication, the leader records every change in a <strong>replication log</strong>, and follower nodes apply those changes to stay in sync. The behavior of the system depends largely on whether replication is <strong>synchronous</strong> or <strong>asynchronous</strong>.</p><p>With <strong>synchronous replication</strong>, the leader waits for replicas to acknowledge a write before responding to the client. This improves consistency but increases latency and can cause failures if replicas are slow or unavailable.</p><p>Because of this trade-off, many systems use <strong>asynchronous replication</strong>, where the leader commits the write immediately and followers replicate the changes afterward. While this improves performance and availability, it introduces <strong>replication lag</strong>.</p><p>Due to this lag, followers may temporarily serve <strong>outdated data</strong>, which can lead to confusing behaviors such as users not seeing their own updates or observing data that appears to move backward. The following sections discuss the common consistency issues caused by this lag and how systems handle them.</p><h3>2. Reading Your Own Writes</h3><p>A common issue occurs when users cannot immediately see their own updates.</p><p>Imagine a user updating their profile bio.</p><pre>UPDATE users<br>SET bio = &#39;Distributed systems enthusiast&#39;<br>WHERE id = 42;</pre><p>The update is written to the <strong>leader database</strong>.</p><p>Immediately after updating, the application fetches the user profile:</p><pre>SELECT bio FROM users WHERE id = 42;</pre><p>If this read request is routed to a <strong>replica</strong>, the replica might not yet have received the update.</p><p>Result:</p><pre>Old bio is returned</pre><p>From the user’s perspective, it feels like <strong>the update failed</strong>.</p><h4>Practical Solution: Read From Leader After Writes</h4><p>A common approach to solving this problem is <strong>read-after-write consistency</strong>, also known as <strong>read-your-writes consistency</strong>. The idea is simple: once a user performs a write, the system ensures that subsequent reads from that user return a version of the data that includes that write.</p><p>One practical way to implement this is by temporarily routing reads to the <strong>leader node</strong>, since the leader always has the most up-to-date state.</p><p>Example application logic:</p><pre>if user_recently_updated_profile:<br>    read_from_leader()<br>else:<br>    read_from_replica()</pre><p>This ensures the user immediately sees their change.</p><h4>Challenges</h4><ul><li><strong>Hot / frequently edited data: </strong>If many users are modifying the same data, forcing reads to the leader can overload it and reduce the scalability benefits of replicas.</li><li><strong>Distributed data centers: </strong>In multi-region systems, routing reads to the leader may introduce <strong>high latency</strong>, especially if the leader is located in a different region.</li><li><strong>Cross-device consistency:</strong> If a user writes from one device and reads from another, the second device may hit a lagging replica unless session state is shared.</li><li><strong>Tracking replication state: </strong>Systems must maintain metadata such as timestamps or replication log positions to determine whether a replica is safe to serve the read.</li></ul><h3>3. Monotonic Reads</h3><p>Another confusing situation happens when data appears to <strong>move backwards in time</strong>.</p><p>Consider a user reading a timeline where reads are served from multiple replicas.</p><p><strong>First request: </strong>The request is routed to <strong>Replica A</strong>, which has already caught up with the latest updates.</p><pre>Post 1<br>Post 2<br>Post 3<br>Post 4</pre><p>The user scrolls through the feed and sees the newest posts.</p><p><strong>Second request: </strong>When the user refreshes the timeline, the request is routed to <strong>Replica B</strong>, which is slightly behind in replication.</p><pre>Post 1<br>Post 2<br>Post 3</pre><p>Now <strong>Post 4 is missing</strong>, even though the user saw it earlier.</p><p>This makes the timeline appear to <strong>move backwards</strong>. The issue occurs because the user reads from replicas that are at different replication states.</p><p><strong>Monotonic reads</strong> guarantee that once a user has seen a particular version of the data, they will never observe an <strong>older version in subsequent reads</strong>, even if the requests are served by different replicas.</p><h4>Practical Solution: Replica Stickiness</h4><p>One common solution is <strong>replica stickiness</strong>, where a user’s session continues reading from the same replica.</p><p>Example routing: User session -&gt; Replica A</p><p>Since replication only moves forward, that replica will only return <strong>newer or equal data over time</strong>, preventing the user from observing older states.</p><p>In practice, this is often implemented at the <strong>load balancer or API gateway layer</strong>. The load balancer assigns a user session to a specific replica and ensures subsequent requests from that session are routed to the same replica.</p><p>For example, the load balancer may:</p><ul><li>Use a <strong>session cookie</strong> or request header to identify the user session.</li><li>Maintain <strong>sticky sessions</strong>, where the same client is consistently routed to the same backend replica.</li><li>Use <strong>consistent hashing</strong> based on a user ID or session ID to map requests to a specific replica.</li></ul><p>This approach keeps the routing logic outside the database and allows the system to enforce monotonic reads while still distributing traffic across multiple replicas.</p><h4>Another Approach: Replication Position Tracking</h4><p>Another approach is to track the <strong>replication log position</strong> that a user has already observed.</p><p>For example, in <strong>PostgreSQL</strong>, replicas replay WAL logs and expose their replication progress. Conceptually the system ensures:</p><pre>Replica replay position &gt;= user&#39;s last seen position</pre><p>This means that if a request is routed to another replica, the system first checks whether that replica has caught up to the state the user has already seen.</p><p>This approach provides stronger guarantees but is more complex to implement. The system must track the <strong>last observed replication position </strong>(such as an LSN) for each session and propagate it with requests. Before serving a read, the routing layer verifies that the selected replica has replayed logs up to that position. If not, the system must wait, choose another replica, or route the request to the leader.</p><h4>Challenges</h4><ul><li><strong>Replica failure: </strong>If the sticky replica fails, the system must switch to another replica while still maintaining monotonic guarantees.</li><li><strong>Load balancing trade-off:</strong> Strict replica stickiness can reduce load balancing flexibility.</li><li><strong>Session metadata tracking: </strong>The system needs to track information such as the user’s <strong>last observed replication position</strong>.</li></ul><p>Monotonic reads help ensure that users experience <strong>data progressing forward</strong>, rather than seeing information disappear temporarily when requests hit different replicas.</p><h3>4. Consistent Prefix Reads</h3><p>Another issue occurs when events appear in the wrong order.</p><p>Consider a messaging system where two messages are sent in sequence:</p><pre>Message 1:<br>&quot;Are you joining the meeting?&quot;<br><br>Message 2:<br>&quot;Yes, I will be there in 10 minutes.&quot;</pre><p>These writes are committed in this exact order on the <strong>leader</strong>.</p><p>However, when another user reads the conversation from replicas, they might observe:</p><pre>&quot;Yes, I will be there in 10 minutes.&quot;<br>&quot;Are you joining the meeting?&quot;</pre><p>The reply appears <strong>before the question</strong>, which creates a confusing experience. This violates the guarantee known as <strong>consistent prefix reads</strong>.</p><p><strong>Consistent prefix reads</strong> ensure that if a sequence of writes occurred in a particular order, any reader should observe them <strong>in that same order</strong>, even when reading from replicas.</p><h4>Why This Happens</h4><p>To scale horizontally, many distributed systems partition data across shards.</p><p>Example distribution:</p><pre>Shard 1 -&gt; Message 1<br>Shard 2 -&gt; Message 2</pre><p>Each shard maintains its own replication log and replicates changes independently.</p><p>If <strong>Shard 2 replicates faster than Shard 1</strong>, a replica might apply the second message before the first one, causing readers to observe events out of order.</p><h4>Practical Solution: Partition Related Data Together</h4><p>A common solution is to ensure <strong>causally related data is stored in the same partition</strong>.</p><p>Example partitioning strategy:</p><pre>partition_key = conversation_id</pre><p>All messages belonging to the same conversation are written to the <strong>same shard</strong>. Since writes in a shard are applied sequentially, the order of messages is preserved during replication.</p><p>This approach is widely used in systems like <strong>chat services, comment threads, and activity feeds</strong>.</p><h4>Another Approach: Causal Ordering</h4><p>Another approach is <strong>causal ordering</strong>, where the system tracks dependencies between writes.</p><p>Conceptually:</p><pre>Message 2 depends on Message 1</pre><p>Replicas apply operations only after their <strong>dependent operations have already been applied</strong>.</p><p>This can be implemented using mechanisms such as:</p><ul><li>logical timestamps</li><li>vector clocks</li><li>dependency metadata in replication logs</li></ul><h3>5. Solutions for Replication Lag</h3><p>Replication lag is a natural side effect of distributed systems. While it cannot be completely eliminated, production systems use several techniques to <strong>minimize its visible impact on users</strong>.</p><h4>Smart Read Routing</h4><p>One common approach is <strong>smart read routing</strong>, where the system decides whether a read should go to the leader or a replica based on the importance of the data.</p><p>Critical reads are often routed to the <strong>leader</strong> to guarantee the latest data.</p><p>Examples:</p><pre>Account balance<br>Payment processing<br>Profile updates</pre><p>Less critical reads can safely be served from <strong>replicas</strong>, allowing the system to scale read traffic.</p><p>Examples:</p><pre>Analytics dashboards<br>Recommendation feeds<br>Search results</pre><h4>Replica Lag Monitoring</h4><p>Production systems continuously monitor the <strong>replication delay</strong> of each replica.</p><p>Conceptually:</p><pre>Replica lag = leader_timestamp - replica_timestamp</pre><p>If a replica falls too far behind the leader, it may start returning significantly outdated data.</p><p>To prevent this, systems may temporarily <strong>remove that replica from the read pool</strong> until it catches up.</p><h4>Bounded Staleness</h4><p>Some systems enforce <strong>bounded staleness</strong>, where reads are allowed only from replicas that are within an acceptable lag threshold.</p><p>Example policy:</p><pre>replica lag &lt; 100ms</pre><p>If a replica exceeds this threshold, the system routes reads to another replica or falls back to the <strong>leader</strong>.</p><p>This approach allows systems to control how stale the data is allowed to be.</p><h4>Session Guarantees</h4><p>Many modern distributed databases provide <strong>session-level consistency guarantees</strong>.</p><p>Examples include:</p><pre>Read-your-writes<br>Monotonic reads</pre><p>These guarantees ensure that even though the system may be <strong>eventually consistent globally</strong>, an individual user experiences <strong>predictable and consistent behavior within their session</strong>.</p><h4>Caching to Mask Replication Lag</h4><p>Caching can also hide replication lag from users. When a write occurs, systems often update the cache immediately along with the database.</p><p>Example flow:</p><pre>Write -&gt; Leader Database<br>Write -&gt; Cache (Redis / Memory store)</pre><p>Subsequent reads can then be served from the <strong>cache</strong>, which already contains the latest data, even if replicas have not yet caught up.</p><p>This technique is widely used in large-scale systems to <strong>reduce database load while maintaining fresh reads for users</strong>.</p><h3>6. References</h3><ul><li>Martin Kleppmann, <em>Designing Data-Intensive Applications</em></li><li>Industry experience and distributed systems practices</li></ul><p>Replication lag is unavoidable in distributed systems. Instead of eliminating it, production systems manage its impact using techniques like smart routing, session guarantees, bounded staleness, and caching.</p><p>Understanding these trade-offs is essential when designing large-scale systems.</p><p>In the next part of this series, we will explore <strong>multi-leader replication</strong> and how systems support writes from multiple regions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7913a818bd31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System Design Interview: Replication Part 2 — Operating Leader-Based Replication]]></title>
            <link>https://medium.com/@rahul.jindal57/system-design-interview-replication-part-2-operating-leader-based-replication-ff085681e7fb?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff085681e7fb</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[database-replication]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[backend-engineering]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Wed, 04 Mar 2026 17:35:42 GMT</pubDate>
            <atom:updated>2026-03-07T06:51:13.096Z</atom:updated>
            <content:encoded><![CDATA[<h4>Managing replicas, handling failures, and understanding replication logs in production systems</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OUrgbHPWGc3Rvjv_" /><figcaption>Photo by <a href="https://unsplash.com/@fennings?utm_source=medium&amp;utm_medium=referral">Marc PEZIN</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This blog is part of a replication series. If you haven’t read the fundamentals yet, start here:</p><p><a href="https://medium.com/@rahul.jindal57/system-design-interview-replication-part-1-fundamentals-f0f4f887775e"><strong>System Design Interview: Replication Part 1 — Fundamentals</strong></a></p><p>In this part, we move from theory to operations — how replicas are actually added, how systems recover when nodes fail, and how replication logs propagate changes across the cluster.</p><h3>Table of Contents</h3><ol><li>Setting Up New Followers</li><li>Handling Node Outages</li><li>Implementation of Replication Logs</li><li>References</li></ol><h3>1. Setting Up New Followers</h3><p>In distributed databases, followers (replicas) are added for <strong>scalability</strong>, <strong>fault tolerance</strong>, or <strong>node replacement</strong>. While the idea sounds simple — “copy data from the leader” — the reality is more subtle.</p><p>The core challenge is that the database is still receiving writes while the copy is happening. A simple file copy would capture different parts of the dataset at different moments in time, producing an inconsistent replica.</p><p>Modern databases solve this using a <strong>snapshot + replication log catch-up</strong> approach.</p><h4>The General Pattern (Applies Across Databases)</h4><p>Regardless of implementation details, setting up a new follower follows the same logical sequence:</p><pre>Leader<br> | snapshot<br> v<br>Follower<br> | replay log<br> v<br>Caught up</pre><ol><li><strong>Take a consistent snapshot</strong> of the leader at a single point in time<br>1. No global write lock<br>2. Same mechanism used for backups</li><li><strong>Associate the snapshot with a log position<br></strong>1.<strong> </strong>This marks “what data is included”<br>2. Everything after this point must be replayed</li><li><strong>Copy the snapshot to the follower</strong></li><li><strong>Replay all changes after the snapshot</strong> using the replication log</li><li>Once fully replayed, the follower is said to have <strong>caught up</strong> and can start normal replication</li></ol><p>This is why simply copying files is never sufficient — we must also know <strong>where in the change stream the copy was taken.</strong></p><h4>How MongoDB Implements This</h4><p>In <strong>MongoDB</strong>, replication is implemented using a <strong>replica set</strong>:</p><ul><li><strong>Primary</strong> → leader node (accepts writes)</li><li><strong>Secondary</strong> → follower nodes</li><li><strong>Oplog</strong> → append-only replication log</li></ul><p>Even a <em>single MongoDB node</em> must run as a replica set if we ever want to add followers. This is because the <strong>oplog only exists in replica sets</strong>.</p><h4>Concept → Practice (Minimal MongoDB Example)</h4><p>Assume we start with <strong>one MongoDB instance</strong>.</p><p><strong>1. Single node becomes a leader</strong></p><p>We initialize a replica set with one member:</p><pre>rs.initiate({<br>  _id: &quot;rs0&quot;,<br>  members: [<br>    { _id: 0, host: &quot;mongo-primary:27017&quot; }<br>  ]<br>})</pre><p>At this point:</p><ul><li>The node becomes <strong>Primary</strong></li><li>MongoDB starts recording all writes in the <strong>oplog</strong></li></ul><p><strong>2. A new follower is added</strong></p><p>Later, when a secondary node is added:</p><pre>rs.add(&quot;mongo-secondary:27017&quot;)</pre><p>MongoDB now performs <strong>initial sync</strong> automatically.</p><p>Internally, MongoDB does the following:</p><ol><li>Takes a <strong>consistent snapshot</strong> of the Primary’s data</li><li>Records the <strong>exact oplog timestamp</strong> corresponding to that snapshot</li><li>Copies the snapshot to the Secondary</li><li>Replays all oplog entries <strong>after</strong> the snapshot</li><li>Starts tailing the oplog continuously</li></ol><p>During this entire process:</p><ul><li>The Primary <strong>continues to accept writes</strong></li><li>No database-wide lock is taken</li><li>Availability is preserved</li></ul><p>Only after the oplog replay is complete is the node considered a usable <strong>Secondary node</strong>.</p><h4>Why Followers Are Not Immediately Useful</h4><p>A follower that has not caught up:</p><ul><li>Cannot safely serve reads</li><li>Cannot participate in elections</li><li>Is effectively invisible to the system</li></ul><p>This distinction matters in system design:</p><blockquote><em>A replica exists before it is </em>useful<em>.</em></blockquote><blockquote>Until it has replayed all changes after the snapshot, it cannot safely serve reads or participate in leader elections.</blockquote><h3>2. Handling Node Outages</h3><p>In a single-leader replication architecture, handling follower outages is relatively straightforward because the leader still has the authoritative copy of the data. When a follower comes back online, it simply reconnects and catches up using the replication log.</p><p>However, managing <strong>leader failure</strong> is significantly more complex. When the leader becomes unavailable, one of the followers must take over its role. This transition process is known as <strong>failover</strong>. Failover can be handled manually by operations teams or automatically by the system.</p><h4>How Automatic Failover Works</h4><p>An automated failover process typically consists of three major steps:</p><pre>Leader fails<br>   ↓<br>Followers detect failure<br>   ↓<br>Election<br>   ↓<br>New leader</pre><p><strong>1. Detecting Leader Failure - </strong>The system must first determine whether the leader is actually unavailable. Failures can occur due to:</p><ul><li>Process crashes</li><li>Power outages</li><li>Hardware failures</li><li>Network partitions</li><li>Severe performance degradation</li></ul><p>Most distributed systems rely on <strong>heartbeat mechanisms</strong> with timeouts. Nodes continuously exchange health-check messages. If the leader fails to respond within a predefined timeout window (for example, 30 seconds), it is considered failed.</p><p><strong>2. Electing a New Leader - </strong>Once the leader is declared unavailable, the remaining replicas initiate a leader election process.</p><ul><li>A majority of replicas must agree on the new leader.</li><li>Typically, the follower with the most up-to-date replicated data is preferred.</li><li>This helps reduce the risk of data loss.</li></ul><p>The majority requirement ensures that only one leader is chosen, preventing inconsistent system states.</p><p><strong>3. Reconfiguring the System - </strong>After a new leader is selected:</p><ul><li>Clients must redirect write requests to the new leader.</li><li>The cluster configuration must be updated.</li><li>If the old leader comes back online, it must recognize that it has been replaced and rejoin as a follower.</li></ul><p>It is critical that the old leader does not continue accepting writes, as that would cause inconsistencies.</p><h4>Challenges and Risks of Failover</h4><p>Failover is not a trivial operation. Several failure modes can introduce data loss or corruption.</p><p><strong>1. Data Loss with Asynchronous Replicatio</strong>n — If the system uses asynchronous replication:</p><ul><li>The old leader may have accepted writes that were not yet replicated to followers.</li><li>When a follower is promoted, it may not contain the latest committed writes.</li><li>If the old leader later rejoins, conflicting versions of data may exist.</li></ul><p>A simple approach is to discard the unreplicated writes from the old leader. However, this violates durability guarantees from the client’s perspective, since acknowledged writes may be lost.</p><p><strong>2. Split Brain — </strong>In some failure scenarios— two nodes may simultaneously believe they are the leader. This condition is called <strong>split brain</strong>. This is dangerous because:</p><ul><li>Both leaders may accept writes.</li><li>Data divergence occurs.</li><li>Reconciling the cluster becomes extremely difficult.</li></ul><p>To mitigate this, many systems implement quorum-based elections and sometimes aggressive safety mechanisms that forcibly shut down one of the competing nodes.</p><p><strong>3. Timeout Configuration Trade-offs — </strong>Choosing the right failure detection timeout is critical.</p><ul><li><strong>Timeout too long</strong> → slower recovery when the leader truly fails.</li><li><strong>Timeout too short</strong> → unnecessary failovers during temporary slowdowns or network glitches.</li></ul><p>For example:</p><ul><li>A temporary traffic spike may increase response times.</li><li>A short-lived network issue may delay heartbeats.</li></ul><p>If the timeout is too aggressive, the system may trigger unnecessary elections, which adds further instability — especially under high load.</p><p>Because failover introduces unavoidable trade-offs between availability, durability, and consistency, there is no perfect solution.</p><p>For this reason, some organisations prefer <strong>manual failover</strong>, even when automated mechanisms are available</p><h3>3. Implementation of Replication Logs</h3><p>In a replicated database system, the leader must somehow communicate its data changes to the followers so that all replicas eventually converge to the same state. This is typically achieved through <strong>replication logs</strong> — records of the changes made on the leader that can be replayed on follower nodes.</p><p>Different database systems implement replication logs in different ways. The design choice affects performance, flexibility, upgradeability, and operational complexity. Below are several commonly used approaches, along with practical examples drawn from real systems such as PostgreSQL and MongoDB.</p><h4>1. Statement-Based Replication</h4><p>The most straightforward approach is <strong>statement-based replication</strong>. In this method, the leader logs every write request it executes and sends those exact SQL statements to the followers. Each follower then parses and executes the same statements locally.</p><p>For example, if the leader executes the following query:</p><pre>INSERT INTO users VALUES (...)</pre><p>The replication log simply contains the same SQL statement, which is then forwarded to followers. Each replica executes the query and inserts the corresponding row.</p><p>Although this approach is conceptually simple, it introduces several reliability issues. Many SQL operations are <strong>non-deterministic</strong>, meaning they may produce different results when executed at different times or on different machines.</p><p>Examples include:</p><ul><li>Functions such as NOW() or RAND() producing different values</li><li><strong>Auto-increment columns</strong>, which may generate different identifiers</li><li><strong>Triggers or stored procedures</strong>, which can produce side effects that vary between replicas</li></ul><p>Some systems attempt to mitigate this by replacing non-deterministic functions with literal values before replication. However, because of the many edge cases involved, statement-based replication is generally considered less robust than other approaches.</p><h4>2. Write-Ahead Log (WAL) Shipping</h4><p>Another approach is <strong>Write-Ahead Log (WAL) shipping</strong>, which replicates changes at a much lower level.</p><p>Most databases maintain an internal <strong>append-only log</strong> that records every modification made to the storage engine. In PostgreSQL, this log is called the <strong>Write-Ahead Log (WAL)</strong>. Instead of sending SQL queries to replicas, the leader streams these WAL records directly to followers.</p><p>Databases already maintain a write-ahead log for crash recovery. Replication systems simply reuse this log to stream changes to replicas.</p><p>When a user executes a query such as:</p><pre>INSERT INTO users (id, name) VALUES (1, &#39;Rahul&#39;);</pre><p>the followers do not receive the SQL query. Instead, they receive low-level instructions describing how the database’s storage blocks changed. A simplified WAL entry might look like:</p><pre>LSN: 0/16B3740<br>RELATION: users<br>ACTION: INSERT<br>DISK BLOCK: 42<br>BYTE CHANGES: [binary data]</pre><p>Followers apply these changes directly to their own storage engine, effectively replaying the exact physical operations performed on the leader.</p><p>This approach has several advantages:</p><ul><li>Replication is <strong>very fast</strong></li><li>Followers become <strong>exact physical copies</strong> of the leader</li></ul><p>However, WAL replication has a key drawback: it is <strong>tightly coupled to the database’s internal storage format</strong>. Because WAL records describe low-level disk changes, the leader and followers typically need to run the <strong>same database version and storage engine implementation</strong>.</p><p>This tight coupling makes certain operations — such as upgrading database versions — more difficult, since replicas must usually remain compatible with the leader’s WAL format.</p><h4>3. Logical (Row-Based) Log Replication</h4><p>To overcome the limitations of physical replication, many systems provide <strong>logical replication</strong>, sometimes called <strong>row-based replication</strong>.</p><p>Instead of replicating SQL statements or disk-level changes, logical replication records the <strong>actual data changes</strong> that occur in tables.</p><p>A logical replication log for an inserted row might look like:</p><pre>TABLE: users<br>OPERATION: INSERT<br>VALUES:<br>  id = 2<br>  name = &#39;Amit&#39;</pre><p>Similarly:</p><ul><li>For a <strong>delete</strong>, the log contains enough information to identify the removed row (often the primary key).</li><li>For an <strong>update</strong>, the log records both the row identifier and the new column values.</li></ul><p>For example, if the leader executes:</p><pre>UPDATE users<br>SET name = &#39;Rahul Sharma&#39;<br>WHERE id = 1;</pre><p>the replication log might record:</p><pre>TABLE: users<br>OPERATION: UPDATE<br>PRIMARY KEY: id = 1<br>NEW VALUES:<br>  name = &#39;Rahul Sharma&#39;</pre><p>Followers apply these changes by locating the corresponding row and updating it.</p><p>A single transaction that modifies multiple rows generates multiple logical log entries. Followers replay them in the same order to maintain consistency.</p><p>Logical replication provides several important benefits:</p><ul><li>It is <strong>decoupled from storage engine internals</strong></li><li>Leaders and followers can sometimes run <strong>different database versions</strong></li><li>Replication logs are <strong>human-readable and easier to process</strong></li><li>External systems can consume these logs for <strong>data pipelines or event streaming</strong></li></ul><p>A well-known example of logical replication is <strong>MongoDB’s oplog</strong>. MongoDB records operations such as inserts, updates, and deletes in a replication log called the <strong>operation log (oplog)</strong>. Secondary nodes continuously read the oplog and apply those operations to their local data.</p><p>Because logical logs operate at the data level rather than the storage level, they provide greater flexibility than WAL-based replication.</p><h4>4. Trigger-Based Replication</h4><p>The replication strategies discussed so far are implemented internally by the database system. In contrast, <strong>trigger-based replication</strong> relies on application-level logic.</p><p>Many databases allow developers to define <strong>triggers</strong>, which automatically execute custom code whenever certain data changes occur. These triggers can capture changes and send them to external systems.</p><p>For example, a database trigger might record modifications to a table in a replication log table:</p><pre>operation = INSERT<br>user_id = 1<br>name = Rahul<br>timestamp = 2026-03-04</pre><p>An external service can then read this log and replicate the data to another database, a message queue, or an analytics pipeline.</p><p>Trigger-based replication is particularly useful when:</p><ul><li>Only <strong>a subset of data</strong> needs to be replicated</li><li>Data must be copied <strong>between different database technologies</strong></li><li>Changes need to be forwarded to <strong>external systems</strong></li></ul><p>Modern systems often implement similar ideas using <strong>change streams</strong> or <strong>event-based architectures</strong>. For example, MongoDB provides <strong>Change Streams</strong>, which allow applications to subscribe to real-time database changes and react to them.</p><p>The primary advantage of trigger-based replication is flexibility. However, because it depends on application logic rather than built-in replication mechanisms, it can introduce additional operational complexity.</p><h3>4. References</h3><ul><li>Martin Kleppmann, <em>Designing Data-Intensive Applications</em></li><li>Industry experience and distributed systems practices</li></ul><p>In the next part, we will explore how <a href="https://medium.com/@rahul.jindal57/system-design-interview-replication-part-3-replication-lag-and-read-consistency-7913a818bd31"><strong>replication lag</strong></a> affects system behavior and examine common anomalies that arise in replicated databases.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff085681e7fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System Design Interview — Replication (Part 1: Fundamentals)]]></title>
            <link>https://medium.com/@rahul.jindal57/system-design-interview-replication-part-1-fundamentals-f0f4f887775e?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0f4f887775e</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[database-replication]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[database-design]]></category>
            <category><![CDATA[backend-engineering]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 13:46:49 GMT</pubDate>
            <atom:updated>2026-03-04T17:36:51.141Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 1: Fundamentals of Data Replication in Distributed Systems</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2efjKjaJJ-tE8gVN" /><figcaption>Photo by <a href="https://unsplash.com/@adamszabo?utm_source=medium&amp;utm_medium=referral">Adam Szabo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Table of Contents</h3><ol><li>Replication in Distributed Systems</li><li>Replication Approaches in Practice</li><li>Leader–Follower Architecture</li><li>Synchronous and Asynchronous Replication</li><li>References</li></ol><h3>1. Replication in Distributed Systems</h3><p>In a distributed environment, replication refers to maintaining multiple copies of the same dataset across different machines. Each machine that holds a copy is known as a <strong>replica</strong>.</p><p>Replication enables systems to function correctly even when individual components fail or become slow. Systems replicate data to address three critical concerns.</p><ol><li><strong>Latency Optimization:</strong> Placing data closer to end users reduces the time required to process requests.</li><li><strong>Fault Tolerance:</strong> If one machine becomes unavailable, replicas allow the system to continue serving requests without interruption.</li><li><strong>Read Scalability:</strong> With multiple replicas, read traffic can be distributed across machines, improving throughput.</li></ol><p>Replicating static data is trivial — data can simply be copied to all machines.</p><p>Complexity arises when data is updated. Changes must be distributed reliably, failures can occur mid-operation, and replicas may temporarily diverge. These challenges make replication one of the hardest problems in distributed system design.</p><h3>2. Replication Approaches in Practice</h3><p>Most systems adopt one of the following strategies to propagate changes between replicas:</p><ul><li><strong>Single-leader replication:</strong> One designated leader accepts all writes and propagates changes to follower replicas.</li><li><strong>Multi-leader replication:</strong> Multiple leaders accept writes concurrently and synchronize changes between themselves.</li><li><strong>Leaderless replication:</strong> Any replica can accept reads and writes, with consistency achieved through quorum or conflict resolution.</li></ul><p>Each approach reflects a different balance between simplicity, consistency, and availability. Choosing a replication strategy involves answering key questions:</p><ul><li>Should writes wait for replicas or proceed immediately?</li><li>How should the system behave when replicas fail or fall behind?</li></ul><p>The answers determine how the system performs under load and during failures.</p><h3>3. Leader–Follower Architecture</h3><p>In leader-based replication, one replica is responsible for accepting all write requests. This replica, known as the <strong>leader</strong>, applies changes locally and then distributes those changes to other replicas, called <strong>followers</strong>. Followers do not accept writes directly from clients.</p><h4>Write Path</h4><ol><li>A client sends a write request to the leader</li><li>The leader persists the change</li><li>The update is propagated to follower replicas</li></ol><h4>Read Path</h4><p>Read requests can be served by either the leader or followers, depending on system configuration. All write operations are still routed through the leader.</p><p>This architecture is commonly supported by modern relational and non-relational databases.</p><h3>4. Synchronous and Asynchronous Replication</h3><p>Replication can be classified based on whether the leader waits for replicas to confirm updates.</p><ul><li><strong>Synchronous replication</strong> requires acknowledgment from a replica before confirming a write</li><li><strong>Asynchronous replication</strong> allows the leader to confirm writes immediately</li></ul><p>Synchronous replication provides stronger durability guarantees but increases response times. Fully synchronous replication can hurt availability, while fully asynchronous replication increases the risk of data loss.</p><p>To balance these concerns, systems often use <strong>semi-synchronous</strong> replication:</p><ul><li>A limited number of replicas confirm writes synchronously</li><li>Remaining replicas are updated asynchronously</li><li>If a synchronous replica becomes unavailable, another replica takes its place</li></ul><p>This approach improves fault tolerance without severely impacting performance.</p><h3>5. References</h3><ul><li>Martin Kleppmann, <em>Designing Data-Intensive Applications</em></li><li>Industry experience and distributed systems practices</li></ul><p>In the next part, we’ll look at how <a href="https://medium.com/@rahul.jindal57/system-design-interview-replication-part-2-operating-leader-based-replication-ff085681e7fb"><strong>leader-based replication</strong></a> behaves when nodes fail or new replicas are added.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0f4f887775e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing GraphQL Resolvers with DataLoader]]></title>
            <link>https://medium.com/@rahul.jindal57/optimizing-graphql-resolvers-with-dataloader-ee22a0937a01?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee22a0937a01</guid>
            <category><![CDATA[dataloader]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[redundancy]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sun, 13 Apr 2025 15:57:27 GMT</pubDate>
            <atom:updated>2025-04-13T15:57:27.739Z</atom:updated>
            <content:encoded><![CDATA[<h4>Minimize Overfetching and Boost Performance with Batching and Caching</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PPiT_YOxISqYcZxM" /><figcaption>Photo by <a href="https://unsplash.com/@ffstop?utm_source=medium&amp;utm_medium=referral">Fotis Fotopoulos</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>GraphQL APIs are powerful but can become performance bottlenecks if not handled efficiently, often due to N+1 problems and redundant DB calls. In this post, we’ll walk through how to use <strong>DataLoader</strong> to batch requests within a single GraphQL query.</p><h3><strong>Understanding the Problem</strong></h3><h4>⚠The N+1 Problem</h4><p>Let’s say our GraphQL schema looks like this:</p><pre>type Post {<br>  id: ID!<br>  title: String!<br>  author: User!<br>}<br><br>type User {<br>  id: ID!<br>  name: String!<br>}<br><br>type Query {<br>  posts: [Post!]!<br>}</pre><p>When we query:</p><pre>{<br>  posts {<br>    title<br>    author {<br>      name<br>    }<br>  }<br>}</pre><p>We’re fetching a list of posts, and for each one, we’re also fetching the author. If our resolver is written naïvely, we’re making one query for posts, and <strong>one query per post</strong> to get the author.</p><p>So if we have 50 posts:</p><pre>1 query to get posts<br>50 queries to get authors<br>= 51 total queries 🤯</pre><p>This is the infamous <strong>N+1 problem</strong>.</p><h3><strong>Naive Implementation: The Problem</strong></h3><p>Let’s build a minimal GraphQL app to <strong>observe the problem</strong>.</p><ol><li><strong>package.json</strong></li></ol><pre>{<br>  &quot;name&quot;: &quot;nplus1-naive&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;dependencies&quot;: {<br>      &quot;apollo-server&quot;: &quot;^3.11.1&quot;,<br>      &quot;graphql&quot;: &quot;^16.6.0&quot;,<br>      &quot;pg&quot;: &quot;^8.11.1&quot;<br>  }<br>}</pre><p><strong>2. db.js</strong></p><pre>const { Pool } = require(&quot;pg&quot;);<br><br>const pool = new Pool({<br>  connectionString: process.env.DATABASE_URL,<br>});<br><br>module.exports = {<br>  query: (text, params) =&gt; pool.query(text, params),<br>};</pre><p><strong>3. schema.js</strong></p><pre>const { gql } = require(&quot;apollo-server&quot;);<br><br>module.exports = gql`<br>  type Post {<br>    id: ID!<br>    title: String!<br>    author: User!<br>  }<br><br>  type User {<br>    id: ID!<br>    name: String!<br>  }<br><br>  type Query {<br>    posts: [Post!]!<br>  }<br>`;</pre><p><strong>4. resolvers.js (Naive: N+1 prone)</strong></p><pre>const db = require(&quot;./db&quot;);<br><br>module.exports = {<br>  Query: {<br>    posts: async () =&gt; {<br>      const res = await db.query(&quot;SELECT * FROM posts&quot;);<br>      return res.rows;<br>    },<br>  },<br>  Post: {<br>    author: async (post) =&gt; {<br>      const res = await db.query(&quot;SELECT * FROM users WHERE id = $1&quot;, [post.author_id]);<br>      return res.rows[0];<br>    },<br>  },<br>};</pre><p><strong>5. index.js</strong></p><pre>const { ApolloServer } = require(&quot;apollo-server&quot;);<br>const typeDefs = require(&quot;./schema&quot;);<br>const resolvers = require(&quot;./resolvers&quot;);<br><br>const server = new ApolloServer({<br>  typeDefs,<br>  resolvers,<br>});<br><br>server.listen().then(({ url }) =&gt; {<br>  console.log(`🚀 Server running at ${url}`);<br>});</pre><h4>Test It:</h4><p>By Seeding a few posts and users manually using psql or a seed script, and run the query. we’ll notice multiple DB calls — that’s the <strong>N+1 issue</strong>.</p><h3><strong>Solving It with DataLoader</strong></h3><p>Now we will solve the N+1 issue with using dataLoader.</p><p>Update<strong> package.json</strong></p><pre>&quot;dependencies&quot;: {<br>  &quot;apollo-server&quot;: &quot;^3.11.1&quot;,<br>  &quot;graphql&quot;: &quot;^16.6.0&quot;,<br>  &quot;pg&quot;: &quot;^8.11.1&quot;,<br>  &quot;dataloader&quot;: &quot;^2.2.2&quot;,<br>}</pre><p><strong>loaders.js</strong></p><pre>const DataLoader = require(&quot;dataloader&quot;);<br>const db = require(&quot;./db&quot;);<br><br>function createUserLoader() {<br>  return new DataLoader(async (userIds) =&gt; {<br>    console.log(&quot;Batch loading users:&quot;, userIds);<br>    let fromDb = [];<br>    if (userIds.length &gt; 0) {<br>       const res = await db.query(&quot;SELECT * FROM users WHERE id = ANY($1)&quot;, [userIds]);<br>       fromDb = res.rows;<br>    }<br><br>   const finalMap = new Map(fromDb.map((u) =&gt; [u.id, u]));<br>   return userIds.map((id) =&gt; finalMap.get(id));<br>  });<br>}<br><br>module.exports = () =&gt; ({<br>  userLoader: createUserLoader(),<br>});</pre><p>Update<strong> resolvers.js</strong></p><pre>module.exports = {<br>  Query: {<br>    posts: async () =&gt; {<br>      const res = await db.query(&quot;SELECT * FROM posts&quot;);<br>      return res.rows;<br>    },<br>  },<br>  Post: {<br>    author: async (post, _, { loaders }) =&gt; {<br>      return loaders.userLoader.load(post.author_id);<br>    },<br>  },<br>};</pre><p>Update<strong> index.js</strong></p><pre>const { ApolloServer } = require(&quot;apollo-server&quot;);<br>const typeDefs = require(&quot;./schema&quot;);<br>const resolvers = require(&quot;./resolvers&quot;);<br>const createLoaders = require(&quot;./loaders&quot;);<br><br>const server = new ApolloServer({<br>  typeDefs,<br>  resolvers,<br>  context: () =&gt; ({<br>    loaders: createLoaders(),<br>  }),<br>});<br><br>server.listen().then(({ url }) =&gt; {<br>  console.log(`🚀 Optimized server at ${url}`);<br>});</pre><h3><strong>Final Comparison</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*6vFsa7-rc9P56bWgT-w_Lw.png" /><figcaption>N+1 Problem solving strategy comparison</figcaption></figure><h3><strong>Conclusion</strong></h3><ul><li><strong>N+1</strong> is real, and deadly at scale.</li><li><strong>DataLoader</strong> handles batching and caching smartly in memory.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee22a0937a01" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CQRS (Command Query Responsibility Segregation)]]></title>
            <link>https://medium.com/@rahul.jindal57/cqrs-command-query-responsibility-segregation-892b22359524?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/892b22359524</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[cqrs]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sun, 06 Apr 2025 08:16:08 GMT</pubDate>
            <atom:updated>2025-04-06T08:16:08.309Z</atom:updated>
            <content:encoded><![CDATA[<h4>Optimizing Reads and Writes with Clean Boundaries</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HYoFRiSmwi9gx4Wl" /><figcaption>Photo by <a href="https://unsplash.com/@pakata?utm_source=medium&amp;utm_medium=referral">Pakata Goh</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>What is CQRS?</strong></h3><p><strong>CQRS (Command Query Responsibility Segregation)</strong> is a pattern that separates <strong>read</strong> and <strong>write</strong> operations into different models and sometimes even services. Instead of using the same data model for both querying and updating data, we <strong>isolate concerns</strong>, allowing us to <strong>optimize and scale</strong> each side independently.</p><h3><strong>Why Use Separate Read/Write Databases?</strong></h3><h4>Command Side (Write):</h4><ol><li><strong>Purpose:</strong> Create/Update/Delete</li><li><strong>DB Model:</strong> Normalized, transactional</li><li><strong>Optimized for:</strong> Data integrity, consistency</li><li><strong>Examples:</strong> PostgreSQL</li></ol><h4><strong>Query Side (Read)</strong></h4><ol><li><strong>Purpose:</strong> Read/Query</li><li><strong>DB Model:</strong> Denormalized, fast access</li><li><strong>Optimized for:</strong> Performance, flexibility</li><li><strong>Examples:</strong> Redis, Elasticsearch, Read-replica</li></ol><h4>Benefits of splitting databases:</h4><ul><li>Each database is tuned for its job (writes vs reads).</li><li>Reduced contention between readers and writers.</li><li>Improved read scalability (read DBs can be replicated).</li><li>Enables polyglot persistence (different DBs per side).</li></ul><h3><strong>How It Works (Step-by-Step)</strong></h3><h4><strong>1. Command Side: Handling Writes</strong></h4><ul><li>Exposes <strong>write APIs</strong> (POST, PUT, DELETE) through Express or NestJS.</li><li>These APIs <strong>only write</strong> to the <strong>Write DB</strong> (e.g. PostgreSQL).</li><li>After a successful operation, the service <strong>emits an event</strong> to an <strong>event bus</strong> (e.g., UserCreated, OrderPlaced).</li><li>These events carry only the data needed to update the <strong>read model</strong>.</li></ul><blockquote><strong>Example</strong>: A POST /users call creates a new user in the Write DB and emits a UserCreated event.</blockquote><h4><strong>2. Event Bus: Propagating State</strong></h4><ul><li>A <strong>message broker</strong> (RabbitMQ, Kafka) distributes events.</li><li>Events are <strong>decoupled</strong> from the request-response cycle.</li><li>Enables <strong>asynchronous communication</strong> between Command and Query sides.</li><li>We can also <strong>store events</strong> for <strong>event sourcing</strong> later.</li></ul><h4><strong>3. Read Model Updater: Syncing Data</strong></h4><ul><li>A background process or microservice <strong>listens to events</strong> from the event bus.</li><li>Based on the event type, it <strong>projects the data</strong> into the <strong>Read DB</strong>.</li><li>We can denormalize or aggregate data to suit fast queries.</li></ul><blockquote><strong>Example</strong>: UserCreated → insert the user’s name and email into the Read DB (no password or other write-model specifics).</blockquote><h4><strong>4. Query Side: Handling Reads</strong></h4><ul><li>Exposes <strong>read-only APIs</strong> (GET) that fetch data from the Read DB.</li><li>This side can use a different DB tech stack: e.g.,<br>-&gt; <strong>Elasticsearch</strong> for full-text search<br>-&gt; <strong>Redis</strong> for caching &amp; high throughput reads<br>-&gt; <strong>MongoDb</strong> for embedding related data in a single document</li><li>Schema here is optimized for <strong>read performance</strong>, not normalization.</li></ul><h3><strong>Key Concepts to Keep in Mind</strong></h3><h4>Eventual Consistency</h4><ul><li>Since events propagate <strong>asynchronously</strong>, the <strong>read model will lag</strong> behind the write model slightly.</li><li>It’s acceptable for many systems (think e-commerce, dashboards, reporting).</li><li>But for banking, stock trading, or critical systems, it might be problematic unless handled carefully.</li></ul><h4>Decoupling Read &amp; Write Models</h4><ul><li><strong>Write models</strong> are normalized, transactional.</li><li><strong>Read models</strong> are denormalized, optimized for specific queries.</li><li>We are not duplicating logic — we are separating concerns.</li></ul><h4>Events Should Be Immutable</h4><ul><li>Once emitted, events should never be changed.</li><li>Include enough data in the event to <strong>rebuild the read model</strong>, even if the write model changes later.</li></ul><h3><strong>Testing Strategy</strong></h3><ul><li><strong>Unit test</strong> write for services and event emitters.</li><li><strong>Integration test</strong> end-to-end data propagation (command → event → read model).</li><li>Use mocks or in-memory brokers during testing.</li></ul><h3><strong>Scaling Tips</strong></h3><ul><li><strong>Read DB Replication:</strong> Easily scale out read queries via replicas.</li><li><strong>Event Reprocessing:</strong> Maintain event history to replay and rebuild the read model.</li><li><strong>Microservices:</strong> Deploy read and write APIs independently.</li></ul><h3><strong>Code Example</strong></h3><p><strong>1️⃣ command-service/index.js</strong></p><pre>const express = require(&#39;express&#39;);<br>const { Pool } = require(&#39;pg&#39;);<br>const { Kafka } = require(&#39;kafkajs&#39;);<br><br><br>const app = express();<br>app.use(express.json());<br><br><br>const db = new Pool({<br> user: &#39;postgres&#39;,<br> password: &#39;postgres&#39;,<br> host: &#39;postgres&#39;,<br> database: &#39;commanddb&#39;,<br> port: 5432,<br>});<br><br><br>const kafka = new Kafka({ brokers: [&#39;kafka:9092&#39;] });<br>const producer = kafka.producer();<br><br><br>(async () =&gt; {<br> await db.query(`CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)`);<br> await producer.connect();<br>})();<br><br><br>app.post(&#39;/users&#39;, async (req, res) =&gt; {<br> const { name } = req.body;<br> const result = await db.query(&#39;INSERT INTO users(name) VALUES($1) RETURNING *&#39;, [name]);<br> const user = result.rows[0];<br><br><br> console.log(`Inserted user ${user.id} into db`)<br><br><br> await producer.send({<br>   topic: &#39;user.created&#39;,<br>   messages: [{ value: JSON.stringify(user) }],<br> });<br><br><br> res.status(201).send(user);<br>});<br><br><br>app.listen(3001, () =&gt; console.log(&#39;✅ Command service running on port 3001&#39;));</pre><p><strong>2️⃣ query-service/index.js</strong></p><pre>const express = require(&#39;express&#39;);<br>const { Kafka } = require(&#39;kafkajs&#39;);<br>const { MongoClient } = require(&#39;mongodb&#39;);<br><br><br>const app = express();<br>app.use(express.json());<br><br><br>const kafka = new Kafka({ brokers: [&#39;kafka:9092&#39;] });<br>const consumer = kafka.consumer({ groupId: &#39;query-group&#39; });<br><br><br>let mongo;<br>let users;<br><br><br>(async () =&gt; {<br> mongo = await MongoClient.connect(&#39;mongodb://mongo:27017&#39;);<br> users = mongo.db(&#39;querydb&#39;).collection(&#39;users&#39;);<br><br><br> await consumer.connect();<br> await consumer.subscribe({ topic: &#39;user.created&#39;, fromBeginning: true });<br><br><br> await consumer.run({<br>   eachMessage: async ({ message }) =&gt; {<br>     const user = JSON.parse(message.value.toString());<br>     await users.updateOne({ id: user.id }, { $set: user }, { upsert: true });<br>     console.log(`Synced user ${user.id} into MongoDB`);<br>   },<br> });<br>})();<br><br><br>app.get(&#39;/users/:id&#39;, async (req, res) =&gt; {<br> const user = await users.findOne({ id: parseInt(req.params.id) });<br> if (!user) return res.status(404).send({ error: &#39;User not found&#39; });<br> res.send(user);<br>});<br><br><br>app.listen(3002, () =&gt; console.log(&#39;✅ Query service running on port 3002&#39;));</pre><p><strong>3️⃣ docker-compose.yml</strong></p><pre>services:<br> postgres:<br>   image: postgres<br>   environment:<br>     POSTGRES_PASSWORD: postgres<br>     POSTGRES_DB: commanddb<br>   ports:<br>     - &quot;5432:5432&quot;<br><br><br> mongo:<br>   image: mongo<br>   ports:<br>     - &quot;27017:27017&quot;<br><br><br> zookeeper:<br>   image: bitnami/zookeeper:latest<br>   environment:<br>     ALLOW_ANONYMOUS_LOGIN: &quot;yes&quot;<br>   ports:<br>     - &quot;2181:2181&quot;<br><br><br> kafka:<br>   image: bitnami/kafka:3.6<br>   ports:<br>     - &quot;9092:9092&quot;<br>   environment:<br>     - KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181<br>     - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092<br>     - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092<br>     - ALLOW_PLAINTEXT_LISTENER=yes<br>   depends_on:<br>     - zookeeper<br><br><br> command-service:<br>   build: ./command-service<br>   ports:<br>     - 3001:3001<br>   depends_on:<br>     - postgres<br>     - kafka<br>   volumes:<br>     - ./command-service/index.js:/app/index.js<br><br><br> query-service:<br>   build: ./query-service<br>   ports:<br>     - 3002:3002<br>   depends_on:<br>     - mongo<br>     - kafka<br>   volumes:<br>     - ./query-service/index.js:/app/index.js</pre><h3><strong>Final Thoughts</strong></h3><p>Implementing CQRS with separate databases is <strong>not trivial</strong>. It adds <strong>complexity</strong>, but also <strong>unlocks scalability and performance</strong>.</p><p>Before adopting this:</p><ul><li>Start with a monolith → modularize → extract</li><li>Introduce CQRS at <strong>domain boundaries</strong>, not everywhere</li><li>Ensure <strong>observability</strong> (logs, traces, metrics) across command and query sides</li><li>Design your <strong>events thoughtfully</strong>, as they’re long-term contracts</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=892b22359524" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript vs TypeScript]]></title>
            <link>https://medium.com/@rahul.jindal57/javascript-vs-typescript-224c7eb5e4d4?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/224c7eb5e4d4</guid>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[type-checking]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sat, 15 Mar 2025 17:49:22 GMT</pubDate>
            <atom:updated>2025-03-15T17:49:22.727Z</atom:updated>
            <content:encoded><![CDATA[<h4>A comprehensive guide to understand the differences between JavaScript and TypeScript</h4><p>JavaScript (JS) and TypeScript (TS) are both widely used in web development, but they differ in their nature, capabilities, and purpose. This article provides an in-depth technical comparison between JavaScript and TypeScript on various aspects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Ai4vcZ9hI993QDHoperTsw.png" /><figcaption>JavaScript vs TypeScript</figcaption></figure><h3>1. Fundamental Differences Between JavaScript and TypeScript</h3><h4>JavaScript:</h4><ol><li><strong>Type System:</strong> Dynamically typed (loosely typed)</li><li><strong>Compilation:</strong> Interpreted by JS engine</li><li><strong>Error Checking:</strong> Runtime errors</li><li><strong>Interfaces:</strong> Not Supported</li><li><strong>Code Maintainability:</strong> Harder for large-scale apps</li><li><strong>Performance:</strong> Faster due to no compilation overhead</li></ol><h4>TypeScript:</h4><ol><li><strong>Type System:</strong> Statically typed (optional type annotations)</li><li><strong>Compilation:</strong> Compiled to JavaScript before execution</li><li><strong>Error Checking:</strong> Compile-time errors</li><li><strong>Interfaces:</strong> Supported</li><li><strong>Code Maintainability:</strong> Easier due to strict typing</li><li><strong>Performance:</strong> Slightly slower due to type checking (during development)</li></ol><h3><strong>2. Execution Model and Compilation Process</strong></h3><h4>JavaScript Execution Model</h4><p>JavaScript is an <strong>interpreted language</strong>, meaning it is executed line-by-line by the JavaScript engine. However, modern JS engines like <strong>V8</strong> (used in Chrome and Node.js) use <a href="https://medium.com/@rahul.jindal57/understanding-just-in-time-jit-compilation-in-v8-a-deep-dive-c98b09c6bf0c"><strong>Just-In-Time (JIT) compilation</strong></a> to improve performance.</p><p><strong>How JavaScript Runs in the Browser</strong></p><ol><li>The browser fetches JavaScript code from an HTML file.</li><li>The JavaScript engine parses the code and converts it into an <strong>Abstract Syntax Tree (AST)</strong>.</li><li>The AST is converted into <strong>bytecode</strong> for interpretation.</li><li>Frequently executed functions are <strong>optimized</strong> into machine code using <a href="https://medium.com/@rahul.jindal57/understanding-just-in-time-jit-compilation-in-v8-a-deep-dive-c98b09c6bf0c">JIT compilation</a>.</li></ol><p><strong>How JavaScript Runs in Node.js</strong></p><ol><li>The Node.js runtime loads the JavaScript file.</li><li>The <strong>V8 engine</strong> compiles it into bytecode.</li><li>The <strong>Ignition interpreter</strong> runs the bytecode.</li><li>The <strong>TurboFan optimizer</strong> converts hot code paths into optimized machine code.</li></ol><h4>TypeScript Compilation Process</h4><p>Unlike JavaScript, TypeScript <strong>must be compiled</strong> into JavaScript before execution. The TypeScript compiler (tsc) performs:</p><ol><li><strong>Lexical Analysis:</strong> Breaks down the code into tokens.</li><li><strong>Parsing:</strong> Generates an AST.</li><li><strong>Type Checking:</strong> Ensures type correctness.</li><li><strong>Transpilation:</strong> Converts TS to JS (removes type annotations).</li><li><strong>Output Generation:</strong> Produces a JavaScript file.</li></ol><p>This means that TypeScript is not directly executed but acts as a <strong>developer tool</strong> to improve code quality.</p><h3><strong>3. Type System: Static vs Dynamic Typing</strong></h3><h4>JavaScript’s Dynamic Typing</h4><p>JavaScript allows variables to hold values of any type, leading to unpredictable behavior:</p><pre>let value = &quot;Hello&quot;;<br>value = 42;  // No error, but could cause issues later</pre><p>Since JavaScript does not enforce types, errors only occur <strong>at runtime</strong>, making debugging harder.</p><h4>TypeScript’s Static Typing</h4><p>TypeScript enforces types at <strong>compile time</strong>, reducing bugs:</p><pre>let value: string = &quot;Hello&quot;;<br>value = 42;  // Error: Type &#39;number&#39; is not assignable to type &#39;string&#39;</pre><p>This prevents unintended type mutations and improves <strong>code reliability</strong>.</p><h4>TypeScript’s Type Inference</h4><p>TypeScript can infer types without explicit annotations:</p><pre>let age = 30; // TypeScript infers &#39;number&#39;</pre><h3>4. Advanced Features in TypeScript</h3><h4><strong>4.1 Interfaces:</strong></h4><p>JavaScript does not support interfaces, but TypeScript does:</p><pre>interface User {<br>  name: string;<br>  age: number;<br>}<br><br>const user: User = { name: &quot;Alice&quot;, age: 25 };</pre><h4><strong>4.2 Enums:</strong></h4><p>TypeScript introduces enum to define a set of named constants:</p><pre>enum Status {<br>  Active,<br>  Inactive,<br>  Pending<br>}<br><br>let currentStatus: Status = Status.Active;</pre><h4><strong>4.3 Generics:</strong></h4><p>Generics allow reusable and type-safe components:</p><pre>function identity&lt;T&gt;(arg: T): T {<br>  return arg;<br>}<br><br>console.log(identity&lt;string&gt;(&quot;Hello&quot;)); // Ensures string type</pre><h4><strong>4.4 Utility Types:</strong></h4><p>TypeScript provides utility types like Partial&lt;T&gt; and Readonly&lt;T&gt;:</p><pre>interface User {<br>  name: string;<br>  age: number;<br>}<br><br>const user: Partial&lt;User&gt; = { name: &quot;John&quot; }; // &#39;age&#39; is optional</pre><h3>5. JavaScript vs TypeScript in Real-World Applications</h3><h4>JavaScript:</h4><ol><li><strong>Small projects:</strong> ✅ Easier to set up</li><li><strong>Large-scale applications: </strong>❌ Harder to maintain</li><li><strong>Performance-sensitive code: </strong>✅ No compilation overhead</li><li><strong>Team collaboration: </strong>❌ Harder to enforce consistency</li></ol><h4>TypeScript:</h4><ol><li><strong>Small projects:</strong> ❌ Overhead of compilation</li><li><strong>Large-scale applications: </strong>✅ Better maintainability</li><li><strong>Performance-sensitive code: </strong>❌ Extra compilation step</li><li><strong>Team collaboration: </strong>✅ Enforces strong typing</li></ol><h3>6. Performance Considerations</h3><h4>JavaScript:</h4><ol><li><strong>Compilation Time: </strong>None</li><li><strong>Runtime Performance: </strong>Faster (less overhead)</li><li><strong>Debugging: </strong>Harder (runtime errors)</li></ol><h4>TypeScript:</h4><ol><li><strong>Compilation Time: </strong>Slower due to type checking</li><li><strong>Runtime Performance: </strong>Similar after compilation</li><li><strong>Debugging: </strong>Easier (compile-time checks)</li></ol><p>TypeScript improves <strong>development experience</strong>, but does not impact <strong>runtime performance</strong> since it compiles to JavaScript.</p><h3><strong>7. When to Use TypeScript vs JavaScript</strong></h3><h4>✅ Use JavaScript if:</h4><ul><li>Building a <strong>small script</strong> or simple web page.</li><li>Need <strong>fast prototyping</strong> with minimal setup.</li></ul><h4>✅ Use TypeScript if:</h4><ul><li>Working on a <strong>large project</strong> with multiple developers.</li><li>Need <strong>better code maintainability</strong> and type safety.</li><li>Enforce <strong>fewer runtime errors</strong>.</li><li>Using <strong>modern frontend frameworks</strong> like Angular or React.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=224c7eb5e4d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[gRPC with Bidirectional Streaming for Real-Time Updates]]></title>
            <link>https://medium.com/@rahul.jindal57/grpc-with-bidirectional-streaming-for-real-time-updates-df07e44e209c?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/df07e44e209c</guid>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[real-time-streaming-data]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[node]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sat, 15 Mar 2025 03:16:41 GMT</pubDate>
            <atom:updated>2025-03-15T03:16:41.045Z</atom:updated>
            <content:encoded><![CDATA[<h4>A beginner guide to explore gRPC and its powerful capabilities for real-time communication.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YSFbRh012nETQTAK" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>gRP vs REST</h3><h4><strong>What is REST?</strong></h4><p>REST (Representational State Transfer) is an architectural style used for designing networked applications. It relies on HTTP methods to perform operations on resources identified by URLs.</p><p><strong>Key Features of REST:</strong></p><ul><li><strong>Stateless:</strong> Each request from a client to the server must contain all the necessary information to process it. The server does not store any client state between requests.</li><li><strong>Resource-Based:</strong> Everything is treated as a resource (e.g., users, orders, products) and is accessed via standard HTTP methods.</li><li><strong>Uses HTTP Verbs:<br> </strong>GET → Retrieve a resource<br> POST → Create a resource<br> PUT/PATCH → Update a resource<br> DELETE → Remove a resource</li><li><strong>JSON and XML Support:</strong> RESTful APIs commonly use JSON for data exchange, though XML is also supported.</li></ul><p><strong>Widely Used:</strong> REST is simple, flexible, and well-supported across web and mobile applications.</p><p><strong>REST API Example (Express.js — Node.js)</strong></p><pre>const express = require(&quot;express&quot;);<br>const app = express();<br>app.use(express.json());<br><br>let users = [{ id: 1, name: &quot;Alice&quot; }, { id: 2, name: &quot;Bob&quot; }];<br><br>app.get(&quot;/users&quot;, (req, res) =&gt; {<br> res.json(users);<br>});<br><br>app.post(&quot;/users&quot;, (req, res) =&gt; {<br> const user = { id: users.length + 1, name: req.body.name };<br> users.push(user);<br> res.status(201).json(user);<br>});<br><br>app.listen(3000, () =&gt; console.log(&quot;Server running on port 3000&quot;));</pre><p><strong>Limitations of REST:</strong></p><ul><li><strong>High Latency:</strong> Each request requires a full HTTP round-trip.</li><li><strong>Lack of Streaming Support:</strong> REST relies on polling or WebSockets for real-time updates.</li><li><strong>Over-fetching/Under-fetching:</strong> The client often gets too much or too little data, requiring additional requests.</li><li><strong>Inefficient for High-Performance Microservices:</strong> REST has higher overhead due to HTTP and JSON serialization.</li></ul><h4><strong>What is gRPC?</strong></h4><p>gRPC is an open-source, high-performance RPC framework developed by Google. It enables efficient communication between microservices and supports multiple programming languages.</p><h4>Key Features of gRPC:</h4><ul><li><strong>Uses Protocol Buffers (Protobufs):</strong> A binary serialization format that is smaller and faster than JSON.</li><li><strong>Supports HTTP/2:</strong> Enables multiplexed streams over a single connection, reducing latency.</li><li><strong>Built-in Streaming:<br> Unary RPC:</strong> Similar to REST (one request, one response).<br> <strong>Server Streaming:</strong> Server streams multiple responses to the client.<br> <strong>Client Streaming:</strong> Client streams multiple requests to the server.<br> <strong>Bidirectional Streaming:</strong> Both client and server stream data in real-time.</li><li><strong>Strongly Typed APIs:</strong> Protobufs enforce strict type safety.</li><li><strong>Multi-Language Support:</strong> Works with JavaScript, Python, Java, Go, C++, and more.</li></ul><p><strong>gRPC Example (Node.js — Unary RPC)</strong></p><p>Define the gRPC service using Protobuf (user.proto):</p><pre>syntax = &quot;proto3&quot;;<br><br>package user;<br><br>service UserService {<br> rpc GetUser (UserRequest) returns (UserResponse);<br>}<br><br>message UserRequest {<br> int32 id = 1;<br>}<br><br>message UserResponse {<br> int32 id = 1;<br> string name = 2;<br>}</pre><p>gRPC Server (Node.js — <strong>server.js):</strong></p><pre>const grpc = require(&quot;@grpc/grpc-js&quot;);<br>const protoLoader = require(&quot;@grpc/proto-loader&quot;);<br><br>const packageDefinition = protoLoader.loadSync(&quot;user.proto&quot;);<br>const userProto = grpc.loadPackageDefinition(packageDefinition).user;<br><br>const users = { 1: { id: 1, name: &quot;Alice&quot; }, 2: { id: 2, name: &quot;Bob&quot; } };<br><br>function getUser(call, callback) {<br> const user = users[call.request.id];<br> if (user) {<br>       callback(null, user);<br> } else {<br>       callback(new Error(&quot;User not found&quot;));<br> }<br>}<br><br>const server = new grpc.Server();<br>server.addService(userProto.UserService.service, { getUser });<br><br>server.bindAsync(&quot;0.0.0.0:50051&quot;, grpc.ServerCredentials.createInsecure(), () =&gt; {<br> console.log(&quot;gRPC server running on port 50051&quot;);<br>});</pre><p>gRPC Client (Node.js — <strong>client.js):</strong></p><pre>const grpc = require(&quot;@grpc/grpc-js&quot;);<br>const protoLoader = require(&quot;@grpc/proto-loader&quot;);<br><br>const packageDefinition = protoLoader.loadSync(&quot;user.proto&quot;);<br>const userProto = grpc.loadPackageDefinition(packageDefinition).user;<br><br>const client = new userProto.UserService(&quot;localhost:50051&quot;, grpc.credentials.createInsecure());<br><br>client.GetUser({ id: 1 }, (error, response) =&gt; {<br> if (!error) {<br>       console.log(&quot;User:&quot;, response);<br> } else {<br>       console.error(error.message);<br> }<br>});</pre><h3><strong>Advantages of gRPC over REST:</strong></h3><p>✅ <strong>High Performance:</strong> Uses binary data over HTTP/2 instead of text-based JSON.<br>✅ <strong>Real-Time Communication:</strong> Built-in support for streaming.<br>✅ <strong>Efficient Serialization:</strong> Protobuf is smaller and faster than JSON.<br>✅ <strong>Automatic Code Generation:</strong> Generates client and server code from .proto files.<br>✅ <strong>Cross-Language Compatibility:</strong> Supports multiple languages natively.</p><h3>gRPC with Bidirectional Streaming</h3><p>Suppose we have a RESTful API that sends stock market price updates to clients. Instead of polling, we want to migrate to gRPC with bidirectional streaming for real-time updates.</p><p>Step 1: Define the Protobuf (stock.proto)</p><pre>syntax = &quot;proto3&quot;;<br><br>package stock;<br><br>service StockService {<br> rpc StreamStockPrices (stream StockRequest) returns (stream StockResponse);<br>}<br><br>message StockRequest {<br> string symbol = 1;<br>}<br><br>message StockResponse {<br> string symbol = 1;<br> double price = 2;<br> string timestamp = 3;<br>}</pre><p>Step 2: Implement the gRPC Server (<strong>server.js)</strong></p><pre>const grpc = require(&quot;@grpc/grpc-js&quot;);<br>const protoLoader = require(&quot;@grpc/proto-loader&quot;);<br><br>async function init() {<br>  const packageDefinition = await protoLoader.load(&quot;stock.proto&quot;);<br>  const stockProto = grpc.loadPackageDefinition(packageDefinition).stock;<br><br>  const stockPrices = { AAPL: 150.5, TSLA: 700.3, AMZN: 3300.1 };<br><br>  function streamStockPrices(call) {<br>   call.on(&quot;data&quot;, (request) =&gt; {<br>   console.log(`Client requested updates for ${request.symbol}`);<br><br>   const interval = setInterval(() =&gt; {<br>     const price = (<br>       stockPrices[request.symbol] +<br>       Math.random() * 4 -<br>       2.5<br>     ).toFixed(2);<br>     <br>     console.log(`Sending updates for ${request.symbol}...`);<br>     call.write({<br>       symbol: request.symbol,<br>       price,<br>       timestamp: new Date().toISOString(),<br>     });<br>   }, 2000);<br><br>   call.on(&quot;end&quot;, () =&gt; {<br>     clearInterval(interval);<br>     call.end();<br>     });<br>   });<br>  }<br><br>  const server = new grpc.Server();<br>  server.addService(stockProto.StockService.service, { streamStockPrices });<br><br>  server.bindAsync(<br>    &quot;0.0.0.0:50052&quot;,<br>    grpc.ServerCredentials.createInsecure(),<br>    () =&gt; {<br>      console.log(&quot;gRPC server running on port 50052&quot;);<br>    }<br>  );<br>}<br><br>init();</pre><p>Step 3: Implement the gRPC Client (<strong>client.js)</strong></p><pre>const grpc = require(&quot;@grpc/grpc-js&quot;);<br>const protoLoader = require(&quot;@grpc/proto-loader&quot;);<br><br>const packageDefinition = protoLoader.loadSync(&quot;stock.proto&quot;);<br>const stockProto = grpc.loadPackageDefinition(packageDefinition).stock;<br><br>const client = new stockProto.StockService(<br>  &quot;localhost:50052&quot;,<br>  grpc.credentials.createInsecure()<br>);<br><br>const call = client.StreamStockPrices();<br><br>call.on(&quot;data&quot;, (response) =&gt; {<br>  console.log(<br> `Received update: ${response.symbol} - $${response.price} at ${response.timestamp}`<br>  );<br>});<br><br>call.write({ symbol: &quot;AAPL&quot; });<br>call.write({ symbol: &quot;TSLA&quot; });<br><br>setTimeout(() =&gt; call.end(), 10000);</pre><p>Now, on running the client.js file we start to get streams of AAPL and TSLA stock price changes in every 2 seconds for the next 10 seconds.</p><h4><strong>Pitfalls of Using gRPC</strong></h4><ol><li>Since gRPC uses <strong>binary Protocol Buffers</strong>, it’s <strong>harder to inspect requests and responses</strong> compared to JSON-based REST APIs.</li><li>Unlike JSON, <strong>ProtoBufs are binary</strong> and not human-readable.</li><li>Developers familiar with REST <strong>must learn ProtoBufs, service definitions, and gRPC concepts (streams, interceptors, etc.).</strong></li></ol><h3>Bonus Topic:</h3><h4>Binary Serialization</h4><p>Binary serialization means that data is stored in a compact, machine-readable format instead of human-readable text like JSON or XML. Protocol Buffers (Protobuf) encode data as a sequence of bytes, making it much <strong>smaller</strong> and <strong>faster</strong> to process.</p><p><strong>How Protobuf Encodes Data?</strong></p><p>For our <strong>service.proto</strong> file:</p><pre>message HelloRequest {<br>  string name = 1;<br>}<br><br>message HelloResponse {<br>  string message = 1;<br>}</pre><p>When we send HelloRequest:</p><pre>{<br>  &quot;name&quot;: &quot;Rahul&quot;<br>}</pre><p><strong>JSON Encoding (Text-Based)</strong>:</p><pre>{<br>  &quot;name&quot;: &quot;Rahul&quot;<br>}</pre><ul><li>Stored as <strong>22 bytes</strong> ({“name”:”Rahul”}).</li><li>Requires additional parsing steps when deserializing.</li></ul><p><strong>Protobuf Encoding (Binary Format)</strong>:</p><ol><li><strong>Field Number:</strong> name = 1</li><li><strong>Type:</strong> string (which is encoded as length-prefixed)</li><li><strong>Binary Representation:</strong></li></ol><pre>0A 05 52 61 68 75 6C</pre><ul><li>0A → Field number 1 + wire type (2 for length-delimited)</li><li>05 → Length of the string (Rahul is 5 characters)</li><li>52 61 68 75 6C → UTF-8 encoding of “Rahul”</li></ul><p>Thus, <strong>Protobuf encoding is only 7 bytes</strong>, compared to 22 bytes in JSON.</p><p>✅ <strong>Protobuf is faster</strong> in serialization/deserialization than JSON.<br>✅ <strong>Data size is smaller</strong>, reducing bandwidth usage.<br>✅ <strong>More efficient for large-scale, real-time gRPC applications.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df07e44e209c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Just-In-Time (JIT) Compilation in V8: A Deep Dive]]></title>
            <link>https://medium.com/@rahul.jindal57/understanding-just-in-time-jit-compilation-in-v8-a-deep-dive-c98b09c6bf0c?source=rss-2b5295e46a98------2</link>
            <guid isPermaLink="false">https://medium.com/p/c98b09c6bf0c</guid>
            <category><![CDATA[advanced]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[v8-engine]]></category>
            <category><![CDATA[jvm]]></category>
            <dc:creator><![CDATA[Rahul Jindal]]></dc:creator>
            <pubDate>Sat, 08 Mar 2025 07:13:39 GMT</pubDate>
            <atom:updated>2025-03-08T07:13:39.540Z</atom:updated>
            <content:encoded><![CDATA[<h4>Exploring V8’s JIT Compilation, Ignition, and TurboFan for Optimized Performance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Ai4vcZ9hI993QDHoperTsw.png" /><figcaption>Just-In-Time (JIT)</figcaption></figure><h3>Introduction</h3><p>Just-In-Time (JIT) compilation is a hybrid execution strategy that combines interpretation and <strong>ahead-of-time (AOT)</strong> compilation. It is the cornerstone of modern JavaScript engines like <strong>V8</strong>, enabling dynamic languages to achieve near-native performance. V8, used in <strong>Chrome</strong> and <strong>Node.js</strong>, employs a multi-tiered architecture to parse, interpret, profile, and optimize JavaScript code. This document provides a granular, step-by-step breakdown of V8’s execution pipeline, including code transformations at each stage, profiling mechanics, and the role of <strong>TurboFan</strong> (V8’s JIT compiler).</p><blockquote>Ahead-of-Time (AOT) compilation is a compilation strategy where source code is converted into machine code <strong>before execution</strong> rather than at runtime. This contrasts with <strong>Just-In-Time (JIT)</strong> compilation, which compiles code dynamically during execution. AOT compilation is commonly used in languages like <strong>C, C++, Rust, and Go </strong>to generate highly optimized binaries before deployment.</blockquote><h3>1. Parsing JavaScript Code</h3><h4><strong>Lexical Analysis and Syntax Parsing</strong></h4><p>When JavaScript code is loaded, V8 begins with <strong>lexical analysis</strong>, breaking the source code into tokens (keywords, identifiers, literals). For example:</p><pre>function sumArray(arr) {<br>    let sum = 0;<br>    for (let i = 0; i &lt; arr.length; i++) {<br>        sum += arr[i];<br>    }<br>    return sum;<br>}</pre><ul><li><strong>Tokenization</strong>: The parser generates tokens like function, sumArray, (, {, let, sum, etc.</li><li><strong>Syntax Validation</strong>: Checks for syntax errors (e.g., missing braces).</li></ul><h4><strong>Abstract Syntax Tree (AST) Generation</strong></h4><p>The parser constructs an AST, a hierarchical representation of code structure. For sumArray, the AST includes:</p><pre>FunctionDeclaration node with sumArray as the name.<br>    BlockStatement for the function body.<br>        VariableDeclaration for sum.<br>            ForStatement for the loop.<br>                VariableDeclaration for i.<br>                BinaryExpression (i &lt; arr.length).<br>                UpdateExpression (i++).<br>                ExpressionStatement (sum += arr[i]).</pre><h4><strong>Scope Analysis</strong></h4><p>V8 analyzes variable scopes. In sumArray, arr, sum, and i are allocated to specific scopes. This step prevents variable leaks and resolves hoisting.</p><h3><strong>2. Bytecode Generation with Ignition</strong></h3><h4><strong>What is Bytecode?</strong></h4><p>Bytecode is a compact, platform-agnostic intermediate representation. Unlike machine code, it is executed by V8’s interpreter, <strong>Ignition</strong>.</p><h4>Bytecode for sumArray</h4><p>Ignition compiles the AST into bytecode. Simplified output for sumArray:</p><pre>LdaZero              ; Load 0 into accumulator<br>Star sum             ; Store accumulator in register &#39;sum&#39;<br>LdaZero              ; Load 0 into accumulator<br>Star i               ; Store in register &#39;i&#39;<br>LdaCurrentContext arr   ; Load &#39;arr&#39; from the current context<br>JumpIfUndefined Done ; Exit if &#39;arr&#39; is undefined<br>Loop:<br>  Ldar i             ; Load &#39;i&#39; into accumulator<br>  LdaCurrentContext arr ; Load &#39;arr&#39;<br>  GetProperty length ; Get arr.length<br>  CompareLT          ; Compare i &lt; arr.length<br>  JumpIfFalse Done   ; Exit loop if false<br>  LdaCurrentContext arr ; Load &#39;arr&#39;<br>  Ldar i             ; Load &#39;i&#39;<br>  GetElement         ; Load arr[i]<br>  Add sum            ; Add sum to accumulator<br>  Star sum           ; Store result in &#39;sum&#39;<br>  Inc i              ; Increment &#39;i&#39;<br>  Jump Loop          ; Repeat loop<br>Done:<br>  Ldar sum           ; Load &#39;sum&#39; into accumulator<br>  Return             ; Return accumulator</pre><p><strong><em>*Note:</em></strong><em> Some instructions may not be exactly correct, the example is just giving a very high level idea as to what might be happening in this phase.</em></p><h3><strong>3. Interpreting and Profiling</strong></h3><h4>Initial Execution via Ignition</h4><p>Ignition executes bytecode line-by-line. For example:</p><pre>const numbers = new Array(1_000_000).fill(1);<br>console.time(&quot;Interpreted&quot;);<br>for (let i = 0; i &lt; 100; i++) {<br> sumArray(numbers); // Runs via bytecode<br>}<br>console.timeEnd(&quot;Interpreted&quot;); // ~1s<br><br>// Command: node --no-opt &lt;filename&gt;.js</pre><h4><strong>Profiling Hotspots</strong></h4><p>V8’s <strong>Runtime Profiler</strong> tracks:</p><ul><li><strong>Function Call Frequency</strong>: How often sumArray is invoked.</li><li><strong>Type Feedback</strong>: Observed types for variables (e.g., arr is an Array).</li><li><strong>Loop Iterations</strong>: The for loop runs 1,000,000 times, marking it as a hotspot.</li></ul><h4>Inline Caches (ICs)</h4><ul><li><strong>Monomorphic vs. Polymorphic</strong>: If arr is always an Array, the IC is monomorphic (fast path). If arr sometimes isn’t, the IC becomes polymorphic (slower).</li><li>Example IC state for arr[i]: Slot 0: Array (monomorphic).</li></ul><h3><strong>4. JIT Compilation with TurboFan</strong></h3><p>The <strong>JIT compiler</strong> translates hot bytecode into <strong>highly optimized native machine code</strong>, applying <strong>various optimizations</strong>.</p><h4><strong>Optimization Steps:</strong></h4><ul><li><strong>Control Flow Graph (CFG)</strong>: TurboFan converts bytecode into a graph of basic blocks.</li><li><strong>Type Specialization</strong>: Based on profiled types:<br>1. arr is an Array.<br>2. i and sum are integers.</li><li><strong>Optimization Techniques</strong>:<br>1. <strong>Loop-Invariant Code Motion</strong>: Moves arr.length outside the loop.<br>2. <strong>Bounds Check Elimination</strong>: Removes checks for arr[i] (if i is within bounds).<br>3. <strong>Inlining</strong>: Directly embeds arr[i] access without function calls.<br>4. <strong>Constant Folding</strong>: Precompute expressions that involve constants at compile time.</li><li><strong>Machine Code Generation</strong>: Produces machine code.</li></ul><h4><strong>Optimized Machine Code for sumArray</strong></h4><p>Simplified x86 assembly:</p><pre>; Initialize sum (rax) and i (rcx) to 0<br>xor rax, rax<br>xor rcx, rcx<br>; Load arr.length into rdx<br>mov rdx, [rdi + 0x10]  ; Assume rdi holds the array<br>cmp rcx, rdx<br>jge Done<br>Loop:<br>  ; Load arr[i] into rbx (no bounds check)<br>  mov rbx, [rdi + 0x20 + rcx*8]<br>  add rax, rbx<br>  inc rcx<br>  cmp rcx, rdx<br>  jl Loop<br>Done:<br>  ret</pre><ul><li><strong><em>Note:</em></strong><em> Some instructions may not be exactly correct, the example is just giving a very high level idea as to what might be happening in this phase.</em></li></ul><h4><strong>Execution of Optimized Code</strong></h4><p>Subsequent calls use the machine code:</p><pre>console.time(&quot;Optimized&quot;);<br>for (let i = 0; i &lt; 100; i++) {<br> sumArray(numbers); // Runs via machine code<br>}<br>console.timeEnd(&quot;Optimized&quot;); // ~100ms (10x faster)<br><br>// Command1: node &lt;filename&gt;.js<br>// Command2: node --trace-opt --trace-deopt &lt;filename&gt;.js</pre><h3><strong>5. Deoptimization: When Assumptions Fail</strong></h3><h4>Causes of Deoptimization</h4><ul><li><strong>Type Mismatch</strong>: Passing a non-Array to sumArray.</li><li><strong>Polymorphic Calls</strong>: Multiple object shapes (e.g., mixed arrays and strings).</li></ul><pre>sumArray({ length: 100 }); // Deoptimizes: &#39;arr&#39; is not an array</pre><h4><strong>Deoptimization Process</strong></h4><ol><li><strong>Bailout</strong>: TurboFan’s machine code detects invalid assumptions.</li><li><strong>Reversion to Bytecode</strong>: Execution returns to Ignition with updated ICs.</li><li><strong>Recompilation</strong>: TurboFan may recompile with broader type guards.</li></ol><p><strong>Example Deoptimization:</strong></p><pre>const mixed = [1, &quot;2&quot;, 3];<br>const arr1 = new Array(100000).fill(1)<br>const arr2 = new Array(10).fill(null).map(() =&gt; mixed[Math.floor(Math.random() * mixed.length)]);<br>const arr3 = new Array(100000).fill(1)<br>console.time(&quot;Deoptimized&quot;);<br>sumArray([...arr1, ...arr2, ...arr3]);<br>console.timeEnd(&quot;Deoptimized&quot;); // Slower due to interpreter fallback</pre><p><strong>Output:</strong></p><pre>// node –trace-opt –trace-deopt &lt;filename&gt;.js<br><br>[marking 0x3b723ff7ba81 &lt;JSFunction (sfi = 0x11ddec048519)&gt; for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: hot and stable]<br>[compiling method 0x3b723ff7ba81 &lt;JSFunction (sfi = 0x11ddec048519)&gt; (target TURBOFAN) OSR, mode: ConcurrencyMode::kConcurrent]<br>[marking 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; for optimization to TURBOFAN, ConcurrencyMode::kConcurrent, reason: small function]<br>[compiling method 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR, mode: ConcurrencyMode::kConcurrent]<br>[completed compiling 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR - took 0.000, 0.417, 0.000 ms]<br>[completed optimizing 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR]<br>[bailout (kind: deopt-eager, reason: not a Smi): begin. deoptimizing 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt;, 0x11ddec049db1 &lt;Code TURBOFAN&gt;, opt id 1, bytecode offset 4, deopt exit 10, FP to SP delta 96, caller SP 0x00016ba7e300, pc 0x000109fd877c]<br>[compiling method 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR, mode: ConcurrencyMode::kConcurrent]<br>[completed compiling 0x3b723ff7ba81 &lt;JSFunction (sfi = 0x11ddec048519)&gt; (target TURBOFAN) OSR - took 0.041, 3.375, 0.000 ms]<br>[completed optimizing 0x3b723ff7ba81 &lt;JSFunction (sfi = 0x11ddec048519)&gt; (target TURBOFAN) OSR]<br>[completed compiling 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR - took 0.000, 0.542, 0.000 ms]<br>[completed optimizing 0x3b723ff7ba41 &lt;JSFunction sumArray (sfi = 0x11ddec0485d1)&gt; (target TURBOFAN) OSR]<br>Deoptimized: 8.361ms</pre><ul><li>From the output it can be observed that code is getting optimized initially but later on getting deoptimized when types do not match anymore.</li><li>Later on function is getting optimized again when type does not get changed.</li></ul><h3><strong>6. The Role of Bytecode in V8</strong></h3><h4>Bytecode Advantages</h4><ul><li><strong>Portability</strong>: Runs on any OS/architecture with V8.</li><li><strong>Ease of Optimization</strong>: Serves as a stable IR (Intermediate Representation)for TurboFan.</li><li><strong>Memory Efficiency</strong>: Smaller size than machine code (critical for large apps).</li></ul><h3><strong>7. Advanced Optimization Techniques</strong></h3><h4>Object Property Access involving Hidden Class Transitions</h4><p>Object property access is a critical operation in JavaScript performance. JIT compilers optimize property access using <strong>hidden classes</strong> and <strong>inline caching</strong>. However, inefficient property access patterns can degrade performance due to increased polymorphism.</p><h4>Understanding Monomorphic, Polymorphic, and Megamorphic Property Access</h4><p>JIT compilation categorizes property accesses into <strong>monomorphic, polymorphic, and megamorphic states</strong>:</p><p><strong>1. Monomorphic Property Access (Fastest)</strong></p><ul><li>The same property is accessed on objects of the same <strong>hidden class</strong> (also called <strong>shape</strong> or <strong>structure</strong>).</li><li>Example:</li></ul><pre>function getName(obj) {<br>  return obj.name; // Always accesses &#39;name&#39; from the same structure.<br>}<br><br>const person1 = { name: &quot;Alice&quot;, age: 25 };<br>const person2 = { name: &quot;Bob&quot;, age: 30 };<br><br>console.log(getName(person1)); // Monomorphic access<br>console.log(getName(person2)); // Still monomorphic (same shape)</pre><ul><li>Since both person1 and person2 share the same hidden class, the JIT compiler generates <strong>efficient inline caches</strong>.</li></ul><p><strong>2. Polymorphic Property Access (Moderate Performance)</strong></p><ul><li>When an access pattern involves multiple hidden classes, but the number of variations is <strong>small</strong> (typically ≤ 4).</li><li>Example:</li></ul><pre>function getName(obj) {<br>  return obj.name;<br>}<br><br>const person = { name: &quot;Alice&quot;, age: 25 };  // Hidden Class #1<br>const animal = { name: &quot;Charlie&quot;, breed: &quot;Dog&quot; }; // Hidden Class #2<br><br>console.log(getName(person)); // Monomorphic initially<br>console.log(getName(animal)); // Becomes polymorphic</pre><ul><li>The JIT compiler maintains a <strong>small inline cache</strong> for different object structures, but performance slightly degrades compared to monomorphic access.</li></ul><p><strong>3. Megamorphic Property Access (Slowest)</strong></p><ul><li>If the property is accessed on many different object shapes, inline caching becomes ineffective.</li><li>Example:</li></ul><pre>function getName(obj) {<br>  return obj.name;<br>}<br><br>const car = { name: &quot;Tesla&quot;, model: &quot;S&quot; }; // Hidden Class #1<br>const book = { name: &quot;1984&quot;, author: &quot;Orwell&quot; }; // Hidden Class #2<br>const city = { name: &quot;Paris&quot;, population: 2000000 }; // Hidden Class #3<br>const fruit = { name: &quot;Apple&quot;, color: &quot;Red&quot; }; // Hidden Class #4<br>const computer = { name: &quot;MacBook&quot;, brand: &quot;Apple&quot; }; // Hidden Class #5<br><br>console.log(getName(car));<br>console.log(getName(book));<br>console.log(getName(city));<br>console.log(getName(fruit));<br>console.log(getName(computer)); // Megamorphic state triggers</pre><ul><li>When too many different hidden classes are accessed, <strong>inline caches degrade</strong>, and the engine switches to <strong>generic property lookup</strong>, which is <strong>significantly slower</strong>.</li></ul><h3>Conclusion</h3><p>V8’s JIT compilation pipeline is a sophisticated blend of interpretation, profiling, and adaptive optimization:</p><ol><li><strong>Parsing</strong>: Source code → AST with scope resolution.</li><li><strong>Bytecode Generation</strong>: <strong>Ignition</strong> produces portable bytecode.</li><li><strong>Profiling</strong>: Runtime data identifies hotspots and type patterns.</li><li><strong>JIT Compilation</strong>: <strong>TurboFan</strong> generates type-specialized machine code.</li><li><strong>Deoptimization</strong>: Safeguards against invalid assumptions, reverting to bytecode.</li></ol><p>This architecture allows JavaScript to balance dynamic flexibility with performance, leveraging runtime feedback to optimize hot code paths while gracefully handling edge cases. Developers can exploit this by writing predictable, type-stable code to minimize deoptimization and maximize TurboFan’s optimizations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c98b09c6bf0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>