<?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 Tushar Khandelwal on Medium]]></title>
        <description><![CDATA[Stories by Tushar Khandelwal on Medium]]></description>
        <link>https://medium.com/@tusharkhandelwal8302?source=rss-5b6ad2a3b52c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*UChugBmOCR7uwzFS</url>
            <title>Stories by Tushar Khandelwal on Medium</title>
            <link>https://medium.com/@tusharkhandelwal8302?source=rss-5b6ad2a3b52c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:57:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tusharkhandelwal8302/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[Design a Scalable Notification Service — System Design Interview Problem]]></title>
            <link>https://medium.com/@tusharkhandelwal8302/design-a-scalable-notification-service-system-design-interview-problem-dcbb82b2b1a8?source=rss-5b6ad2a3b52c------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcbb82b2b1a8</guid>
            <category><![CDATA[sde-system-design]]></category>
            <category><![CDATA[high-level-implementation]]></category>
            <category><![CDATA[notification-service]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[scalable-microservices]]></category>
            <dc:creator><![CDATA[Tushar Khandelwal]]></dc:creator>
            <pubDate>Sun, 29 Jun 2025 17:03:40 GMT</pubDate>
            <atom:updated>2025-06-29T17:03:40.466Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design a Scalable Notification Service — System Design Interview Problem</h3><p>A <strong>notification service</strong> is a system responsible for delivering timely and relevant information to users across various channels such as SMS, email, push notifications, and in-app messages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PlnGIMprVJamKcP68XwPsg.png" /><figcaption>Unified Notification Hub</figcaption></figure><h3>Requirements</h3><h3>Functional Requirements:</h3><ol><li><strong>Multi-channel Support</strong>: The system must support sending notifications through various channels including email, SMS, push notifications, and in-app messages.</li><li><strong>Multiple Notification Types</strong>: Support transactional (e.g., order confirmation), promotional (e.g., discount offers), and system-generated alerts (e.g., password reset).</li><li><strong>Scheduled Delivery</strong>: Support scheduling of notifications for future delivery.</li><li><strong>Rate Limiting</strong>: Ensure that users receive only a limited number of promotional messages in a given day to prevent spam.</li><li><strong>Retry Mechanism</strong>: Handle notification delivery failures, retrying when necessary (e.g., failed SMS or email).</li></ol><h3>Non-Functional Requirements:</h3><ul><li><strong>Scalability:</strong> The system should handle millions of notifications per minute, supporting millions of concurrent users.</li><li><strong>High availability:</strong> Ensure minimal downtime so that notifications are delivered even in case of failures.</li><li><strong>Reliability:</strong> Guarantee at-least-once delivery of notifications, with the possibility of exactly-once semantics for certain use cases.</li><li><strong>Low latency:</strong> Notifications should be sent as quickly as possible to ensure timely delivery.</li></ul><h3>Scale Estimation</h3><p>Let’s estimate the scale to better inform design decisions.</p><ul><li><strong>Users</strong>: Assume the system serves <strong>50 million daily users</strong>.</li><li><strong>Notifications per user</strong>: On average, each user receives <strong>5 notifications/day</strong>.</li><li><strong>Peak load</strong>: Assume the peak time sees <strong>1 million notifications</strong> within 1 minutes (a common scenario during flash sales, for example).</li></ul><p>This means the system should handle:</p><ul><li><strong>Notifications per day:</strong> 50 million x 5 = 250 million notifications/day</li><li><strong>Peak notifications per second:</strong> 1 million / 60 **= ~**17,000 notifications/second</li></ul><h3>Storage Requirements</h3><p>Assuming average notification and user data size of 1KB.</p><ul><li><strong>Storage for user data:</strong> 50 million * 1 KB = 50GB</li><li><strong>Daily Storage for notifications:</strong> 50 million * 5 * 1 KB = 250GB</li></ul><h3>HLD</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vIiDE0WI-l1NrFH9Tyag_A.png" /><figcaption>High Level Diagram(Microservices Integration with Centralized Notification Service)</figcaption></figure><h3>Complete Notification System HLD — Component-by-Component Handling Functional &amp; Non Functional Requirements</h3><h3>🧑 User</h3><ul><li>The <strong>origin</strong> of any event (e.g., comment, like, purchase, login).</li></ul><h3>🔀 Load Balancer(Ensures Availability)</h3><ul><li>Ensures <strong>high availability</strong> by distributing incoming traffic across multiple backend servers.</li><li>Supports <strong>Multi-AZ Replication</strong> for failover and reliability.</li></ul><h3>🛡️ API Gateway(Ensures Security)</h3><ul><li>Acts as the <strong>entry point</strong> for all clients.</li><li>Authenticates users via tokens (e.g., OAuth, JWT).</li><li>Performs request validation and routing.</li><li>Also supports rate-limiting, throttling, and basic monitoring.</li></ul><h3>📬 Async Event Queue (Kafka or RabbitMQ)</h3><ul><li>Stores user-triggered events <strong>asynchronously</strong> for decoupled processing.</li><li>Provides durability, buffering, and support for <strong>at-least-once</strong> delivery.</li><li>Helps the system handle <strong>burst traffic</strong> without overload.</li></ul><h3>🧠 Classifier</h3><ul><li>Consumes messages from the queue.</li><li>Separates events into:</li><li><strong>Transactional</strong> (OTP, password reset)</li><li><strong>Promotional</strong> (discounts, new feature alerts)</li><li>Could also apply filtering rules (e.g., block duplicate promos).</li></ul><h3>🗃️ Database &amp; Redis Cache</h3><ul><li><strong>DB</strong> holds user profiles, channel preferences, and historical data.</li><li><strong>Sharding strategy</strong>:</li><li><strong>User-based sharding</strong>: horizontal scale based on user ID.</li><li><strong>Time-based sharding</strong>: for archiving old data.</li><li><strong>Redis</strong>:</li><li>Caches frequent lookups (preferences, online status).</li><li>Reduces DB pressure and improves latency.</li></ul><h3>🧮 Digester</h3><ul><li>Collects <strong>low-priority messages</strong> for online users.</li><li>Aggregates them into <strong>summary notifications</strong> (e.g., “5 people liked your photo”).</li><li>Sends these via <strong>in-app notifications</strong>, avoiding noisy emails/SMS.</li></ul><h3>🚦 Channel Router</h3><ul><li>Decides the <strong>best channel</strong> based on:</li><li>User preferences (email over SMS, etc.)</li><li>Message priority</li><li>System health or availability of third-party APIs</li><li>Routes to one or more processors accordingly.</li></ul><h3>📤 Channel Processors</h3><p>Each processor formats, queues, and sends the notification:</p><ul><li><strong>WhatsApp Processor</strong>:</li><li>Talks to WhatsApp Business API.</li><li>Useful for transactional notifications.</li><li><strong>Email Processor</strong>:</li><li>Sends via services like SES, SendGrid, Mailgun.</li><li><strong>SMS Processor</strong>:</li><li>Works with Twilio, AWS SNS, or local telcos.</li></ul><h3>📊 Rate Limiter</h3><ul><li>Protects channels like SMS/WhatsApp from overuse.</li><li>Especially important for high-priority transactional messages.</li><li>Can use token bucket or leaky bucket algorithms.</li></ul><h3>📌 Priority Queues (P0, P1, P2)</h3><ul><li><strong>P0</strong>: Immediate (e.g., OTPs) — real-time</li><li><strong>P1</strong>: Important (e.g., password change alerts)</li><li><strong>P2</strong>: Low-priority (e.g., likes)</li></ul><h3>🗑️ Dead Letter Queue (DLQ)</h3><ul><li>Stores failed messages for debugging, retries, or analysis.</li><li>Critical for ensuring <strong>no silent failures</strong>.</li></ul><h3>📈 Centralized Logging &amp; Alerts</h3><ul><li>Powered by tools like <strong>Prometheus, Grafana, ELK Stack, or Datadog</strong>.</li><li>Tracks:</li><li>Delivery success/failure</li><li>Latency across services</li><li>Retry attempts and DLQ size</li></ul><h3>🗄️ Old Archived Data</h3><ul><li>Stores historical notification data (cold storage).</li><li>May be used for analytics, reporting, or legal compliance.</li></ul><h3>APIs</h3><p><strong>Notification Request Creation:</strong></p><h3>Sample Request:</h3><pre>{<br>  &quot;requestId&quot;: &quot;abc123&quot;,<br>  &quot;timestamp&quot;: &quot;2024-09-17T14:00:00Z&quot;,<br>  &quot;notificationType&quot;: &quot;transactional&quot;,<br>  &quot;channels&quot;: [&quot;email&quot;, &quot;sms&quot;, &quot;push&quot;],<br>  &quot;recipient&quot;: {<br>    &quot;userId&quot;: &quot;user789&quot;,<br>    &quot;email&quot;: &quot;user@example.com&quot;<br>  },<br>  &quot;message&quot;: {<br>    &quot;subject&quot;: &quot;Order Confirmation&quot;,<br>    &quot;body&quot;: &quot;Thank you for your order! Your order #123456 has been confirmed.&quot;,<br>    &quot;attachments&quot;: [&quot;&lt;https://example.com/invoice123456.pdf&gt;&quot;],<br>    &quot;smsText&quot;: &quot;Thank you for your order! Order #123456 confirmed.&quot;,<br>    &quot;pushNotification&quot;: {<br>      &quot;title&quot;: &quot;Order Confirmed&quot;,<br>      &quot;body&quot;: &quot;Your order #123456 has been confirmed. Check your email for details.&quot;,<br>      &quot;icon&quot;: &quot;&lt;https://example.com/icon.png&gt;&quot;,<br>      &quot;action&quot;: {<br>        &quot;type&quot;: &quot;viewOrder&quot;,<br>        &quot;url&quot;: &quot;&lt;https://example.com/order/123456&gt;&quot;<br>      }<br>    }<br>  },<br>  &quot;schedule&quot;: {<br>    &quot;sendAt&quot;: &quot;2024-09-17T15:00:00Z&quot;<br>  },<br>  &quot;metadata&quot;: {<br>    &quot;priority&quot;: &quot;high&quot;,<br>    &quot;retries&quot;: 3<br>  }<br>}</pre><h3>2) Notification Service Ingestion</h3><p>The <strong>Notification Service</strong> (via an <strong>API Gateway / Load Balancer</strong>) receives the notification request.</p><p>The request is <strong>authenticated</strong> and <strong>validated</strong> to ensure it’s coming from an authorized source and all necessary information (recipient, message, channels, etc.) is present and correct.</p><h3>3) Fetching User Preferences</h3><h3>Sample Response from User Preference Service:</h3><pre>{<br>  &quot;userId&quot;: &quot;user789&quot;,<br>  &quot;preferences&quot;: {<br>    &quot;channels&quot;: {<br>      &quot;transactional&quot;: [&quot;email&quot;, &quot;push&quot;],<br>      &quot;promotional&quot;: [&quot;sms&quot;],<br>      &quot;systemAlert&quot;: [&quot;push&quot;, &quot;sms&quot;]<br>    },<br>    &quot;doNotDisturb&quot;: {<br>      &quot;enabled&quot;: true,<br>      &quot;startTime&quot;: &quot;22:00&quot;,<br>      &quot;endTime&quot;: &quot;08:00&quot;,<br>      &quot;timezone&quot;: &quot;America/New_York&quot;<br>    },<br>    &quot;dailyLimits&quot;: {<br>      &quot;promotionalLimit&quot;: 2,<br>      &quot;promotionalSentToday&quot;: 1<br>    },<br>    &quot;optOut&quot;: {<br>      &quot;email&quot;: false,<br>      &quot;sms&quot;: false,<br>      &quot;push&quot;: false<br>    },<br>    &quot;preferredTimeForDelivery&quot;: {<br>      &quot;enabled&quot;: true,<br>      &quot;startTime&quot;: &quot;09:00&quot;,<br>      &quot;endTime&quot;: &quot;21:00&quot;,<br>      &quot;timezone&quot;: &quot;America/New_York&quot;<br>    }<br>  }<br>}</pre><h3>4)Queueing the Notification</h3><p>Once the <strong>Notification Service</strong> has created and formatted the messages for the required channels, it places each message into the respective <strong>topic</strong> in the <strong>Notification Queue System</strong> (e.g., <strong>Kafka</strong>, <strong>RabbitMQ</strong>, <strong>AWS SQS</strong>)</p><ul><li>The <strong>Email Message</strong> is placed into the <strong>Email Topic</strong>.</li><li>The <strong>SMS Message</strong> is placed into the <strong>SMS Topic</strong>.</li><li>The <strong>Push Notification Message</strong> is placed into the <strong>Push Topic</strong>.</li></ul><h3>5) Sending the Notification</h3><p>Each <strong>Channel Processor</strong> handles the delivery of the notification through the specified channel:</p><h3>Email Processor:</h3><ul><li>Connects to the email provider (e.g., <strong>SendGrid</strong>, <strong>Mailgun</strong>, <strong>Amazon SES</strong>).</li><li>Sends the email, ensuring it follows the user’s preferences (e.g., HTML vs. plain text).</li><li>Handles errors such as bounces or invalid email addresses.</li></ul><h3>SMS Processor:</h3><ul><li>Connects to the SMS provider (e.g., <strong>Twilio</strong>, <strong>Nexmo</strong>).</li><li>Sends the SMS with any formatting adjustments to meet character limits or regional requirements.</li><li>Handles issues such as invalid phone numbers or network errors.</li></ul><h3>Push Notification Processor:</h3><ul><li>Uses services like <strong>Firebase Cloud Messaging (FCM)</strong> for Android or <strong>Apple Push Notification Service (APNs)</strong> for iOS.</li><li>Sends the push notification, including any metadata (e.g., app-specific actions or icons).</li><li>Handles failures such as expired device tokens or offline devices.</li></ul><h3>In-App Notification Processor:</h3><ul><li>Sends the in-app notification via <strong>WebSockets</strong> or <strong>long polling</strong> to the user’s active session.</li><li>Formats the message for display in the app’s UI, adhering to any app-specific display rules.</li></ul><h3>6) Monitoring and Logging</h3><p>To ensure smooth operation at scale, the system should have:</p><ul><li><strong>Centralized Logging</strong>: Use tools like <strong>ELK Stack</strong> or <strong>Prometheus/Grafana</strong> to collect logs from various components and monitor the health of the system.</li><li><strong>Alerting</strong>: Set up alerts for failures (e.g., notification delivery failure rates exceed a threshold).</li><li><strong>Metrics</strong>: Track metrics like success rate, failure rate, delivery latency, and throughput for each channel.</li></ul><h3>7) Sharding &amp; Partitioning</h3><p>To efficiently handle large datasets, particularly for user data and notification logs, sharding and partitioning distribute the load across multiple databases or geographic regions:</p><ul><li><strong>User-based Sharding</strong>: Distribute users across different databases or regions based on geographic location or user ID to balance the load.</li><li><strong>Time-based Partitioning</strong>: Organize notification logs into time-based partitions (e.g., daily or monthly) to improve query performance and manage large volumes of historical data.</li></ul><h3>8) Caching</h3><p>Implement caching with solutions like <strong>Redis</strong> or <strong>Memcached</strong> to store frequently accessed data, such as user preferences.</p><p>Caching reduces the load on the database and improves response times for real-time notifications by avoiding repeated database lookups.</p><h3>Thankyou for Reading!!!</h3><h3>If you found it valuable, hit a like ❤️ and consider subscribing for more such content every week.!!!</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcbb82b2b1a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>