<?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>DEV Community: Majd-sufyan</title>
    <description>The latest articles on DEV Community by Majd-sufyan (@majdsufian).</description>
    <link>https://dev.to/majdsufian</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F994237%2Fb175894c-e29a-40be-951e-7a149ab97295.jpg</url>
      <title>DEV Community: Majd-sufyan</title>
      <link>https://dev.to/majdsufian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/majdsufian"/>
    <language>en</language>
    <item>
      <title>Async &amp; Messaging: System Design Journey... Week 6</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Mon, 21 Sep 2026 15:16:15 +0000</pubDate>
      <link>https://dev.to/majdsufian/async-messaging-system-design-journey-week-6-12k7</link>
      <guid>https://dev.to/majdsufian/async-messaging-system-design-journey-week-6-12k7</guid>
      <description>&lt;h1&gt;
  
  
  Week 6: Async &amp;amp; Messaging: Designing Systems That Don't Have to Do Everything Now
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In Week 6, I focused on a part of system design that changes how I think about communication between components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous vs asynchronous processing&lt;/li&gt;
&lt;li&gt;Queues and message brokers&lt;/li&gt;
&lt;li&gt;Pub/Sub and event-driven architecture&lt;/li&gt;
&lt;li&gt;Message delivery guarantees&lt;/li&gt;
&lt;li&gt;Retries and Dead Letter Queues&lt;/li&gt;
&lt;li&gt;Idempotency and duplicate messages&lt;/li&gt;
&lt;li&gt;Ordering and failure handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main idea I took away this week:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Not every piece of work needs to happen while the user is waiting for the response.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Moving work to asynchronous processing can improve responsiveness, absorb traffic spikes, and reduce coupling between services, but it also introduces a completely new set of failure scenarios that we need to design for.&lt;/p&gt;




&lt;h1&gt;
  
  
  Synchronous vs Asynchronous Processing
&lt;/h1&gt;

&lt;p&gt;Let's start with something simple.&lt;/p&gt;

&lt;p&gt;Imagine an e-commerce system where a user creates an order.&lt;/p&gt;

&lt;p&gt;A synchronous approach could look 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;User
  ↓
Order Service
  ↓
Payment
  ↓
Inventory
  ↓
Email
  ↓
Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything happens before the user receives a response.&lt;/p&gt;

&lt;p&gt;This is straightforward, but it creates a problem:&lt;/p&gt;

&lt;p&gt;If one of those operations is slow or unavailable, the entire request can become slow or fail.&lt;/p&gt;

&lt;p&gt;With asynchronous processing, we can separate the work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  ↓
Order Service
  ↓
Queue
  ↓
Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Queue
  ↓
Order Worker
  ↓
Payment
  ↓
Inventory
  ↓
Email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user doesn't need to wait for every operation to finish.&lt;/p&gt;

&lt;p&gt;The system can acknowledge the request and process the remaining work in the background.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Use Asynchronous Processing?
&lt;/h1&gt;

&lt;p&gt;There are several reasons.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Reduce user-facing latency
&lt;/h3&gt;

&lt;p&gt;Suppose sending an email takes a couple of seconds.&lt;/p&gt;

&lt;p&gt;There is usually little value in forcing the user to wait for the email service before receiving an order confirmation.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create order
     ↓
Queue email task
     ↓
Respond to user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The email can be processed separately.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Handle traffic spikes
&lt;/h3&gt;

&lt;p&gt;Imagine a system normally receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 orders/minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but suddenly receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10,000 orders/minute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A queue can act as a buffer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10,000 requests
       ↓
     Queue
       ↓
Workers process at manageable speed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of forcing every downstream service to immediately process the entire spike.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Decouple services
&lt;/h3&gt;

&lt;p&gt;Without messaging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Service
      ↓
Payment Service
      ↓
Inventory Service
      ↓
Email Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Order Service becomes tightly connected to everything it needs to call.&lt;/p&gt;

&lt;p&gt;With messaging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             ┌→ Payment
             │
Order → Queue├→ Inventory
             │
             └→ Email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Different consumers can process the work independently.&lt;/p&gt;

&lt;p&gt;This creates a more loosely coupled architecture.&lt;/p&gt;




&lt;h1&gt;
  
  
  Producer and Consumer
&lt;/h1&gt;

&lt;p&gt;Two terms appear constantly when working with messaging systems:&lt;/p&gt;

&lt;h3&gt;
  
  
  Producer
&lt;/h3&gt;

&lt;p&gt;The component that creates or sends a message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Service
      ↓
   Producer
      ↓
    Queue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Consumer
&lt;/h3&gt;

&lt;p&gt;The component that reads and processes the message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Queue
  ↓
Consumer
  ↓
Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic pattern is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Producer → Queue → Consumer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message might represent work that needs to happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PaymentRequested"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"12345"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or an event that something already happened:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"OrderCreated"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"12345"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Queues vs Pub/Sub
&lt;/h1&gt;

&lt;p&gt;One distinction I wanted to understand clearly this week was the difference between a queue and Pub/Sub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Queue
&lt;/h2&gt;

&lt;p&gt;A queue is generally about &lt;strong&gt;work that needs to be processed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             ┌→ Worker 1
Queue ───────┼→ Worker 2
             └→ Worker 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple workers can share the workload.&lt;/p&gt;

&lt;p&gt;The queue allows the system to buffer work and process it as capacity becomes available.&lt;/p&gt;

&lt;p&gt;A simple way to think about it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Someone needs to do this work."&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Pub/Sub
&lt;/h2&gt;

&lt;p&gt;Pub/Sub is more about &lt;strong&gt;broadcasting an event to multiple interested consumers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 ┌→ Email
                 │
OrderCreated ────┼→ Analytics
                 │
                 └→ Inventory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Order Service doesn't necessarily need to directly call each service.&lt;/p&gt;

&lt;p&gt;It publishes:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;and different consumers can react to that event independently.&lt;/p&gt;

&lt;p&gt;The mental model I use is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Queue = process this work.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pub/Sub = something happened; whoever cares can react.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Event-Driven Architecture
&lt;/h1&gt;

&lt;p&gt;This leads to a bigger concept: &lt;strong&gt;event-driven architecture&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of building a chain of direct service calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Service
     ↓
Inventory Service
     ↓
Email Service
     ↓
Analytics Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Service
     ↓
OrderCreated
     ↓
Message Broker
     ↓
 ┌───┼────┬──────┐
 ↓   ↓    ↓      ↓
Email Inventory Analytics Fraud
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Order Service is essentially saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"An order was created."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It doesn't necessarily need to know exactly which services are listening.&lt;/p&gt;

&lt;p&gt;This can make systems more loosely coupled and easier to extend.&lt;/p&gt;

&lt;p&gt;For example, adding a new analytics consumer doesn't necessarily require changing the Order Service itself.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Problem With Asynchronous Systems
&lt;/h1&gt;

&lt;p&gt;This is where the topic becomes much more interesting.&lt;/p&gt;

&lt;p&gt;Once we introduce queues, we also introduce new failure modes.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Queue
  ↓
Order Worker
  ↓
Charge Customer
  ↓
Update Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if the worker crashes after charging the customer but before acknowledging the message?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Queue
  ↓
Order Worker
  ↓
Charge Customer
  ↓
💥 CRASH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system may not know whether the message was successfully processed.&lt;/p&gt;

&lt;p&gt;Should it try again?&lt;/p&gt;

&lt;p&gt;This leads to &lt;strong&gt;message delivery guarantees&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  At-Most-Once Delivery
&lt;/h1&gt;

&lt;p&gt;At-most-once means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A message is processed zero or one time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The message might be lost if processing fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message
   ↓
Consumer
   ↓
Failure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No retry means no duplicate processing, but the message may never be processed.&lt;/p&gt;

&lt;p&gt;This can be acceptable for some non-critical use cases, such as certain analytics events.&lt;/p&gt;




&lt;h1&gt;
  
  
  At-Least-Once Delivery
&lt;/h1&gt;

&lt;p&gt;At-least-once means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The system tries to ensure the message is processed, but it may be processed more than once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message #123
     ↓
Consumer
     ↓
Process message
     ↓
💥 Crash before acknowledgement
     ↓
Message delivered again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message #123
     ↓
Processed
     ↓
Processed AGAIN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the most important concepts from this week.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At-least-once delivery means consumers need to be prepared for duplicates.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Exactly-Once Delivery
&lt;/h1&gt;

&lt;p&gt;Exactly-once sounds like the ideal solution:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Process every message exactly once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In distributed systems, however, guaranteeing true exactly-once processing across multiple components can be difficult.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Consumer
   ↓
Payment Provider
   ↓
Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The payment might succeed, but the consumer could crash before recording that success.&lt;/p&gt;

&lt;p&gt;When the message is retried, the consumer may not know whether the original payment happened.&lt;/p&gt;

&lt;p&gt;Because of this, a common design approach is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;At-least-once delivery + idempotent consumers.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rather than assuming duplicates will never happen, we design the system so duplicates are safe.&lt;/p&gt;




&lt;h1&gt;
  
  
  Idempotency
&lt;/h1&gt;

&lt;p&gt;This was one of the concepts from Week 4 that became much more concrete this week.&lt;/p&gt;

&lt;p&gt;An operation is idempotent when performing it multiple times produces the same final result as performing it once.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET status = "ACTIVE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doing this once or ten times still results in:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;balance = balance + €100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is not naturally idempotent.&lt;/p&gt;

&lt;p&gt;Doing it twice gives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+€200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+€100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This becomes particularly important for payments and other operations where duplicate processing has real consequences.&lt;/p&gt;




&lt;h1&gt;
  
  
  Handling Duplicate Messages
&lt;/h1&gt;

&lt;p&gt;One common approach is to give every event a unique ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eventId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"abc-123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PaymentRequested"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"order-456"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The consumer can keep track of processed event IDs.&lt;/p&gt;

&lt;p&gt;If it receives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abc-123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;again, it can recognize that the event was already processed and avoid performing the operation twice.&lt;/p&gt;

&lt;p&gt;The important principle is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;If your messaging system can deliver a message more than once, your consumer needs to be designed accordingly.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Retries
&lt;/h1&gt;

&lt;p&gt;Not every failure is permanent.&lt;/p&gt;

&lt;p&gt;A downstream service might simply be temporarily unavailable.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Attempt 1 → failure
       ↓
wait
       ↓
Attempt 2 → failure
       ↓
wait
       ↓
Attempt 3 → success
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of retrying immediately and indefinitely, systems commonly use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited retry attempts&lt;/li&gt;
&lt;li&gt;Backoff&lt;/li&gt;
&lt;li&gt;Jitter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 sec
  ↓
2 sec
  ↓
4 sec
  ↓
8 sec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reduces the risk of making an already struggling service even more overloaded.&lt;/p&gt;




&lt;h1&gt;
  
  
  Dead Letter Queues
&lt;/h1&gt;

&lt;p&gt;But what happens when a message keeps failing?&lt;/p&gt;

&lt;p&gt;We don't want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Message
  ↓
Retry
  ↓
Retry
  ↓
Retry
  ↓
Retry
  ↓
Forever...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, after a certain number of failures, we can move it to a &lt;strong&gt;Dead Letter Queue (DLQ)&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;Message
   ↓
Attempt 1 ❌
   ↓
Attempt 2 ❌
   ↓
Attempt 3 ❌
   ↓
DLQ
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DLQ gives engineers a place to inspect problematic messages without allowing them to continuously interfere with normal processing.&lt;/p&gt;

&lt;p&gt;For example, a message might end up there because of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invalid data&lt;/li&gt;
&lt;li&gt;A persistent downstream failure&lt;/li&gt;
&lt;li&gt;A software bug&lt;/li&gt;
&lt;li&gt;An unexpected edge case&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Message Ordering
&lt;/h1&gt;

&lt;p&gt;Another question I considered was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Does the order of messages matter?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine these events:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. OrderCreated
2. OrderCancelled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If they are processed in the opposite order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OrderCancelled
      ↓
OrderCreated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the system could end up in an incorrect state.&lt;/p&gt;

&lt;p&gt;But not every system needs strict ordering.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;may not require a specific order.&lt;/p&gt;

&lt;p&gt;This means ordering should be treated as a &lt;strong&gt;business requirement&lt;/strong&gt;, not something we automatically enable everywhere.&lt;/p&gt;

&lt;p&gt;There is also a trade-off: stronger ordering requirements can restrict how much work can be processed in parallel.&lt;/p&gt;




&lt;h1&gt;
  
  
  Designing an Order Processing System
&lt;/h1&gt;

&lt;p&gt;For this week's design exercise, I used an order processing system to bring these concepts together.&lt;/p&gt;

&lt;p&gt;A simplified architecture looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    User
                      ↓
                POST /orders
                      ↓
                Order Service
                      ↓
                  Order DB
                      ↓
                OrderCreated
                      ↓
                    Queue
                      ↓
                 Order Worker
                  /    |    \
                 ↓     ↓     ↓
             Payment Inventory Email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The synchronous boundary could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Create Order
 ↓
Save Order
 ↓
Publish message
 ↓
Return response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the remaining processing happens asynchronously.&lt;/p&gt;

&lt;p&gt;The exact boundary depends on the requirements.&lt;/p&gt;

&lt;p&gt;For example, if the user needs an immediate payment result, payment might need to remain synchronous. If the result can be processed later, it may be a good candidate for asynchronous processing.&lt;/p&gt;

&lt;p&gt;The important design question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Which operations actually need to happen before the user receives a response?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Backend Lens
&lt;/h1&gt;

&lt;p&gt;This week's design exercise made me look at asynchronous systems differently.&lt;/p&gt;

&lt;p&gt;Instead of only asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"How do I process this message?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I started asking:&lt;/p&gt;

&lt;h3&gt;
  
  
  What if the message is processed twice?
&lt;/h3&gt;

&lt;p&gt;Can the consumer safely handle it?&lt;/p&gt;

&lt;h3&gt;
  
  
  What if processing fails halfway?
&lt;/h3&gt;

&lt;p&gt;Could the operation leave the system in an inconsistent state?&lt;/p&gt;

&lt;h3&gt;
  
  
  What if the downstream service is temporarily unavailable?
&lt;/h3&gt;

&lt;p&gt;Should we retry?&lt;/p&gt;

&lt;p&gt;How many times?&lt;/p&gt;

&lt;h3&gt;
  
  
  What if the message never succeeds?
&lt;/h3&gt;

&lt;p&gt;Should it go to a DLQ?&lt;/p&gt;

&lt;h3&gt;
  
  
  Does ordering matter?
&lt;/h3&gt;

&lt;p&gt;If so, what level of ordering is actually required?&lt;/p&gt;

&lt;p&gt;These questions are where a simple queue turns into a real system-design problem.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;p&gt;The biggest lessons I took from Week 6 were:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Async processing isn't just about performance
&lt;/h3&gt;

&lt;p&gt;It can also help with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decoupling&lt;/li&gt;
&lt;li&gt;Traffic spikes&lt;/li&gt;
&lt;li&gt;Resilience&lt;/li&gt;
&lt;li&gt;Independent scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Async systems introduce new failure modes
&lt;/h3&gt;

&lt;p&gt;Once work becomes asynchronous, we have to think about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicates&lt;/li&gt;
&lt;li&gt;Lost messages&lt;/li&gt;
&lt;li&gt;Retries&lt;/li&gt;
&lt;li&gt;Partial failures&lt;/li&gt;
&lt;li&gt;Ordering&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. At-least-once delivery changes how consumers are designed
&lt;/h3&gt;

&lt;p&gt;If duplicates are possible, consumers need to be able to handle them safely.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Idempotency is extremely important
&lt;/h3&gt;

&lt;p&gt;Especially for operations such as payments, where processing the same event twice can have real consequences.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Don't make everything asynchronous
&lt;/h3&gt;

&lt;p&gt;The important question isn't:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Should this be async?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"What actually needs to happen before I can respond to the user?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Reflection
&lt;/h1&gt;

&lt;p&gt;Week 6 helped me connect several concepts from the previous weeks.&lt;/p&gt;

&lt;p&gt;In Week 4, I learned about retries, timeouts, circuit breakers, and idempotency in the context of distributed calls.&lt;/p&gt;

&lt;p&gt;This week, I saw the same ideas from a different perspective.&lt;/p&gt;

&lt;p&gt;When communication becomes asynchronous, &lt;strong&gt;failure is no longer just a request that returns an error&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A message can be delayed, duplicated, processed partially, retried, or moved to a dead letter queue.&lt;/p&gt;

&lt;p&gt;That means reliable asynchronous systems aren't simply about adding a queue.&lt;/p&gt;

&lt;p&gt;They're about designing what happens &lt;strong&gt;when things don't go as planned&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And that is probably the biggest lesson I took from this week:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Asynchronous architecture gives you more flexibility, but it also makes failure handling part of the design itself.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  What's Next?
&lt;/h1&gt;

&lt;p&gt;Next week, I'll dive deeper into &lt;strong&gt;distributed systems and consistency&lt;/strong&gt;, including replication, consistency models, and the trade-offs involved when data exists across multiple machines.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>backend</category>
      <category>scalability</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>APIs &amp; Service Architecture. System Design Journey... Week 5</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Tue, 08 Sep 2026 14:01:14 +0000</pubDate>
      <link>https://dev.to/majdsufian/apis-service-architecture-system-design-journey-week-5-2c23</link>
      <guid>https://dev.to/majdsufian/apis-service-architecture-system-design-journey-week-5-2c23</guid>
      <description>&lt;h1&gt;
  
  
  Learn System Design with Me — Week 5 / 12
&lt;/h1&gt;

&lt;h1&gt;
  
  
  APIs &amp;amp; Service Architecture
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In Week 5, I shifted my focus from how systems store and process data to &lt;strong&gt;how different clients and services communicate with them&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The main goals this week were to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand the differences between &lt;strong&gt;REST, GraphQL, and gRPC&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Learn how to think about &lt;strong&gt;API design as part of system architecture&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Understand &lt;strong&gt;API versioning&lt;/strong&gt; and why it matters&lt;/li&gt;
&lt;li&gt;Design an API that can support multiple clients, such as web and mobile&lt;/li&gt;
&lt;li&gt;Redesign an existing API with scalability and maintainability in mind&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing that became clear this week is that an API is much more than a collection of endpoints.&lt;/p&gt;

&lt;p&gt;It is a &lt;strong&gt;contract between systems&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  REST vs GraphQL vs gRPC
&lt;/h2&gt;

&lt;p&gt;One of the main topics this week was understanding that there is no universally "best" API technology.&lt;/p&gt;

&lt;p&gt;The right choice depends on the communication pattern and requirements of the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  REST
&lt;/h3&gt;

&lt;p&gt;REST is probably the style I've worked with the most throughout my career.&lt;/p&gt;

&lt;p&gt;It is resource-oriented and commonly uses HTTP methods such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PUT&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;PATCH&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DELETE&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET /users/123
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;REST is simple, widely supported, and works very well for public-facing APIs.&lt;/p&gt;




&lt;h3&gt;
  
  
  GraphQL
&lt;/h3&gt;

&lt;p&gt;GraphQL takes a different approach.&lt;/p&gt;

&lt;p&gt;Instead of the server defining exactly which fields are returned by each endpoint, the client can request the data it needs.&lt;/p&gt;

&lt;p&gt;For example, a mobile application might only need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;avatar&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while a web application might request additional information.&lt;/p&gt;

&lt;p&gt;This can be especially useful when different clients have very different data requirements.&lt;/p&gt;

&lt;p&gt;However, GraphQL also introduces additional complexity around:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Query complexity&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Authorization&lt;/li&gt;
&lt;li&gt;Monitoring&lt;/li&gt;
&lt;li&gt;Preventing expensive queries&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  gRPC
&lt;/h3&gt;

&lt;p&gt;gRPC is designed more for &lt;strong&gt;service-to-service communication&lt;/strong&gt; than typical public APIs.&lt;/p&gt;

&lt;p&gt;It uses Protocol Buffers and provides strongly typed contracts between services.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order Service
      ↓
   gRPC call
      ↓
Payment Service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main takeaway for me was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;REST, GraphQL, and gRPC aren't competitors where one simply replaces the others.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They solve different communication problems.&lt;/p&gt;

&lt;p&gt;A system can even use multiple approaches at the same time.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Web / Mobile
     ↓
  REST API
     ↓
Backend Services
     ↓
    gRPC
     ↓
Other Internal Services
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  API Versioning
&lt;/h2&gt;

&lt;p&gt;Another important topic this week was &lt;strong&gt;API evolution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;APIs often live much longer than the code that initially created them.&lt;/p&gt;

&lt;p&gt;Once clients depend on an API, changing its contract can break those clients.&lt;/p&gt;

&lt;p&gt;For example, imagine we initially have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET /api/v1/users/123
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the response contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, we want to make a breaking change to the response.&lt;/p&gt;

&lt;p&gt;Instead of immediately breaking existing clients, we can introduce:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET /api/v2/users/123
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows older clients to continue using &lt;code&gt;v1&lt;/code&gt; while newer clients migrate to &lt;code&gt;v2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The important lesson wasn't simply &lt;em&gt;"use &lt;code&gt;/v1&lt;/code&gt; and &lt;code&gt;/v2&lt;/code&gt;."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It was understanding that &lt;strong&gt;API contracts need to evolve without unnecessarily breaking consumers&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Designing a Multi-Client API
&lt;/h2&gt;

&lt;p&gt;For this week's design exercise, I considered a system used by both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web clients&lt;/li&gt;
&lt;li&gt;Mobile clients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first this sounds simple.&lt;/p&gt;

&lt;p&gt;But the requirements of the clients can be very different.&lt;/p&gt;

&lt;p&gt;A web application might have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large screen&lt;/li&gt;
&lt;li&gt;Fast network&lt;/li&gt;
&lt;li&gt;More bandwidth&lt;/li&gt;
&lt;li&gt;More complex UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A mobile application might have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited bandwidth&lt;/li&gt;
&lt;li&gt;Higher latency&lt;/li&gt;
&lt;li&gt;Smaller payload requirements&lt;/li&gt;
&lt;li&gt;Unreliable network connections&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates an important API design question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Should every client receive exactly the same data?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not necessarily.&lt;/p&gt;




&lt;h2&gt;
  
  
  API Design Trade-offs
&lt;/h2&gt;

&lt;p&gt;One approach is to create a single REST API that serves all clients.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              ┌── Web
              │
Client ───────┼── Mobile
              │
              └── Other clients
                    ↓
                 REST API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is simple and easy to maintain initially.&lt;/p&gt;

&lt;p&gt;But as clients become more complex, the API can start returning either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too much data&lt;/li&gt;
&lt;li&gt;Too little data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one reason technologies such as GraphQL can become attractive for multi-client systems.&lt;/p&gt;

&lt;p&gt;The important part is not choosing GraphQL automatically.&lt;/p&gt;

&lt;p&gt;It is recognizing the &lt;strong&gt;trade-off between simplicity and flexibility&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  API as a Contract
&lt;/h2&gt;

&lt;p&gt;One of my biggest takeaways from this week was thinking about APIs as contracts.&lt;/p&gt;

&lt;p&gt;When another system consumes your API, it depends on things such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request format&lt;/li&gt;
&lt;li&gt;Response structure&lt;/li&gt;
&lt;li&gt;Error behavior&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Performance expectations&lt;/li&gt;
&lt;li&gt;Versioning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changing an API therefore isn't just a code change.&lt;/p&gt;

&lt;p&gt;It can become a &lt;strong&gt;system-wide change&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is why API design should consider not only how an endpoint works today, but also how it might evolve in the future.&lt;/p&gt;




&lt;h2&gt;
  
  
  Redesigning an Existing API
&lt;/h2&gt;

&lt;p&gt;As part of the practical work, I also looked at how an existing API could be redesigned.&lt;/p&gt;

&lt;p&gt;Instead of only asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Does this endpoint work?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I started asking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who consumes this API?&lt;/li&gt;
&lt;li&gt;What data does each client actually need?&lt;/li&gt;
&lt;li&gt;Which operations are read-heavy?&lt;/li&gt;
&lt;li&gt;What happens when the API evolves?&lt;/li&gt;
&lt;li&gt;Which changes would break existing clients?&lt;/li&gt;
&lt;li&gt;Should this communication use REST, GraphQL, or gRPC?&lt;/li&gt;
&lt;li&gt;How should errors be represented?&lt;/li&gt;
&lt;li&gt;How should the API be versioned?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions make API design feel much more like a system design problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reflections
&lt;/h2&gt;

&lt;p&gt;Before this week, I mostly thought about APIs from the perspective of implementing endpoints.&lt;/p&gt;

&lt;p&gt;This week made me think about APIs from the perspective of &lt;strong&gt;communication between systems&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The technology is only one part of the decision.&lt;/p&gt;

&lt;p&gt;The more important questions are:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Who is consuming the API?&lt;/p&gt;

&lt;p&gt;What are their requirements?&lt;/p&gt;

&lt;p&gt;How will the API evolve?&lt;/p&gt;

&lt;p&gt;What happens when there are hundreds of clients depending on it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That shift in perspective was probably the biggest lesson from Week 5.&lt;/p&gt;




&lt;h2&gt;
  
  
  What’s Next — Week 6
&lt;/h2&gt;

&lt;p&gt;Next week, I'll move deeper into distributed systems and asynchronous communication:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Message queues&lt;/li&gt;
&lt;li&gt;Event-driven architecture&lt;/li&gt;
&lt;li&gt;Kafka&lt;/li&gt;
&lt;li&gt;Asynchronous processing&lt;/li&gt;
&lt;li&gt;Designing systems that don't need every operation to happen synchronously&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The journey continues 🚀&lt;/p&gt;

</description>
      <category>api</category>
      <category>architecture</category>
      <category>backend</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Reliability, Failures &amp; Designing a Payment API: System Design Journey... Week 4</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Thu, 25 Jun 2026 18:46:00 +0000</pubDate>
      <link>https://dev.to/majdsufian/system-design-journey-week-4-reliability-failures-designing-a-payment-api-ggi</link>
      <guid>https://dev.to/majdsufian/system-design-journey-week-4-reliability-failures-designing-a-payment-api-ggi</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In Week 4, I focused on a topic that every distributed system eventually faces:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Failures are inevitable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No matter how well a system is designed, networks fail, servers crash, databases become unavailable, and requests time out.&lt;/p&gt;

&lt;p&gt;The goal this week was to understand how reliable systems continue operating despite these failures.&lt;/p&gt;

&lt;p&gt;My main focus areas were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fault tolerance&lt;/li&gt;
&lt;li&gt;Retries and timeouts&lt;/li&gt;
&lt;li&gt;Circuit breakers&lt;/li&gt;
&lt;li&gt;Idempotency&lt;/li&gt;
&lt;li&gt;Designing systems that avoid cascading failures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To apply these concepts, I designed a simplified Payment API, where correctness matters more than almost any other requirement.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reliability vs Availability
&lt;/h2&gt;

&lt;p&gt;One idea that stood out immediately is that a system can be available without being reliable.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A payment service might always respond&lt;/li&gt;
&lt;li&gt;But accidentally charge a customer twice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Technically, the system is available.&lt;/p&gt;

&lt;p&gt;But it is not reliable.&lt;/p&gt;

&lt;p&gt;This changed how I think about backend systems.&lt;/p&gt;

&lt;p&gt;Users care less about whether a request returns a response and more about whether the system behaves correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Timeouts, Retries &amp;amp; Circuit Breakers
&lt;/h2&gt;

&lt;p&gt;Most distributed systems communicate over unreliable networks.&lt;/p&gt;

&lt;p&gt;Sometimes a request succeeds, but the response never arrives.&lt;/p&gt;

&lt;p&gt;Sometimes a downstream service becomes slow.&lt;/p&gt;

&lt;p&gt;Sometimes it becomes completely unavailable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timeouts
&lt;/h2&gt;

&lt;p&gt;A timeout prevents requests from waiting forever.&lt;/p&gt;

&lt;p&gt;Instead of hanging indefinitely, the request fails after a predefined period.&lt;/p&gt;

&lt;p&gt;This protects resources and prevents thread exhaustion.&lt;/p&gt;




&lt;h2&gt;
  
  
  Retries
&lt;/h2&gt;

&lt;p&gt;Retries allow temporary failures to recover automatically.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporary network issue&lt;/li&gt;
&lt;li&gt;Short database outage&lt;/li&gt;
&lt;li&gt;Service restart&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, retries can also be dangerous.&lt;/p&gt;

&lt;p&gt;If thousands of clients immediately retry a failing service, they can amplify the outage.&lt;/p&gt;

&lt;p&gt;This is known as a retry storm.&lt;/p&gt;




&lt;h2&gt;
  
  
  Circuit Breakers
&lt;/h2&gt;

&lt;p&gt;Circuit breakers help prevent cascading failures.&lt;/p&gt;

&lt;p&gt;When a downstream service starts failing repeatedly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New requests are stopped early&lt;/li&gt;
&lt;li&gt;The service is given time to recover&lt;/li&gt;
&lt;li&gt;Resources are protected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A circuit breaker acts similarly to an electrical fuse.&lt;/p&gt;

&lt;p&gt;Instead of allowing one failure to spread across the system, it isolates the problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Idempotency: The Most Important Concept This Week
&lt;/h2&gt;

&lt;p&gt;The biggest lesson from Week 4 was idempotency.&lt;/p&gt;

&lt;p&gt;An operation is idempotent when performing it multiple times produces the same result as performing it once.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;Creating a payment &lt;strong&gt;is not naturally idempotent.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If a payment request is processed twice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The customer may be charged twice&lt;/li&gt;
&lt;li&gt;Financial records become inconsistent&lt;/li&gt;
&lt;li&gt;Customer trust is lost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To solve this problem, payment APIs typically require an Idempotency Key.&lt;/p&gt;

&lt;p&gt;The client sends a unique identifier with the request:&lt;/p&gt;

&lt;p&gt;POST /payments&lt;/p&gt;

&lt;p&gt;Idempotency-Key: abc123&lt;/p&gt;

&lt;p&gt;If the same request is retried:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server recognizes the key&lt;/li&gt;
&lt;li&gt;Returns the original result&lt;/li&gt;
&lt;li&gt;Prevents duplicate charges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows clients to safely retry requests when failures occur.&lt;/p&gt;




&lt;h2&gt;
  
  
  Applying the Concepts: Designing a Payment API
&lt;/h2&gt;

&lt;p&gt;To practice these ideas, I designed a simplified payment processing system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create payments&lt;/li&gt;
&lt;li&gt;Retrieve payment status&lt;/li&gt;
&lt;li&gt;Prevent duplicate charges&lt;/li&gt;
&lt;li&gt;Return payment history&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Non-Functional Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High reliability&lt;/li&gt;
&lt;li&gt;Strong consistency&lt;/li&gt;
&lt;li&gt;Low latency&lt;/li&gt;
&lt;li&gt;Fault tolerance&lt;/li&gt;
&lt;li&gt;High availability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unlike previous systems, correctness is more important than raw performance.&lt;/p&gt;




&lt;h2&gt;
  
  
  High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;The system consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stateless API servers&lt;/li&gt;
&lt;li&gt;PostgreSQL for durable storage&lt;/li&gt;
&lt;li&gt;Redis for idempotency lookups&lt;/li&gt;
&lt;li&gt;Load balancer&lt;/li&gt;
&lt;li&gt;Payment processor integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Request flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client submits payment request&lt;/li&gt;
&lt;li&gt;API validates the idempotency key&lt;/li&gt;
&lt;li&gt;Payment is stored in the database&lt;/li&gt;
&lt;li&gt;The external payment provider is called&lt;/li&gt;
&lt;li&gt;The result is returned to the client&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;Failure Scenarios&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the most useful exercises this week was identifying failure modes.&lt;/p&gt;

&lt;p&gt;This exercise reinforced an important lesson:&lt;/p&gt;

&lt;p&gt;Designing for failure is often more important than designing for success.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Changed in My Thinking
&lt;/h2&gt;

&lt;p&gt;Before this week, I often thought about performance first.&lt;/p&gt;

&lt;p&gt;Now I find myself asking different questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens if this request runs twice?&lt;/li&gt;
&lt;li&gt;What happens if the downstream service fails?&lt;/li&gt;
&lt;li&gt;What happens if the response never arrives?&lt;/li&gt;
&lt;li&gt;What happens if retries overload the system?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions feel much closer to how real production systems are designed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reflections
&lt;/h2&gt;

&lt;p&gt;Week 4 was less about scaling and more about correctness.&lt;/p&gt;

&lt;p&gt;The most valuable takeaway was realizing that distributed systems spend a surprising amount of time handling situations where things go wrong.&lt;/p&gt;

&lt;p&gt;Reliability is not achieved by preventing failures.&lt;/p&gt;

&lt;p&gt;It is achieved by expecting failures and designing systems that can recover from them.&lt;/p&gt;




&lt;h2&gt;
  
  
  What’s Next — Week 5
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Replication&lt;/li&gt;
&lt;li&gt;Consistency models&lt;/li&gt;
&lt;li&gt;Read replicas&lt;/li&gt;
&lt;li&gt;Leader-follower architectures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The journey continues 🚀&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>distributedsystems</category>
      <category>sre</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Caching: System Design Journey... Week 3</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Wed, 20 May 2026 15:09:12 +0000</pubDate>
      <link>https://dev.to/majdsufian/system-design-learning-journey-week-3-caching-304b</link>
      <guid>https://dev.to/majdsufian/system-design-learning-journey-week-3-caching-304b</guid>
      <description>&lt;p&gt;In Week 3, I focused on one of the most important concepts in scalable backend systems:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid hitting the database unnecessarily.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After learning how databases store and retrieve data in Week 2, the next logical question became:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when traffic grows?&lt;/li&gt;
&lt;li&gt;What if millions of users request the same data repeatedly?&lt;/li&gt;
&lt;li&gt;How do systems remain fast under heavy read load?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This week was all about caching.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The main goals were to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand why caching exists&lt;/li&gt;
&lt;li&gt;Learn common cache strategies&lt;/li&gt;
&lt;li&gt;Explore cache invalidation trade-offs&lt;/li&gt;
&lt;li&gt;Apply these concepts by designing a high-traffic product catalog system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compared to previous weeks, this felt like the point where system design started becoming much closer to real-world backend engineering.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Why Caching Exists *&lt;/em&gt;&lt;br&gt;
Databases are powerful, but they are also expensive resources.&lt;/p&gt;

&lt;p&gt;Even optimized relational databases eventually become bottlenecks under massive read traffic.&lt;/p&gt;

&lt;p&gt;Imagine a product page on an e-commerce platform:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A product may be viewed millions of times&lt;/li&gt;
&lt;li&gt;But the actual product data changes very rarely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Without caching:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Client → API → Database&lt;/code&gt;&lt;br&gt;
for every request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With caching:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Client → API → Cache → Response&lt;/code&gt;&lt;br&gt;
The database is queried once, and the cache serves the remaining requests.&lt;/p&gt;

&lt;p&gt;This dramatically reduces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Latency&lt;/li&gt;
&lt;li&gt;Database load&lt;/li&gt;
&lt;li&gt;Infrastructure costs&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Key Insight: Caching Improves Performance but Adds Complexity
&lt;/h2&gt;

&lt;p&gt;One of the biggest lessons this week was:&lt;/p&gt;

&lt;p&gt;Cache is easy to add, but difficult to keep correct.&lt;/p&gt;

&lt;p&gt;Caching introduces a new challenge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens when cached data becomes outdated?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the famous problem of cache invalidation.&lt;/p&gt;


&lt;h2&gt;
  
  
  Cache Invalidation: The Hard Problem
&lt;/h2&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;A product is cached with:&lt;/p&gt;

&lt;p&gt;Price = $999&lt;/p&gt;

&lt;p&gt;An admin later updates the database:&lt;/p&gt;

&lt;p&gt;Price = $899&lt;/p&gt;

&lt;p&gt;But the cache still returns:&lt;/p&gt;

&lt;p&gt;$999&lt;/p&gt;

&lt;p&gt;This creates stale data.&lt;/p&gt;

&lt;p&gt;The core challenge becomes:&lt;/p&gt;

&lt;p&gt;How do we keep cache and database synchronized without hurting performance?&lt;/p&gt;


&lt;h2&gt;
  
  
  Cache-Aside Pattern
&lt;/h2&gt;

&lt;p&gt;The main strategy I focused on was the cache-aside pattern.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Request arrives
2. Check cache

IF HIT:
    return cached data

IF MISS:
    query database
    store result in cache
    return response

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

&lt;/div&gt;



&lt;p&gt;This approach is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple&lt;/li&gt;
&lt;li&gt;Common in production systems&lt;/li&gt;
&lt;li&gt;Easy to implement with Redis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also reinforced another important principle:&lt;/p&gt;

&lt;p&gt;Cache should be treated as an optimization layer, not the source of truth.&lt;/p&gt;




&lt;h2&gt;
  
  
  Applying the Concepts: High-Traffic Product Catalog
&lt;/h2&gt;

&lt;p&gt;To apply these ideas, I designed a simplified high-traffic product catalog system similar to what platforms like Amazon or Shopify might use.&lt;/p&gt;




&lt;h2&gt;
  
  
  Functional Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;View products&lt;/li&gt;
&lt;li&gt;Search products&lt;/li&gt;
&lt;li&gt;Update product details&lt;/li&gt;
&lt;li&gt;View featured / trending products&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Non-Functional Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Very low latency&lt;/li&gt;
&lt;li&gt;High read throughput&lt;/li&gt;
&lt;li&gt;High availability&lt;/li&gt;
&lt;li&gt;Scalability under heavy traffic&lt;/li&gt;
&lt;li&gt;Fault tolerance&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;Figure 1: Product catalog architecture with Redis caching layer.&lt;/p&gt;

&lt;p&gt;The system consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stateless API servers behind a load balancer&lt;/li&gt;
&lt;li&gt;PostgreSQL as the primary data store&lt;/li&gt;
&lt;li&gt;Redis as the caching layer&lt;/li&gt;
&lt;li&gt;Cache-aside strategy for reads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The majority of requests are served directly from Redis, dramatically reducing pressure on the database.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Should Be Cached?
&lt;/h2&gt;

&lt;p&gt;This week also helped me think more carefully about what should actually be cached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good candidates for caching:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Product details&lt;/li&gt;
&lt;li&gt;Product lists&lt;/li&gt;
&lt;li&gt;Trending products&lt;/li&gt;
&lt;li&gt;Frequently viewed items&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Less ideal candidates:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rapidly changing inventory&lt;/li&gt;
&lt;li&gt;Real-time stock counts&lt;/li&gt;
&lt;li&gt;Frequently updated pricing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This highlighted an important design trade-off:&lt;/p&gt;

&lt;p&gt;The more dynamic the data, the harder caching becomes.&lt;/p&gt;




&lt;h2&gt;
  
  
  TTL (Time To Live)
&lt;/h2&gt;

&lt;p&gt;Another concept I explored was TTL (Time To Live).&lt;/p&gt;

&lt;p&gt;Instead of keeping cached data forever, cached entries automatically expire after a period of time.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Product cache expires after 5 minutes&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simpler invalidation strategy&lt;/li&gt;
&lt;li&gt;Prevents very stale data&lt;/li&gt;
&lt;li&gt;Reduces cache management complexity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trade-off:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slightly outdated data may temporarily exist&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Failure Considerations
&lt;/h2&gt;

&lt;p&gt;The system was designed so that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If Redis fails → the database still serves requests&lt;/li&gt;
&lt;li&gt;If API instances crash → traffic is routed elsewhere&lt;/li&gt;
&lt;li&gt;If traffic spikes → stateless services scale horizontally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This reinforced a recurring system design principle:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Reliability often comes from graceful degradation.&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Even if performance drops, the system should continue functioning correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Implementation Notes
&lt;/h2&gt;

&lt;p&gt;The product catalog system was implemented using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kotlin + Spring Boot&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;Redis&lt;/li&gt;
&lt;li&gt;Cache-aside caching strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The focus this week was less about framework syntax and more about understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;access patterns&lt;/li&gt;
&lt;li&gt;cache trade-offs&lt;/li&gt;
&lt;li&gt;latency optimization&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  This week helped me understand that:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Databases alone are not enough at scale&lt;/li&gt;
&lt;li&gt;Caching is fundamental for read-heavy systems&lt;/li&gt;
&lt;li&gt;Cache invalidation is where complexity begins&lt;/li&gt;
&lt;li&gt;Not all data should be cached equally&lt;/li&gt;
&lt;li&gt;System design is mostly about trade-offs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What’s Next — Week 4
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Replication &amp;amp; consistency&lt;/li&gt;
&lt;li&gt;Primary databases vs read replicas&lt;/li&gt;
&lt;li&gt;Designing systems that survive failures and replication lag
The journey continues 🚀&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>backend</category>
      <category>devjournal</category>
      <category>performance</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Storage &amp; Retrieval: System Design Journey... Week 2</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Thu, 09 Apr 2026 11:07:33 +0000</pubDate>
      <link>https://dev.to/majdsufian/system-design-learning-journey-week-2-storage-retrieval-114o</link>
      <guid>https://dev.to/majdsufian/system-design-learning-journey-week-2-storage-retrieval-114o</guid>
      <description>&lt;p&gt;In &lt;strong&gt;Week 2&lt;/strong&gt;, I shifted focus from high-level scalability concepts to &lt;strong&gt;how data is actually stored, retrieved, and optimized&lt;/strong&gt; in real systems.&lt;/p&gt;

&lt;p&gt;The main goals this week were to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build intuition around &lt;strong&gt;storage engines and access patterns&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Understand &lt;strong&gt;SQL vs NoSQL trade-offs&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Learn where &lt;strong&gt;indexes and caching&lt;/strong&gt; fit into system design&lt;/li&gt;
&lt;li&gt;Apply these ideas by designing and implementing a &lt;strong&gt;User Profile Service&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compared to Week 1, this week went &lt;em&gt;deeper&lt;/em&gt; into backend fundamentals—and required slowing down to truly understand what happens under the hood.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Learnings from &lt;em&gt;Designing Data-Intensive Applications — Chapter 3&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Chapter 3 (Storage &amp;amp; Retrieval) was significantly more challenging than earlier chapters—but also much more rewarding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Storage Engines: Why Data Layout Matters
&lt;/h3&gt;

&lt;p&gt;One of the biggest takeaways was that &lt;strong&gt;databases are not magic&lt;/strong&gt;. They rely on concrete data structures that directly impact performance.&lt;/p&gt;

&lt;p&gt;Two broad approaches stood out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Log-structured storage&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Writes are fast (append-only)&lt;/li&gt;
&lt;li&gt;Reads require compaction and indexes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B-tree–based storage&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Optimized for reads&lt;/li&gt;
&lt;li&gt;Supports range queries efficiently&lt;/li&gt;
&lt;li&gt;Common in relational databases (e.g., PostgreSQL)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This helped me understand why certain databases excel at specific workloads.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The way data is written on disk determines what queries are fast—or painfully slow.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Indexes: Trading Space for Speed
&lt;/h3&gt;

&lt;p&gt;Indexes are one of the most important tools for performance, but they are &lt;strong&gt;not free&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Key insights:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An index is a &lt;strong&gt;separate data structure&lt;/strong&gt; (not just “extra metadata”)&lt;/li&gt;
&lt;li&gt;Indexes speed up reads but:

&lt;ul&gt;
&lt;li&gt;Slow down writes&lt;/li&gt;
&lt;li&gt;Increase storage usage&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Indexes should reflect &lt;strong&gt;real query patterns&lt;/strong&gt;, not hypothetical ones&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This clarified a common misconception I had earlier:&lt;/p&gt;

&lt;p&gt;Indexes don’t automatically exist for every field—you must &lt;strong&gt;design them intentionally&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  SQL vs NoSQL: It’s About Trade-offs, Not Trends
&lt;/h3&gt;

&lt;p&gt;Instead of “SQL vs NoSQL”, the chapter reframed the question as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What guarantees does your system need?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Key comparisons:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;SQL&lt;/th&gt;
&lt;th&gt;NoSQL&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Schema&lt;/td&gt;
&lt;td&gt;Strong, enforced&lt;/td&gt;
&lt;td&gt;Flexible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transactions&lt;/td&gt;
&lt;td&gt;Strong consistency&lt;/td&gt;
&lt;td&gt;Often eventual&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaling&lt;/td&gt;
&lt;td&gt;Harder (sharding)&lt;/td&gt;
&lt;td&gt;Easier horizontally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Querying&lt;/td&gt;
&lt;td&gt;Powerful&lt;/td&gt;
&lt;td&gt;Limited / specialized&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This made it clear why &lt;strong&gt;relational databases are still a strong default choice&lt;/strong&gt;, especially for systems that require correctness and structured data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Applying the Concepts: User Profile Service
&lt;/h2&gt;

&lt;p&gt;To apply these ideas, I designed and implemented a &lt;strong&gt;User Profile Service&lt;/strong&gt;—a common real-world backend component.&lt;/p&gt;




&lt;h2&gt;
  
  
  Functional Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create user profiles&lt;/li&gt;
&lt;li&gt;Retrieve user information&lt;/li&gt;
&lt;li&gt;Update user details&lt;/li&gt;
&lt;li&gt;Delete users&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Non-Functional Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Low latency&lt;/strong&gt; (read-heavy system)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;High availability&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt; (growing user base)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fault tolerance&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Optional analytics&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Scale Assumptions
&lt;/h2&gt;

&lt;p&gt;To guide design decisions, I assumed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Users created per day:&lt;/strong&gt; ~10,000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reads per day:&lt;/strong&gt; ~1,000,000&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read/write ratio:&lt;/strong&gt; ~100:1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This clearly indicates a &lt;strong&gt;read-heavy system&lt;/strong&gt;, making caching and indexing critical.&lt;/p&gt;




&lt;h2&gt;
  
  
  High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; High-level flows for create, read, update, delete, and analytics.&lt;/p&gt;

&lt;p&gt;The system consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stateless API servers behind a load balancer&lt;/li&gt;
&lt;li&gt;PostgreSQL as the primary data store&lt;/li&gt;
&lt;li&gt;Redis for caching user profiles&lt;/li&gt;
&lt;li&gt;Optional analytics endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reads are optimized through caching, while writes remain strongly consistent.&lt;/p&gt;




&lt;h2&gt;
  
  
  Data Modeling &amp;amp; Indexing
&lt;/h2&gt;

&lt;p&gt;The core data model includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;userId&lt;/code&gt; (primary key)&lt;/li&gt;
&lt;li&gt;User profile fields (name, address, metadata)&lt;/li&gt;
&lt;li&gt;Timestamps for creation and updates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where Indexes Fit
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;primary key index&lt;/strong&gt; enables fast lookup by user ID&lt;/li&gt;
&lt;li&gt;Additional indexes are unnecessary unless new access patterns emerge (e.g., search by email)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This aligns with the lesson from DDIA:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Indexes should be driven by real queries—not assumptions.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Caching Strategy
&lt;/h2&gt;

&lt;p&gt;A key design decision was &lt;strong&gt;what to cache&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Chosen Approach: Cache the Entire User Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Cache key: &lt;code&gt;userId&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Cache value: full user profile&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this works well:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User profiles are relatively small&lt;/li&gt;
&lt;li&gt;Read requests usually need the full object&lt;/li&gt;
&lt;li&gt;Simplifies cache logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cache Invalidation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;On update → invalidate cache&lt;/li&gt;
&lt;li&gt;On delete → invalidate cache&lt;/li&gt;
&lt;li&gt;Cache misses fall back to the database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This follows the &lt;strong&gt;cache-aside pattern&lt;/strong&gt;, which is simple and robust.&lt;/p&gt;




&lt;h2&gt;
  
  
  Failure Considerations
&lt;/h2&gt;

&lt;p&gt;The system is designed so that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If Redis fails → database still serves reads (slower, but correct)&lt;/li&gt;
&lt;li&gt;If an API instance crashes → load balancer routes traffic elsewhere&lt;/li&gt;
&lt;li&gt;If traffic spikes → stateless services scale horizontally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This reinforced an important lesson:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cache is an optimization, not a dependency.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Implementation Notes
&lt;/h2&gt;

&lt;p&gt;The service was implemented using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Kotlin + Spring Boot&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Redis&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Stateless REST APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The focus was not on framework features, but on &lt;strong&gt;mapping system design decisions into real code&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What’s Next — Week 3
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Replication &amp;amp; consistency&lt;/li&gt;
&lt;li&gt;Read replicas vs primary databases&lt;/li&gt;
&lt;li&gt;Designing systems that survive partial failures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The journey continues 🚀&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>sql</category>
      <category>database</category>
      <category>scala</category>
    </item>
    <item>
      <title>Scalability &amp; URL Shortener: System Design Journey... Week 1</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Thu, 26 Mar 2026 13:13:48 +0000</pubDate>
      <link>https://dev.to/majdsufian/system-design-learning-journey-week-1-scalability-url-shortener-17h2</link>
      <guid>https://dev.to/majdsufian/system-design-learning-journey-week-1-scalability-url-shortener-17h2</guid>
      <description>&lt;p&gt;Over the next 12 weeks, I’m focusing on one goal:&lt;/p&gt;

&lt;p&gt;Becoming more confident in system design by going beyond theory and applying it in practice.&lt;/p&gt;

&lt;p&gt;I’ll be documenting everything I learn — the concepts, the mistakes, and the design decisions.&lt;/p&gt;

&lt;p&gt;Each week focuses on a different topic, with a real system to apply it.&lt;/p&gt;

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

&lt;h1&gt;
  
  
  Week 1 — Scalability Basics &amp;amp; Designing a URL Shortener
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In Week 1 of my system design learning plan, I focused on &lt;strong&gt;foundational scalability concepts&lt;/strong&gt; and applied them by designing and implementing a &lt;strong&gt;URL shortener system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The goal was not just to design a working system, but to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build intuition for &lt;strong&gt;data-intensive systems&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Practice thinking in &lt;strong&gt;trade-offs&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Connect&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Learnings from &lt;em&gt;Designing Data-Intensive Applications — Chapter 1&lt;/em&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Data-Intensive vs Compute-Intensive Systems
&lt;/h3&gt;

&lt;p&gt;Most modern applications are &lt;strong&gt;data-intensive&lt;/strong&gt;, meaning the primary challenges are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Volume of data&lt;/li&gt;
&lt;li&gt;Speed of change&lt;/li&gt;
&lt;li&gt;Access patterns (reads vs writes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CPU is rarely the bottleneck.&lt;/p&gt;

&lt;p&gt;This shifted my thinking from &lt;em&gt;“what framework should I use?”&lt;/em&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What data do I store?&lt;/li&gt;
&lt;li&gt;How often is it accessed?&lt;/li&gt;
&lt;li&gt;What guarantees does it need?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This mindset directly influenced how I designed the URL shortener.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reliability, Scalability, and Maintainability
&lt;/h3&gt;

&lt;p&gt;Chapter 1 emphasizes that &lt;strong&gt;failures are inevitable&lt;/strong&gt;, but systems should be designed so faults don’t become failures.&lt;/p&gt;

&lt;p&gt;Key takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fault tolerance matters even for “simple” systems&lt;/li&gt;
&lt;li&gt;Scalability is about &lt;strong&gt;how systems behave as load increases&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Maintainability is critical because most software costs are long-term&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These principles guided decisions like stateless services, caching, and asynchronous processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load Balancing Fundamentals
&lt;/h2&gt;

&lt;p&gt;A load balancer solves two core problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Availability&lt;/strong&gt; — no single server failure brings the system down&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt; — traffic can be distributed across multiple instances&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Important insights:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load balancers require &lt;strong&gt;stateless application servers&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Distribution strategies (round-robin, least connections) affect latency&lt;/li&gt;
&lt;li&gt;Health checks are essential to avoid routing traffic to failed instances&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  URL Shortener — System Design
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Functional Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Accept long URLs&lt;/li&gt;
&lt;li&gt;Generate unique short keys&lt;/li&gt;
&lt;li&gt;Redirect users to the original URL&lt;/li&gt;
&lt;li&gt;Store mappings reliably&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-Functional Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Low latency (read-heavy workload)&lt;/li&gt;
&lt;li&gt;High availability&lt;/li&gt;
&lt;li&gt;Scalability&lt;/li&gt;
&lt;li&gt;Fault tolerance&lt;/li&gt;
&lt;li&gt;Optional analytics&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  High-Level Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; High-level request flow for URL creation, redirection, and analytics.&lt;/p&gt;

&lt;p&gt;The system consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stateless API servers behind a load balancer&lt;/li&gt;
&lt;li&gt;PostgreSQL for persistent storage&lt;/li&gt;
&lt;li&gt;Redis for low-latency reads&lt;/li&gt;
&lt;li&gt;Asynchronous analytics processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Redirects are optimized for speed, while analytics are handled in the background.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design Improvement: Asynchronous Analytics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initial Approach (Synchronous)
&lt;/h3&gt;

&lt;p&gt;Initially, redirect analytics were updated &lt;strong&gt;synchronously&lt;/strong&gt; inside the redirect request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;mapping&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findByShortUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shortUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;redirectCount&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;RedirectToOriginalResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;originalUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;p&gt;The user waits for a database write that provides no immediate value.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UserRequest
→DBRead(10ms)
→DBWrite(10ms)
→Response

Total ≈20ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Improved Approach (Asynchronous)
&lt;/h3&gt;

&lt;p&gt;After revisiting DDIA’s discussion on &lt;strong&gt;latency and performance&lt;/strong&gt;, I moved analytics updates to run asynchronously.&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redirect happens immediately&lt;/li&gt;
&lt;li&gt;Analytics are processed in the background&lt;/li&gt;
&lt;li&gt;Redirects remain fast even if analytics fail&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Request
→ DBRead (10ms)
→ Async event / queue
→ Response

User latency ≈10ms

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why This Matters
&lt;/h3&gt;

&lt;p&gt;This change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduced redirect latency by ~50%&lt;/li&gt;
&lt;li&gt;Improved resilience&lt;/li&gt;
&lt;li&gt;Accepted &lt;strong&gt;eventual consistency&lt;/strong&gt; where strict accuracy isn’t required&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;A key system design principle reinforced here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not all data needs the same consistency guarantees.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;The system was implemented using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Kotlin + Spring Boot&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PostgreSQL&lt;/strong&gt; for persistence&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redis&lt;/strong&gt; for caching&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Asynchronous analytics processing&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📌 Source code:&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/Majd-sufian/12-week-System-Design-Learning-Plan/tree/main/url-shorten-project" rel="noopener noreferrer"&gt;https://github.com/Majd-sufian/12-week-System-Design-Learning-Plan/tree/main/url-shorten-project&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reflections
&lt;/h2&gt;

&lt;p&gt;This week helped me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shift from component-level thinking to &lt;strong&gt;system behavior thinking&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Make &lt;strong&gt;explicit design trade-offs&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Connect theory (DDIA) to real implementation decisions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Designing even a “simple” system like a URL shortener revealed how quickly concerns like latency, fault tolerance, and scalability appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  What’s Next — Week 2
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Deeper dive into databases&lt;/li&gt;
&lt;li&gt;Data modeling &amp;amp; indexing&lt;/li&gt;
&lt;li&gt;Designing read-heavy systems under higher load&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>systemdesign</category>
      <category>database</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Easiest Way to Get OpenAI Keys for Free</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Fri, 30 Aug 2024 12:31:00 +0000</pubDate>
      <link>https://dev.to/majdsufian/the-easiest-way-to-get-openai-keys-for-free-4pdc</link>
      <guid>https://dev.to/majdsufian/the-easiest-way-to-get-openai-keys-for-free-4pdc</guid>
      <description>&lt;p&gt;Ever wondered how some people get their hands on OpenAI API keys without breaking a sweat? Well, a quick look at GitHub might give you the answer! 😅&lt;/p&gt;

&lt;p&gt;Check out this image—someone accidentally pushed their OpenAI API key right into their GitHub repo. It’s a common mistake, but it means anyone who finds it can grab that key and use it for free. Yep, just like that.&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%2Fzkj73eq2kfu1gaw8apgs.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%2Fzkj73eq2kfu1gaw8apgs.png" alt=" " width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But seriously, don’t do this! To avoid leaking your own keys:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use environment variables&lt;/strong&gt; instead of hardcoding them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add &lt;code&gt;.env&lt;/code&gt; files to your &lt;code&gt;.gitignore&lt;/code&gt;&lt;/strong&gt; so they don’t get uploaded.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scan your repos&lt;/strong&gt; for secrets with automated tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rotate your keys&lt;/strong&gt; if they ever get exposed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay safe out there, and keep those keys hidden! 🔒&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/TJawtKM6OCKkvwCIqX/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img width="380" src="https://i.giphy.com/media/TJawtKM6OCKkvwCIqX/giphy.gif" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>api</category>
      <category>openai</category>
    </item>
    <item>
      <title>Explore My Portfolio and Share Your Feedback! 🕺</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Tue, 27 Aug 2024 21:24:22 +0000</pubDate>
      <link>https://dev.to/majdsufian/explore-my-portfolio-and-share-your-feedback-mp5</link>
      <guid>https://dev.to/majdsufian/explore-my-portfolio-and-share-your-feedback-mp5</guid>
      <description>&lt;p&gt;I'm excited to share my new portfolio, a dynamic and interactive space where you can explore my latest projects, skills, and experiences. I would love for you to visit my portfolio, try it out, and share your feedback with me. Your input is invaluable as I continue to refine and improve my work!&lt;/p&gt;

&lt;h4&gt;
  
  
  Here’s a peek at the tools and frameworks that power my site:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Next.js&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Three.js &amp;amp; @react-three/fiber&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Framer Motion&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tailwind CSS &amp;amp; Tailwind Merge&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sentry&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check out the code behind this portfolio on &lt;a href="https://github.com/Majd-sufian/Portfolio-v2" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; and explore it live at &lt;a href="https://www.majd-sufyan.site/" rel="noopener noreferrer"&gt;https://www.majd-sufyan.site/&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's Connect!
&lt;/h4&gt;

&lt;p&gt;If you like what you see, I’d love to connect with you on &lt;a href="https://www.linkedin.com/in/majd-sufyan/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;. Following me there is the best way to stay updated on my latest projects and connect over shared interests.&lt;/p&gt;

&lt;p&gt;Your feedback is crucial to me, so please don’t hesitate to reach out after exploring my portfolio. I’m looking forward to hearing your thoughts!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeHdzYTY3MGN5NjNkem9pMzh3OGtqYzlka25rNGhhczllN3NvYWp2MSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/7WvAUvZZTRpSuudobh/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img width="" src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeHdzYTY3MGN5NjNkem9pMzh3OGtqYzlka25rNGhhczllN3NvYWp2MSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/7WvAUvZZTRpSuudobh/giphy.gif" height=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>portfolio</category>
    </item>
    <item>
      <title>Good vs. great Programmer 💪</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Tue, 27 Aug 2024 21:03:58 +0000</pubDate>
      <link>https://dev.to/majdsufian/good-vs-great-programmer-4dk2</link>
      <guid>https://dev.to/majdsufian/good-vs-great-programmer-4dk2</guid>
      <description>&lt;h3&gt;
  
  
  How to Answer Technical Questions Like a Pro
&lt;/h3&gt;

&lt;p&gt;Answering technical interview questions is all about showing off your problem-solving skills and understanding! A great response not only highlights what you know but also how you think. In this article, we’ll explore fun ways to improve your answers to common web and software development questions, turning good responses into fantastic ones. &lt;u&gt;&lt;strong&gt;&lt;em&gt;Stick around for some friendly tips to help you shine in your technical interviews!&lt;/em&gt;&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMHRueTd0OHpuY3FocDhibnNjODl3YzY0dzNoaW8zbGxuMW5vZW9wZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/7WvAUvZZTRpSuudobh/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img width="" src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMHRueTd0OHpuY3FocDhibnNjODl3YzY0dzNoaW8zbGxuMW5vZW9wZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/7WvAUvZZTRpSuudobh/giphy.gif" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Handling a Slow Database Query
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt; "What would you do if a database query is running much slower than expected in a production environment?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good Developer:&lt;/strong&gt;&lt;br&gt;
"I would start by checking the database indexes to ensure they are set up correctly. If needed, I would add indexes on the columns used in the query’s WHERE clause. I would also look into optimizing the query by reducing the number of joins or breaking it down into smaller queries."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Great Developer:&lt;/strong&gt;&lt;br&gt;
"First, I’d analyze the slow query using the database's query analyzer or execution plan to pinpoint where the bottleneck is. Common issues might include missing indexes, inefficient joins, or excessive data retrieval. After identifying the problem, I would optimize the query by adding or adjusting indexes, refactoring the query for better performance, and possibly denormalizing some data if appropriate. I would also consider caching frequent queries, especially if the underlying data doesn’t change often. Monitoring tools should be implemented to continuously assess query performance. If performance issues persist, I might evaluate the database structure and consider more scalable solutions like partitioning, sharding, or even migrating to a NoSQL database for specific use cases."&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Debugging a Memory Leak in a Web Application
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt; "How would you approach diagnosing and fixing a memory leak in a web application?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good Developer:&lt;/strong&gt;&lt;br&gt;
"I would use a memory profiler to track down the memory leak. Once identified, I’d look for places in the code where objects are not being released properly, like event listeners or large data structures, and make sure they are cleaned up."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Great Developer:&lt;/strong&gt;&lt;br&gt;
"I’d begin by identifying the symptoms of the memory leak, such as increased memory usage over time or performance degradation. Using a memory profiler, I would track the application's memory usage to locate the exact spot where the leak occurs. Common culprits might include lingering event listeners, unoptimized DOM manipulation, or large data objects that aren't being properly garbage collected. After finding the issue, I’d refactor the code to ensure proper cleanup, such as removing event listeners when they are no longer needed, avoiding global variables, and optimizing data structures. Post-fix, I’d implement automated memory monitoring to catch any future leaks early. Additionally, I’d review the entire codebase for similar patterns that could cause memory issues."&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Optimizing Front-End Performance
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Interviewer:&lt;/strong&gt; "What steps would you take to improve the performance of a web application that is loading slowly on the client side?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good Developer:&lt;/strong&gt;&lt;br&gt;
"I would start by minimizing the size of CSS and JavaScript files through minification and concatenation. I’d also look into lazy loading images and using a content delivery network (CDN) to serve static assets."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Great Developer:&lt;/strong&gt;&lt;br&gt;
"First, I’d conduct a performance audit using tools like Google Lighthouse or WebPageTest to identify the main bottlenecks. Optimizations would include reducing file sizes through minification, concatenation, and compression (e.g., Gzip or Brotli), as well as ensuring efficient use of CSS and JavaScript by eliminating unused code and deferring non-critical scripts. Implementing lazy loading for images and other resources, using a CDN for faster asset delivery, and leveraging browser caching would also be key strategies. Beyond these, I’d consider optimizing rendering performance by reducing reflows and repaints, utilizing asynchronous JavaScript loading, and considering service workers for caching assets in progressive web apps (PWAs). Finally, I’d set up ongoing monitoring of performance metrics to ensure the site continues to meet performance goals."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExYnFkZ3IwMzJubDRmZGFqbzY5ZTEwOHUzaGE1NDl1cGN3MzRucWw2bSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/qaGk4IG7Thf4kQeSZZ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img width="480" src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExYnFkZ3IwMzJubDRmZGFqbzY5ZTEwOHUzaGE1NDl1cGN3MzRucWw2bSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/qaGk4IG7Thf4kQeSZZ/giphy.gif" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Tips for Answering Technical Questions in Interviews
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understand the Problem First:&lt;/strong&gt; Before jumping to a solution, ask a few questions to clarify the problem. This helps you give a more focused answer!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consider Multiple Angles:&lt;/strong&gt; Consider the problem from different angles—what quick fixes can help, what long-term solutions are possible, and what trade-offs should you keep in mind? This shows a deeper understanding!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Explain Your Thought Process:&lt;/strong&gt; Rather than just giving a solution, walk through your reasoning. Interviewers are often more interested in how you think than in whether you know the exact answer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Discuss Both Quick Wins and Long-Term Solutions:&lt;/strong&gt; A great answer blends quick fixes with long-term improvements, showing you care about both immediate needs and the future!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mention Monitoring and Maintenance:&lt;/strong&gt; Emphasizing ongoing monitoring and maintenance shows you know that fixing a problem isn’t just about going live!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Be Honest About Trade-Offs:&lt;/strong&gt; Acknowledge that every solution has its trade-offs. Discussing these shows that you are aware of the complexities involved in technical decisions.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these tips, you can turn a good answer into a great one, positioning yourself as a thoughtful, knowledgeable, and thorough developer in technical interviews.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExbHJnNGk0MHA4MWs5ZXhpY2RwbGdrZTFqd2JzdG9zenoycjZobjhhMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/5eG28RENYdnMrrHuwq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img width="500" src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExbHJnNGk0MHA4MWs5ZXhpY2RwbGdrZTFqd2JzdG9zenoycjZobjhhMSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/5eG28RENYdnMrrHuwq/giphy.gif" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>web</category>
      <category>development</category>
      <category>react</category>
    </item>
    <item>
      <title>Creating Custom Hooks in React and TypeScript 👨‍💻</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Wed, 04 Jan 2023 22:16:10 +0000</pubDate>
      <link>https://dev.to/majdsufian/creating-custom-hooks-in-react-and-typescript-7ho</link>
      <guid>https://dev.to/majdsufian/creating-custom-hooks-in-react-and-typescript-7ho</guid>
      <description>&lt;p&gt;React is a popular JavaScript library for building user interfaces, and TypeScript is a popular superset of JavaScript that adds optional static typing and other features. Together, React and TypeScript can provide a powerful toolset for building scalable and maintainable web applications.&lt;/p&gt;

&lt;p&gt;One of the key features of React is its ability to reuse code through the use of custom hooks. Custom hooks are functions that allow you to extract component logic into reusable functions. They are a powerful way to share logic across your application, and can help to reduce the complexity of your code.&lt;/p&gt;

&lt;p&gt;To create a custom hook in React and TypeScript, you will need to follow a few steps.&lt;/p&gt;

&lt;p&gt;First, create a new file in your project and name it useMyHook.ts. This file will contain the code for your custom hook.&lt;/p&gt;

&lt;p&gt;Next, define the function signature for your custom hook. A custom hook should always begin with the word "use" and should follow the rules for naming functions in TypeScript. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useMyHook&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the custom hook is named &lt;code&gt;useMyHook&lt;/code&gt; and it returns a tuple containing the current value of &lt;code&gt;count&lt;/code&gt; and a function for updating &lt;code&gt;count&lt;/code&gt;. The hook also uses the &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; hooks from React, which are used to manage state and side effects in a functional component.&lt;/p&gt;

&lt;p&gt;To use your custom hook in a React component, import it into the component file and use it like any other hook. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMyHook&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./useMyHook&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMyHook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component uses the &lt;code&gt;useMyHook&lt;/code&gt; custom hook to manage the count state and render a button that increments the count when clicked.&lt;/p&gt;




&lt;p&gt;Custom hooks are a powerful way to share logic across your React components and can help to reduce the complexity of your code. By following the steps outlined above, you can easily create custom hooks in React and TypeScript and take advantage of the benefits they offer.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React Cheatsheet for 2023 ✍️</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Sat, 31 Dec 2022 13:12:27 +0000</pubDate>
      <link>https://dev.to/majdsufian/react-cheatsheet-for-2023-14bd</link>
      <guid>https://dev.to/majdsufian/react-cheatsheet-for-2023-14bd</guid>
      <description>&lt;p&gt;Welcome to my React cheat sheet! In this article, I'll cover all the common concepts and techniques we use every day when developing in React. I'll be covering topics such as React elements, element attributes, element styles, fragments, components, props, children props, conditionals, lists, context, and hooks.&lt;/p&gt;

&lt;p&gt;Whether you're an experienced developer or new to React, this cheat sheet aims to provide you with a quick reference for all the key concepts and techniques you need to know to build powerful and scalable applications with React.&lt;/p&gt;

&lt;p&gt;So let's get started and dive into my React cheatsheet!&lt;/p&gt;

&lt;h2&gt;
  
  
  React Element Attributes
&lt;/h2&gt;

&lt;p&gt;JSX requires a different syntax for its attributes.&lt;/p&gt;

&lt;p&gt;Since JSX is really JavaScript and JavaScript uses a camelcase naming convention (that is, “camelCase”), attributes are written differently than HTML.&lt;/p&gt;

&lt;p&gt;The most common example is the &lt;code&gt;class&lt;/code&gt; attribute, which we write as &lt;code&gt;className&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Element Styles
&lt;/h2&gt;

&lt;p&gt;In addition to attributes, elements can also have inline styles applied to them. To set inline styles in React, you'll need to use the &lt;code&gt;style&lt;/code&gt; attribute and pass in an object containing the desired styles. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;16px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Fragments
&lt;/h2&gt;

&lt;p&gt;Sometimes, you may want to render a group of elements without adding an extra DOM node around them. In these cases, you can use React fragments, which allow you to wrap multiple elements in a single container without adding any extra markup. To use a fragment, you can use the &amp;lt;&amp;gt; syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;First&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Second&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Components
&lt;/h2&gt;

&lt;p&gt;Components are the building blocks of a React application and allow you to split your UI into reusable pieces. You can create a component by defining a JavaScript function or class that returns a React element. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Props
&lt;/h2&gt;

&lt;p&gt;Props (short for "properties") are a way for components to accept data from their parent components. When a parent component renders a child component, it can pass data to the child component through props. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MyButton&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Click me&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Children Props
&lt;/h2&gt;

&lt;p&gt;Sometimes, you may want to render a component's children inside the component itself. To do this, you can use the &lt;code&gt;props.children property&lt;/code&gt;, which represents the content between the opening and closing tags of a component. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyContainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MyContainer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Content&lt;/span&gt; &lt;span class="nx"&gt;goes&lt;/span&gt; &lt;span class="nx"&gt;here&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/MyContainer&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Conditionals
&lt;/h2&gt;

&lt;p&gt;React allows you to use JavaScript expressions to conditionally render elements based on certain conditions. For example, you can use the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator to only render an element if a certain condition is true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="nx"&gt;back&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Lists
&lt;/h2&gt;

&lt;p&gt;React provides a map function that allows you to render a list of elements based on an array of data. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item 1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item 2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item 3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;))}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React Context
&lt;/h2&gt;

&lt;p&gt;React Context is a way to pass data through the component tree without having to pass props down manually at every level. It is often used for global data that needs to be accessible by multiple components, such as a user's theme preference or a language setting.&lt;/p&gt;

&lt;p&gt;To create a context, you can use the &lt;code&gt;createContext&lt;/code&gt; function and provide a default value for the context. Then, you can use the &lt;code&gt;Provider&lt;/code&gt; component to provide the context value to its children, and the &lt;code&gt;useContext&lt;/code&gt; hook to consume the context value from a component.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Hooks
&lt;/h2&gt;

&lt;p&gt;React Hooks are a new addition to React that allow you to use state and other features in functional components, rather than just class-based components. There are several built-in hooks that provide common functionality, such as managing state or performing side effects.&lt;/p&gt;

&lt;p&gt;I am going to cover the 6 essential hooks you absolutely need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;li&gt;useRef&lt;/li&gt;
&lt;li&gt;useContext&lt;/li&gt;
&lt;li&gt;useCallback&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React &lt;code&gt;useState&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook allows you to add state to a functional component. It returns an array with two elements: the current state value and a function to update it. You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increment&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React &lt;code&gt;useEffect&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook allows you to perform side effects in a functional component, such as making an API call or setting up a subscription. It takes a function as an argument that will be called after the component renders. You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Perform side effect here&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React &lt;code&gt;useRef&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useRef&lt;/code&gt; hook allows you to create a mutable reference to a DOM element or value. It returns a mutable &lt;code&gt;ref&lt;/code&gt; object whose &lt;code&gt;current&lt;/code&gt; property is initialized to the provided argument (or &lt;code&gt;null&lt;/code&gt; if no argument is given). You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React &lt;code&gt;useContext&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useContext&lt;/code&gt; hook allows you to consume a context value from a functional component. It takes the context object as an argument and returns the current context value. You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React &lt;code&gt;useCallback&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useCallback&lt;/code&gt; hook returns a memoized callback function that only changes if one of the dependencies has changed. It can be used to optimize the performance of components that rely on expensive functions or callbacks. You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// Expensive function or callback&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependency1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependency2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React &lt;code&gt;useMemo&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useMemo&lt;/code&gt; hook returns a memoized value that only changes if one of the dependencies has changed. It can be used to optimize the performance of expensive calculations by only re-computing the value when necessary. You can use the hook like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Expensive calculation&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependency1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependency2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we provided a cheat sheet for some of the most common techniques and concepts used in React development, including element attributes, styles, fragments, components, props, conditionals, lists, context, and hooks. By familiarizing yourself with these concepts, you'll be well-equipped to build powerful and scalable applications with React.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>You should know about the filter(Boolean) trick 💡</title>
      <dc:creator>Majd-sufyan</dc:creator>
      <pubDate>Sat, 31 Dec 2022 12:23:09 +0000</pubDate>
      <link>https://dev.to/majdsufian/you-should-know-aobut-the-filterboolean-trick-4g0</link>
      <guid>https://dev.to/majdsufian/you-should-know-aobut-the-filterboolean-trick-4g0</guid>
      <description>&lt;p&gt;The &lt;code&gt;Array.prototype.filter()&lt;/code&gt; method in JavaScript allows you to create a new array with only the elements that pass a certain test. The &lt;code&gt;filter()&lt;/code&gt; method takes a callback function as an argument, which should return a Boolean value indicating whether or not the element should be included in the new array.&lt;/p&gt;

&lt;p&gt;One use case for the &lt;code&gt;filter()&lt;/code&gt; method is to remove falsy values from an array. You can do this by using the Boolean function as the callback for the &lt;code&gt;filter()&lt;/code&gt; method. The &lt;code&gt;Boolean&lt;/code&gt; function returns &lt;code&gt;true&lt;/code&gt; for values that are "truthy" and &lt;code&gt;false&lt;/code&gt; for values that are "falsy". In JavaScript, the following values are considered falsy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; (zero)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;''&lt;/code&gt; (empty string)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NaN&lt;/code&gt; (Not a Number)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of how to use the &lt;code&gt;filter(Boolean)&lt;/code&gt; trick to remove falsy values from an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;truthyValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;truthyValues&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [1, 'hello', true]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;filter()&lt;/code&gt; method is called on the values array and passed the &lt;code&gt;Boolean&lt;/code&gt; function as the callback. The &lt;code&gt;filter()&lt;/code&gt; method will then iterate through each element in the &lt;code&gt;values&lt;/code&gt; array and include any elements for which the &lt;code&gt;Boolean&lt;/code&gt; function returns &lt;code&gt;true&lt;/code&gt; in the new &lt;code&gt;truthyValues&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;Here is another example of using the &lt;code&gt;filter(Boolean)&lt;/code&gt; trick to remove falsy values from an array of objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bill&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;validUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;validUsers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/* 
[
  { id: 1, name: 'John', age: 30 },
  { id: 2, name: 'Jane', age: 25 },
  { id: 3, name: 'Bill', age: 35 },
  { id: 4, name: 'Mary', age: 40 }
]
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;filter()&lt;/code&gt; method is used to remove any users who have an empty string for their &lt;code&gt;name&lt;/code&gt; or a &lt;code&gt;null&lt;/code&gt; value for their &lt;code&gt;age&lt;/code&gt;. The &lt;code&gt;filter()&lt;/code&gt; method is passed a callback function that uses the &lt;code&gt;Boolean&lt;/code&gt; function to check if the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; properties of each user are truthy. The &lt;code&gt;filter()&lt;/code&gt; method will then include only the users for which the callback function returns &lt;code&gt;true&lt;/code&gt; in the new &lt;code&gt;validUsers&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;filter(Boolean)&lt;/code&gt; trick is a useful technique for removing falsy values from an array in JavaScript. It can be a powerful way to filter and transform data to meet your specific needs.&lt;/p&gt;




&lt;p&gt;I hope you will find this trcik helpful and will help you in your journey. Feel free to give me suggestions.&lt;/p&gt;

&lt;p&gt;Take care developer 👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
