<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Fatima Alam</title>
    <description>The latest articles on Forem by Fatima Alam (@alamfatima1999).</description>
    <link>https://forem.com/alamfatima1999</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F914425%2Ff3327309-bd4f-41c8-8b41-5884dbf46fd3.jpeg</url>
      <title>Forem: Fatima Alam</title>
      <link>https://forem.com/alamfatima1999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alamfatima1999"/>
    <language>en</language>
    <item>
      <title>All things Kafka</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Fri, 20 Mar 2026 05:10:32 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/all-things-kafka-3cdb</link>
      <guid>https://forem.com/alamfatima1999/all-things-kafka-3cdb</guid>
      <description>&lt;p&gt;Kafka Crash Course: From Concept to a Hands-On Python Project&lt;br&gt;
Have you ever wondered how giant platforms like Uber, Netflix, or LinkedIn handle millions of events per second in real-time? The answer, more often than not, is Apache Kafka.&lt;/p&gt;

&lt;p&gt;If you are a microservices developer, data engineer, or just curious about event-driven architecture, understanding Kafka is no longer optional—it's a superpower.&lt;/p&gt;

&lt;p&gt;In this post, we will deconstruct what Kafka is, why it exists, and build a real-world "Notify Me" backend using Java.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem – "Tight Coupling"&lt;/strong&gt;&lt;br&gt;
Imagine you are building a simple e-commerce application called "AllThingsStore." In the beginning, you use a synchronous, monolithic architecture where microservices talk directly to each other.&lt;/p&gt;

&lt;p&gt;When a customer places an order:&lt;/p&gt;

&lt;p&gt;Order Service calls Payment Service.&lt;/p&gt;

&lt;p&gt;Payment Service calls Inventory Service.&lt;/p&gt;

&lt;p&gt;Inventory Service calls Notification Service.&lt;/p&gt;

&lt;p&gt;This works fine...What could go wrong??&lt;br&gt;
Until Black Friday. Suddenly, thousands of orders rush in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem visualized&lt;/strong&gt;:&lt;br&gt;
Here is what happens when direct, synchronous communication fails under load.&lt;/p&gt;

&lt;p&gt;In traditional microservices, systems use &lt;em&gt;Synchronous Communication&lt;/em&gt;. If the "Order Service" calls the "Payment Service" directly, it must wait for a response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Domino Effect&lt;/em&gt;&lt;/strong&gt;: If one service lags or crashes (especially during peak traffic), the entire chain freezes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Single Point of Failure&lt;/em&gt;&lt;/strong&gt;: A 10-minute outage in one service can lead to hours of backlog and business loss.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpc1zzjcd7l7dwf1zg05m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpc1zzjcd7l7dwf1zg05m.png" alt=" " width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EG&lt;/strong&gt;: When Payment Service gets overwhelmed and crashes, the entire chain freezes. The Order Service is stuck waiting for a response that will never come, leading to a catastrophic system failure and lost revenue. This is called &lt;em&gt;tight coupling&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Kafka Solution – "Event-Driven"&lt;/strong&gt;&lt;br&gt;
What if we redesigned the system so that services don't need to know about each other directly? This is where Apache Kafka enters the scene as a central message broker.&lt;/p&gt;

&lt;p&gt;Instead of Order Service calling Payment Service directly, it simply publishes an event: "An order was placed."&lt;/p&gt;

&lt;p&gt;Order Service (now a Producer) hands this event to Kafka and continues its work. It doesn't wait. It trusts Kafka to deliver.&lt;/p&gt;

&lt;p&gt;Kafka then organizes this event into a logical category called a Topic (e.g., "orders"). Any other service that needs this information (now a Consumer) simply subscribes to that topic.&lt;/p&gt;

&lt;p&gt;Key Kafka Concepts:&lt;br&gt;
&lt;strong&gt;Producers&lt;/strong&gt;: Services that create and send events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consumers&lt;/strong&gt;: Services that read and process events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics&lt;/strong&gt;: A logical bucket or category for events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brokers&lt;/strong&gt;: The actual servers that store and manage the data. Unlike traditional message brokers, Kafka persists data to disk, allowing it to be re-read (which is not the case with Rabbit MQ, Active MQ, Pub/Sub&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mastering Kafka’s Architecture&lt;/strong&gt;&lt;br&gt;
To truly harness Kafka, you must understand how it scales. Two main concepts achieve this: Partitions and Consumer Groups.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Partitions&lt;/strong&gt;
A Topic isn't just one big file. To allow for parallel processing, Kafka splits a Topic into multiple Partitions. Think of a partition as a commit log where data is only appended at the end.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This architecture enables multiple producers to write to different partitions simultaneously and multiple consumers to read from different partitions simultaneously.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdci591a7arjb8fixymw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdci591a7arjb8fixymw7.png" alt=" " width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Consumer Groups&lt;/strong&gt;
To read partitions in parallel, consumers are organized into Consumer Groups. Kafka automatically ensures that each partition in a topic is assigned to exactly one consumer within that group. This provides &lt;strong&gt;massive scalability&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Summarizing the Scale: &lt;strong&gt;The KRaft Architecture&lt;/strong&gt;&lt;br&gt;
Modern Kafka (v3.0+) has replaced its dependency on an external orchestration tool (&lt;strong&gt;Zookeeper&lt;/strong&gt;) with a built-in system called KRaft (Kafka Raft).&lt;/p&gt;

&lt;p&gt;This simplifies the architecture. A modern Kafka node can act as both a Broker (storing data) and a Controller (managing cluster consensus).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hands-On Project: A Java Notify me Service for an E-Comm&lt;/strong&gt;&lt;br&gt;
Now for the best part: bringing these concepts to life! We will build a simple "Notification" simulator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User clicks "Notify Me"
        ↓
Subscription saved (DB)

Inventory Service
item restocked
        ↓
Kafka Producer publishes event

Topic: item-restocked
        ↓
Kafka Broker
        ↓
Notification Service (Consumer)
        ↓
Fetch subscribed users
Send Email / Push Notification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Producer (Client App): Inventory Service which makes fake products and publishes them to an Products topic.&lt;/p&gt;

&lt;p&gt;Consumer (Order Processor): Notification Service that listens to the Products topic in real-time and "processes" the new products.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Kafka Setup (Local)&lt;/p&gt;

&lt;p&gt;Install Apache Kafka&lt;/p&gt;

&lt;p&gt;Start services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;zookeeper-server-start.sh config/zookeeper.properties
kafka-server-start.sh config/server.properties
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create topic&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kafka-topics.sh &lt;span class="nt"&gt;--create&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--topic&lt;/span&gt; item-restocked &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--bootstrap-server&lt;/span&gt; localhost:9092 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--partitions&lt;/span&gt; 3 &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--replication-factor&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;IntelliJ Project Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create Maven project&lt;/p&gt;

&lt;p&gt;Add dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.kafka&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;kafka-clients&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.6.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Event Model&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RestockEvent&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;RestockEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;productId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getProductId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Kafka Producer&lt;/strong&gt; (&lt;em&gt;Inventory Service&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;When item stock becomes available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.apache.kafka.clients.producer.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Properties&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RestockProducer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;KafkaProducer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Properties&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"localhost:9092"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key.serializer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"org.apache.kafka.common.serialization.StringSerializer"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value.serializer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"org.apache.kafka.common.serialization.StringSerializer"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KafkaProducer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;publishRestock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;ProducerRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ProducerRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"item-restocked"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;send&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Usage inside Inventory Service&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getStock&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="nc"&gt;RestockProducer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;publishRestock&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Kafka Consumer&lt;/strong&gt; (&lt;em&gt;Notification Service&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.apache.kafka.clients.consumer.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.time.Duration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RestockConsumer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Properties&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Properties&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"localhost:9092"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"group.id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"notification-service"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key.deserializer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"org.apache.kafka.common.serialization.StringDeserializer"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value.deserializer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
                &lt;span class="s"&gt;"org.apache.kafka.common.serialization.StringDeserializer"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;KafkaConsumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KafkaConsumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;subscribe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"item-restocked"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

            &lt;span class="nc"&gt;ConsumerRecords&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                    &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;poll&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofMillis&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

            &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConsumerRecord&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;records&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

                &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

                &lt;span class="n"&gt;notifyUsers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;notifyUsers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="nc"&gt;SubscriptionRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getUsers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Sending notification to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Subscription Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When user clicks Notify Me&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Subscription
------------
userId
productId
createdAt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;subscription&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example Event Flow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inventory updated
Stock = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Producer sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Topic: item-restocked
Message: productId=iphone15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consumer receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Notification service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fetch subscribers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user1
user2
user3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;email / push notification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why Kafka Works Well Here&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Benefits&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;decouples inventory and notification service&lt;br&gt;
scalable fanout to millions of users&lt;br&gt;
asynchronous processing&lt;br&gt;
fault tolerant&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Production Improvements&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Real systems add&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Redis → store top subscribers&lt;br&gt;
Kafka partitions → scale notifications&lt;br&gt;
Email service workers&lt;br&gt;
Rate limiting&lt;br&gt;
Batch notifications&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>beginners</category>
      <category>microservices</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Consistent Hashing</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Mon, 02 Mar 2026 19:31:09 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/consistent-hashing-1h17</link>
      <guid>https://forem.com/alamfatima1999/consistent-hashing-1h17</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need to distribute keys (data, requests, cache entries) across multiple servers.&lt;br&gt;
A common approach is:&lt;br&gt;
bucket = hash(key) % N&lt;br&gt;
Where N is the number of servers.&lt;br&gt;
The Problem?&lt;br&gt;
When N changes (a server is added or removed), almost all keys get reassigned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This causes?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Massive data movement&lt;/li&gt;
&lt;li&gt;Cache invalidation&lt;/li&gt;
&lt;li&gt;Network overhead&lt;/li&gt;
&lt;li&gt;We need a way to minimize key movement when servers change.&lt;/li&gt;
&lt;li&gt;The Solution: Consistent Hashing&lt;/li&gt;
&lt;li&gt;Instead of modulo arithmetic, consistent hashing:&lt;/li&gt;
&lt;li&gt;Maps both servers and keys onto a circular hash space (a ring).&lt;/li&gt;
&lt;li&gt;Assigns each key to the next server clockwise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This small change ensures that when servers are added or removed, only a small subset of keys move.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Step 1 — Create the Hash Ring&lt;br&gt;
Assume hash space = 0 to 99 (circular).&lt;/p&gt;

&lt;p&gt;Place 3 servers:&lt;br&gt;
A → 10&lt;br&gt;
B → 40&lt;br&gt;
C → 80&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 ----10(A)----40(B)----80(C)----99 -&amp;gt; back to 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2 — Add Keys&lt;br&gt;
Key Hash&lt;br&gt;
K1  5&lt;br&gt;
K2  25&lt;br&gt;
K3  50&lt;br&gt;
K4  85&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Assignment Rule&lt;/em&gt;:&lt;br&gt;
Move clockwise → first server you hit owns the key.&lt;/p&gt;

&lt;p&gt;Result:&lt;br&gt;
K1 (5) → A (10)&lt;br&gt;
K2 (25) → B (40)&lt;br&gt;
K3 (50) → C (80)&lt;br&gt;
K4 (85) → wrap → A (10)&lt;/p&gt;

&lt;p&gt;Distribution:&lt;br&gt;
A → K1, K4&lt;br&gt;
B → K2&lt;br&gt;
C → K3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Removing a Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remove Server B (40).&lt;br&gt;
New ring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 ----10(A)---------80(C)----99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which keys move?&lt;br&gt;
Only keys that belonged to B.&lt;br&gt;
That is:&lt;br&gt;
K2&lt;br&gt;
Reassign:&lt;br&gt;
K2 (25) → next clockwise → C (80)&lt;/p&gt;

&lt;p&gt;New distribution:&lt;br&gt;
A → K1, K4&lt;br&gt;
C → K2, K3&lt;/p&gt;

&lt;p&gt;Only one key moved.&lt;br&gt;
Adding a Server&lt;br&gt;
Add Server D → 30&lt;/p&gt;

&lt;p&gt;New ring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 ----10(A)----30(D)----80(C)----99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which keys are affected?&lt;br&gt;
Only keys between 10 and 30.&lt;/p&gt;

&lt;p&gt;That is:&lt;br&gt;
K2 (25)&lt;br&gt;
K2 moves from C → D.&lt;/p&gt;

&lt;p&gt;New distribution:&lt;br&gt;
A → K1, K4&lt;br&gt;
D → K2&lt;br&gt;
C → K3&lt;/p&gt;

&lt;p&gt;Again, only one key moved.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Why Consistent Hashing Is Better Than Traditional Hashing&lt;br&gt;
Traditional Hashing&lt;/em&gt; ?&lt;br&gt;
&lt;strong&gt;hash(key) % N&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If N changes (e.g., 3 → 4 servers):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Almost all keys get reassigned.&lt;/li&gt;
&lt;li&gt;Massive reshuffling occurs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Consistent Hashing&lt;/em&gt;&lt;br&gt;
When a server is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Removed → only its keys move.&lt;/li&gt;
&lt;li&gt;Added → only nearby keys move.&lt;/li&gt;
&lt;li&gt;Movement is proportional to 1 / number of servers, not the entire dataset.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consistent hashing minimizes data movement by placing servers and keys on a circular hash space and assigning each key to the next server clockwise.&lt;/p&gt;

&lt;p&gt;That design makes distributed systems scalable and stable.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>distributedsystems</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Netflix Stranger Things S5 premiere Outage</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Mon, 01 Dec 2025 17:42:13 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/netflix-stranger-things-s5-premiere-outage-3plo</link>
      <guid>https://forem.com/alamfatima1999/netflix-stranger-things-s5-premiere-outage-3plo</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stranger Things S5 premiere&lt;/strong&gt; -&amp;gt; Not a &lt;em&gt;capacity&lt;/em&gt;, but a &lt;em&gt;shape&lt;/em&gt; problem. How &lt;em&gt;synchronized users&lt;/em&gt;, &lt;em&gt;cache coldness&lt;/em&gt; and &lt;em&gt;aggressive retries&lt;/em&gt; turned a hot release into a short-lived outage.&lt;/p&gt;

&lt;p&gt;When the latest season of a blockbuster drops, millions of people behave like one organism: they all hit play at once. This time the surge didn’t look like the usual rounded hill of traffic — it looked like a vertical ladder: a &lt;strong&gt;0→kaboom&lt;/strong&gt; 🔥 spike in milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ey8b1jmuez0uiae9x5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ey8b1jmuez0uiae9x5o.png" alt=" " width="388" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well Netflix didn’t fail because it couldn’t scale; it failed because the &lt;strong&gt;shape of the traffic&lt;/strong&gt; broke assumptions baked into caching, retry logic and control planes. Here’s how that happened, what went wrong, and how the incident was recovered in minutes.&lt;/p&gt;

&lt;p&gt;Scaled systems can still fail if real users act differently than synthetic load tests: synchronized &lt;strong&gt;first-access events + cache cold starts + aggressive client retries&lt;/strong&gt; = &lt;strong&gt;request storm&lt;/strong&gt; that overwhelms control and data planes in milliseconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8xbkumocgeje78rx2j8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx8xbkumocgeje78rx2j8.png" alt=" " width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🤯 Confusing right??&lt;br&gt;
Let's dive in deep then&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The story, step-by-step&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Synchronized demand&lt;/strong&gt; —&amp;gt; a vertical ladder, not a hill&lt;br&gt;
Millions of users didn’t browse — they searched and hit play. That means first access behavior for each user (auth 🔐, metadata fetches, entitlement checks, CDN validation) executed simultaneously. This is not “more traffic” in the ordinary sense; it’s many cold sessions all at once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The critical difference&lt;/strong&gt; -&amp;gt; traffic shape vs traffic size&lt;br&gt;
Most autoscaling and load tests assume gradual increases or randomized requests. Here the size could have been handled — but the shape (an instantaneous wall) caused concentrated load on critical coordination points (auth, metadata, caches).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Cache cold-starts → cache-miss storm&lt;/strong&gt; → higher latency &amp;amp; memory pressure&lt;br&gt;
Edge caches / Redis were not warm for this particular object set. Each user triggered the same cache misses, producing many backend DB/metadata reads per user instead of cheap cache hits. Those synchronous misses amplified latency and memory pressure: classic request storm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Smart TVs were the first to go down&lt;/strong&gt; —&amp;gt; and why that’s important&lt;br&gt;
TV apps aggressively retry on failure and request higher-res manifests (so larger payloads). Retries multiply the original traffic in milliseconds and quickly saturate bandwidth and backend queues. Even extra headroom in bandwidth can’t absorb an exponential retry multiplier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Autoscaling reacted&lt;/strong&gt; —&amp;gt; but too late&lt;br&gt;
Autoscaling worked: control plane observed high metrics and spun more instances. But scaling happens in seconds/minutes; the spike arrived in milliseconds. By the time new capacity was available, retries had saturated services and increased latencies so much that the system was effectively degraded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. It wasn’t a hardware failure&lt;/strong&gt; —&amp;gt; it was behavioral overload&lt;br&gt;
This was not disks, NICs or CPUs failing. The system became bottlenecked due to request shape, retries and control-plane lag — i.e., how users and clients behaved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How the system recovered 🔄&lt;/strong&gt; (&lt;em&gt;and what the incident response did right&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Detect anomaly&lt;/strong&gt; —&amp;gt; monitoring caught the abnormal traffic shape and sudden latency/failure spikes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stabilize first, diagnose later&lt;/strong&gt; —&amp;gt; they immediately reduced the blast radius instead of trying to find a perfect root cause under active failure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Graceful degradation &amp;amp; load-shedding&lt;/strong&gt; —&amp;gt; rate limit non-critical requests, deprioritize background or non-essential API calls. Some clients were slowed so that others could stream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate limiting 🚦 / request shaping&lt;/strong&gt; —&amp;gt; tiered throttles and token buckets prevented control-plane saturation and allowed streaming tokens to be issued at a controlled pace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Partial restore&lt;/strong&gt; —&amp;gt; restore the core streaming functionality first; investigate detailed root cause after the user-impacting symptoms were minimized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No client update needed&lt;/strong&gt; —&amp;gt; changes were server-side (policies at edge / API Gateway) and the service returned within ~5 minutes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Root causes&lt;/strong&gt; (summary)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traffic shape&lt;/strong&gt; -&amp;gt; traffic size — extreme synchronization induced simultaneous cold-start work for many users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cache cold misses&lt;/strong&gt; —&amp;gt; downstream systems experienced multiplied demand because caches were not warmed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggressive client retries&lt;/strong&gt; —&amp;gt; smart TVs and other clients retried rapidly, amplifying load.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Autoscaling lag ⚙️&lt;/strong&gt; —&amp;gt; control plane and horizontal scaling were too slow compared to the spike velocity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Control-plane overload&lt;/strong&gt; —&amp;gt; not only data plane—control systems (auth/token issuers, rate-limiters) were hit hard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr2pms62hu9zhjn5v9lpk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr2pms62hu9zhjn5v9lpk.png" alt=" " width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Design lessons &amp;amp; takeaways&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Design for traffic volume shape, not just size&lt;/strong&gt;&lt;/em&gt;. Consider worst-case first-access bursts when estimating capacity.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Treat first access as the worst-case scenario&lt;/strong&gt;&lt;/em&gt;. Warm caches proactively, use pre-warming strategies before premieres.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Build recovery-first systems, not perfection&lt;/strong&gt;&lt;/em&gt;. Partial service with graceful degradation is preferable to full failure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Limit and shape retries client-side&lt;/strong&gt;&lt;/em&gt;. Backoff windows and jitter prevent synchronized retry storms ⚠️.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Split control-plane and data-plane scaling assumptions&lt;/strong&gt;&lt;/em&gt;. Make critical control-plane functions horizontally scalable and fast to spin up.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Plan pre-warm/CDN-population strategies for launches&lt;/strong&gt;&lt;/em&gt;. Pre-heat caches and edge nodes for known release times.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Run synchronized-user load-tests&lt;/strong&gt;&lt;/em&gt;. Simulate large numbers of first-time users hitting the same object set at the same exact millisecond to validate behavior.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Prioritize mitigating retries and bursts over raw throughput&lt;/strong&gt;&lt;/em&gt;. Retry amplification can outstrip any static extra capacity.&lt;/p&gt;

&lt;p&gt;This outage was not a sign that the cloud failed — it was a reminder that &lt;strong&gt;humans and devices don’t produce uniform loads&lt;/strong&gt;. They synchronize. They retry. They magnify small edge-cases into &lt;strong&gt;systemic stress&lt;/strong&gt;. The fix is less about infinite servers and more about shaping traffic: &lt;em&gt;smart client design&lt;/em&gt;, &lt;em&gt;robust edge policies, pre-warming&lt;/em&gt;, and &lt;em&gt;incident playbooks&lt;/em&gt; that keep the lights on while the forensic work starts.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>systemdesign</category>
      <category>redis</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Decorator Pattern</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sun, 05 Oct 2025 11:55:56 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/decorator-pattern-2162</link>
      <guid>https://forem.com/alamfatima1999/decorator-pattern-2162</guid>
      <description>&lt;p&gt;Who doesn't like customizations!!. Personally I wouldn't mind extra cheese on my pizza 😉.&lt;/p&gt;

&lt;p&gt;Just like that when you design systems made for customizations or those who let the user customize their orders you need the &lt;strong&gt;Decorator Design Pattern.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you’re building a &lt;strong&gt;vehicle configuration platform&lt;/strong&gt; (like Tesla’s online configurator 🚘).&lt;/p&gt;

&lt;p&gt;A customer can start with a basic car, and then add features dynamically like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sunroof 🌞&lt;/li&gt;
&lt;li&gt;Sports Package 🏎️&lt;/li&gt;
&lt;li&gt;Advanced Sound System 🔊
Each feature adds extra cost and description, without changing the original Car class — perfect use case for the Decorator Pattern.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Component Interface&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Vehicle {
    String getDescription();
    double getCost();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Concrete Component&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class BasicCar implements Vehicle {
    @Override
    public String getDescription() {
        return "Basic Car";
    }

    @Override
    public double getCost() {
        return 500000.0; // base price
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Abstract Decorator&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class VehicleDecorator implements Vehicle {
    protected Vehicle vehicle;

    public VehicleDecorator(Vehicle vehicle) {
        this.vehicle = vehicle;
    }

    @Override
    public String getDescription() {
        return vehicle.getDescription();
    }

    @Override
    public double getCost() {
        return vehicle.getCost();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Concrete Decorators&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each decorator adds a unique feature and modifies cost/description.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SunroofDecorator extends VehicleDecorator {
    public SunroofDecorator(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public String getDescription() {
        return vehicle.getDescription() + ", Sunroof";
    }

    @Override
    public double getCost() {
        return vehicle.getCost() + 40000.0;
    }
}

class SportsPackageDecorator extends VehicleDecorator {
    public SportsPackageDecorator(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public String getDescription() {
        return vehicle.getDescription() + ", Sports Package";
    }

    @Override
    public double getCost() {
        return vehicle.getCost() + 120000.0;
    }
}

class SoundSystemDecorator extends VehicleDecorator {
    public SoundSystemDecorator(Vehicle vehicle) {
        super(vehicle);
    }

    @Override
    public String getDescription() {
        return vehicle.getDescription() + ", Premium Sound System";
    }

    @Override
    public double getCost() {
        return vehicle.getCost() + 35000.0;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Main Class — Building the Custom Car&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class VehicleDecoratorDemo {
    public static void main(String[] args) {
        Vehicle car = new BasicCar();
        System.out.println(car.getDescription() + " ₹" + car.getCost());

        // Add sunroof
        car = new SunroofDecorator(car);
        System.out.println(car.getDescription() + " ₹" + car.getCost());

        // Add sports package
        car = new SportsPackageDecorator(car);
        System.out.println(car.getDescription() + " ₹" + car.getCost());

        // Add sound system
        car = new SoundSystemDecorator(car);
        System.out.println(car.getDescription() + " ₹" + car.getCost());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧩 &lt;strong&gt;Real-World Analogies&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Domain&lt;/th&gt;
&lt;th&gt;Example Decorators&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;E-Commerce&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Apply coupon, add gift wrap, add insurance&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Vehicles&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add GPS, airbags, turbo engine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Gaming&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add armor, magic boost, invisibility cloak&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Media Streaming&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add subtitles, change resolution, enable HDR&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>architecture</category>
      <category>designpatterns</category>
      <category>java</category>
    </item>
    <item>
      <title>Observer Design Pattern</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sat, 04 Oct 2025 16:47:13 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/observer-design-pattern-2kak</link>
      <guid>https://forem.com/alamfatima1999/observer-design-pattern-2kak</guid>
      <description>&lt;p&gt;How does your favorite websites like Amazon and Flipkart instantly reflect changes like when a product’s price drops and your shopping app flashes a notification, or when someone likes your post and it appears in real time on your feed? &lt;/p&gt;

&lt;p&gt;That’s not magic — it’s the &lt;strong&gt;Observer Design Pattern&lt;/strong&gt; in action.&lt;/p&gt;

&lt;p&gt;At its core, the Observer Pattern is all about &lt;strong&gt;communication and synchronization&lt;/strong&gt;. It creates a seamless link between objects — so when one object changes, all others that depend on it are automatically updated. Think of it as a digital chain reaction: one event triggers a cascade of updates, all without tightly coupling the system together.&lt;/p&gt;

&lt;p&gt;This pattern powers everything from &lt;strong&gt;UI frameworks and live dashboards **to **stock market apps, chat systems, and real-time notifications&lt;/strong&gt;. It’s the silent backbone behind modern reactive systems that need to stay always up to date.&lt;/p&gt;

&lt;p&gt;In this post, we’ll break down how the Observer Pattern works, when to use it, and explore a real-world example to help you master one of the most elegant ways to build flexible, event-driven software.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observable&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;

abstract class Observable {
    protected List&amp;lt;Observer&amp;gt; observers = new ArrayList&amp;lt;&amp;gt;();

    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers(Object arg) {
        for (Observer observer : observers) {
            observer.update(this, arg); // push model
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Observer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class Observer {
    public abstract void update(Observable observable, Object arg);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Product&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Product {
    private String id;
    private String name;
    private double price;
    private int stock;

    public Product(String id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public double getPrice() { return price; }
    public int getStock() { return stock; }

    public void setPrice(double price) { this.price = price; }
    public void setStock(int stock) { this.stock = stock; }

    @Override
    public String toString() {
        return name + " (₹" + price + ", stock: " + stock + ")";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Product Inventory&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ProductInventory extends Observable {
    private Map&amp;lt;String, Product&amp;gt; products = new HashMap&amp;lt;&amp;gt;();

    public void addProduct(Product product) {
        products.put(product.getId(), product);
        System.out.println("Added product: " + product);
        notifyObservers("Added: " + product.getName());
    }

    public void updatePrice(String productId, double newPrice) {
        Product p = products.get(productId);
        if (p != null) {
            p.setPrice(newPrice);
            System.out.println("\nPrice updated for " + p.getName() + " -&amp;gt; ₹" + newPrice);
            notifyObservers(p);
        }
    }

    public void updateStock(String productId, int newStock) {
        Product p = products.get(productId);
        if (p != null) {
            p.setStock(newStock);
            System.out.println("\nStock updated for " + p.getName() + " -&amp;gt; " + newStock + " units");
            notifyObservers(p);
        }
    }

    public Product getProduct(String id) {
        return products.get(id);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Website Observer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class WebsitePlatform extends Observer {
    @Override
    public void update(Observable observable, Object arg) {
        if (arg instanceof Product) {
            Product product = (Product) arg;
            System.out.println("🌐 Website Updated: " + product.getName() +
                               " is now ₹" + product.getPrice() +
                               " | Stock: " + product.getStock());
        } else if (arg instanceof String) {
            System.out.println("🌐 Website Notification: " + arg);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mobile App Observer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MobileAppPlatform extends Observer {
    @Override
    public void update(Observable observable, Object arg) {
        if (arg instanceof Product) {
            Product product = (Product) arg;
            System.out.println("📱 Mobile App Alert: " + product.getName() +
                               " now ₹" + product.getPrice() +
                               " | Stock: " + product.getStock());
        } else if (arg instanceof String) {
            System.out.println("📱 App Notification: " + arg);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Kiosk Observer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class KioskPlatform extends Observer {
    @Override
    public void update(Observable observable, Object arg) {
        if (arg instanceof Product) {
            Product product = (Product) arg;
            System.out.println("🏪 Kiosk Screen Updated: " + product.getName() +
                               " — Price: ₹" + product.getPrice() +
                               ", Stock: " + product.getStock());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Main Class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ECommerceObserverMain {
    public static void main(String[] args) {
        // Create observable inventory
        ProductInventory inventory = new ProductInventory();

        // Create observers (platforms)
        WebsitePlatform website = new WebsitePlatform();
        MobileAppPlatform app = new MobileAppPlatform();
        KioskPlatform kiosk = new KioskPlatform();

        // Register observers
        inventory.addObserver(website);
        inventory.addObserver(app);
        inventory.addObserver(kiosk);

        // Add products
        Product iphone = new Product("P001", "iPhone 15", 79999, 10);
        Product macbook = new Product("P002", "MacBook Air", 119999, 5);

        inventory.addProduct(iphone);
        inventory.addProduct(macbook);

        // Simulate updates
        inventory.updatePrice("P001", 75999);
        inventory.updateStock("P002", 3);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>architecture</category>
      <category>designpatterns</category>
      <category>programming</category>
    </item>
    <item>
      <title>Strategy Design Pattern</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sat, 04 Oct 2025 16:47:04 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/strategy-design-pattern-29jo</link>
      <guid>https://forem.com/alamfatima1999/strategy-design-pattern-29jo</guid>
      <description>&lt;p&gt;Imagine something that can be changed at runtime and is already been used and has to be replicated again.&lt;/p&gt;

&lt;p&gt;Think of an e-comm app like Amazon where you can choose from different Payment methods -&amp;gt; Cash, Card (Credit Card, Debit Card), UPI, etc... Now if you want to buy a Prime membership wouldn't you expect all these Payment methods there too??&lt;/p&gt;

&lt;p&gt;Let's look at how such a thing is actually carried and scaled out.&lt;/p&gt;

&lt;p&gt;This is your ShoppingCart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ShoppingCart {
    public void checkout(String paymentType, int amount) {
        if (paymentType.equals("CREDIT")) {
            System.out.println("Paid " + amount + " with Credit Card");
        } else if (paymentType.equals("PAYPAL")) {
            System.out.println("Paid " + amount + " with PayPal");
        } else if (paymentType.equals("UPI")) {
            System.out.println("Paid " + amount + " with UPI");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is your Subscription&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Subscription {
    public void checkout(String paymentType, int amount) {
        if (paymentType.equals("CREDIT")) {
            System.out.println("Paid " + amount + " with Credit Card");
        } else if (paymentType.equals("PAYPAL")) {
            System.out.println("Paid " + amount + " with PayPal");
        } else if (paymentType.equals("UPI")) {
            System.out.println("Paid " + amount + " with UPI");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't they look the same!!&lt;br&gt;
Now imagine if you want to add a new method "Cash" for both of them. Yes you are right!! This is a classic example of &lt;strong&gt;Code Redundancy&lt;/strong&gt;&lt;br&gt;
We could have instead did something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PaymentStrategy {
    void pay(int amount);
}

class CreditCardPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " with Credit Card");
    }
}

class PayPalPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " with PayPal");
    }
}

class UpiPayment implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid " + amount + " with UPI");
    }
}

class ShoppingCart {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}

class Subscription {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is now &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;More convenient.&lt;/li&gt;
&lt;li&gt;More scalable.&lt;/li&gt;
&lt;li&gt;Easily maintainable.&lt;/li&gt;
&lt;li&gt;No redundancy.&lt;/li&gt;
&lt;li&gt;More reliable&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Liskov Substitution Principle</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sat, 04 Oct 2025 16:46:48 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/liskov-substitution-principle-2nhc</link>
      <guid>https://forem.com/alamfatima1999/liskov-substitution-principle-2nhc</guid>
      <description>&lt;p&gt;If the Parent class has a method which is not required in a new Child class in this case our design becomes unscalable.&lt;br&gt;
There is a need to remove this method from this Parent Class and create a Child class which inherits from this Parent Class and all the present Child Classes now are sub Child of this Child Class now.&lt;br&gt;
For eg -&amp;gt; &lt;br&gt;
🚗 Before (LSP Violation)&lt;/p&gt;

&lt;p&gt;Here, Vehicle has hasEngine(). But Bicycle doesn’t really have an engine, so overriding it with null breaks LSP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Base class
class Vehicle {
    public int numberOfWheels() {
        return 0;
    }

    public Boolean hasEngine() {
        return true;
    }
}

// Car inherits Vehicle
class Car extends Vehicle {
    @Override
    public int numberOfWheels() {
        return 4;
    }

    @Override
    public Boolean hasEngine() {
        return true;
    }
}

// Motorcycle inherits Vehicle
class Motorcycle extends Vehicle {
    @Override
    public int numberOfWheels() {
        return 2;
    }

    @Override
    public Boolean hasEngine() {
        return true;
    }
}

// 🚲 Bicycle incorrectly overrides hasEngine
class Bicycle extends Vehicle {
    @Override
    public int numberOfWheels() {
        return 2;
    }

    @Override
    public Boolean hasEngine() {
        return null; // ❌ Problem: breaks LSP
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a client expects every Vehicle to have a meaningful hasEngine(), Bicycle breaks that expectation.&lt;/p&gt;

&lt;p&gt;✅ After (Refactored to Respect LSP)&lt;/p&gt;

&lt;p&gt;We separate Engine-related vehicles into another class.&lt;br&gt;
Now, Bicycle does not need to lie about hasEngine().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Base Vehicle class
class Vehicle {
    public int numberOfWheels() {
        return 0;
    }
}

// Engine-based vehicles
class EngineVehicle extends Vehicle {
    public boolean hasEngine() {
        return true;
    }
}

// Car inherits EngineVehicle
class Car extends EngineVehicle {
    @Override
    public int numberOfWheels() {
        return 4;
    }
}

// Motorcycle inherits EngineVehicle
class Motorcycle extends EngineVehicle {
    @Override
    public int numberOfWheels() {
        return 2;
    }
}

// Bicycle inherits Vehicle (no engine here)
class Bicycle extends Vehicle {
    @Override
    public int numberOfWheels() {
        return 2;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔑 Key Difference&lt;/p&gt;

&lt;p&gt;Before: All Vehicles were forced to define hasEngine(), but Bicycle violated LSP.&lt;/p&gt;

&lt;p&gt;After: We introduced EngineVehicle, so only engine-based vehicles must define engine-related behavior.&lt;/p&gt;

&lt;p&gt;Car and Motorcycle → extend EngineVehicle&lt;/p&gt;

&lt;p&gt;Bicycle → extends plain Vehicle&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Design Patterns</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sat, 04 Oct 2025 16:46:41 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/design-patterns-bo4</link>
      <guid>https://forem.com/alamfatima1999/design-patterns-bo4</guid>
      <description>&lt;p&gt;Index&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/alamfatima1999/liskov-substitution-principle-2nhc"&gt;Liskov Substitution Principle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/alamfatima1999/strategy-design-pattern-29jo"&gt;Strategy Design Pattern&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/alamfatima1999/observer-design-pattern-2kak"&gt;Observer Design Pattern&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/alamfatima1999/decorator-pattern-2162"&gt;Decorator Pattern&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>architecture</category>
      <category>designpatterns</category>
      <category>learning</category>
    </item>
    <item>
      <title>Index of Handling Real-Time Systems</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sun, 31 Aug 2025 05:43:24 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/index-of-handling-real-time-systems-c0d</link>
      <guid>https://forem.com/alamfatima1999/index-of-handling-real-time-systems-c0d</guid>
      <description>&lt;ol&gt;
&lt;li&gt; &lt;a href="https://dev.to/alamfatima1999/synchronize-1dhm"&gt;Synchronized.&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/alamfatima1999/isolation-levels-28le"&gt;Database Isolation Levels.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/alamfatima1999/distributed-concurrency-control-4h1i"&gt;Distributed Concurrency Control.&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>systemdesign</category>
      <category>distributedsystems</category>
      <category>database</category>
    </item>
    <item>
      <title>Handling Real-Time Systems</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sun, 31 Aug 2025 05:32:45 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/index-of-handling-real-time-systems-509m</link>
      <guid>https://forem.com/alamfatima1999/index-of-handling-real-time-systems-509m</guid>
      <description>&lt;p&gt;Imagine you and your friend both try to withdraw money from the same bank account at the exact same time. If the system isn’t careful, it might let both withdrawals go through, even if there isn’t enough money. This is exactly the kind of problem &lt;strong&gt;databases face when multiple users access data simultaneously.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’ve already studied &lt;strong&gt;ACID&lt;/strong&gt; properties, you know the theory behind reliable transactions. But how do real-world systems actually manage multiple users interacting with the database simultaneously?&lt;/p&gt;

&lt;p&gt;Think about it: how do platforms like &lt;strong&gt;BookMyShow&lt;/strong&gt;, &lt;strong&gt;Indigo&lt;/strong&gt;, or &lt;strong&gt;IRCTC&lt;/strong&gt; handle massive traffic, especially during peak times—like booking that coveted &lt;strong&gt;Tatkaal&lt;/strong&gt; ticket—without crashing or mixing up data?&lt;/p&gt;

&lt;p&gt;In this guide, we’ll break down &lt;strong&gt;concurrency&lt;/strong&gt;, &lt;strong&gt;isolation&lt;/strong&gt;, and &lt;strong&gt;consistency&lt;/strong&gt; issues in both &lt;strong&gt;centralized&lt;/strong&gt; and &lt;strong&gt;distributed database systems&lt;/strong&gt;. Step by step, you’ll see how real-time systems juggle multiple operations in parallel while keeping data accurate and consistent.&lt;/p&gt;

&lt;p&gt;Let’s dive in and paint a clear picture of what really happens behind the scenes when millions of users interact with a database at the same time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/alamfatima1999/index-of-handling-real-time-systems-c0d"&gt;Here's a reference for entire series of these topics.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>realtime</category>
      <category>threads</category>
    </item>
    <item>
      <title>Distributed Concurrency Control</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sun, 31 Aug 2025 05:32:01 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/distributed-concurrency-control-4h1i</link>
      <guid>https://forem.com/alamfatima1999/distributed-concurrency-control-4h1i</guid>
      <description>&lt;p&gt;In modern &lt;strong&gt;distributed systems&lt;/strong&gt;, &lt;strong&gt;multiple users&lt;/strong&gt; or applications often access and modify the same data &lt;strong&gt;concurrently&lt;/strong&gt;. Without proper control, this can lead to inconsistent data, race conditions, or system crashes. This is where Distributed Concurrency Control &lt;strong&gt;(DCC)&lt;/strong&gt; comes in—a set of techniques to manage concurrent operations safely across distributed databases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Distributed Concurrency Control?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Managing Bank Transactions Safely Across Systems&lt;/p&gt;

&lt;p&gt;Imagine a customer, Alice, transferring money between her savings and checking accounts in a distributed banking system. At the same time, another transaction is trying to deduct a loan payment from her checking account.&lt;/p&gt;

&lt;p&gt;Without proper coordination, these concurrent operations could overwrite each other, causing incorrect balances or inconsistent states. Distributed Concurrency Control ensures such conflicts are managed safely across multiple nodes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Goals&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: Database remains in a valid state after transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation&lt;/strong&gt;: Transactions behave as if executed one after another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Atomicity&lt;/strong&gt;: Each transaction completes fully or not at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bank Transaction Scenario&lt;/p&gt;

&lt;p&gt;Initial State:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Account&lt;/th&gt;
&lt;th&gt;Balance&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Savings&lt;/td&gt;
&lt;td&gt;5000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checking&lt;/td&gt;
&lt;td&gt;2000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Transactions&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;T1 (Savings → Checking transfer)&lt;/strong&gt;: Transfer 1000 from Savings to Checking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T2 (Loan Payment)&lt;/strong&gt;: Deduct 1500 from Checking.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Types of Distributed Concurrency Control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are two main strategies for managing concurrency in distributed systems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Pessimistic Concurrency Control (PCC)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimistic Concurrency Control (OCC)&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. Pessimistic Concurrency Control (PCC)&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;PCC&lt;/strong&gt; assumes conflicts between transactions are likely. It prevents conflicts by &lt;strong&gt;locking data before accessing it&lt;/strong&gt;. If another transaction wants the same data, it must wait.&lt;/li&gt;
&lt;li&gt;PCC assumes conflicts are &lt;strong&gt;likely&lt;/strong&gt; and &lt;strong&gt;prevents them upfront using locks&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// T1: Transfer Money
lock(savingsAccount, EXCLUSIVE);
lock(checkingAccount, EXCLUSIVE); 
savingsAccount.balance -= 1000;
checkingAccount.balance += 1000;
commit();
unlock(savingsAccount, checkingAccount);

// T2 will wait for locks
lock(checkingAccount, EXCLUSIVE);
checkingAccount.balance -= 1500;
commit();
unlock(checkingAccount);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it works?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1 (Transfer)&lt;/th&gt;
&lt;th&gt;T2 (Loan Payment)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;t1&lt;/td&gt;
&lt;td&gt;LOCK Savings (X-lock)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t2&lt;/td&gt;
&lt;td&gt;LOCK Checking (X-lock)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t3&lt;/td&gt;
&lt;td&gt;Deduct 1000 from Savings&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t4&lt;/td&gt;
&lt;td&gt;Add 1000 to Checking&lt;/td&gt;
&lt;td&gt;BLOCKED (Waiting for X-lock)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t5&lt;/td&gt;
&lt;td&gt;COMMIT &amp;amp; Release Locks&lt;/td&gt;
&lt;td&gt;LOCK Checking → Deduct 1500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t6&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;COMMIT&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Locks prevent conflicts.&lt;/li&gt;
&lt;li&gt;Other transactions wait until locks are released.&lt;/li&gt;
&lt;li&gt;Suitable for &lt;strong&gt;Repeatable Read and Serializable isolation levels&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pessimistic Concurrency Control Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T1: [LOCK Savings+Checking] → [UPDATE] → [COMMIT]
T2: [WAIT] → [LOCK] → [UPDATE] → [COMMIT]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of PCC&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevents conflicts proactively.&lt;/li&gt;
&lt;li&gt;Suitable for high-conflict environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Locks can cause deadlocks.&lt;/li&gt;
&lt;li&gt;Reduced concurrency if many transactions wait for locks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Optimistic Concurrency Control (OCC)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OCC&lt;/strong&gt; assumes conflicts are &lt;strong&gt;rare&lt;/strong&gt;, so it &lt;strong&gt;does not lock data&lt;/strong&gt;. Transactions proceed and &lt;strong&gt;validate at commit&lt;/strong&gt;.&lt;br&gt;
Code -&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# T1: Transfer Money
savings_local = read(savingsAccount)
checking_local = read(checkingAccount)
savings_local -= 1000
checking_local += 1000

if validate(savingsAccount, checkingAccount):
    write(savingsAccount, savings_local)
    write(checkingAccount, checking_local)
else:
    abort_and_retry()

# T2: Loan Payment
checking_local = read(checkingAccount)
checking_local -= 1500
if validate(checkingAccount):
    write(checkingAccount, checking_local)
else:
    abort_and_retry()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it works?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;T1 (Transfer)&lt;/th&gt;
&lt;th&gt;T2 (Loan Payment)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;t1&lt;/td&gt;
&lt;td&gt;Read Savings = 5000, Checking = 2000&lt;/td&gt;
&lt;td&gt;Read Checking = 2000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t2&lt;/td&gt;
&lt;td&gt;Update local copies&lt;/td&gt;
&lt;td&gt;Update local copy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t3&lt;/td&gt;
&lt;td&gt;Validate → no conflicts&lt;/td&gt;
&lt;td&gt;Validate → conflict detected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t4&lt;/td&gt;
&lt;td&gt;Commit: Savings = 4000, Checking = 3000&lt;/td&gt;
&lt;td&gt;ABORT → retry&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Key Points:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Transactions proceed without locks.&lt;/li&gt;
&lt;li&gt;Conflicts detected at commit trigger retries.&lt;/li&gt;
&lt;li&gt;Suitable for &lt;strong&gt;Read Uncommitted and Read Committed isolation levels&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Optimistic Concurrency Control Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;T1: [READ local copy] → [UPDATE local] → [VALIDATE PASS] → [COMMIT]
T2: [READ local copy] → [UPDATE local] → [VALIDATE FAIL] → [RETRY]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of OCC:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No locking overhead.&lt;/li&gt;
&lt;li&gt;High concurrency in low-conflict environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transactions may need to retry frequently in high-conflict scenarios.&lt;/li&gt;
&lt;li&gt;Requires careful validation logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How PCC and OCC Map to Isolation Levels&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concurrency Control&lt;/th&gt;
&lt;th&gt;Isolation Levels it Supports&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Pessimistic (PCC)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Repeatable Read, Serializable&lt;/td&gt;
&lt;td&gt;Locks prevent dirty &amp;amp; non-repeatable reads, ensures serializability&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Optimistic (OCC)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Read Committed, Read Uncommitted&lt;/td&gt;
&lt;td&gt;Validation ensures committed data, allows higher concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Database Isolation Levels</title>
      <dc:creator>Fatima Alam</dc:creator>
      <pubDate>Sun, 31 Aug 2025 04:50:43 +0000</pubDate>
      <link>https://forem.com/alamfatima1999/isolation-levels-28le</link>
      <guid>https://forem.com/alamfatima1999/isolation-levels-28le</guid>
      <description>&lt;h2&gt;
  
  
  💡 Database Isolation Levels
&lt;/h2&gt;

&lt;p&gt;Imagine you’re driving through a busy intersection.&lt;br&gt;
Cars = transactions&lt;br&gt;
Intersection = database&lt;br&gt;
Traffic lights = isolation levels&lt;/p&gt;

&lt;p&gt;If there are no rules, everyone just drives whenever they want → chaos.&lt;br&gt;
That’s why databases use Isolation Levels: rules that control how transactions move so data doesn’t crash into each other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🚦 The Traffic Light Analogy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think of a busy intersection.&lt;br&gt;
Cars = transactions&lt;br&gt;
Intersection = database&lt;br&gt;
Traffic lights = isolation levels&lt;/p&gt;

&lt;p&gt;🚗 Read Uncommitted → No traffic lights. Everyone just rushes in. Fast, but lots of accidents (dirty reads).&lt;/p&gt;

&lt;p&gt;🚦 Read Committed → Red/green lights exist, but they only control entry. Cars can still switch lanes suddenly (non-repeatable reads).&lt;/p&gt;

&lt;p&gt;🛑 Repeatable Read → Not only red/green, but lanes are locked until you exit the junction. No car can push you out — but surprise, new cars might still show up in an empty lane (phantoms).&lt;/p&gt;

&lt;p&gt;👮 Serializable → A strict traffic cop makes cars go one batch at a time. Zero accidents, but slow traffic.&lt;/p&gt;

&lt;p&gt;Understanding isolation levels is key to writing correct and concurrent database applications.&lt;br&gt;
This guide explains all levels with clear tables, anomalies, and transaction flows so that learning is fast and practical.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Isolation Level&lt;/th&gt;
&lt;th&gt;Key Points&lt;/th&gt;
&lt;th&gt;Concurrency&lt;/th&gt;
&lt;th&gt;Isolation Strength&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Uncommitted&lt;/td&gt;
&lt;td&gt;Can read uncommitted (dirty) data. Dirty Read, Non-Repeatable Read, Phantom Read possible.&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;Reads only committed data. Prevents Dirty Read. Non-Repeatable &amp;amp; Phantom Read possible.&lt;/td&gt;
&lt;td&gt;Medium-High&lt;/td&gt;
&lt;td&gt;Medium-Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;Reads held via shared locks until commit. Prevents Dirty &amp;amp; Non-Repeatable Read. Phantom Read possible.&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;Strictest. Uses range locks. Prevents Dirty, Non-Repeatable &amp;amp; Phantom Reads.&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Highest&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;2️⃣ What Problems Can Happen? (Accidents on the Road)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- Dirty Read 🚨 → You look at another car’s lane change before it’s even finished. (Reading uncommitted data).&lt;/li&gt;
&lt;li&gt;- Non-Repeatable Read 🔄 → You check once, see a car in lane 1. You look again, and it has moved to lane 2. (Same row, different value).&lt;/li&gt;
&lt;li&gt;- Phantom Read 👻 → You look at the junction, count 2 cars. A moment later, new cars have appeared out of nowhere. (New rows magically appear).&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Isolation Level&lt;/th&gt;
&lt;th&gt;Dirty Read&lt;/th&gt;
&lt;th&gt;Non-Repeatable Read&lt;/th&gt;
&lt;th&gt;Phantom Read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Uncommitted&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3️⃣ Transaction Examples (Traffic Flow Timelines)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s watch how two cars (T1 &amp;amp; T2) drive through the intersection.&lt;br&gt;
Time flows downward.&lt;br&gt;
S-lock = car blocking a lane for looking.&lt;br&gt;
X-lock = car blocking a lane for turning/changing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Read Uncommitted (Dirty Read possible)&lt;/em&gt;&lt;br&gt;
One car drives in without waiting, and another peeks too early.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
| Time ↓ | T1                    | T2                 |
| ------ | --------------------- | ------------------ |
| t1     | BEGIN                 |                    |
| t2     | UPDATE Alice (X-lock) |                    |
| t3     |                       | READ Alice (dirty) |
| t4     | COMMIT                |                    |


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Read Committed (Non-Repeatable Read possible)&lt;/em&gt;&lt;br&gt;
Cars wait for the green light, but switching lanes between looks is allowed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
| Time ↓ | T1                                          | T2                    |
| ------ | ------------------------------------------- | --------------------- |
| t1     | BEGIN                                       |                       |
| t2     | READ Alice (S-lock,&amp;lt;br&amp;gt;released after read) |                       |
| t3     |                                             | UPDATE Alice (X-lock) |
| t4     |                                             | COMMIT                |
| t5     | READ Alice again (new value)                |                       |
| t6     | COMMIT                                      |                       |

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Repeatable Read (Phantom Read possible)&lt;/em&gt;&lt;br&gt;
Your lane is locked until you leave, but new lanes can still open.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
| Time ↓ | T1                                   | T2                                |
| ------ | ------------------------------------ | --------------------------------- |
| t1     | BEGIN                                |                                   |
| t2     | READ Alice (S-lock held till commit) |                                   |
| t3     |                                      | UPDATE Alice (X-lock,&amp;lt;br&amp;gt;blocked) |
| t4     | COMMIT                               |                                   |
| t5     |                                      | UPDATE Alice (succeeds)           |
| t6     |                                      | COMMIT                            |


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Serializable (No anomalies)&lt;/em&gt;&lt;br&gt;
The traffic cop says: “One batch at a time. Wait your turn.”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
| Time ↓ | T1                                                                            | T2                            |
| ------ | ----------------------------------------------------------------------------- | ----------------------------- |
| t1     | BEGIN                                                                         |                               |
| t2     | READ Accounts WHERE balance&amp;gt;1000&amp;lt;br&amp;gt;(S-lock + range lock&amp;lt;br&amp;gt;held till commit) |                               |
| t3     |                                                                               | INSERT Bob(2000)&amp;lt;br&amp;gt;(blocked) |
| t4     | COMMIT                                                                        |                               |
| t5     |                                                                               | INSERT succeeds               |
| t6     |                                                                               | COMMIT                        |

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4️⃣ Crux / Key Differences&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature / Isolation&lt;/th&gt;
&lt;th&gt;Read Uncommitted&lt;/th&gt;
&lt;th&gt;Read Committed&lt;/th&gt;
&lt;th&gt;Repeatable Read&lt;/th&gt;
&lt;th&gt;Serializable&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dirty Read&lt;/td&gt;
&lt;td&gt;Can read uncommitted (dirty) data ✅&lt;/td&gt;
&lt;td&gt;Prevents dirty read ❌&lt;/td&gt;
&lt;td&gt;Prevents dirty read ❌&lt;/td&gt;
&lt;td&gt;Prevents dirty read ❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Non-Repeatable Read&lt;/td&gt;
&lt;td&gt;Non-Repeatable read possible ✅&lt;/td&gt;
&lt;td&gt;Non-Repeatable read possible ✅&lt;/td&gt;
&lt;td&gt;Prevents Non-Repeatable read ❌&lt;/td&gt;
&lt;td&gt;Prevents Non-Repeatable read ❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phantom Read&lt;/td&gt;
&lt;td&gt;Phantom read possible ✅&lt;/td&gt;
&lt;td&gt;Phantom read possible ✅&lt;/td&gt;
&lt;td&gt;Phantom read possible ✅&lt;/td&gt;
&lt;td&gt;Prevents Phantom read ❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;S-lock on read&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;Short-lived&lt;/td&gt;
&lt;td&gt;Held till commit&lt;/td&gt;
&lt;td&gt;Held + Range&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;X-lock on write&lt;/td&gt;
&lt;td&gt;Exclusive lock applied ✅&lt;/td&gt;
&lt;td&gt;Exclusive lock applied ✅&lt;/td&gt;
&lt;td&gt;Exclusive lock applied ✅&lt;/td&gt;
&lt;td&gt;Exclusive lock applied ✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Concurrency&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Medium-High&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Isolation strength&lt;/td&gt;
&lt;td&gt;Lowest&lt;/td&gt;
&lt;td&gt;Medium-Low&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Highest&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
