<?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 Ankit on Medium]]></title>
        <description><![CDATA[Stories by Ankit on Medium]]></description>
        <link>https://medium.com/@ankitpandeycu?source=rss-a2de71368544------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wgGvqVQoeb-Rl-AgSu7aAQ@2x.jpeg</url>
            <title>Stories by Ankit on Medium</title>
            <link>https://medium.com/@ankitpandeycu?source=rss-a2de71368544------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:42:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankitpandeycu/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[Strong Consistency vs Eventual Consistency in System Design]]></title>
            <link>https://ankitpandeycu.medium.com/strong-consistency-vs-eventual-consistency-in-system-design-9390b52a5705?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/9390b52a5705</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Sat, 14 Feb 2026 18:32:15 GMT</pubDate>
            <atom:updated>2026-02-14T18:32:15.392Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*C6JYJz-OFm-RtjdoiRGsfg.png" /><figcaption>Source: Google</figcaption></figure><p>If you’ve ever prepared for a <strong>system design interview</strong>, you’ve probably faced this question:</p><blockquote><em>“Would you use strong consistency or eventual consistency?”</em></blockquote><p>Most candidates answer with definitions.</p><p>Few answer with trade-offs.</p><p>And that’s the difference between a decent answer and a senior-level answer.</p><p>In this guide, we’ll cover:</p><ul><li>What strong consistency really means</li><li>What eventual consistency really means</li><li>5 real-world system design examples</li><li>A mental model to answer this in interviews</li></ul><h3>What Is Strong Consistency?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YNdDjrHCla2F-LeyrZW61w.png" /><figcaption>Strong Consistency: All changes are immediately reflected</figcaption></figure><p>In distributed systems, <strong>strong consistency</strong> guarantees:</p><p>After a write completes, every subsequent read returns the latest value.</p><p>No stale reads.<br> No temporary divergence.<br> All replicas reflect the same state.</p><h3>Benefits:</h3><ul><li>Data correctness</li><li>No ambiguity</li><li>Safe for financial or critical systems</li></ul><h3>Costs:</h3><ul><li>Higher latency</li><li>Coordination overhead (leader, quorum, locking)</li><li>Reduced availability during failures</li></ul><p>You are optimizing for <strong>correctness over performance</strong>.</p><h3>What Is Eventual Consistency?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yoW6CCSuGoJRykfsoMDTyw.png" /><figcaption>Eventual Consistency: Changes are reflected with delay</figcaption></figure><p><strong>Eventual consistency</strong> guarantees:</p><p>If no new updates occur, all replicas will eventually converge to the same value.</p><p>However, during normal operation:</p><ul><li>Some reads may return stale data</li><li>Different users may temporarily see different values</li></ul><h3>Benefits:</h3><ul><li>High availability</li><li>Low latency</li><li>Better scalability</li></ul><h3>Trade-off:</h3><p>Temporary inconsistency is allowed. You are optimizing for <strong>performance and availability over immediate correctness</strong>.</p><h3>The Mental Model Interviewers Actually Want</h3><p>Stop asking:</p><p><em>“Which one is better?”</em></p><p>Start asking:</p><p><em>“What happens if this data is stale?”</em></p><p>Consistency decisions in distributed systems are business decisions.</p><p>If stale data causes:</p><ul><li>Financial loss</li><li>Data corruption</li><li>Security issues</li></ul><p>You need strong consistency.</p><p>If stale data only causes:</p><ul><li>Temporary UI mismatch</li><li>Slight delay in metrics</li><li>Minor visual inconsistency</li></ul><p>Eventual consistency is acceptable.</p><p>This framing immediately improves your system design answer quality.</p><h3>5 System Design Examples (With Trade-off Evaluation)</h3><h3>1. Bank Account Balance System</h3><p>Scenario:<br> A user transfers money and immediately checks balance.</p><p>If reads are stale:</p><ul><li>Double spending may occur</li><li>Financial records become inconsistent</li></ul><p>Decision: <strong>Strong Consistency</strong></p><p>For payment systems and ledgers, correctness is non-negotiable.</p><h3>2. Instagram or Social Media Like Count</h3><p><strong>Scenario:</strong><br> You like a post. Another user sees the old count for 2 seconds.</p><p>Impact?<br> None significant.</p><p>Decision: <strong>Eventual Consistency</strong></p><p>Social feeds prioritize:</p><ul><li>Low latency</li><li>Global scalability</li><li>High availability</li></ul><p>Temporary inaccuracy is acceptable.</p><h3>3. E-commerce Inventory During Flash Sale</h3><p><strong>Scenario:</strong><br> Only 1 item left. Two users click “Buy” at the same time.</p><p>If inventory updates are eventually consistent:<br> Overselling happens.</p><p>Decision:</p><ul><li><strong>Strong consistency for stock deduction</strong></li><li>Eventual consistency for product views or recommendations</li></ul><p>This is a classic hybrid consistency model.</p><h3>4. Messaging System (Chat Application)</h3><p><strong>Scenario:</strong><br> Users send messages in a conversation.</p><p>Requirements:</p><ul><li>Correct message ordering within a chat</li><li>Global replication across regions</li></ul><p>Decision:</p><ul><li>Strong consistency within a chat shard</li><li>Eventual consistency across regions</li></ul><p>Most real-world distributed systems mix both models.</p><h3>5. Analytics Dashboard</h3><p><strong>Scenario:</strong><br>Admin views revenue graph.</p><p>Data delayed by <strong>10 seconds</strong>.</p><p>Is it critical?<br> Usually not.</p><p>Decision: <strong>Eventual Consistency</strong></p><p>Analytics pipelines are often:</p><ul><li>Write-heavy</li><li>Aggregation-based</li><li>Optimized for throughput</li></ul><p>Immediate accuracy is less critical than scalability.</p><h3>Advanced Insight: Consistency Is Not Binary</h3><p>In real system architecture:</p><ul><li>Payments → Strong consistency</li><li>Feeds → Eventual consistency</li><li>Logs → Eventual consistency</li><li>Configuration metadata → Strong consistency</li></ul><p>Within the same distributed system, multiple consistency models coexist.</p><p>If you suggest only one model everywhere in an interview, you signal inexperience.</p><h3>How to Answer This in a System Design Interview</h3><p>When asked:</p><p>“Should we use strong or eventual consistency?”</p><p>Structure your answer:</p><ol><li>Identify the data domain.</li><li>Evaluate business impact of stale reads.</li><li>Analyze latency and availability requirements.</li><li>Decide consistency level.</li><li>Mention hybrid model possibility.</li></ol><p>This structured thinking signals senior-level understanding.</p><h3>Final Interview Shortcut</h3><p>Ask yourself:</p><p>If stale data exists for 5 seconds…</p><p>Does it cause:</p><ul><li>Monetary damage?</li><li>Legal issues?</li><li>Data corruption?</li></ul><p><strong>If yes</strong> → Choose strong consistency.</p><p><strong>If no</strong> → Eventual consistency is likely acceptable.</p><p><em>Hope it helps!</em></p><p><strong>AnkitCode99 here….</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9390b52a5705" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Zomato Handled 7.5 Million Orders in One Day Without Crashing ]]></title>
            <link>https://ankitpandeycu.medium.com/how-zomato-handled-7-5-million-orders-in-one-day-without-crashing-614486c68513?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/614486c68513</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Thu, 12 Feb 2026 18:19:16 GMT</pubDate>
            <atom:updated>2026-02-12T18:19:16.027Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Zomato Handled 7.5 Million Orders in One Day Without Crashing (Bulkhead Pattern Explained)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*0_V9AvFbHtr5bUgANm99Pg.png" /><figcaption>Image Source: X</figcaption></figure><p><em>At massive scale, failures are guaranteed. The real engineering challenge is preventing one small issue from collapsing everything.</em></p><p>On New Year’s Eve, Zomato processed <strong>7.5 million orders in a single day</strong> — one of the highest traffic spikes for any consumer internet platform in India.</p><p>At that scale, distributed systems don’t fail <em>if</em> — they fail <em>when</em>.</p><ul><li>A payment gateway will timeout.</li><li>A service will spike in latency.</li><li>A database replica will lag.</li><li>A pod will restart.</li></ul><p>The real engineering question isn’t how to scale.</p><p>It’s this:</p><blockquote>When something fails, does the entire system collapse?</blockquote><p>What likely prevented that collapse is a distributed systems principle called the <strong>Bulkhead Pattern</strong> — a failure isolation strategy used in high-traffic microservices architectures.</p><h3>Why Large-Scale Distributed Systems Fail</h3><p>In early-stage systems, services are often tightly coupled:</p><ul><li>Shared thread pools</li><li>Shared database connections</li><li>Synchronous service-to-service calls</li><li>Common infrastructure boundaries</li></ul><p>This works — until traffic spikes or a dependency degrades.</p><p>Now imagine New Year’s Eve traffic.</p><p>Orders surge to <strong>150–200+ per second</strong>. Payment latency increases from 200ms to 3 seconds.</p><p>What happens next in a poorly isolated system?</p><ul><li>Threads block.</li><li>Connection pools exhaust.</li><li>Retry storms begin.</li><li>CPU spikes.</li><li>Upstream services back up.</li><li>Health checks fail.</li><li>Pods restart.</li><li>Everything spirals.</li></ul><p>You didn’t just have a payment issue. You had a <strong>cascading failure</strong>, and cascading failures are what bring large systems down.</p><h3>What Is the Bulkhead Pattern in micro-services Architecture?</h3><p>In ships, bulkheads divide compartments. If one section floods, the entire ship doesn’t sink.</p><p>In distributed systems, bulkheads isolate:</p><ul><li>Services</li><li>Resources</li><li>Failure domains</li></ul><p>So when one component fails, others continue operating.</p><p>It’s not just about splitting services.</p><p>It’s about splitting <strong><em>blast radius</em></strong>.</p><h3>How Failure Isolation Likely Shows Up in Zomato’s Architecture</h3><p>You’ve probably experienced this yourself.</p><ul><li>Payment failed, but browsing worked.</li><li>Tracking lagged, but orders were placed.</li><li>Notifications didn’t arrive, but food still showed up.</li></ul><p>That’s not luck. That’s isolation.</p><p>Let’s break down what bulkheads look like in practice.</p><h3>1. Separate Resource Pools</h3><p>Payment service does not share:</p><ul><li>Thread pools</li><li>Memory pools</li><li>DB connection pools</li></ul><p>With order ingestion service. If payment blocks, order processing still functions.</p><p>Isolation at the resource level prevents exhaustion spillover.</p><h3>2. Independent Scaling Boundaries</h3><p>Delivery tracking spikes?</p><p>Scale tracking horizontally.</p><p>Order ingestion remains unaffected.</p><ul><li>Separate autoscaling groups.</li><li>Separate Kubernetes deployments</li><li>Separate compute pools.</li></ul><p>Different scaling domains = smaller failure domains.</p><h3>3. Async Boundaries for Non-Critical Services</h3><p>Notifications, analytics, recommendations — these should not sit on the critical path.</p><p>Instead, they pass through queues.</p><p>If notifications lag, orders are still placed.</p><p>Async boundaries absorb spikes and prevent synchronous collapse.</p><h3>4. Controlled Retries and Circuit Breakers</h3><p>Unbounded retries destroy systems.</p><p>Bulkhead-aware architectures use:</p><ul><li>Circuit breakers</li><li>Timeouts</li><li>Bounded retries</li><li>Fail-fast strategies</li></ul><p>Instead of retry storms amplifying load, the system degrades gracefully.</p><h3>Microservices Alone Don’t Guarantee Isolation</h3><p>Important distinction:</p><p>Just having microservices does NOT mean you have bulkheads.</p><p>If:</p><ul><li>All services share the same database cluster</li><li>All calls are synchronous</li><li>All retries are aggressive</li><li>All services run in the same node pool</li></ul><p>You’ve distributed services.</p><p>But you haven’t reduced the blast radius.</p><p>Bulkhead is about <strong>failure domain boundaries</strong>, not service count.</p><h3>Scaling vs Resilience: The Real System Design Mindset</h3><p>Most engineers think scaling solves everything.</p><p>Add servers. Add replicas. Add caching.</p><p>Scaling handles load.</p><p>Bulkheads handle chaos.</p><p>At 7.5 million orders per day, load is predictable.</p><p>Failures are not.</p><p>Senior engineers don’t just ask:</p><p>“How will this scale?”</p><p>They ask:</p><p>“If this service dies, who dies with it?”</p><p>That question defines resilience.</p><h3>Why This Matters in System Design Interviews</h3><p>In system design interviews, candidates often focus on:</p><ul><li>Horizontal scaling</li><li>Caching strategies</li><li>Database sharding</li></ul><p>But experienced interviewers look for something deeper:</p><p>Understanding of <strong>failure isolation</strong>.</p><p>Because real-world distributed systems don’t break under normal load.</p><p>They break under partial failure.</p><p>Explaining blast radius control, circuit breaking, and resource isolation is often what differentiates mid-level engineers from senior engineers.</p><h3>Architecture Comparison: With vs Without Bulkheads</h3><p>Without Isolation:</p><p>User → API → Payment → Order → Tracking → Shared DB<br> (All sharing threads, connections, infrastructure)</p><p>With Bulkhead Pattern:</p><p>User → API<br> → Order Service (isolated resources)<br> → Payment Service (separate pool)<br> → Tracking Service (independent scaling)<br> → Notification Queue (async boundary)</p><p>Now when Payment degrades:</p><p>Orders still process.<br> Tracking still updates.<br> System bends — but doesn’t collapse.</p><h3>Final Thought</h3><p>Handling 7.5 million orders in a day isn’t just about:</p><ul><li>Horizontal scaling</li><li>Load balancing</li><li>Caching</li></ul><p>It’s about ensuring that when something inevitably breaks, the failure stays contained.</p><p>That’s the difference between:</p><p>“<strong>Some users facing issues</strong>”<br> and<br>“<strong>The platform is down.</strong>”</p><p>The next time you see only one feature fail in an app…</p><p>Don’t call it a bug. Call it containment.</p><p><em>Hope it helps!</em></p><p><strong>AnkitCode99 here….</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=614486c68513" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Thundering Herd Problem Explained Through Twitter’s Write Path]]></title>
            <link>https://ankitpandeycu.medium.com/the-thundering-herd-problem-explained-through-twitters-write-path-32300a3f7d53?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/32300a3f7d53</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Wed, 04 Feb 2026 19:10:16 GMT</pubDate>
            <atom:updated>2026-02-04T19:10:16.657Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*K7C7CC7n9A4OLegQfUXwIQ.jpeg" /><figcaption>Image Credit: Reddit</figcaption></figure><p>When India won the 2024 T20 World Cup, something predictable happened.</p><p>Millions of people picked up their phones and tweeted at almost the <strong>exact same moment</strong>.</p><p>From a systems perspective, this is not just “high traffic”.<br> This is a classic distributed systems problem called the <strong>thundering herd problem</strong> — and if not handled correctly, it can bring even large platforms down.</p><p>In this post, I’ll break down <strong>how large-scale systems like Twitter conceptually handle massive write spikes</strong>, without going into any proprietary details.</p><h3>What Is the Thundering Herd Problem?</h3><p>The thundering herd problem happens when a <strong>huge number of requests hit the same system at the same time</strong>, competing for shared resources.</p><p>In write-heavy systems, this usually means:</p><ul><li>sudden database overload</li><li>lock contention</li><li>retries making the spike worse</li><li>cascading failures across services</li></ul><p>Important distinction:</p><blockquote><em>This is not a traffic problem.<br>This is a </em><strong><em>synchronization problem</em></strong><em>.</em></blockquote><h3>Why “Just Scale More Servers” Doesn’t Work</h3><p>A common beginner instinct is:</p><blockquote><em>“If traffic increases, just add more servers.”</em></blockquote><p>That approach fails for three reasons:</p><ol><li><strong>Auto-scaling is slow</strong><br> Scaling infrastructure takes seconds or minutes. Viral events happen in milliseconds.</li><li><strong>Databases don’t scale like stateless APIs</strong><br> You can spin up API servers quickly. Databases are the real bottleneck.</li><li><strong>Retries amplify the herd</strong><br> When writes fail, clients retry — increasing pressure exactly when the system is weakest.</li></ol><p>This is why large systems <strong>don’t send every write directly to the database</strong>.</p><h3>The Core Idea: Absorb the Spike, Don’t Fight It</h3><p>The goal is simple:</p><blockquote><strong><em>Don’t stop the spike. Smooth it.</em></strong></blockquote><p>Instead of handling writes synchronously, large-scale systems <strong>buffer, distribute, and process writes asynchronously</strong>.</p><p>This is the key mental model.</p><h4>Conceptual Write Flow (High Level)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGF8CGCCHIwNNWt_v0YZaA.png" /></figure><h4>1. Write Buffering Using Queues</h4><p>When millions of tweets arrive simultaneously, they are <strong>not written directly to the database</strong>.</p><p>Instead, systems use a <strong>queue</strong> as a shock absorber.</p><p>Why queues matter:</p><ul><li>They absorb sudden spikes</li><li>They decouple user experience from backend load</li><li>They allow controlled processing at a sustainable rate</li></ul><p>Common tools used in industry:</p><ul><li>Apache Kafka</li><li>AWS SQS</li><li>RabbitMQ</li></ul><p>Helpful reference: <a href="https://martinfowler.com/articles/patterns-of-distributed-systems/queue-based-load-leveling.html">https://martinfowler.com/articles/patterns-of-distributed-systems/queue-based-load-leveling.html</a></p><h4>2. Sharding to Avoid Hotspots</h4><p>Even with queues, writing everything to a single database node would fail.</p><p>That’s where <strong>sharding</strong> comes in.</p><p>Writes are distributed across partitions based on keys like:</p><ul><li>user_id</li><li>tweet_id</li><li>hash-based partitioning</li></ul><p>This ensures:</p><ul><li>no single database node becomes a hotspot</li><li>writes can be processed in parallel</li></ul><p>Good primer on sharding: <a href="https://www.digitalocean.com/community/tutorials/understanding-database-sharding">https://www.digitalocean.com/community/tutorials/understanding-database-sharding</a></p><h4>3. Asynchronous Processing for Fast User Experience</h4><p>From the user’s perspective:</p><ul><li>they hit “Tweet”</li><li>the tweet appears instantly</li></ul><p>Behind the scenes:</p><ul><li>the request is acknowledged early</li><li>actual persistence happens asynchronously</li></ul><p>This design ensures:</p><ul><li>low latency for users</li><li>backend systems stay protected</li><li>failures can be retried safely</li></ul><p>This pattern is often called <strong>async write-behind</strong>.</p><p>Reference: <a href="https://docs.microsoft.com/en-us/azure/architecture/patterns/async-request-reply">https://docs.microsoft.com/en-us/azure/architecture/patterns/async-request-reply</a></p><h3>What About Read Traffic?</h3><p>Write spikes are only half the story.</p><p>Once tweets are written:</p><ul><li>timelines need to be updated</li><li>caches get refreshed</li><li>recommendation systems react</li></ul><p>Read-path handling is a separate (and even harder) problem involving:</p><ul><li>fan-out strategies</li><li>cache warming</li><li>eventual consistency</li></ul><p>I’ll cover that in a separate post.</p><h3>How to Explain This in Interviews (30-Second Version)</h3><p>If you remember only one thing, remember this:</p><blockquote><em>“During massive write spikes, large-scale systems don’t write directly to the database.<br> They buffer writes using queues, distribute them across shards, and process them asynchronously.<br> This smooths traffic spikes and prevents cascading failures.”</em></blockquote><p><strong>This answer alone puts you ahead of most candidates.</strong></p><h3>Final Thought</h3><p>Big events don’t break systems because of traffic.<br> They break systems because of <strong>poor buffering and coordination</strong>.</p><p>The best systems don’t panic under load.<br> They <strong>absorb, distribute, and recover</strong>.</p><p>If you’re interested, I’ll follow this up with:</p><ul><li>handling read-heavy spikes</li><li>cache stampede vs thundering herd</li><li>timeline fan-out strategies</li></ul><p>Let me know.</p><p><em>Hope it helps!</em></p><p><strong>AnkitCode99 here….</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32300a3f7d53" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HyperDex and Multi-Attribute Search: Brilliant Theory, Limited Adoption]]></title>
            <link>https://ankitpandeycu.medium.com/hyperdex-and-multi-attribute-search-brilliant-theory-limited-adoption-7728e55974db?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/7728e55974db</guid>
            <category><![CDATA[search-engines]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[nosql]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Fri, 12 Sep 2025 14:43:04 GMT</pubDate>
            <atom:updated>2025-09-12T14:43:04.937Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6lbkZEOyM96zCfv2dBb_Tw.jpeg" /><figcaption>Hyperspace Hashing vs Traditional Indexing</figcaption></figure><h4>The Background</h4><p>When we think of databases, we usually trade off between <strong>speed</strong> and <strong>flexibility</strong>.</p><ul><li>SQL databases give expressive queries but slow down at scale.</li><li>NoSQL databases give speed and scale but often struggle with flexible querying.</li></ul><p>This is where <strong>HyperDex</strong> once looked revolutionary — a distributed, searchable key-value store that promised <strong>super-efficient multi-attribute search</strong>. It was academically elegant, lightning fast, and ACID-compliant. Yet, despite its brilliance, it never became the industry standard.</p><p>Let’s explore how <strong>HyperDex</strong> worked, why it was different, where it excelled, and why the industry went in another direction.</p><h3>The Challenge: Multi-Attribute Search at Scale</h3><p>Imagine storing user profiles:</p><pre>{<br>  &quot;username&quot;: &quot;ankit123&quot;,<br>  &quot;location&quot;: &quot;Bangalore&quot;,<br>  &quot;age&quot;: 28,<br>  &quot;interests&quot;: [&quot;databases&quot;, &quot;AI&quot;, &quot;reading&quot;]<br>}</pre><p>Now imagine querying:</p><ul><li>All users in <strong>Bangalore</strong></li><li>Age between <strong>25–30</strong></li><li>Interested in <strong>databases</strong></li></ul><p>How do today’s databases handle this?</p><ul><li><strong>Relational databases</strong>: Multiple indexes + query planner → efficient for small data, but joins and scans get expensive at scale.</li><li><strong>NoSQL stores (Cassandra, DynamoDB, MongoDB)</strong>: Fast for single-key lookups, but multi-attribute queries often require <strong>denormalization</strong> or <strong>external search engines</strong>.</li></ul><p>HyperDex tried to solve this problem in a new way.</p><h3>Enter Hyperspace Hashing</h3><p>The core innovation of HyperDex was <strong>Hyperspace Hashing</strong>.</p><p>Instead of indexing attributes separately, HyperDex mapped objects into a <strong>multi-dimensional space</strong> where:</p><ul><li>Each axis represented an attribute (e.g., age, location, interests).</li><li>Each data record was hashed into this hyperspace at deterministic coordinates.</li><li>Queries were translated into <strong>hyper-rectangles</strong> in this space.</li></ul><p><strong>The result?</strong> Multi-attribute queries were <strong>as efficient as single-key lookups</strong>.</p><h3>Why was this better than conventional indexing?</h3><ul><li><strong>Speed</strong>: No multiple B-trees or joins. Query → direct coordinate lookup.</li><li><strong>Scalability</strong>: Hyperspace naturally distributed across nodes.</li><li><strong>Consistency</strong>: HyperDex was fully ACID, unlike many NoSQL systems of its time.</li></ul><p>It was like combining the <strong>query flexibility of SQL</strong> with the <strong>scale of NoSQL</strong>.</p><h3>Performance and Memory Usage</h3><p>So how did HyperDex compare against mainstream databases?</p><h4>Query Performance</h4><ul><li><strong>HyperDex</strong>: Multi-attribute queries ran in <strong>O(1) or O(log n)</strong> time. Benchmarks showed <strong>2–4× speedup</strong> over Cassandra and MongoDB for multi-field searches.</li><li><strong>Cassandra / DynamoDB</strong>: Excellent for single-key lookups, weak for multi-attribute without schema gymnastics.</li><li><strong>MongoDB</strong>: Handles secondary indexes decently, but performance degrades with multiple non-selective fields.</li><li><strong>Elasticsearch</strong>: Optimized for multi-attribute and text queries, but slower than HyperDex for structured lookups due to inverted index scans.</li></ul><h4>Memory &amp; Storage Usage</h4><ul><li><strong>HyperDex</strong>: Needed to store <strong>multiple projections</strong> of data → <strong>1.5×–3× overhead</strong>.</li><li><strong>Cassandra / DynamoDB</strong>: Very efficient, minimal overhead.</li><li><strong>MongoDB</strong>: Moderate overhead, grows with index count.</li><li><strong>Elasticsearch</strong>: Heavy overhead — typically <strong>2×–4× raw data size</strong>.</li></ul><h3>Side-by-Side Comparison</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w0G88o4u4WB7PR_Ol_b1vQ.png" /></figure><h3>Why HyperDex Didn’t Take Over</h3><p>Despite its brilliance, HyperDex didn’t see wide adoption. Here’s why:</p><ol><li><strong>Operational Complexity</strong><br> Managing data movement and rebalancing in hyperspace was tricky at scale.</li><li><strong>Ecosystem Gap</strong><br> MongoDB, Cassandra, DynamoDB, and Elasticsearch built huge ecosystems, tools, and integrations. HyperDex remained niche.</li><li><strong>Specialized Use Case</strong><br> Not all apps need true hyperspace-powered multi-attribute queries. Many real-world needs were solved with <strong>secondary indexes</strong> or <strong>search engines</strong>.</li><li><strong>Academic Roots</strong><br> HyperDex was a research project first. While conceptually elegant, it lacked the production hardening that mainstream databases built over years.</li></ol><h3>What the Industry Uses Today</h3><p>Instead of Hyperspace Hashing, the industry embraced <strong>polyglot persistence</strong> — using different tools for different jobs:</p><ul><li><strong>Elasticsearch / OpenSearch</strong> → Full-text + multi-attribute filtering (e.g., e-commerce filters).</li><li><strong>MongoDB</strong> → JSON documents with secondary indexes.</li><li><strong>Cassandra / DynamoDB</strong> → High-throughput writes and single-key lookups.</li><li><strong>Postgres (with JSONB + GIN indexes)</strong> → Surprisingly powerful for structured + semi-structured search.</li></ul><p>Big companies often <strong>combine systems</strong>:</p><ul><li><strong>Uber</strong>: Cassandra + Elasticsearch</li><li><strong>Airbnb</strong>: MySQL + Elasticsearch</li><li><strong>Netflix</strong>: DynamoDB + Elasticsearch</li></ul><h3>Final Thoughts</h3><p>HyperDex’s <strong>Hyperspace Hashing</strong> was one of those rare, <strong>ahead-of-its-time innovations</strong>. It showed that multi-attribute queries could be solved in a mathematically elegant way, without sacrificing speed.</p><p>But real-world adoption requires more than elegance — it requires ecosystems, tooling, and operational simplicity. That’s why the industry instead leaned on <strong>Elasticsearch, MongoDB, DynamoDB, and Postgres</strong>, where multi-attribute queries are good enough and the ecosystem is robust.</p><p>If you’re building today, you’ll likely reach for <strong>Elasticsearch</strong> or <strong>Postgres with indexes</strong> for multi-attribute search. But studying HyperDex is still worthwhile — it’s a fascinating example of how rethinking indexing can challenge long-held assumptions in database design.</p><p>Hope you learnt something interesting from this.</p><p>AnkitCode99 here….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7728e55974db" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How We Saved 40% by Migrating from Redis Labs to AWS ElastiCache Valkey]]></title>
            <link>https://ankitpandeycu.medium.com/how-we-saved-40-by-migrating-from-redis-labs-to-aws-elasticache-valkey-cfd387c2a935?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfd387c2a935</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[cost-optimization]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Wed, 06 Aug 2025 10:10:18 GMT</pubDate>
            <atom:updated>2026-11-12T13:12:28.817Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JoswctAESLSpFMtnUN6b1w.png" /><figcaption>End result of migration</figcaption></figure><p>In the world of high-growth applications, infrastructure costs can scale faster than users. For us, <strong>Redis caching</strong> was one of those silent cost drivers. We were running a <strong>managed Redis instance on Redis Labs</strong>, which started at <strong>$5,000 per month</strong> — with projections showing a jump to <strong>$12,000 per month</strong> as our data and traffic grew.</p><p>The solution? A carefully planned migration to <strong>AWS ElastiCache Valkey</strong>, delivering a <strong>40% cost reduction</strong> without sacrificing performance or uptime.</p><p>In this article, I’ll share how we made the switch, why we chose Valkey over AWS Managed Redis, and the lessons learned from this journey.</p><h3>Why We Rethought Our Redis Strategy</h3><p>When we signed our initial Redis Labs contract, $5,000 per month felt like a reasonable investment for enterprise-grade managed Redis. But as our traffic surged, it became clear that costs would <strong>triple by May 2025</strong>, stretching to $12,000 monthly.</p><p>At that point, staying put wasn’t sustainable. We needed a solution that was:</p><ul><li><strong>Cost-efficient</strong> for long-term scaling</li><li><strong>Compatible</strong> with our existing Redis-powered caching layer</li><li><strong>Proven</strong> for reliability and uptime</li></ul><p>That’s when we began exploring AWS alternatives.</p><h3>Redis Labs vs AWS ElastiCache Valkey</h3><p>Our evaluation narrowed down to:</p><ul><li><strong>AWS ElastiCache for Redis</strong> — reliable, battle-tested, but still costly.</li><li><strong>AWS ElastiCache Valkey</strong> — a newer Redis-compatible caching engine, promising the same functionality at a lower price point.</li></ul><p>When we compared projected costs, Valkey clearly stood out. Even as our cache size and keys grew, ElastiCache Valkey would cost us around <strong>$5,000 per month</strong> — essentially flatlining our infrastructure costs instead of letting them balloon.</p><h3>Benchmarking: Redis OSS vs Valkey</h3><p>Before migrating production workloads, we ran a <strong>proof of concept (POC)</strong> to validate performance.</p><p>Our caching use case was relatively straightforward:</p><ul><li><strong>Simple key-value pairs</strong> for session data, config caching, and microservice lookups</li><li><strong>No advanced Redis data structures</strong> like sorted sets, streams, or modules</li></ul><p>Given this, the risk of moving to Valkey was minimal. When we benchmarked <strong>Redis OSS</strong> against <strong>AWS ElastiCache Valkey</strong>, we found:</p><ul><li><strong>Identical latency and throughput</strong> for our KV-heavy workload</li><li><strong>No code changes required</strong> since Valkey is Redis-compatible</li><li><strong>Lower resource utilization</strong> for the same throughput</li></ul><p>This simplicity in our use case was a key deciding factor — it made Valkey an easy and low-risk replacement.</p><h3>Zero-Downtime Migration Plan</h3><p>Cutting over a cache layer is tricky — especially when uptime is non-negotiable. Here’s how we executed a <strong>zero-downtime migration</strong>:</p><ol><li><strong>Spin up Valkey in parallel:</strong> We provisioned an ElastiCache Valkey cluster alongside our Redis Labs instance.</li><li><strong>Live sync with RIOT:</strong> Using the <strong>RIOT (Redis Input/Output Tool)</strong>, we continuously ingested live writes into Valkey. A background job batched older data until both clusters were in sync.</li><li><strong>Monitor key counts and data size:</strong> We tracked both Redis and Valkey metrics to ensure near parity before cutover.</li><li><strong>Low-traffic cutover:</strong> During nighttime low-traffic hours, we updated microservice environment variables to point to Valkey.</li><li><strong>Zero code changes:</strong> Since Valkey is a <strong>drop-in Redis replacement</strong>, the switch required no application-level modifications.</li></ol><p>The transition was seamless — no downtime, no data loss. Below is a high level demonstration of RIOT’s replicate command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1rq885fjQKxyLEsu5aRpIQ.jpeg" /></figure><h3>The Impact: 40% Cost Savings</h3><p>The results were immediate and measurable:</p><ul><li><strong>40% savings</strong> compared to Redis Labs managed Redis</li><li><strong>20% savings</strong> compared to AWS ElastiCache for Redis</li><li><strong>Zero performance degradation</strong> — latency and throughput remained stable</li><li><strong>Simplified scaling</strong> for future growth without budget shocks</li><li><strong>Built-in Failover &amp; High Availability:</strong> Unlike Redis Labs, which provided enterprise-grade HA out of the box, AWS ElastiCache Valkey’s managed clusters offered seamless failover within the same region. During our POC, simulated node failures were automatically handled by replica promotion, keeping cache availability intact with negligible failover times.</li></ul><h3>Key Lessons from the Migration</h3><p>For teams considering a Redis-to-Valkey migration, here are our main takeaways:</p><ul><li><strong>Always run a workload-specific POC.</strong> Vendor claims don’t replace your own benchmarks.</li><li><strong>Plan for real-time sync.</strong> Tools like RIOT make zero-downtime migrations feasible.</li><li><strong>Time your cutover wisely.</strong> Switch during off-peak hours to reduce risk.</li><li><strong>Think long-term.</strong> What looks affordable today may balloon tomorrow.</li></ul><h3>Final Thoughts</h3><p>Our migration from <strong>Redis Labs managed Redis to AWS ElastiCache Valkey</strong> was more than a cost-saving exercise. It proved that <strong>modern open-source compatible solutions</strong> can deliver enterprise-grade performance without enterprise-grade pricing.</p><p>If you’re running Redis at scale and staring at a rising invoice, it might be time to explore Valkey. The savings — and the peace of mind — could be worth it.</p><h3>References</h3><ol><li><a href="https://redis.github.io/riot/#_replication">https://redis.github.io/riot/#_replication</a></li><li><a href="https://aws.amazon.com/elasticache/pricing/">https://aws.amazon.com/elasticache/pricing/</a></li><li><a href="https://valkey.io/">https://valkey.io/</a></li></ol><p>Hope you learnt something new and it helps.</p><p>AnkitCode99 here….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfd387c2a935" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Node.js’ Single-Threaded Nature and Its Limitations]]></title>
            <link>https://ankitpandeycu.medium.com/understanding-node-js-single-threaded-nature-and-its-limitations-71b8f2af763e?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/71b8f2af763e</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[basics-of-programming]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Sun, 29 Jun 2025 06:20:20 GMT</pubDate>
            <atom:updated>2025-06-29T06:20:20.123Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KIjloAF4rlYPx1y-kng-eQ.jpeg" /></figure><h4>Background</h4><p>Node.js is often praised for handling thousands of concurrent connections with ease — but beneath this magic lies a single thread. So how does it manage so much, and where does it break?</p><p>Built on Chrome’s V8 engine and backed by the asynchronous power of the libuv library, Node.js embraces a single-threaded model that is both its strength and its Achilles’ heel. While we’ve previously explored Node’s asynchronous internals in <a href="https://medium.com/@ankitpandeycu/dissecting-node-js-asynchronous-behaviour-5cca28052188">Dissecting Node.js Asynchronous Behaviour</a>, this post takes a more practical turn.</p><p>This is the first article in a <strong>hands-on series</strong> where we explore how Node.js behaves under real-world workloads — and how to scale beyond its single-threaded constraints using worker threads, child processes, and IPC.</p><h4>The Node.js Execution Model (Simplified)</h4><p>At its core, Node.js runs JavaScript on a single thread. However, it achieves impressive concurrency by offloading I/O tasks to a thread pool managed by libUV. Here’s a quick breakdown:</p><ul><li><strong>JavaScript Thread</strong>: Runs your application code.</li><li><strong>libuv Thread Pool</strong>: Handles file system access, DNS lookups, and other non-blocking operations.</li><li><strong>Event Loop</strong>: Coordinates execution by processing callbacks and events in a loop.</li></ul><p>This model allows Node.js to handle multiple tasks seemingly in parallel, without ever spinning up multiple JavaScript threads.</p><h4>The Event Loop in Action</h4><p>Let’s demystify the event loop with a simple example:</p><pre>setTimeout(() =&gt; console.log(&quot;Timer&quot;), 0);<br>setImmediate(() =&gt; console.log(&quot;Immediate&quot;));<br>process.nextTick(() =&gt; console.log(&quot;Next Tick&quot;));</pre><p><strong>Expected Output:</strong></p><pre>Next Tick<br>Timer<br>Immediate</pre><p><strong>Why?</strong> process.nextTick is handled before any I/O tasks. setTimeout and setImmediate land in different phases of the event loop, with setTimeout generally winning in this scenario. Each iteration of the event loop is called a <em>tick</em>, and it has several phases:</p><ul><li>Timers (setTimeout, setInterval)</li><li>Pending Callbacks</li><li>Idle/Prepare</li><li>Poll</li><li>Check (setImmediate)</li><li>Close Callbacks</li></ul><p><strong>Another example with file I/O:</strong></p><pre>const fs = require(&#39;fs&#39;);<br>fs.readFile(__filename, () =&gt; {<br>  console.log(&#39;File read complete&#39;);<br>});<br>setImmediate(() =&gt; console.log(&#39;Immediate&#39;));<br>setTimeout(() =&gt; console.log(&#39;Timeout&#39;), 0);<br>console.log(&#39;Sync log&#39;);</pre><p><strong>Expected Output:</strong></p><pre>Sync log<br>Timeout<br>Immediate<br>File read complete</pre><p>This example reinforces how asynchronous I/O does not block the thread — but all of it still runs on a single JavaScript thread.</p><h4>When the Model Breaks: CPU-Bound Work</h4><p>Node.js excels at I/O-bound tasks. But when faced with CPU-bound tasks, its single-threaded nature shows its limits.</p><p>Consider a naive recursive Fibonacci function:</p><pre>function fib(n) {<br>  if (n &lt;= 1) return 1;<br>  return fib(n - 1) + fib(n - 2);<br>}</pre><pre>console.time(&#39;fib&#39;);<br>fib(40);<br>console.timeEnd(&#39;fib&#39;);</pre><p>Now wrap this with a setTimeout:</p><pre>setTimeout(() =&gt; console.log(&#39;Hello after delay&#39;), 100);<br>fib(40);</pre><p>Even though we asked for a message after 100ms, the delay will spike into <em>seconds</em>, because the event loop is blocked by the Fibonacci computation.</p><p>This is where Node’s asynchronous magic fails — <strong>because async ≠ parallel</strong>.</p><p>Another real example with image processing:</p><pre>const sharp = require(&#39;sharp&#39;);<br>const fs = require(&#39;fs&#39;);<br>sharp(&#39;input.jpg&#39;)<br>  .resize(1000)<br>  .toFile(&#39;resized.jpg&#39;, (err, info) =&gt; {<br>    if (err) throw err;<br>    console.log(&#39;Image resized&#39;);<br>  });</pre><p>When doing multiple such operations in a loop or alongside API handling, the event loop can lag noticeably.</p><h4>Real-World Bottlenecks</h4><p>Here are common examples where Node’s single thread struggles:</p><ul><li><strong>Image processing</strong> (e.g. sharp, canvas)</li><li><strong>Large payload parsing</strong> (e.g. big JSON files)</li><li><strong>Heavy computations</strong> (e.g. encryption, sorting large arrays)</li><li><strong>Synchronous third-party libraries</strong> (e.g. crypto libraries without async APIs)</li></ul><p>Example with synchronous JSON parsing:</p><pre>const fs = require(&#39;fs&#39;);<br>const data = fs.readFileSync(&#39;large.json&#39;);<br>const parsed = JSON.parse(data);<br>console.log(&#39;JSON parsed&#39;);</pre><p>If large.json is very big, this can freeze your server for noticeable seconds.</p><p>In each case, the CPU load blocks the event loop, delaying everything — including I/O and scheduled timers.</p><h4>What’s Next?</h4><p>So if Node.js is single-threaded, how do we offload CPU-heavy work? That’s where <strong>worker threads</strong>, <strong>child processes</strong>, and <strong>inter-process communication</strong> (IPC) come into play.</p><p>In the next post, we’ll explore how <strong>Worker Threads</strong> allow you to spawn additional JavaScript threads to parallelize CPU-intensive tasks, while maintaining performance for your main event loop.</p><p>Stay tuned — the deeper we go, the more powerful your Node.js apps will become.</p><p><strong>Pro Tip</strong>: Keep your event loop free. If you must do heavy lifting, offload it early — either to native libraries or separate threads/processes.</p><p><em>Liked this post? Follow for Part 2 where we dive into Worker Threads and real-world parallelism in Node.js.</em></p><p>Hope it helps!!!<br>AnkitCode99 here….</p><p><strong>Previous blog for reference</strong></p><p><em>Dissecting Node.js’ async behviour: </em><a href="https://medium.com/@ankitpandeycu/dissecting-node-js-asynchronous-behaviour-5cca28052188"><em>https://medium.com/@ankitpandeycu/dissecting-node-js-asynchronous-behaviour-5cca28052188</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71b8f2af763e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dissecting Node.js Asynchronous Behaviour]]></title>
            <link>https://ankitpandeycu.medium.com/dissecting-node-js-asynchronous-behaviour-5cca28052188?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/5cca28052188</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Sun, 29 Sep 2024 07:42:07 GMT</pubDate>
            <atom:updated>2024-09-29T07:42:07.085Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*aDus792RvLR2faLPtGJIVg.png" /><figcaption>The power of Asynchronous execution.</figcaption></figure><h3>Background</h3><p>Since joining my first company, I’ve been captivated by Node.js’s ability to handle high I/O loads. Driven by curiosity, I often stayed late to explore its documentation and experiment with various scenarios, eager to understand its asynchronous capabilities.</p><p>Through my exploration, I uncovered the intricate workings of event loops, thread pools, and C++ APIs — all essential for Node.js’s asynchronous magic. The more I learned, the more I realized how much there was to discover.</p><p>Today, I invite you to delve into the core of Node.js with me. We’ll examine how it utilizes thread pools and C++ APIs to excel in asynchronous programming, enhancing our appreciation for this powerful technology — whether you’re a seasoned developer or just starting out.</p><h3>Warm Up</h3><p>The event loop is the central piece of Node.js, allowing it to manage asynchronous tasks efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*tPPoP-Y5OrlTUGLUI9GgdQ.png" /></figure><p>Unlike traditional synchronous programming, where function calls block further execution, the event loop enables concurrent processing by offloading tasks like file reading or HTTP requests to the Node.js’ LibUV thread pool. This allows the main thread to continue executing other code while waiting for these operations to complete.</p><h4>Key Components of the Event Loop</h4><ol><li><strong>Call Stack:</strong> This is where synchronous code execution occurs. Functions are pushed onto the stack when called and popped off when they complete.</li><li><strong>Event Queue:</strong> Once an asynchronous operation completes, its callback is placed in the event queue. The event loop continuously checks this queue for tasks to execute when the call stack is empty.</li><li><strong>Microtask Queue:</strong> This queue handles promises and process.nextTick() callbacks. Microtasks have higher priority than regular callbacks in the event queue and are processed before moving on to the next task in the event queue.</li></ol><h3>Handling Asynchronous Operations</h3><p>Node.js utilizes two main queues for managing asynchronous operations:</p><ul><li>Callback Queue: When an I/O operation completes, its callback function is added here. The event loop processes these callbacks when the call stack is clear.</li><li>Microtask Queue: Used for handling promise resolutions and process.nextTick(). This queue ensures that microtasks are executed immediately after the currently executing script and before any other I/O tasks.</li></ul><h3>Diving In</h3><p>In a Node.js application, operations such as file I/O, cryptographic hash computations, database calls over a network, and external API calls can be time-consuming and may block the main thread if not handled properly.</p><p>To maintain responsiveness, Node.js leverages <strong>LibUV</strong>, which provides a <strong>thread pool</strong> (default size 4)to handle these time-consuming tasks. When an asynchronous operation is initiated, such as reading a file or computing a hash, LibUV delegates these tasks to the thread pool. This allows the main event loop to continue processing other requests while the I/O or computation is performed in the background.</p><p>LibUV internally uses multiple queues for various operations:</p><ol><li><strong>Pending Queue</strong>: This is used for I/O operations that are in progress.</li><li><strong>Completion Queue</strong> or <strong>Completion Callbacks</strong>: This is where callbacks for completed I/O operations are queued.</li><li><strong>Work Queue</strong>: This refers to the queue of tasks to be processed by the thread pool.</li></ol><ul><li>The <strong>“Pending Queue”</strong> holds I/O operations that have been initiated but are not yet complete.</li><li>When an operation completes, its callback is moved to the <strong>“Completion Queue”.</strong></li><li>The<strong> “Work Queue”</strong> is separate and is used for offloading CPU-intensive tasks to the thread pool.</li></ul><p>Once the task is complete, LibUV returns the result to the main thread, ensuring that the application remains efficient and responsive even under heavy load. <a href="https://nodejs.org/api/cli.html#uv_threadpool_sizesize">The <strong><em>fs, crypto, dns &amp; zlib </em></strong>modules of Node.js relies on LibUV.</a></p><p>Let’s see a simple illustration of LibUV’s thread pool usage.</p><pre>const crypto = require(&#39;crypto&#39;);<br><br>const MAX_CALLS = 4;<br><br>const start = Date.now();<br><br>for (let i = 0; i &lt; MAX_CALLS; i++) {<br>    crypto.pbkdf2(`password+${(i+1)*10}`, &#39;uniqueSalt&#39;, 100000, 512, &#39;sha512&#39;, () =&gt; {<br>        console.log(&#39;Hash:&#39;, i + 1, Date.now() - start);<br>    });<br>}</pre><p>The code above generates 4 hashed passwords using the crypto module of Node.js, upon runing the code with command node index.js we see an output similar to :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*EOey2fQMiKgQcpwBH7QglA.png" /><figcaption>4 threads, 4 hashes.</figcaption></figure><p>The closeness in completion time of these 4 operations gives us a hint that the tasks have been running parallelly on <em>separate threads.</em></p><p>Next, let’s try to tweak the value of MAX_CALLS to 6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*mCgzmEoUfBorrKigewL2NA.png" /><figcaption>4 threads, 6 hashes</figcaption></figure><p>The output seems to be quite interesting now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*kCKtOq7YGlHysOJXCjevMw.png" /></figure><p>If you plot the time taken by threads on a graph you would notice that the actual computation of hash took similar time for all 6 hashes, but due to the default <strong>thread pool size of 4</strong>, only first 4 operations were able to run in parallel whereas others were in the <strong>waiting queue</strong>. As soon as <em>thread-1 </em>finishes its job, it picks the job at the front of waiting queue, same thing is done by <em>thread-4</em>.</p><p>So, though we always thought of Node.js as a single-threaded JS runtime environment, it uses some magic under the hood to perform tasks in parallel.</p><p>The thread pool size is configurable and we can set it’s value upto 1024 (<a href="https://docs.libuv.org/en/v1.x/threadpool.html"><em>as per libUV documentation</em></a>). In the above example, while starting the Node.js process we can specify the number of threads as 6 by setting the property <strong>UV_THREADPOOL_SIZE=6</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*VVOBawsQllPRTQ--sbkhNQ.png" /><figcaption>6 threads, 6 hashes</figcaption></figure><blockquote>But since Software Engineering is all about — ‘<strong>You get some, you loose some</strong>’, it’s crucial to note that using too many threads in the thread pool can have downsides:</blockquote><ol><li><strong>Increased memory usage:</strong> Each thread consumes memory. Too many threads can lead to excessive memory consumption, potentially causing performance issues or out-of-memory errors.</li><li><strong>Context switching overhead:</strong> With too many threads, the system may spend more time switching between threads, reducing overall efficiency.</li><li><strong>Diminishing returns:</strong> Beyond a certain point, adding more threads may not improve performance and could even degrade it due to increased resource contention.</li><li><strong>Potential for thread starvation:</strong> If too many long-running tasks are queued, it might lead to thread starvation, where some operations have to wait for extended periods.</li></ol><p>Based upon your application needs try to monitor performance with different number of threads to ensure optimal resource usage.</p><h4>Diving Deeper</h4><p>Till now, we talked about operations like file I/O and hash computations both of which are handled by LibUV thread pool’s threads, but have you ever thought how does Node.js handles the <strong>network calls</strong> or how does the DB Client we use (mongoose, sequelize etc.) handles thousands of concurrent calls easily.</p><p>In such scenarios not only the LibUV thread pool works but the OS kernel threads also come into the picture.</p><p>Let’s break this down step by step in simple terms:</p><ol><li><strong>LibUV’s Event Loop (User Space)<br></strong>When you make a request, like calling an API or accessing a database, it doesn’t make you wait until the response comes back. Instead, libuv puts the request in a queue and allows the program to do other things while waiting. Once the response is ready, libuv’s event loop picks it up and processes it.</li><li><strong>Handling Concurrent Requests: </strong>LibUV is smart, it doesn’t create one thread per request, which would take up a lot of resources. Instead, it uses <strong>asynchronous I/O</strong>, which is a fancy way of saying it keeps an eye on many requests at the same time. <br>For network calls (like database queries over TCP or HTTP), it typically uses the Operating System’s <strong>epoll (Linux)</strong>, <strong>kqueue (BSD/macOS)</strong>, or <strong>IOCP (Windows)</strong>.</li><li><strong>Kernel’s Role: </strong>The operating system’s kernel helps manage all these requests. When you ask the kernel to fetch data from a database, for example, the kernel <strong>registers this task and tracks</strong> it until it’s done. It doesn’t need a ton of threads either; it just waits for the I/O (input/output) operation to be ready and then notifies libuv.</li></ol><h4><strong>How these kernel APIs work internally</strong>:</h4><ul><li>These APIs operate by <strong>registering file descriptors</strong> (for example, network sockets, pipes, etc.) that the application wants to monitor.</li><li>The operating system <strong>monitors</strong> these file descriptors for specific events, like data being ready to read from a socket or a socket being ready to accept new data for writing.</li><li>When an event happens, the OS signals it, and libuv takes action based on that signal.</li><li>Libuv integrates these APIs into the Node.js <strong>event loop</strong>, which is a central part of Node.js’s architecture. The event loop constantly listens for I/O events and schedules them to be executed when they are ready.</li></ul><h4>Key points which make this design choice scalable:</h4><ul><li><strong>Event-driven design</strong>: Non-blocking I/O means that resources (threads, memory) are only used when necessary, minimizing the footprint even for large numbers of connections.</li><li><strong>Efficient I/O multiplexing</strong>: The kernel’s polling mechanisms allow it to monitor thousands of file descriptors (network connections) without using threads.</li><li><strong>Callback architecture</strong>: LibUV uses callbacks to handle I/O completion, which avoids the need for creating threads for each connection or request.</li></ul><h4>To be explored</h4><p>1. Linux file descriptors — <a href="https://medium.com/@dhar.ishan04/here-is-all-you-need-to-know-about-file-descriptors-in-linux-d93f05166026">https://medium.com/@dhar.ishan04/here-is-all-you-need-to-know-about-file-descriptors-in-linux-d93f05166026</a></p><p>2. LibUV Architecture — <a href="https://codedamn.com/news/nodejs/libuv-architecture">https://codedamn.com/news/nodejs/libuv-architecture</a></p><p>3. LibUV Networking — <a href="https://docs.libuv.org/en/v1.x/guide/networking.html">https://docs.libuv.org/en/v1.x/guide/networking.html</a></p><p>4. LibUV FileSystem — <a href="https://docs.libuv.org/en/v1.x/guide/filesystem.html">https://docs.libuv.org/en/v1.x/guide/filesystem.html</a></p><p>5. IO Multiplexing &amp; Epoll — <a href="https://nima101.github.io/io_multiplexing">https://nima101.github.io/io_multiplexing</a></p><p>I hope this article was worth your time (do clap if it was 😉) and you learnt something new in this. You can refer the above articles &amp; links to learn such interesting concepts. Till then keep exploring the beauty of Software Engineering.</p><p>Hope it helps!!!<br>AnkitCode99 here….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5cca28052188" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Introduction of Radix Tree]]></title>
            <link>https://ankitpandeycu.medium.com/unleashing-the-potential-of-radix-tree-35e6c5d3b49d?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/35e6c5d3b49d</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[advanced-data-structures]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[trees]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Sat, 21 Sep 2024 20:30:39 GMT</pubDate>
            <atom:updated>2024-09-22T06:48:54.404Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Igcm0k1rPd-F6mlpUYFcCQ.png" /><figcaption>Tries v/s Radix Tree</figcaption></figure><h3>Background</h3><p>Prefix matching is a critical operation in various applications, particularly in string searching and data retrieval systems. It allows for efficient querying of strings that start with a given substring, making it essential for functionalities like autocomplete, search suggestions, and routing in network systems.</p><h4>Solution 1: The TRIE data structure</h4><p>A <strong>Trie</strong>, or prefix tree, is a specialized tree structure that facilitates prefix matching by storing characters of strings in a hierarchical manner. Each node represents a character, and paths from the root to the nodes represent prefixes of the strings stored in the Trie. The complexity of tries grows with the length of the key, not the number of elements in the structure. They suffer <strong>bad space performance</strong> when the keys are sparse.</p><p><strong>Understanding downside of using Trie</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*ho8mvCRwNhG5wmm02fjS0g.png" /></figure><p>Each node in a trie represents a portion of a key, specifically the beginning part shared by multiple keys. This shared beginning is called a <em>prefix</em>. We don’t need to explicitly store this prefix in the node because we can deduce it from the path we took to reach that node. The crucial information each node needs is how to locate its child nodes.</p><p>To locate child nodes, we need a mechanism to identify all nodes that represent prefixes one character longer than the current one. This is the purpose of the children array. In this implementation, we&#39;re working with an 8-bit character set, which means there are <em>256 possible characters for each position</em> in the key. This results in what&#39;s called a fan-out of 256, meaning each node can potentially have up to 256 child nodes.</p><p>The inefficiency of this trie structure becomes apparent when we consider the potential for wasted space in each node. Let’s illustrate this with our example words: “dog”, “dot”, “ease”, and “east”.</p><p>The root node would have two valid pointers: one to “d” and one to “e”.</p><ul><li>The “d” node would have one pointer to “o”.</li><li>The “o” node would have pointers to “g” and “t”.</li><li>The “e” node would have one pointer to “a”.</li><li>The “a” node would have one pointer to “s”.</li><li>The “s” node would have pointers to “e” and “t”.</li></ul><p>In total, we have 9 nodes. Assuming we’re using the English alphabet (26 letters), each node would have an array of 26 pointers. This means we have 9 * 26 = 234 pointers in total. However, only 7 of these pointers are actually used (the ones we described above). This means 227 out of 234 pointers, are empty and wasting space.</p><p>If each pointer occupies 8 bytes of memory, we’re using 234 * 8 = 1,872 bytes to store what is essentially just 15 bytes of information (the letters in our four words). This demonstrates the significant memory inefficiency of this trie implementation, especially for small datasets.</p><h4>Solution 2: Radix Tree data structure</h4><p>I came across this data structure while going through the internals of <a href="https://fastify.dev/">fastify.js</a> and <a href="https://gofiber.io/">go-fiber</a>.</p><p>A <strong>Radix Tree</strong>, or<em> compact prefix tree</em>, is an optimization of the Trie that <strong><em>combines nodes with single children</em></strong> to reduce space complexity. It retains the same hierarchical structure but eliminates unnecessary nodes, making it more memory-efficient.</p><p>If we take the example strings provided above we can see that the corresponding Radix Tree would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*BS7nzICA2rAqr3IK2NGVIQ.png" /></figure><p>When we compress our standard trie down into a radix tree such that every <em>single child node</em> was merged with its parent, we only needed 6 nodes to represent that same group of keys. Recall that every time we intialize a node, we also need to initialize an array with (usually) 26 keys, one for each of the 26 letters of the English alphabet. By eliminating 3 nodes in the process of compression, we saved that much space and memory that we would have needed to allocate otherwise.</p><h4><strong>Performance:</strong></h4><ul><li><strong>Space Efficiency:</strong> By merging nodes with single children, Radix Trees can significantly reduce memory usage compared to standard Tries.</li><li><strong>Search Complexity:</strong> The search operation remains <strong>O</strong>(m) on average, similar to Tries, but with reduced overhead due to fewer nodes.</li></ul><h4>Real-life use cases:</h4><ol><li><strong>API Request Routing</strong>: Fastify, a high-performance web framework for Node.js, employs a <strong>Radix Tree</strong> for its routing mechanism, optimizing the way it handles incoming requests and matches them to defined routes. This approach enhances performance and memory efficiency, especially in applications with numerous routes.<br>Let’s say we have the following routes:</li></ol><pre>fastify.get(&#39;/api/users&#39;, handleGetUsers)<br>fastify.get(&#39;/api/users/:id&#39;, handleGetUser)<br>fastify.get(&#39;/api/products&#39;, handleGetProducts)<br>fastify.get(&#39;/api/products/:id&#39;, handleGetProduct)<br>fastify.get(&#39;/blog/posts&#39;, handleGetBlogPosts)<br>fastify.get(&#39;/blog/posts/:id&#39;, handleGetBlogPost)</pre><p>The Radix Tree for all these routes would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*qJhPqdi1YYH5POkzX-bNrQ.png" /></figure><p>Key points about this compressed structure:</p><ol><li>The “/api/” prefix is stored once, saving memory and reducing lookup time.</li><li>The “blog/posts” path is compressed into a single node.</li><li>Parameter nodes (“:id”) are still separate to allow for dynamic routing.</li></ol><p>When Fastify receives a request, it traverses this tree to find the matching route. For example, a request to “/api/users/123” would:</p><ol><li>Match the “/api/” node</li><li>Then match the “users” node</li><li>Finally match the “:id” parameter node, extracting “123” as the id</li></ol><p>This structure allows Fastify to perform route matching very quickly, even with a large number of routes, making it highly performant for web applications.</p><p>2. <strong>IP Address Lookup in DNS</strong>: Suppose we want to store and look up the following IP addresses:</p><ol><li>192.168.0.1</li><li>192.168.0.2</li><li>192.168.0.10</li><li>192.168.1.1</li></ol><p><strong>Step 1: Binary Representation</strong></p><p>Here are the binary representations of the IP addresses:</p><ul><li>192.168.0.1 → 11000000.10101000.00000000.00000001</li><li>192.168.0.2 → 11000000.10101000.00000000.00000010</li><li>192.168.0.10 → 11000000.10101000.00000000.00001010</li><li>192.168.1.1 → 11000000.10101000.00000001.00000001</li></ul><p><strong>Step 2: Building the Radix Tree</strong></p><ol><li><strong>Insert </strong><strong>192.168.0.1</strong>:</li></ol><ul><li>Start with an empty radix tree.</li><li>Insert the full binary string: 11000000.10101000.00000000.00000001.</li></ul><p>2.<strong> Insert </strong><strong>192.168.0.2</strong>:</p><ul><li>The first 30 bits (11000000.10101000.00000000.000000) are the same as 192.168.0.1.</li><li>Since the prefixes match, the tree will branch only at the point of difference (last bit): 01 for .1 and 10 for .2.</li></ul><p>3.<strong> Insert </strong><strong>192.168.0.10</strong>:</p><ul><li>The first 28 bits (11000000.10101000.00000000.0000) match the existing entries.</li><li>At the 29th bit, the path will branch: 0001 for .1, 0010 for .2, and 1010 for .10.</li></ul><p><strong>4. Insert </strong><strong>192.168.1.1</strong>:</p><ul><li>The first 25 bits match (11000000.10101000.00000001).</li><li>The tree will branch off at the 26th bit for this address.</li></ul><p><strong>Step 3: Path Compression</strong></p><p>In a standard trie, we would store each bit of the binary address in separate nodes, leading to a tree that is 32 levels deep for each address. However, with <strong>path compression</strong>, we compress nodes that have only one child by collapsing them into a single edge, representing the common prefix.</p><h4>Path Compression Example:</h4><p>Let’s focus on 192.168.0.1 and 192.168.0.2:</p><ul><li>The first 30 bits of these two addresses are identical: 11000000.10101000.00000000.000000.</li><li>Instead of creating 30 separate nodes, we compress them into a single edge that represents the entire 30-bit prefix.</li><li>At the point where the paths diverge (31st bit), we create a branch: 01 for .1 and 10 for .2.</li></ul><p><strong>Final Compressed Radix Tree Structure:</strong></p><ul><li><strong>Root</strong>: Represents the common prefix of all IP addresses (192.168.0 in decimal, or the first 24 bits in binary: 11000000.10101000.00000000).</li><li><strong>Branch 1</strong>: Represents 192.168.0.1 and 192.168.0.2, with the compressed path up to the 30th bit, then branching into two nodes (01 for .1 and 10 for .2).</li><li><strong>Branch 2</strong>: Represents 192.168.0.10 with a longer path compressed up to 28 bits, and the unique part represented thereafter.</li><li><strong>Branch 3</strong>: Represents 192.168.1.1 starting to diverge at the 25th bit with a different compressed path.</li></ul><p><strong>Visualization of Path Compression</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*4rNKaodPcuhP1Z4pXsrpVw.png" /></figure><h4>Conclusion</h4><p>In summary, both Tries and Radix Trees serve crucial roles in prefix matching scenarios. While Tries are straightforward and effective for many applications, Radix Trees offer enhanced space efficiency without sacrificing performance. The choice between them depends on specific use cases and constraints related to memory usage and speed requirements.</p><p><strong>References &amp; Further Read</strong></p><ol><li>OpenGenus – https://iq.opengenus.org/radix-tree/amp/</li><li>Advance Routing Tech – https://astconsulting.in/blog/2023/07/22/advanced-routing-fastify/</li><li>Adaptive Radix Tree – https://db.in.tum.de/~leis/papers/ART.pdf</li></ol><p>Hope you learnt something new today!</p><p>AnkitCode99 here….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35e6c5d3b49d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DP is easy — Part-9]]></title>
            <link>https://ankitpandeycu.medium.com/dp-is-easy-part-9-bdce0b0aa7?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdce0b0aa7</guid>
            <category><![CDATA[dynamic-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Mon, 05 Aug 2024 20:11:08 GMT</pubDate>
            <atom:updated>2024-08-05T20:34:57.531Z</atom:updated>
            <content:encoded><![CDATA[<h3>DP is easy — Part-9</h3><p><em>“The best thing about a boolean is even if you are wrong, you are only off by a bit.” — Anonymous</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*C4-FexqtgkoXGYYvB92opg.png" /></figure><p>Hey reader hope you are had a good time learning and exploring not only this awesome world but also Dynamic Programming. On this positive note let’s start with our today’s blog. Today we’ll again solve an interesting problem on DP-on-Grid, and as a spoiler let me hint this to you that solving this problem would make your concepts of DP on Grid crystal clear.</p><p><strong>Announcement — </strong>Since I have covered a good number of the problems required to build a good foundation for Dynamic Programming, its time for us to level-up our learning. So, this is the last blog of DP on Grids topic and upcoming topic which I would be covering is <strong>Digit-DP</strong> which is one of the most crucial DP category when it comes to company coding rounds or coding competitions.</p><h3>Let’s start with the discussion</h3><p>Today we’ll be solving a 1600 rated Codeforces problem named <a href="https://codeforces.com/contest/429/problem/B">Working Out</a>, this is the best DP-on-Grid problem I solved till date and it required some good observation skills. Here’s the summarized problem statement : <em>“ “A 2-D matrix A with n rows and m columns is presented to you; A[i][j] represents the number of calories burned. A boy and a girl begin at opposite corners of this matrix. The boy needs to get to cell (n,m) from cell (1,1). Conversely, the girl must travel from cell (n,1) to cell (1,m). The boy has down and right movement. The girl has right and up movement. The amount in cell A[i][j] is added to their total calories burned as they visit a cell and then marked to 0. You must optimize the total number of calories burned by both of them provided that they only interact in a single cell and that this cell’s cost shall not be included in either of their total.”</em></p><h3>Let’s start solving</h3><p>Before jumping to the solution, let’s check some observations:</p><ol><li>What are the invalid moves.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aSZmnKo_nTLgrcQ2FIU0Lg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S2fVH5XLWyKnVqfO2cjv4Q.png" /><figcaption>Un-optimal cases</figcaption></figure><p>There are other similar cases which could either have a less optimal final value or might be invalidating the condition of interacting only once.</p><p>2. Upon observing carefully, we can see that we have only 2 valid moves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ZcNDMD7IKzL4Xt9VCExCQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZTMri1Mkc_oVgTDFiKwaQQ.png" /><figcaption>Valid moves</figcaption></figure><p>The observation for valid moves shows us that, if we have the <strong><em>maximum possible sum of calories from starting cell till cell where both boy and girl are present before interaction</em></strong> and <strong><em>the maximum possible sum of calories from the cell post interaction till the last cells</em></strong>, we can simply iterate over all the cells and compute the maximum possible calories they burn together.</p><p>The problem now boils down to the similar concept we learnt in the <a href="https://medium.com/@ankitpandeycu/dp-is-easy-part-7-5a363b3c6485">Part-7 blog</a>, in this problem rather than computing the minimum path sum we have to solve for maximum path sum. Also, from the preceding paragraph we can see that we need to compute 4 values of <strong>Maximum Path Sum </strong>from:</p><ol><li>(0, 0) -&gt; (r, c); while moving right and down</li><li>(n-1, 0) -&gt; (r, c); while moving up and right</li><li>(0, m-1) -&gt; (r, c); while moving left and down</li><li>(n-1, m-1) -&gt; (r, c); while moving left and up</li></ol><p>the pseudocode would look somewhat similar to</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l88QV0fzwAClwz2M8DU1nA.png" /></figure><p>Once we have dp values for all 4 cases, we can simply, iterate for all cells in the range [1,1] to [n-2,m-2], the one which yields us the maximum sum of both the valid cases we discussed above</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sl8IBCK3monPWhdyIN7l9A.png" /></figure><p><strong>Time Complexity :</strong> O(n*m).</p><p>Yippeee, we solved yet another 1600 rated DP problem. The tricky part about this problem was making the observations since we were supposed to interact only once.</p><p>Hope the explanation was simple and you’ve learnt something new.</p><p>For any doubts in the above explanation or regarding solutions of the mentioned practice problems you can mail me at — ankitpandeycu@gmail.com</p><p>Share this blog with your peers if you find it useful.</p><p>AnkitCode99 here….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdce0b0aa7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Strategy Pattern simplified]]></title>
            <link>https://ankitpandeycu.medium.com/strategy-pattern-simplified-b9c3f460715e?source=rss-a2de71368544------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9c3f460715e</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[low-level-design]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[object-oriented-design]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Ankit]]></dc:creator>
            <pubDate>Fri, 08 Sep 2023 18:11:41 GMT</pubDate>
            <atom:updated>2023-09-08T18:11:41.719Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*8demACWju7ZmA6TaKk9PSg.png" /></figure><p>The Strategy Design Pattern is a fundamental component of behavioral design patterns in software engineering. It serves a crucial role in structuring and organizing algorithms and behaviors within a software system. In essence, this pattern revolves around the concept of providing a structured and interchangeable set of algorithms while allowing clients to select and switch between these algorithms dynamically, all without the need to alter existing code. This sophisticated mechanism not only enhances the maintainability of the code-base but also significantly contributes to code reusability and flexibility.</p><h4>Components of the Strategy Pattern</h4><ol><li><strong>Context</strong>: This is the class that maintains a reference to the strategy interface and can switch between different strategies. It delegates the algorithm to the strategy object. (<em>The handsome human in the above banner</em> 😉)</li><li><strong>Strategy</strong>: The interface or abstract class that defines the contract for concrete strategies. All concrete strategies must implement this interface.(<em>The payment screen in the banner</em>)</li><li><strong>Concrete Strategy</strong>: These are the actual implementations of the strategy interface. They contain specific algorithms that the context can use. (<em>The individual payment methods</em>)</li></ol><h4>When to Use the Strategy Pattern</h4><p>The Strategy Pattern becomes particularly relevant when:</p><ul><li>You have multiple related algorithms or behaviors that vary in their implementation.</li><li>You want to allow clients to choose an algorithm at runtime.</li><li>You want to isolate the client code from the specifics of algorithm implementations.</li><li>You want to avoid code duplication by promoting code reusability.</li></ul><h4>Let’s dive in to see a real world example</h4><p>Let’s say we want to create a simple shopping cart application where we can calculate the total cost of items in the cart after applying some discount (<em>which obviously everyone like</em>). We’ll define different strategies for calculating the total cost based on different types of discounts. To achieve that we do as follows:</p><ol><li>Define the Discount Strategy Interface:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T8iPa3MWIsKY88G_w4dw3A.png" /></figure><p>2. Define concrete implementations of the type of discounts you want to offer, for ex: percentage discount, fixed discount of $100, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pyb2IlBv_jKHcFpDotC9BA.png" /></figure><p>3. Create a Shopping Cart Class and use the <strong>Discount Strategy </strong>using:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mMUCGDB_OC-yPHs3RSQoxQ.png" /></figure><blockquote>Note: Here for my example I’ve used a <strong>composition</strong> relation between Shopping Cart and Discount Strategy, another way of implementation is using association. (For those who don’t know, Composition relation means every Shopping cart object needs creation of a Discount Strategy object; i.e. they are strongly associated. Using Association, would have just given us the flexibility to create Shopping Cart object with creation of Discount Strategy object.)</blockquote><p>And, we are done!!!</p><p>Now to see the above created class in action we can create a driver class <strong>ShoppingApp.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YSWIZI1fLykvMr2NM3JM6w.png" /></figure><p>We created different discount strategies (No Discount, Percentage Discount, and Fixed Discount) and used them interchangeably in the <strong>ShoppingCart </strong>class to calculate the total cost based on the selected strategy. This demonstrates the Strategy Pattern, allowing you to switch between different discount calculation algorithms without modifying the <strong>ShoppingCart</strong> class.</p><blockquote>In the above analogy, <strong>ShoppingCart</strong> class is our <strong>context, DiscountStrategy </strong>is<strong> </strong>the <strong>strategy </strong>and <strong>NoDiscountStrategy</strong>, <strong>PercentageDiscountStrategy </strong>&amp; <strong>FixedDiscountStrategy </strong>are the concrete implementation of our strategy.</blockquote><h4>General Template of Strategy Pattern:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JiiaaMRbbwj6g1_MncPtQQ.png" /></figure><p>This generic template demonstrates that the context contains an object of strategy interface(or abstract class) which has 2 or more concrete implementations and the context can use any of the implementation during the runtime without any change in the context class.</p><h4>Summarizing</h4><p>Strategy Patterns provides us:<br>1. Family of Interchangeable Algorithms that can be changed during run-time.</p><p>2. Encapsulation of Algorithms into individual implementations.</p><p>3. <strong>Code Reusability</strong>: Since algorithms are encapsulated independently, they can be reused across different parts of the application or even in entirely different projects. This reusability reduces redundancy, saves development time, and maintains consistency in algorithm implementations.</p><p>4. <strong>Scalability of Design: </strong>Any new requirement can easily be catered by simply creating a new strategy implementation which inherits the strategy interface.</p><h4>Conclusion</h4><p>The Strategy Design Pattern is a powerful tool for managing interchangeable algorithms in your applications. It promotes code reusability, flexibility, and clean code practices. By encapsulating algorithms in separate strategy classes and allowing the client to switch between them dynamically, we can build more maintainable and scalable software.</p><p>For any doubts in the above explanation feel free to reach me at — <a href="/service/mailto:ankitpandeycu@gmail.com">ankitpandeycu@gmail.com</a></p><p>Share this blog with your peers if you find it useful.</p><p><em>Hope it helps!</em></p><p><em>AnkitCode99 here….</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9c3f460715e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>