<?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: Arnav Sharma</title>
    <description>The latest articles on DEV Community by Arnav Sharma (@arnavsharma2711).</description>
    <link>https://dev.to/arnavsharma2711</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2687372%2Fa440aef5-9cc5-4d6c-ab13-3bbbe77f4c6e.jpg</url>
      <title>DEV Community: Arnav Sharma</title>
      <link>https://dev.to/arnavsharma2711</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arnavsharma2711"/>
    <language>en</language>
    <item>
      <title>How End-to-End Encryption Really Works: Understanding the Diffie–Hellman Key Exchange</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sun, 12 Oct 2025 09:06:58 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/how-end-to-end-encryption-really-works-understanding-the-diffie-hellman-key-exchange-1b3f</link>
      <guid>https://dev.to/arnavsharma2711/how-end-to-end-encryption-really-works-understanding-the-diffie-hellman-key-exchange-1b3f</guid>
      <description>&lt;p&gt;Every time I hear a service proudly claim &lt;em&gt;“We’re end-to-end encrypted”&lt;/em&gt;, I can’t help but wonder:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do they actually share their encryption keys?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think about it — when you send a message on WhatsApp or Signal, your phone and your friend’s phone somehow end up using the same secret key to encrypt and decrypt messages. But here’s the catch: that key never travels through a secure channel. It passes through the open internet — routers, servers, ISPs — all of which are visible to anyone who wants to snoop.&lt;/p&gt;

&lt;p&gt;So the obvious question is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can two devices agree on a secret key without ever sending it directly over the network?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer lies in one of the most elegant pieces of cryptography ever invented — the &lt;strong&gt;Diffie–Hellman Key Exchange&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧩 The Core Idea
&lt;/h3&gt;

&lt;p&gt;Let’s imagine two people — &lt;strong&gt;Vipul&lt;/strong&gt; and &lt;strong&gt;Meera&lt;/strong&gt; — who want to talk securely.&lt;/p&gt;

&lt;p&gt;They can communicate over a public channel, which everyone (including a hacker named &lt;strong&gt;Suresh&lt;/strong&gt;) can listen to.&lt;/p&gt;

&lt;p&gt;Their goal is to somehow create a shared secret that &lt;em&gt;only they&lt;/em&gt; know, without ever sending the secret itself.&lt;/p&gt;

&lt;p&gt;Here’s where Diffie–Hellman shines. It lets Vipul and Meera each choose &lt;strong&gt;private secrets&lt;/strong&gt;, share some &lt;strong&gt;public information&lt;/strong&gt;, and still end up with the &lt;strong&gt;same shared key&lt;/strong&gt; — one that even Suresh can’t figure out, despite seeing all their messages.&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚙️ How It Works (Step-by-Step)
&lt;/h3&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%2Fy6qk0nc9gvud0uatuihl.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%2Fy6qk0nc9gvud0uatuihl.png" alt=" " width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s break it down in simple terms.&lt;/p&gt;

&lt;p&gt;=&amp;gt; 1. &lt;strong&gt;Agree on public numbers:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Vipul and Meera start by agreeing on two public numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;large prime number&lt;/strong&gt;  &lt;code&gt;p&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;base&lt;/strong&gt; (also called a generator) &lt;code&gt;g&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two values can be known to everyone — even Suresh can see them.&lt;/p&gt;

&lt;p&gt;=&amp;gt; 2. &lt;strong&gt;Each picks a private secret:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vipul chooses a secret number &lt;code&gt;a&lt;/code&gt; (kept private).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meera chooses a secret number &lt;code&gt;b&lt;/code&gt; (kept private).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;=&amp;gt; 3. &lt;strong&gt;They create their public keys:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vipul computes &lt;code&gt;A = g^a mod p&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meera computes &lt;code&gt;B = g^b mod p&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then they &lt;strong&gt;exchange&lt;/strong&gt; these public keys &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; openly.&lt;/p&gt;

&lt;p&gt;=&amp;gt; 4. &lt;strong&gt;They compute the shared secret:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vipul computes &lt;code&gt;S = B^a mod p&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Meera computes &lt;code&gt;S = A^b mod p&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both get the &lt;strong&gt;same shared secret&lt;/strong&gt;  &lt;code&gt;S&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even though Suresh knows &lt;code&gt;g&lt;/code&gt;, &lt;code&gt;p&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt;, and &lt;code&gt;B&lt;/code&gt;, he can’t easily find &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;b&lt;/code&gt; — because that would mean solving the &lt;strong&gt;discrete logarithm problem&lt;/strong&gt;, which is practically impossible for large numbers.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔒 Why It’s Secure
&lt;/h3&gt;

&lt;p&gt;The security of Diffie–Hellman lies in the fact that it’s easy to compute &lt;code&gt;g^a mod p&lt;/code&gt;, but extremely hard to reverse it — that is, to find &lt;code&gt;a&lt;/code&gt; from &lt;code&gt;g^a mod p&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This makes it possible for Vipul and Meera to exchange some public data and still end up with a secret that no one else can reconstruct.&lt;/p&gt;




&lt;h3&gt;
  
  
  🌍 Real-World Uses
&lt;/h3&gt;

&lt;p&gt;Diffie–Hellman isn’t just theory — it’s used everywhere around us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HTTPS (TLS/SSL):&lt;/strong&gt; When you see the lock icon in your browser, your device has likely used Diffie–Hellman (or a variant) to set up a secure connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Messaging apps:&lt;/strong&gt; Protocols like &lt;strong&gt;Signal’s Double Ratchet Algorithm&lt;/strong&gt; build upon Diffie–Hellman to keep your chats safe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;VPNs:&lt;/strong&gt; Secure tunnels between your device and a server often rely on DH or its modern form, &lt;strong&gt;ECDH&lt;/strong&gt; (Elliptic-Curve Diffie–Hellman).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧠 A Modern Twist: Elliptic-Curve Diffie–Hellman (ECDH)
&lt;/h3&gt;

&lt;p&gt;Traditional Diffie–Hellman uses large prime numbers, which can be computationally heavy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ECDH&lt;/strong&gt; replaces those with points on an &lt;strong&gt;elliptic curve&lt;/strong&gt;, achieving the same level of security with much smaller keys — faster and more efficient.&lt;/p&gt;

&lt;p&gt;That’s why ECDH is now used in &lt;strong&gt;HTTPS, SSH, and secure messaging&lt;/strong&gt; apps like Signal and WhatsApp.&lt;/p&gt;




&lt;h3&gt;
  
  
  🪄 The Takeaway
&lt;/h3&gt;

&lt;p&gt;The beauty of the Diffie–Hellman Key Exchange is that it lets two people — like Vipul and Meera — create a shared secret key &lt;strong&gt;without ever sending it directly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It’s pure mathematical magic that forms the foundation of the encryption we rely on every day.&lt;/p&gt;

&lt;p&gt;So the next time an app says &lt;em&gt;“end-to-end encrypted”&lt;/em&gt;, you’ll know what’s happening behind the scenes:&lt;/p&gt;

&lt;p&gt;a quiet, invisible handshake powered by math — where public numbers meet private secrets to build unbreakable trust. 🔐&lt;/p&gt;

</description>
      <category>programming</category>
      <category>security</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Browser Storage Explained: LocalStorage vs SessionStorage vs IndexedDB vs Cookies</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Mon, 01 Sep 2025 09:19:03 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/browser-storage-explained-localstorage-vs-sessionstorage-vs-indexeddb-vs-cookies-283k</link>
      <guid>https://dev.to/arnavsharma2711/browser-storage-explained-localstorage-vs-sessionstorage-vs-indexeddb-vs-cookies-283k</guid>
      <description>&lt;p&gt;When building modern web applications, storing data in the browser is essential for improving user experience, reducing server load, and enabling offline capabilities. But with multiple storage options available—&lt;strong&gt;LocalStorage, SessionStorage, IndexedDB, and Cookies&lt;/strong&gt;—it’s easy to get confused about which one to use and when.&lt;/p&gt;

&lt;p&gt;Let’s break them down with a clear comparison.&lt;/p&gt;




&lt;h2&gt;
  
  
  🌐 1. LocalStorage
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it is&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
LocalStorage is a simple key–value storage mechanism provided by the browser. Data is stored &lt;strong&gt;persistently&lt;/strong&gt;, meaning it remains even after the browser is closed and reopened.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Capacity: ~5–10 MB (depends on browser).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data persists until explicitly cleared (by code or user).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronous API (may block the main thread if used heavily).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stores &lt;strong&gt;string data only&lt;/strong&gt; (objects must be stringified).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Saving user preferences (theme, layout).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Caching small API responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Storing lightweight app state (e.g., last visited tab).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Best for: Simple, persistent key-value storage.&lt;/p&gt;




&lt;h2&gt;
  
  
  🕒 2. SessionStorage
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it is&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
SessionStorage works similarly to LocalStorage but is &lt;strong&gt;tied to the browser session&lt;/strong&gt;. Data is cleared when the tab is closed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Capacity: ~5 MB.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data is isolated per tab/window (even the same site in a new tab won’t share it).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronous API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Stores &lt;strong&gt;string data only&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Temporary form data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preventing multiple submissions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data needed only while the page is open.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Best for: Short-lived data specific to a single tab.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 3. IndexedDB
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it is&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
IndexedDB is a &lt;strong&gt;low-level, NoSQL database&lt;/strong&gt; built into browsers. It allows storing large amounts of structured data, including files/blobs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Capacity: Hundreds of MBs to GBs (depends on browser &amp;amp; user storage limits).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data persists until cleared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous API (non-blocking).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can store &lt;strong&gt;objects, arrays, blobs, and more&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Supports transactions, indexes, and queries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Offline-first applications (e.g., note-taking apps, todo apps).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Large datasets (caching JSON, images, or videos).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complex querying and searching inside the browser.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Best for: Storing &lt;strong&gt;large or structured data&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🍪 4. Cookies
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What they are&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Cookies are the oldest form of browser storage, designed primarily for &lt;strong&gt;server–client communication&lt;/strong&gt;. They can store small pieces of data and are sent with every HTTP request to the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key features&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Capacity: ~4 KB per cookie.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can be set to expire or persist.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sent to the server with each request (can affect performance).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessible by both client-side JavaScript and the server (if not marked &lt;code&gt;HttpOnly&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can be secured with flags like &lt;code&gt;Secure&lt;/code&gt;, &lt;code&gt;HttpOnly&lt;/code&gt;, &lt;code&gt;SameSite&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Authentication tokens (though JWT/localStorage is now more common).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remembering logged-in sessions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tracking (though privacy regulations limit this now).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Best for: &lt;strong&gt;Server–client communication&lt;/strong&gt; and small, secure data.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚖️ Comparison Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Local Storage&lt;/th&gt;
&lt;th&gt;Session Storage&lt;/th&gt;
&lt;th&gt;IndexedDB&lt;/th&gt;
&lt;th&gt;Cookies&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Storage limit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~5–10 MB&lt;/td&gt;
&lt;td&gt;~5 MB&lt;/td&gt;
&lt;td&gt;Hundreds MBs–GBs&lt;/td&gt;
&lt;td&gt;~4 KB per cookie&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Persistence&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Until cleared&lt;/td&gt;
&lt;td&gt;Until tab closes&lt;/td&gt;
&lt;td&gt;Until cleared&lt;/td&gt;
&lt;td&gt;Configurable expiry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Data type&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Strings only&lt;/td&gt;
&lt;td&gt;Strings only&lt;/td&gt;
&lt;td&gt;Structured (objects, blobs)&lt;/td&gt;
&lt;td&gt;Strings only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Accessibility&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;JS only&lt;/td&gt;
&lt;td&gt;JS only&lt;/td&gt;
&lt;td&gt;JS only&lt;/td&gt;
&lt;td&gt;JS + Server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Sent to server&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best for&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Preferences, cache&lt;/td&gt;
&lt;td&gt;Tab-specific data&lt;/td&gt;
&lt;td&gt;Large/complex offline data&lt;/td&gt;
&lt;td&gt;Auth, sessions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&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%2Fsruw34nt5lchjoqrs576.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%2Fsruw34nt5lchjoqrs576.png" alt=" " width="800" height="800"&gt;&lt;/a&gt;    &lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Browser storage is not “one size fits all.” Each option is designed for different use cases: LocalStorage and SessionStorage are great for small, simple data; IndexedDB is the powerhouse for complex offline apps; and Cookies are still essential for server communication.&lt;/p&gt;

&lt;p&gt;👉 The right choice depends on your app’s &lt;strong&gt;data size, persistence needs, and whether the server requires access&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>database</category>
    </item>
    <item>
      <title>What Happens When You Type a URL in the Browser?</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Wed, 27 Aug 2025 13:03:59 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/what-happens-when-you-type-a-url-in-the-browser-5dno</link>
      <guid>https://dev.to/arnavsharma2711/what-happens-when-you-type-a-url-in-the-browser-5dno</guid>
      <description>&lt;p&gt;Have you ever wondered what &lt;em&gt;really&lt;/em&gt; happens when you type &lt;code&gt;www.google.com&lt;/code&gt; into your browser and hit &lt;strong&gt;Enter&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;It feels instant — but under the hood, your browser, operating system, and the internet work together through a fascinating sequence of steps. Let’s break it down step by step.&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%2Fcm8u0di331zfy8qrzldg.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%2Fcm8u0di331zfy8qrzldg.png" alt=" " width="800" height="788"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. &lt;strong&gt;You Enter the URL&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When you type a URL (Uniform Resource Locator) like &lt;code&gt;https://example.com&lt;/code&gt;, the browser starts interpreting it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protocol&lt;/strong&gt;: &lt;code&gt;https://&lt;/code&gt; tells the browser how to communicate (secure HTTP here).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain Name&lt;/strong&gt;: &lt;code&gt;example.com&lt;/code&gt; needs to be translated into an IP address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path &amp;amp; Query&lt;/strong&gt;: &lt;code&gt;/products?id=123&lt;/code&gt; tells the server which resource you want.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. &lt;strong&gt;Browser Checks Cache&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before making any request, the browser looks for shortcuts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browser cache&lt;/strong&gt; → Has this site been visited before?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operating System cache&lt;/strong&gt; → Stored DNS lookups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Router cache&lt;/strong&gt; → Local network remembers common addresses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ISP cache&lt;/strong&gt; → Your internet provider may store DNS results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the IP address for the domain is already cached, the browser skips DNS lookup. Otherwise…&lt;/p&gt;




&lt;h2&gt;
  
  
  3. &lt;strong&gt;DNS Resolution (Domain → IP)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Your browser needs to know the actual server address (like &lt;code&gt;142.250.182.14&lt;/code&gt; for Google). This is where &lt;strong&gt;DNS (Domain Name System)&lt;/strong&gt; comes in:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ask the &lt;strong&gt;local DNS resolver&lt;/strong&gt; (usually at your ISP).&lt;/li&gt;
&lt;li&gt;If not found, it queries the &lt;strong&gt;Root DNS servers&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Then moves to the &lt;strong&gt;Top-Level Domain (TLD) servers&lt;/strong&gt; (like &lt;code&gt;.com&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Finally, it reaches the &lt;strong&gt;Authoritative DNS server&lt;/strong&gt; that knows the exact IP of &lt;code&gt;example.com&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Result: The domain name is translated into an IP address.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. &lt;strong&gt;Browser Establishes a Connection (TCP Handshake)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have the IP, the browser establishes a connection using &lt;strong&gt;TCP (Transmission Control Protocol)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SYN&lt;/strong&gt;: Browser says hello.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SYN-ACK&lt;/strong&gt;: Server replies hello.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ACK&lt;/strong&gt;: Browser confirms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is called the &lt;strong&gt;3-way handshake&lt;/strong&gt;. It ensures both browser and server are ready to communicate.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. &lt;strong&gt;Secure Connection (TLS Handshake for HTTPS)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Since most sites use HTTPS, another step kicks in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Browser and server agree on encryption protocols.&lt;/li&gt;
&lt;li&gt;They exchange certificates to verify trust (via Certificate Authorities).&lt;/li&gt;
&lt;li&gt;A secure encrypted channel is created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ensures data you send/receive is private.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. &lt;strong&gt;Browser Sends HTTP Request&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Finally, the browser sends a request, 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;GET / HTTP/1.1
Host: example.com
User-Agent: Chrome/120.0
Accept: text/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells the server: “Hey, I want the homepage of &lt;code&gt;example.com&lt;/code&gt;.”&lt;/p&gt;




&lt;h2&gt;
  
  
  7. &lt;strong&gt;Server Processes the Request&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;On the server side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The request reaches the &lt;strong&gt;web server software&lt;/strong&gt; (like Nginx, Apache, Node.js).&lt;/li&gt;
&lt;li&gt;The server may fetch data from &lt;strong&gt;databases&lt;/strong&gt;, run &lt;strong&gt;application logic&lt;/strong&gt;, or return a &lt;strong&gt;static file&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The server responds with an &lt;strong&gt;HTTP response&lt;/strong&gt;, e.g.:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 200 OK
Content-Type: text/html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…followed by the HTML of the page.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. &lt;strong&gt;Browser Receives the Response&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The browser now gets the raw HTML. But it’s just the beginning:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parse HTML&lt;/strong&gt; → Build the &lt;strong&gt;DOM (Document Object Model)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parse CSS&lt;/strong&gt; → Build the &lt;strong&gt;CSSOM (CSS Object Model)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run JavaScript&lt;/strong&gt; → Modify DOM/CSSOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Construct Render Tree&lt;/strong&gt; → Combine DOM + CSSOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layout &amp;amp; Paint&lt;/strong&gt; → Calculate positions and paint pixels on screen.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  9. &lt;strong&gt;Loading Extra Resources&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The HTML often contains references to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSS files&lt;/li&gt;
&lt;li&gt;JavaScript files&lt;/li&gt;
&lt;li&gt;Images, fonts, videos&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these triggers more &lt;strong&gt;HTTP requests&lt;/strong&gt;, which go through similar DNS + TCP + TLS steps (unless cached).&lt;/p&gt;




&lt;h2&gt;
  
  
  10. &lt;strong&gt;You See the Page&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At last, all resources are downloaded, parsed, and rendered. The page becomes interactive as JavaScript finishes execution.&lt;/p&gt;

&lt;p&gt;From typing a URL to seeing a fully loaded website, this entire process usually happens in &lt;strong&gt;milliseconds&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ TL;DR (Quick Recap)
&lt;/h2&gt;

&lt;p&gt;When you type a URL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Browser checks cache&lt;/li&gt;
&lt;li&gt;DNS resolves domain → IP&lt;/li&gt;
&lt;li&gt;TCP handshake&lt;/li&gt;
&lt;li&gt;TLS handshake (HTTPS)&lt;/li&gt;
&lt;li&gt;Browser sends HTTP request&lt;/li&gt;
&lt;li&gt;Server processes request&lt;/li&gt;
&lt;li&gt;Browser receives response&lt;/li&gt;
&lt;li&gt;Browser parses + renders page&lt;/li&gt;
&lt;li&gt;Additional resources load&lt;/li&gt;
&lt;li&gt;Page displayed to you&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🌐 Why Should You Care?
&lt;/h2&gt;

&lt;p&gt;Understanding this flow helps developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debug performance bottlenecks&lt;/li&gt;
&lt;li&gt;Improve website loading speed&lt;/li&gt;
&lt;li&gt;Secure web apps with HTTPS&lt;/li&gt;
&lt;li&gt;Appreciate the hidden complexity behind a “simple” click&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;👉 Next time you hit Enter in your browser, remember: an entire orchestra of systems plays in perfect sync to deliver that page in the blink of an eye.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>A/B Testing 101: A Beginner’s Guide to Smarter Decisions</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Tue, 26 Aug 2025 01:32:58 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/ab-testing-101-a-beginners-guide-to-smarter-decisions-3k5p</link>
      <guid>https://dev.to/arnavsharma2711/ab-testing-101-a-beginners-guide-to-smarter-decisions-3k5p</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;“Without data, you’re just another person with an opinion.”&lt;br&gt;&lt;br&gt;
— W. Edwards Deming&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whether you’re launching a new feature, redesigning your app, or tweaking a call-to-action button, product decisions can feel like a gamble. &lt;strong&gt;What if users don’t like it? What if conversions drop?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;A/B testing&lt;/strong&gt; comes to the rescue. It’s one of the simplest yet most powerful tools for making &lt;strong&gt;data-backed decisions&lt;/strong&gt; instead of relying on guesswork.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is A/B Testing?
&lt;/h2&gt;

&lt;p&gt;At its core, &lt;strong&gt;A/B testing is an experiment.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
You take two (or more) versions of something, show them to different user groups at the same time, and measure which one performs better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Testing the color of a “Sign Up” button.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version A&lt;/strong&gt; → Green button&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version B&lt;/strong&gt; → Blue button&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version C&lt;/strong&gt; → Red button&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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%2F9w7oyrcmcezmvkuist2l.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%2F9w7oyrcmcezmvkuist2l.png" alt=" " width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If Version B consistently drives more signups, you’ve found your winner.&lt;/p&gt;

&lt;p&gt;👉 It’s not about opinions — it’s about evidence.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why A/B Testing Matters
&lt;/h2&gt;

&lt;p&gt;A/B testing is more than button colors. It helps teams:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Make Data-Driven Decisions&lt;/strong&gt; → Replace gut feelings with evidence.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Reduce Risk&lt;/strong&gt; → Test small changes before rolling out big ones.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Optimize Key Metrics&lt;/strong&gt; → Conversions, engagement, revenue — all measurable.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Create a Learning Culture&lt;/strong&gt; → Every test teaches you about your users.&lt;br&gt;&lt;br&gt;
✅ &lt;strong&gt;Scale Confidently&lt;/strong&gt; → With proof that changes work, scaling becomes safer.&lt;/p&gt;

&lt;p&gt;💡 Fun fact: Companies like Google, Amazon, and Netflix run &lt;strong&gt;thousands of A/B tests every year&lt;/strong&gt; to optimize everything from recommendations to pricing.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Does A/B Testing Work?
&lt;/h2&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%2F69amu73um0a72qzm2e0l.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%2F69amu73um0a72qzm2e0l.png" alt=" " width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running an A/B test typically involves &lt;strong&gt;three major steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Success Metrics&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
What are you measuring? Click-through rate? Time on page? Purchases?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Split Your Traffic Randomly&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Half your users see &lt;strong&gt;Version A&lt;/strong&gt;, the other half see &lt;strong&gt;Version B&lt;/strong&gt;. (Some tests include multiple variants — A/B/C, etc.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Analyze Results&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Use statistical methods (like confidence intervals or p-values) to check if the difference is &lt;strong&gt;real&lt;/strong&gt; and not just random.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;👉 Pro tip: Don’t end tests too early — trends can flip as more data comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  Components of a Strong A/B Test
&lt;/h2&gt;

&lt;p&gt;For an effective test, you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hypothesis&lt;/strong&gt; → A clear statement of what you expect.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Example: “Changing the button color from green to blue will increase clicks by 10%.”&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Variants&lt;/strong&gt; → Different versions you’re testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sample Size&lt;/strong&gt; → Enough users to make the test statistically valid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Metrics&lt;/strong&gt; → Clearly defined KPIs (e.g., sign-ups, purchases, bounce rate).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Control vs Experiment&lt;/strong&gt; → One version stays the same (control), the other changes (experiment).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⚠️ &lt;strong&gt;Warning:&lt;/strong&gt; If your sample size is too small, your results will be unreliable — like flipping a coin only twice.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Pitfalls in A/B Testing
&lt;/h2&gt;

&lt;p&gt;Even well-meaning teams can fall into traps:&lt;/p&gt;

&lt;p&gt;❌ &lt;strong&gt;Testing the Wrong Things&lt;/strong&gt; → Not every detail needs testing (don’t waste time on logo size).&lt;br&gt;&lt;br&gt;
❌ &lt;strong&gt;Stopping Tests Too Soon&lt;/strong&gt; → Early results often flip after more data.&lt;br&gt;&lt;br&gt;
❌ &lt;strong&gt;Chasing Vanity Metrics&lt;/strong&gt; → More clicks don’t always mean more conversions.&lt;br&gt;&lt;br&gt;
❌ &lt;strong&gt;Small Sample Sizes&lt;/strong&gt; → Leads to misleading results.&lt;br&gt;&lt;br&gt;
❌ &lt;strong&gt;Ignoring Qualitative Data&lt;/strong&gt; → Numbers tell &lt;em&gt;what&lt;/em&gt; happened, but not &lt;em&gt;why&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for A/B Testing
&lt;/h2&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%2Fuonojtavtt64tghwd12o.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%2Fuonojtavtt64tghwd12o.png" alt=" " width="800" height="841"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want reliable, actionable insights:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start with a Clear Hypothesis&lt;/strong&gt; → Know what you’re testing and why.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Focus on One Variable at a Time&lt;/strong&gt; → Changing too many things makes it impossible to know what worked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run Tests Long Enough&lt;/strong&gt; → Capture weekdays, weekends, and normal usage patterns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Segment Your Audience&lt;/strong&gt; → A change might work for new users but not for returning ones.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always Document Results&lt;/strong&gt; → Even failed experiments teach you something.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Beyond A/B: Advanced Testing
&lt;/h2&gt;

&lt;p&gt;Once you’re comfortable, you can go further:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multivariate Testing (MVT)&lt;/strong&gt; → Test multiple elements at once (e.g., button color + headline).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-Armed Bandit&lt;/strong&gt; → Automatically shifts more traffic to winning variants as results come in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Personalization Experiments&lt;/strong&gt; → Different users see different versions based on behavior, not random splits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real-World Examples
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Booking.com&lt;/strong&gt; → Runs &lt;em&gt;over 1,000 concurrent tests&lt;/em&gt; at any time to tweak pricing, messaging, and UX.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Amazon&lt;/strong&gt; → Tests everything from product recommendations to checkout flows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Netflix&lt;/strong&gt; → A/B tested thumbnails, trailers, and UI layouts to increase watch time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the giants are doing it, there’s a reason: it works.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;A/B testing is not just about “what button color works best.” It’s about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Building a culture of learning.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reducing risk through evidence.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuously improving your product with real user data.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember the mantra:&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Test → Measure → Learn → Repeat&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Do this consistently, and you’ll turn product decisions from guesswork into science.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>design</category>
      <category>learning</category>
      <category>testing</category>
    </item>
    <item>
      <title>Understanding CORS: Why Your API Request Failed</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sat, 23 Aug 2025 16:18:34 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/understanding-cors-why-your-api-request-failed-387o</link>
      <guid>https://dev.to/arnavsharma2711/understanding-cors-why-your-api-request-failed-387o</guid>
      <description>&lt;p&gt;If you’ve built a frontend that talks to an API, chances are you’ve seen this error in your console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' 
has been blocked by CORS policy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, it feels random. Your API is up, your code looks fine… so why does the browser stop you?&lt;/p&gt;

&lt;p&gt;The answer is &lt;strong&gt;CORS (Cross-Origin Resource Sharing)&lt;/strong&gt; — one of those things that sounds complicated, but once you “get it,” debugging becomes way easier.&lt;/p&gt;

&lt;p&gt;Let’s break it down step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is CORS, really?
&lt;/h2&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%2F9py5iek4m9shjv3t44it.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%2F9py5iek4m9shjv3t44it.png" alt=" " width="648" height="444"&gt;&lt;/a&gt;&lt;br&gt;
CORS is a &lt;strong&gt;security feature in browsers&lt;/strong&gt;. It basically says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“A website should not be able to make requests to another site unless the other site explicitly says it’s okay.”&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;ul&gt;
&lt;li&gt;Your frontend runs at &lt;code&gt;http://localhost:3000&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Your backend API is at &lt;code&gt;https://api.example.com&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are &lt;strong&gt;different origins&lt;/strong&gt; (different host/port/protocol), so the browser treats the request as &lt;strong&gt;cross-origin&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And here’s the key: &lt;strong&gt;without CORS, any malicious website could secretly call APIs on your behalf (like your bank), and steal data.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So when your browser blocks that request, it’s not being mean — it’s protecting you.&lt;/p&gt;


&lt;h2&gt;
  
  
  How does CORS actually work?
&lt;/h2&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%2Flrepol67nrc33ylwp2hl.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%2Flrepol67nrc33ylwp2hl.png" alt=" " width="800" height="477"&gt;&lt;/a&gt;&lt;br&gt;
Here’s the flow whenever you hit an API from another origin:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Browser Adds an Origin Header&lt;/strong&gt;
Example:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Origin: http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Server Decides if It’s Allowed&lt;/strong&gt;
It must respond with something like:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Access-Control-Allow-Origin: http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If it matches, the browser lets the request go through. If not → &lt;strong&gt;CORS error&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Preflight Requests (the “double request” thing you see)&lt;/strong&gt;
For some requests (&lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, custom headers, etc.), the browser first sends an &lt;strong&gt;OPTIONS&lt;/strong&gt; request (aka preflight).
The server must reply with allowed methods and headers:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Access-Control-Allow-Methods: GET, POST, PUT  
   Access-Control-Allow-Headers: Content-Type, Authorization  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If anything’s missing, your actual request never gets sent.&lt;/p&gt;


&lt;h2&gt;
  
  
  Why your request failed (common reasons)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No CORS headers at all&lt;/strong&gt;&lt;br&gt;
→ The server just didn’t set them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Wrong origin allowed&lt;/strong&gt;&lt;br&gt;
→ You’re on &lt;code&gt;http://localhost:3000&lt;/code&gt;, but the server only allows &lt;code&gt;https://myapp.com&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preflight not handled&lt;/strong&gt;&lt;br&gt;
→ The server ignores the OPTIONS request, so the browser stops right there.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Credentials issue&lt;/strong&gt;&lt;br&gt;
→ You’re sending cookies/tokens, but the server didn’t set&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Access-Control-Allow-Credentials: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to fix CORS issues
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. Set CORS headers on the server
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;Node.js + Express&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Origin&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="s2"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Methods&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="s2"&gt;GET, POST, PUT, DELETE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Headers&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="s2"&gt;Content-Type, Authorization&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;next&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;(Pro tip: just use the &lt;code&gt;cors&lt;/code&gt; package, it saves you headaches.)&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Use &lt;code&gt;*&lt;/code&gt; in dev (but not in prod)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access-Control-Allow-Origin: *
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great for local testing. Terrible for production. Never expose private APIs this way.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Enable credentials if needed
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Credentials&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="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in frontend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. Use a proxy during development
&lt;/h3&gt;

&lt;p&gt;If you can’t change the backend (e.g., third-party APIs), use a proxy (Vite/CRA/Webpack dev server) so the request &lt;em&gt;looks&lt;/em&gt; like it comes from the same origin.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Whitelist only the domains you trust.&lt;/li&gt;
&lt;li&gt;Always handle OPTIONS requests properly.&lt;/li&gt;
&lt;li&gt;Remember: CORS exists to &lt;strong&gt;protect users&lt;/strong&gt;, not annoy developers (even though it feels that way sometimes).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;CORS errors are annoying, but they’re also a sign your browser is doing its job.&lt;/p&gt;

&lt;p&gt;The trick is to remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;server decides&lt;/strong&gt; who can call it.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;browser enforces&lt;/strong&gt; that rule.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So next time you see the dreaded &lt;em&gt;“blocked by CORS policy”&lt;/em&gt;, don’t panic. Check the headers, check the origin, and you’ll know exactly what’s wrong.&lt;/p&gt;

</description>
      <category>api</category>
      <category>programming</category>
      <category>security</category>
    </item>
    <item>
      <title>How SQL Queries Really Work: From Parsing to Execution</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sat, 23 Aug 2025 10:32:51 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/how-sql-queries-really-work-from-parsing-to-execution-468</link>
      <guid>https://dev.to/arnavsharma2711/how-sql-queries-really-work-from-parsing-to-execution-468</guid>
      <description>&lt;p&gt;&lt;em&gt;Ever wondered what happens after you hit run on a SQL query? Here’s a clear journey from parsing to execution.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you’ve ever written a SQL query and hit &lt;em&gt;run&lt;/em&gt;, you might assume the database just reads it top-to-bottom and executes it line by line. But under the hood, something much more sophisticated happens.&lt;/p&gt;

&lt;p&gt;Every SQL query goes through a &lt;strong&gt;three-step journey&lt;/strong&gt; before you see results:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parse → Optimize → Execute&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding this process not only deepens your knowledge of databases but also helps you write faster queries, debug performance issues, and make the most of indexes and query planners.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Query Parsing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Happens During Parsing?
&lt;/h3&gt;

&lt;p&gt;When you send a SQL query to the database, the first thing it does is &lt;strong&gt;parse&lt;/strong&gt; it. Parsing ensures your query is valid and can be understood by the database engine.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Syntax Check:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The database checks if your SQL follows proper syntax. If you miss a semicolon or misuse a keyword, parsing fails immediately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Validation Against Schema:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The query is checked against the database schema. For example, does the table &lt;code&gt;users&lt;/code&gt; exist? Does the column &lt;code&gt;age&lt;/code&gt; belong to it?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Parse Tree Generation
&lt;/h3&gt;

&lt;p&gt;If everything checks out, the query is transformed into a &lt;strong&gt;parse tree&lt;/strong&gt; — a structured representation of the query. Instead of being just text, it becomes a hierarchical tree that the database can work with.&lt;/p&gt;

&lt;p&gt;Think of this as turning your SQL into a machine-readable structure.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Query Optimization
&lt;/h2&gt;

&lt;p&gt;Once parsed, the query moves to the &lt;strong&gt;optimizer&lt;/strong&gt; — the brain of the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logical Optimization
&lt;/h3&gt;

&lt;p&gt;The optimizer rewrites the query to simplify it without changing its meaning. Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Removing unnecessary filters&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reordering operations for efficiency&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplifying expressions (&lt;code&gt;WHERE age &amp;gt; 18 AND age &amp;gt; 10&lt;/code&gt; → &lt;code&gt;WHERE age &amp;gt; 18&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cost-Based Optimization
&lt;/h3&gt;

&lt;p&gt;Here’s where the database really shines. The optimizer estimates the &lt;strong&gt;cost&lt;/strong&gt; of different ways to execute your query — measuring factors like disk I/O, CPU usage, and memory.&lt;/p&gt;

&lt;p&gt;It then chooses the most efficient strategy, considering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indexes:&lt;/strong&gt; Should it scan sequentially or use an index?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Join Orders:&lt;/strong&gt; Should &lt;code&gt;users&lt;/code&gt; be joined with &lt;code&gt;orders&lt;/code&gt; first, or the other way around?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access Paths:&lt;/strong&gt; Which path to data retrieval is fastest?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execution Plan Creation
&lt;/h3&gt;

&lt;p&gt;The optimizer produces an &lt;strong&gt;execution plan&lt;/strong&gt; — a detailed blueprint of how the query will run.&lt;/p&gt;

&lt;p&gt;Developers can peek into this with commands like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The plan shows whether the database will do an &lt;strong&gt;index scan&lt;/strong&gt;, &lt;strong&gt;sequential scan&lt;/strong&gt;, or some other strategy.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Query Execution
&lt;/h2&gt;

&lt;p&gt;Now the plan is handed off to the &lt;strong&gt;execution engine&lt;/strong&gt;, which actually retrieves the data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution Engine Role
&lt;/h3&gt;

&lt;p&gt;The engine takes the execution plan and performs step-by-step operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Steps in Execution
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Table Scans:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   _Sequential Scan_: Reads the entire table row by row.

-   _Index Scan_: Uses an index to quickly jump to matching rows.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Applying Filters and Projections:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Apply the &lt;code&gt;WHERE&lt;/code&gt; condition (&lt;code&gt;age &amp;gt; 30&lt;/code&gt;) and select only the requested columns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Joins and Aggregations:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
If your query has joins or aggregates (&lt;code&gt;SUM&lt;/code&gt;, &lt;code&gt;COUNT&lt;/code&gt;, &lt;code&gt;GROUP BY&lt;/code&gt;), these are computed at this stage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Returning Results to the Client:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Results are &lt;strong&gt;streamed&lt;/strong&gt; back row by row, not all at once, which allows large result sets to be handled efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  4. Real-World Example
&lt;/h2&gt;

&lt;p&gt;Let’s walk through a simple query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parsing:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The query is checked for valid syntax, confirms that the &lt;code&gt;users&lt;/code&gt; table exists, and that the &lt;code&gt;age&lt;/code&gt; column is valid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Optimization:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The optimizer considers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Sequential scan (read all rows, filter &lt;code&gt;age &amp;gt; 30&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;  Index scan (if an index exists on &lt;code&gt;age&lt;/code&gt;, jump directly to rows &amp;gt; 30)
It picks the cheaper one.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execution:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
The chosen plan runs. Rows with &lt;code&gt;age &amp;gt; 30&lt;/code&gt; are fetched, filtered, and returned to the client.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Perfect — let’s expand &lt;strong&gt;Section 5&lt;/strong&gt; with the &lt;strong&gt;actual execution order of SQL queries&lt;/strong&gt;, which is very different from the way we &lt;em&gt;write&lt;/em&gt; them. Here’s the revised section with a clear step-by-step execution flow:&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Common Misconceptions About SQL Execution
&lt;/h2&gt;

&lt;h3&gt;
  
  
  “SQL just runs top to bottom” → Wrong.
&lt;/h3&gt;

&lt;p&gt;SQL is &lt;strong&gt;declarative&lt;/strong&gt;: you describe &lt;em&gt;what&lt;/em&gt; you want, not &lt;em&gt;how&lt;/em&gt; to get it. The &lt;strong&gt;optimizer&lt;/strong&gt; and &lt;strong&gt;execution engine&lt;/strong&gt; decide the &lt;em&gt;how&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logical Order vs Execution Order
&lt;/h3&gt;

&lt;p&gt;When you write a query, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column_list&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;grouping_column&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="n"&gt;group_condition&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;column_list&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But internally, the database executes it in a &lt;strong&gt;different order&lt;/strong&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  Actual Execution Order
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;FROM&lt;/strong&gt; – Identify the source tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JOIN / ON&lt;/strong&gt; – Combine tables based on join conditions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WHERE&lt;/strong&gt; – Filter rows before grouping.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GROUP BY&lt;/strong&gt; – Group rows into buckets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HAVING&lt;/strong&gt; – Filter groups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; – Choose which columns/expressions to return.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DISTINCT&lt;/strong&gt; – Remove duplicates if requested.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ORDER BY&lt;/strong&gt; – Sort the results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;LIMIT / OFFSET&lt;/strong&gt; – Return only a subset of rows.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&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="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Rows are pulled from &lt;code&gt;users&lt;/code&gt; (&lt;code&gt;FROM&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Filtered to only those with &lt;code&gt;age &amp;gt; 30&lt;/code&gt; (&lt;code&gt;WHERE&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Grouped by &lt;code&gt;name&lt;/code&gt; (&lt;code&gt;GROUP BY&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Groups with fewer than 2 rows are removed (&lt;code&gt;HAVING&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Final columns are selected (&lt;code&gt;SELECT&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Results sorted by &lt;code&gt;name&lt;/code&gt; (&lt;code&gt;ORDER BY&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Top 5 returned (&lt;code&gt;LIMIT&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why sometimes putting a condition in &lt;code&gt;WHERE&lt;/code&gt; vs &lt;code&gt;HAVING&lt;/code&gt; makes a big difference — because &lt;strong&gt;row filters happen before grouping, while group filters happen after grouping&lt;/strong&gt;.&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%2Ftmahj4f1nkje2zjd0cl6.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%2Ftmahj4f1nkje2zjd0cl6.png" alt=" " width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Why This Knowledge Helps Developers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write Efficient Queries:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Knowing how queries are parsed and optimized helps you design queries the optimizer can handle well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debug Performance Issues:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Slow query? Run &lt;code&gt;EXPLAIN&lt;/code&gt; to see if the optimizer picked a bad plan.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understand Indexes and Execution Plans:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Indexes don’t always help. Understanding execution flow helps you decide when to add (or avoid) them.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;SQL queries don’t just &lt;em&gt;run&lt;/em&gt;. They go through a journey:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parse → Optimize → Execute&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next time you run a query, remember that your database is doing a lot of behind-the-scenes work to give you results efficiently.&lt;/p&gt;

&lt;p&gt;👉 Pro tip: Use &lt;code&gt;EXPLAIN&lt;/code&gt; to peek into your database’s brain and learn how your queries really run.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Pub/Sub Architecture: Push vs Pull Messaging (When to Use Each)</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Thu, 21 Aug 2025 06:08:12 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/pull-based-vs-push-based-pubsub-explained-1k7m</link>
      <guid>https://dev.to/arnavsharma2711/pull-based-vs-push-based-pubsub-explained-1k7m</guid>
      <description>&lt;h2&gt;
  
  
  What is Pub/Sub?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Publish/Subscribe (pub/sub)&lt;/strong&gt; is a messaging pattern where &lt;strong&gt;publishers&lt;/strong&gt; send messages without knowing who will consume them, and &lt;strong&gt;subscribers&lt;/strong&gt; receive messages without knowing who produced them.&lt;br&gt;&lt;br&gt;
A &lt;strong&gt;broker&lt;/strong&gt; (like Kafka, RabbitMQ, SQS, Redis) usually sits in the middle to handle message delivery.&lt;/p&gt;

&lt;p&gt;This decoupling allows systems to communicate asynchronously and scale independently.&lt;/p&gt;




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

&lt;p&gt;Pub/Sub solves several common problems in distributed systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Decoupling&lt;/strong&gt; – Publishers don’t need to know subscribers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt; – Multiple subscribers can process messages independently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reliability&lt;/strong&gt; – Messages are queued until consumed (depending on broker).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt; – One message can be delivered to many subscribers (fanout).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Use pub/sub when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You want &lt;strong&gt;event-driven architecture&lt;/strong&gt; (e.g., user signup triggers emails, notifications).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to &lt;strong&gt;scale workers&lt;/strong&gt; dynamically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need &lt;strong&gt;loose coupling&lt;/strong&gt; between microservices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to ensure &lt;strong&gt;asynchronous processing&lt;/strong&gt; (background jobs, pipelines).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t use pub/sub when you need &lt;strong&gt;strict request/response&lt;/strong&gt; (synchronous API calls).&lt;/p&gt;




&lt;h2&gt;
  
  
  How Pub/Sub (Types)
&lt;/h2&gt;

&lt;p&gt;There are two main ways subscribers receive messages:&lt;/p&gt;

&lt;h3&gt;
  
  
  🔹 Push-based Pub/Sub
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;broker pushes messages&lt;/strong&gt; directly to subscribers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Subscribers don’t need to poll.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example: &lt;strong&gt;RabbitMQ&lt;/strong&gt; pushes messages to consumers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Low latency&lt;br&gt;&lt;br&gt;
✅ Easy subscriber logic&lt;br&gt;&lt;br&gt;
❌ Risk of overwhelming slow subscribers&lt;/p&gt;




&lt;h3&gt;
  
  
  🔹 Pull-based Pub/Sub
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Subscribers request (pull)&lt;/strong&gt; messages from the broker when ready.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example: &lt;strong&gt;AWS SQS&lt;/strong&gt; requires consumers to poll for messages.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Subscribers control pace (backpressure handling)&lt;br&gt;&lt;br&gt;
✅ Reliable retries&lt;br&gt;&lt;br&gt;
❌ Slightly higher latency due to polling&lt;/p&gt;




&lt;h2&gt;
  
  
  Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Push-based Pub/Sub&lt;/th&gt;
&lt;th&gt;Pull-based Pub/Sub&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Delivery model&lt;/td&gt;
&lt;td&gt;Broker delivers&lt;/td&gt;
&lt;td&gt;Subscriber requests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Latency&lt;/td&gt;
&lt;td&gt;Lower (instant delivery)&lt;/td&gt;
&lt;td&gt;Higher (polling delay)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backpressure control&lt;/td&gt;
&lt;td&gt;Harder&lt;/td&gt;
&lt;td&gt;Easier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subscriber logic&lt;/td&gt;
&lt;td&gt;Simple (just receive)&lt;/td&gt;
&lt;td&gt;More complex (polling)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example tech&lt;/td&gt;
&lt;td&gt;RabbitMQ, Redis Pub/Sub&lt;/td&gt;
&lt;td&gt;AWS SQS, Kafka&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Which Tech Uses Which One and How?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🐇 RabbitMQ (Push-based)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Consumers register with a queue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RabbitMQ &lt;strong&gt;pushes messages&lt;/strong&gt; to consumers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can configure &lt;strong&gt;prefetch&lt;/strong&gt; to avoid overloading consumers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great for &lt;strong&gt;real-time apps&lt;/strong&gt; (chat, IoT, financial trading).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ☁️ AWS SQS (Pull-based)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Messages are stored in SQS queues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Consumers must &lt;strong&gt;poll SQS&lt;/strong&gt; to fetch them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can use &lt;strong&gt;long polling&lt;/strong&gt; for efficiency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great for &lt;strong&gt;cloud pipelines, serverless apps, microservices decoupling&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real World Example
&lt;/h2&gt;

&lt;p&gt;Imagine an &lt;strong&gt;e-commerce platform&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A customer places an order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Publisher&lt;/strong&gt;: Order service emits an &lt;code&gt;order_created&lt;/code&gt; event.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Push-based&lt;/strong&gt;: A notification service instantly gets the event via RabbitMQ and sends an SMS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pull-based&lt;/strong&gt;: A reporting service pulls events from SQS at its own pace to update sales dashboards.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Both &lt;strong&gt;pull-based&lt;/strong&gt; and &lt;strong&gt;push-based&lt;/strong&gt; pub/sub models have their strengths:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Push-based&lt;/strong&gt; is best when you need &lt;strong&gt;low latency, real-time delivery&lt;/strong&gt; (RabbitMQ, Redis Pub/Sub).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pull-based&lt;/strong&gt; is best when you need &lt;strong&gt;scalability, reliability, and backpressure control&lt;/strong&gt; (AWS SQS, Kafka).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 The right choice depends on your use case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;real-time notifications&lt;/strong&gt;, go with &lt;strong&gt;push&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;background processing or analytics&lt;/strong&gt;, go with &lt;strong&gt;pull&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In modern architectures, teams often combine &lt;strong&gt;both models&lt;/strong&gt;—push for immediate actions, pull for batch or heavy workloads.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
      <category>backend</category>
      <category>pubsub</category>
    </item>
    <item>
      <title>Views in PostgreSQL: A Complete Guide with Examples</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sun, 17 Aug 2025 14:51:18 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/views-in-postgresql-a-complete-guide-with-examples-375j</link>
      <guid>https://dev.to/arnavsharma2711/views-in-postgresql-a-complete-guide-with-examples-375j</guid>
      <description>&lt;p&gt;When working with relational databases like &lt;strong&gt;PostgreSQL&lt;/strong&gt;, you often need to simplify complex queries, enforce data security, or speed up reporting. That’s where &lt;strong&gt;Views&lt;/strong&gt; come in.  &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;view&lt;/strong&gt; is essentially a &lt;strong&gt;virtual table&lt;/strong&gt; defined by a query. Instead of writing the same complex query again and again, you save it as a view and query it like a table.  &lt;/p&gt;

&lt;p&gt;In this blog, we’ll explore the &lt;strong&gt;different types of views in PostgreSQL&lt;/strong&gt;, their use cases, pros and cons, examples, and best practices.  &lt;/p&gt;




&lt;h2&gt;
  
  
  🏷️ Types of Views in PostgreSQL
&lt;/h2&gt;




&lt;h3&gt;
  
  
  1. Simple View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A view built from a &lt;strong&gt;single table&lt;/strong&gt; without joins or aggregations.  &lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hide unnecessary columns
&lt;/li&gt;
&lt;li&gt;Simplify queries
&lt;/li&gt;
&lt;li&gt;Enforce limited access
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="nb"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="nb"&gt;BOOLEAN&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Simple view showing only basic employee info&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;employee_public_info&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employee_public_info&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;Pros&lt;/strong&gt;: Lightweight, updatable, always real-time&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Doesn’t reduce computation cost&lt;/p&gt;


&lt;h3&gt;
  
  
  2. Complex View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A view involving &lt;strong&gt;joins, group by, or aggregates&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simplify reporting queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Centralize business logic&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="nb"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sale_date&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Complex view: total sales per department&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;department_sales&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_sales&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;department_sales&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;Pros&lt;/strong&gt;: Great for dashboards, reduces repeated SQL code&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Usually not updatable, may be slow on large datasets&lt;/p&gt;


&lt;h3&gt;
  
  
  3. Materialized View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A &lt;strong&gt;pre-computed, stored result set&lt;/strong&gt; of a query. Unlike normal views, it &lt;strong&gt;stores data on disk&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Speed up expensive queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Power analytics dashboards&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;monthly_sales&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;DATE_TRUNC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'month'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sale_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;DATE_TRUNC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'month'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sale_date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Refresh when new data arrives&lt;/span&gt;
&lt;span class="n"&gt;REFRESH&lt;/span&gt; &lt;span class="n"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;monthly_sales&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;monthly_sales&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;month&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;Pros&lt;/strong&gt;: Huge performance boost, can be indexed&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Data can get stale, needs manual refresh&lt;/p&gt;


&lt;h3&gt;
  
  
  4. Updatable View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A view where you can &lt;strong&gt;INSERT, UPDATE, DELETE&lt;/strong&gt; rows directly.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Provide restricted updates without exposing full table
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Only active employees&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;active_employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Update via the view&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;active_employees&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;Pros&lt;/strong&gt;: Secure way to allow controlled updates&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Works only with simple queries&lt;/p&gt;


&lt;h3&gt;
  
  
  5. Read-Only View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A view that &lt;strong&gt;cannot be updated&lt;/strong&gt; (complex logic, joins, or aggregates).&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Safely expose analytics reports
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Employees with high sales&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;top_performers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_sales&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;top_performers&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;Pros&lt;/strong&gt;: Safe for reporting&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Can’t modify data through it&lt;/p&gt;


&lt;h3&gt;
  
  
  6. Recursive View
&lt;/h3&gt;

&lt;p&gt;📌 &lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A view built using a &lt;strong&gt;recursive CTE&lt;/strong&gt; to handle hierarchies.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Why use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Org charts, folder structures, category trees
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Hierarchy of employees and managers&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;employee_hierarchy&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;emp_cte&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
    &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;emp_cte&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;emp_cte&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employee_hierarchy&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;Pros&lt;/strong&gt;: Perfect for recursive data&lt;br&gt;&lt;br&gt;
⚖️ &lt;strong&gt;Cons&lt;/strong&gt;: Can get slow for deep hierarchies&lt;/p&gt;


&lt;h2&gt;
  
  
  🛒 Real-World Example: Views in an E-commerce App
&lt;/h2&gt;

&lt;p&gt;Imagine you’re building an &lt;strong&gt;e-commerce platform&lt;/strong&gt;. Here’s how different views fit in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple View&lt;/strong&gt; → &lt;code&gt;customer_directory&lt;/code&gt; (hide sensitive columns)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complex View&lt;/strong&gt; → &lt;code&gt;category_revenue&lt;/code&gt; (total revenue per category)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Materialized View&lt;/strong&gt; → &lt;code&gt;daily_sales&lt;/code&gt; (fast dashboards with precomputed data)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Updatable View&lt;/strong&gt; → &lt;code&gt;active_products&lt;/code&gt; (only allow editing active products)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Read-Only View&lt;/strong&gt; → &lt;code&gt;top_customers&lt;/code&gt; (find VIPs safely)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Recursive View&lt;/strong&gt; → &lt;code&gt;category_hierarchy&lt;/code&gt; (nested categories navigation)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By mixing these types, your database becomes &lt;strong&gt;secure, performant, and easy to query&lt;/strong&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  ⚡ Performance Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Index Materialized Views&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_daily_sales_date&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;daily_sales&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Refresh Strategy&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Refresh materialized views with `REFRESH MATERIALIZED VIEW` at scheduled times (e.g., nightly with `pg_cron`).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Optimize Complex Views&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Break into smaller views or switch to materialized views if performance is bad.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;WITH CHECK OPTION&lt;/code&gt;&lt;/strong&gt; for updatable views&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;active_products&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="k"&gt;OPTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Recursive Views: Add Depth Limit&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🏆 Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;simple &amp;amp; complex views&lt;/strong&gt; for query simplification and security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;materialized views&lt;/strong&gt; for performance-heavy reporting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;updatable views&lt;/strong&gt; with care.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;recursive views&lt;/strong&gt; for hierarchical data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always balance &lt;strong&gt;freshness vs performance&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the right type of view, you can make your PostgreSQL database &lt;strong&gt;faster, safer, and easier to work with&lt;/strong&gt; 🚀&lt;/p&gt;

</description>
      <category>database</category>
      <category>postgres</category>
      <category>backend</category>
      <category>programming</category>
    </item>
    <item>
      <title>Protobuf vs JSON Explained: Speed, Size &amp; When to Use Each</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sun, 27 Jul 2025 20:20:18 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/protobuf-vs-json-explained-speed-size-when-to-use-each-5gg6</link>
      <guid>https://dev.to/arnavsharma2711/protobuf-vs-json-explained-speed-size-when-to-use-each-5gg6</guid>
      <description>&lt;p&gt;Modern applications thrive on fast and efficient communication. While &lt;strong&gt;JSON&lt;/strong&gt; has long been the go-to format for data exchange, many large-scale tech companies are now rethinking their choice. One such example is &lt;strong&gt;Atlassian&lt;/strong&gt;, which made a strategic shift from JSON to &lt;strong&gt;Protocol Buffers (Protobuf)&lt;/strong&gt; to optimize performance and reduce payload sizes across services.&lt;/p&gt;

&lt;p&gt;Their motivation? Faster API responses, smaller data transfers, and better compatibility for growing systems.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll break down what JSON and Protobuf are, compare their strengths and weaknesses, and walk you through how to use Protobuf in a Node.js project. If you're wondering whether it's time for your project to evolve beyond JSON, this guide is for you.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. What is JSON?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JSON (JavaScript Object Notation)&lt;/strong&gt; is a lightweight, human-readable data format commonly used in web APIs and configs. It's based on JavaScript syntax and supported by virtually all programming languages.&lt;/p&gt;

&lt;p&gt;Example:&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;"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;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"isActive"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&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;h2&gt;
  
  
  2. Why is JSON Preferred?
&lt;/h2&gt;

&lt;p&gt;JSON is often the go-to format because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;✅ &lt;strong&gt;Readable&lt;/strong&gt;: Easily understood and edited by humans.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ &lt;strong&gt;Simple&lt;/strong&gt;: No need for extra libraries in JavaScript/Node.js.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ &lt;strong&gt;Interoperable&lt;/strong&gt;: Works across all platforms and languages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ &lt;strong&gt;Debug-friendly&lt;/strong&gt;: Can be logged and inspected directly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But JSON has limitations in &lt;strong&gt;performance&lt;/strong&gt;, &lt;strong&gt;file size&lt;/strong&gt;, and &lt;strong&gt;type safety&lt;/strong&gt;, especially in large systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. About Protobuf
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Protocol Buffers (Protobuf)&lt;/strong&gt; is a binary serialization format developed by Google. It encodes data into compact, fast, and structured bytes using &lt;code&gt;.proto&lt;/code&gt; definitions.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🚀 Compact binary format&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;⚡ Faster parsing and serialization than JSON&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✅ Schema enforcement via &lt;code&gt;.proto&lt;/code&gt; files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔁 Backward and forward compatibility support&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example &lt;code&gt;.proto&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight protobuf"&gt;&lt;code&gt;&lt;span class="na"&gt;syntax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"proto3"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="na"&gt;name&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="kt"&gt;int32&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="na"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;
  
  
  4. Protobuf vs JSON: Key Differences
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;JSON&lt;/th&gt;
&lt;th&gt;Protobuf&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Format&lt;/td&gt;
&lt;td&gt;Text (UTF-8)&lt;/td&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Readable&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Larger&lt;/td&gt;
&lt;td&gt;Smaller&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Speed&lt;/td&gt;
&lt;td&gt;Slower&lt;/td&gt;
&lt;td&gt;Faster&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema&lt;/td&gt;
&lt;td&gt;Optional&lt;/td&gt;
&lt;td&gt;Required (&lt;code&gt;.proto&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Type Safety&lt;/td&gt;
&lt;td&gt;Weak&lt;/td&gt;
&lt;td&gt;Strong&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Versioning&lt;/td&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;td&gt;Built-in support&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  5. How to Use Protobuf in Node.js
&lt;/h2&gt;

&lt;p&gt;We’ll use &lt;a href="https://www.npmjs.com/package/protobufjs" rel="noopener noreferrer"&gt;&lt;code&gt;protobufjs&lt;/code&gt;&lt;/a&gt;, a popular library for Protobuf support in Node.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔧 Step 1: Install the Package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;protobufjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  📝 Step 2: Create a &lt;code&gt;.proto&lt;/code&gt; File
&lt;/h3&gt;

&lt;p&gt;Save this as &lt;code&gt;user.proto&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight protobuf"&gt;&lt;code&gt;&lt;span class="na"&gt;syntax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"proto3"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="na"&gt;name&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="kt"&gt;int32&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="na"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;h3&gt;
  
  
  ⚙️ Step 3: Load and Use in Node.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;protobuf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;protobufjs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&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;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;protobuf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user.proto&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;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookupType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Create a new user object&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;Alice&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;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;is_active&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="c1"&gt;// Verify the payload&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;errMsg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errMsg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;errMsg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Encode the payload to a buffer&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&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;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;finish&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Encoded buffer:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Decode the buffer back to an object&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Decoded object:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🧪 Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Encoded buffer: &amp;lt;Buffer 0a 05 41 6c 69 63 65 10 1c 18 01&amp;gt;
Decoded object: { name: 'Alice', age: 28, isActive: true }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  6. When to Use Protobuf
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ Use Protobuf when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You’re building &lt;strong&gt;microservices&lt;/strong&gt; or using &lt;strong&gt;gRPC&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance and bandwidth are critical&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data payloads are large or frequent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need &lt;strong&gt;strict schemas&lt;/strong&gt; and &lt;strong&gt;versioning&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ✅ Use JSON when:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You want human-readable data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Working with REST APIs or frontend-heavy apps&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need quick debugging and prototyping&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;Recommendation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Human readability&lt;/td&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Protobuf&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Browser apps&lt;/td&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microservices&lt;/td&gt;
&lt;td&gt;Protobuf&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Debugging&lt;/td&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema evolution&lt;/td&gt;
&lt;td&gt;Protobuf&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Both formats have their place. JSON is perfect for human-facing interfaces and simple APIs, while Protobuf excels in high-performance and structured systems.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Tip&lt;/strong&gt;: If you're using gRPC or building APIs for scale, start with Protobuf.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Got questions about implementing Protobuf in your Node.js backend? Drop them in the comments or reach out on GitHub!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How Indexes Work in Databases: Deep Dive into MongoDB &amp; PostgreSQL</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Fri, 18 Jul 2025 05:23:09 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/how-indexes-work-in-databases-deep-dive-into-mongodb-postgresql-3om9</link>
      <guid>https://dev.to/arnavsharma2711/how-indexes-work-in-databases-deep-dive-into-mongodb-postgresql-3om9</guid>
      <description>&lt;p&gt;I’ve been working with databases like &lt;strong&gt;MongoDB&lt;/strong&gt; and &lt;strong&gt;PostgreSQL&lt;/strong&gt; for quite a while now. One thing I’ve used frequently—sometimes almost instinctively—is &lt;strong&gt;indexing&lt;/strong&gt;. Whether it was speeding up queries, optimizing reads, or designing complex filters, indexes always played a role. But recently, I felt the need to dive deeper and understand &lt;strong&gt;how indexing is actually implemented&lt;/strong&gt;, what features it offers beyond performance, and how different databases approach it.&lt;/p&gt;

&lt;p&gt;Let’s explore indexing from scratch, how it works in MongoDB and PostgreSQL, and some best practices I’ve learned along the way.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 What is Indexing?
&lt;/h2&gt;

&lt;p&gt;Indexing is like the table of contents in a book. Instead of scanning every single page (or row), you can jump directly to what you need. Technically, an &lt;strong&gt;index is a data structure&lt;/strong&gt; that allows the database to locate data faster, typically implemented using variations of &lt;strong&gt;B-trees&lt;/strong&gt;, &lt;strong&gt;hash maps&lt;/strong&gt;, or other specialized structures.&lt;/p&gt;

&lt;p&gt;Think of it as a roadmap for the database engine to retrieve data efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Why Indexing?
&lt;/h2&gt;

&lt;p&gt;Without indexing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Queries perform &lt;strong&gt;full table scans&lt;/strong&gt;, slowing down performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complex filters, sorting, and joins become &lt;strong&gt;resource-intensive&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scalability is limited as datasets grow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With indexing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You get &lt;strong&gt;faster query execution&lt;/strong&gt; (especially on large datasets).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sorting and filtering&lt;/strong&gt; become efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It improves &lt;strong&gt;read-heavy workloads&lt;/strong&gt; significantly.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short: indexing is &lt;strong&gt;the backbone of performance optimization&lt;/strong&gt; in databases.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Indexing in MongoDB
&lt;/h2&gt;

&lt;p&gt;MongoDB, being a document-oriented NoSQL database, has its own flavor of indexing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Single Field Index&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&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;createIndex&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="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compound Index&lt;/strong&gt; (multiple fields):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;customerId&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;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&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;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Text Index&lt;/strong&gt;: For full-text search.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Geospatial Index&lt;/strong&gt;: Useful for "nearby" queries (e.g., delivery apps).&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;places&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2dsphere&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multikey Index&lt;/strong&gt;: Automatically created for array fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hashed Index&lt;/strong&gt;: Mainly used for sharding.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Wildcard Index&lt;/strong&gt;: Indexes unknown/nested fields without schema knowledge.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MongoDB indexes are flexible and schema-less, which makes them ideal for fast-changing document structures.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Indexing in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;PostgreSQL offers one of the most robust and versatile indexing systems among relational databases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;B-tree Index (default)&lt;/strong&gt;: Great for equality and range queries.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_name&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GIN Index&lt;/strong&gt;: For full-text search, arrays, JSONB.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_gin&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;gin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Expression Index&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_lower_email&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;LOWER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Partial Index&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_active_users&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hash Index&lt;/strong&gt;: Only for equality (&lt;code&gt;=&lt;/code&gt;) comparisons.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GiST Index&lt;/strong&gt;: For geospatial, full-text, and fuzzy search.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BRIN Index&lt;/strong&gt;: For huge tables where data is stored sequentially (e.g., logs).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Covering Index (INCLUDE clause)&lt;/strong&gt;: Useful when you want to avoid accessing the actual table.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Postgres indexing is extremely powerful when it comes to &lt;strong&gt;structured data, analytics, and optimization&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🆚 MongoDB vs PostgreSQL Indexing
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;MongoDB&lt;/th&gt;
&lt;th&gt;PostgreSQL&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Default Index Type&lt;/td&gt;
&lt;td&gt;B-tree&lt;/td&gt;
&lt;td&gt;B-tree&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full-Text Search&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;text&lt;/code&gt; index (basic)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;GIN&lt;/code&gt; + &lt;code&gt;tsvector&lt;/code&gt; (advanced)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Geospatial Support&lt;/td&gt;
&lt;td&gt;2d/2dsphere&lt;/td&gt;
&lt;td&gt;GiST, PostGIS (more powerful)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multikey Index&lt;/td&gt;
&lt;td&gt;Yes (array support)&lt;/td&gt;
&lt;td&gt;Limited (not auto-handled)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Partial Index&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Expression Index&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Covering Index&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes (with &lt;code&gt;INCLUDE&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wildcard Index&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;MongoDB shines with &lt;strong&gt;array fields and dynamic schemas&lt;/strong&gt;, thanks to multikey and wildcard indexes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PostgreSQL excels in &lt;strong&gt;complex querying, full-text search, and advanced filtering&lt;/strong&gt;, thanks to its expression and GIN/GiST indexes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Indexing Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Index what you query&lt;/strong&gt; — not every column/field needs an index.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Don’t over-index&lt;/strong&gt; — too many indexes hurt writes (insert/update/delete).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use compound indexes&lt;/strong&gt; for multi-field filters/sorting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Monitor usage&lt;/strong&gt; with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; (Postgres) or &lt;code&gt;explain()&lt;/code&gt; (MongoDB).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rebuild or drop unused indexes&lt;/strong&gt; regularly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use partial indexes&lt;/strong&gt; for conditional queries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In MongoDB&lt;/strong&gt;, avoid deep nested fields in indexes — they may not perform as expected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;Indexing isn’t just a performance booster — it’s a design strategy. Understanding how your database uses indexes under the hood helps you write better queries, build more scalable systems, and avoid bottlenecks. Whether you're working with a flexible document store like MongoDB or a powerful relational system like PostgreSQL, mastering indexing can make a huge difference in real-world applications.&lt;/p&gt;

&lt;p&gt;Next time you're writing a query, ask yourself: &lt;strong&gt;"Is there an index for that?"&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>database</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why Use NVM? Manage Node.js Versions Like a Pro</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sat, 05 Jul 2025 19:44:18 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/why-use-nvm-manage-nodejs-versions-like-a-pro-3a3j</link>
      <guid>https://dev.to/arnavsharma2711/why-use-nvm-manage-nodejs-versions-like-a-pro-3a3j</guid>
      <description>&lt;p&gt;Ever been stuck with a project that only works with Node.js v14 while your system has v22?&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;NVM (Node Version Manager)&lt;/strong&gt; saves the day. If you're a JavaScript or Node.js developer, using NVM is &lt;em&gt;almost essential&lt;/em&gt;. Let's break down &lt;strong&gt;why you should use NVM&lt;/strong&gt; and &lt;strong&gt;how to set it up&lt;/strong&gt; in less than 5 minutes.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 Why Use NVM?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Switch Between Node.js Versions Effortlessly&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Projects often require different Node.js versions. With NVM, you can switch between them with a single command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm use 18
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;Isolate Project Dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Avoid global conflicts. Each project can use its own Node.js version, so you're never stuck debugging weird issues due to mismatched versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Avoid Breaking Global Installs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Upgrading Node globally can break tools like &lt;code&gt;npm&lt;/code&gt;, &lt;code&gt;yarn&lt;/code&gt;, or &lt;code&gt;next&lt;/code&gt;. NVM keeps it safe by isolating installs.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Great for Open Source Contributions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Many open-source projects specify a Node.js version in &lt;code&gt;.nvmrc&lt;/code&gt;. With NVM, all you need is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm use
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…and you're good to go.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ How to Install NVM (Mac/Linux)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Install via cURL or Wget
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# using curl&lt;/span&gt;
curl &lt;span class="nt"&gt;-o-&lt;/span&gt; https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

&lt;span class="c"&gt;# or using wget&lt;/span&gt;
wget &lt;span class="nt"&gt;-qO-&lt;/span&gt; https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Add NVM to your shell profile
&lt;/h3&gt;

&lt;p&gt;After installation, add this to your &lt;code&gt;.bashrc&lt;/code&gt;, &lt;code&gt;.zshrc&lt;/code&gt;, or &lt;code&gt;.profile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;NVM_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;&lt;span class="s2"&gt;/.nvm"&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NVM_DIR&lt;/span&gt;&lt;span class="s2"&gt;/nvm.sh"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NVM_DIR&lt;/span&gt;&lt;span class="s2"&gt;/nvm.sh"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then restart your terminal or run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.bashrc
&lt;span class="c"&gt;# or&lt;/span&gt;
&lt;span class="nb"&gt;source&lt;/span&gt; ~/.zshrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Verify Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see something like &lt;code&gt;0.39.7&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Using NVM
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install a Node version
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm &lt;span class="nb"&gt;install &lt;/span&gt;20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use a specific version
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm use 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set default version
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm &lt;span class="nb"&gt;alias &lt;/span&gt;default 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check installed versions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📁 Bonus: Use &lt;code&gt;.nvmrc&lt;/code&gt; for Project-Level Versioning
&lt;/h2&gt;

&lt;p&gt;Add a &lt;code&gt;.nvmrc&lt;/code&gt; file in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"18"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .nvmrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, whenever you're in the project directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nvm use
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📦 Windows? Try &lt;code&gt;nvm-windows&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you're on Windows, use the &lt;a href="https://github.com/coreybutler/nvm-windows" rel="noopener noreferrer"&gt;nvm-windows&lt;/a&gt; fork:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download the installer from the &lt;a href="https://github.com/coreybutler/nvm-windows/releases" rel="noopener noreferrer"&gt;release page&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Follow the setup instructions&lt;/li&gt;
&lt;li&gt;Done!&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🚀 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Whether you're working solo or contributing to multiple projects, &lt;strong&gt;NVM makes your Node.js workflow cleaner and safer&lt;/strong&gt;. No more "version not supported" errors, no more global chaos.&lt;/p&gt;

</description>
      <category>node</category>
      <category>nvm</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>MongoDB Transactions in Node.js: Complete Guide with Real Examples</title>
      <dc:creator>Arnav Sharma</dc:creator>
      <pubDate>Sat, 05 Jul 2025 19:38:48 +0000</pubDate>
      <link>https://dev.to/arnavsharma2711/mongodb-transactions-in-nodejs-complete-guide-with-real-examples-28fl</link>
      <guid>https://dev.to/arnavsharma2711/mongodb-transactions-in-nodejs-complete-guide-with-real-examples-28fl</guid>
      <description>&lt;h1&gt;
  
  
  🔁 MongoDB Transactions in Node.js — The Complete Guide with Mongoose
&lt;/h1&gt;

&lt;p&gt;If you think MongoDB isn’t meant for complex, multi-step operations like SQL transactions — think again.&lt;/p&gt;

&lt;p&gt;With replica sets and sharded clusters, MongoDB supports &lt;strong&gt;ACID-compliant transactions&lt;/strong&gt;. And in this blog, I’ll walk you through how to use them in a real-world Node.js backend.&lt;/p&gt;

&lt;h3&gt;
  
  
  👉 When Do You Even Need Transactions?
&lt;/h3&gt;

&lt;p&gt;Let’s say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re deducting money from one user and adding it to another.&lt;/li&gt;
&lt;li&gt;You’re updating multiple collections together (like orders + inventory).&lt;/li&gt;
&lt;li&gt;Or, you just want to make sure &lt;strong&gt;either everything is updated or nothing is&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MongoDB’s default operations are atomic only &lt;strong&gt;at the document level&lt;/strong&gt;. If you need &lt;strong&gt;multi-document atomicity&lt;/strong&gt;, transactions are your tool.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧱 Setup
&lt;/h2&gt;

&lt;p&gt;I'll be using Mongoose here. First, install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connect to your database (make sure you're on a &lt;strong&gt;replica set&lt;/strong&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="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://localhost:27017/your-db?replicaSet=rs0&lt;/span&gt;&lt;span class="dl"&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;useNewUrlParser&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="na"&gt;useUnifiedTopology&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  💼 Real-World Example: Wallet Transfer
&lt;/h2&gt;

&lt;p&gt;Let’s build a simple transfer system between two users.&lt;/p&gt;

&lt;h3&gt;
  
  
  User Schema
&lt;/h3&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;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&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;userSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Schema&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&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;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userSchema&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🔁 Transaction Logic
&lt;/h3&gt;

&lt;p&gt;Here’s how you’d do a &lt;code&gt;transferMoney()&lt;/code&gt; function that updates both sender and receiver in a single transaction:&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;transferMoney&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;receiverId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startSession&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startTransaction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;try&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;sender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;senderId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;session&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;receiver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;receiverId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Insufficient balance&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;sender&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="nx"&gt;session&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;receiver&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="nx"&gt;session&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commitTransaction&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;✅ Transaction committed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abortTransaction&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;❌ Transaction aborted:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endSession&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧪 Try It Out
&lt;/h2&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="k"&gt;async &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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://localhost:27017/your-db?replicaSet=rs0&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;alice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&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="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&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;bob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&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="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;transferMoney&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;alice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedAlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;alice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&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;updatedBob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_id&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice's balance:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updatedAlice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 70&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob's balance:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;updatedBob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// 80&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚠️ Common Mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;❌ Not using a replica set — transactions won't work on standalone MongoDB.&lt;/li&gt;
&lt;li&gt;❌ Forgetting to pass the &lt;code&gt;session&lt;/code&gt; into all operations inside the transaction.&lt;/li&gt;
&lt;li&gt;❌ Trying to use transactions in high-throughput scenarios where performance is critical (transactions are slower).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔄 Retry Pattern (Optional but Ideal)
&lt;/h2&gt;

&lt;p&gt;In production, your transaction can fail due to transient network issues. Ideally, wrap your logic in a &lt;strong&gt;retry loop&lt;/strong&gt; with exponential backoff.&lt;/p&gt;

&lt;p&gt;You can also check for &lt;code&gt;TransientTransactionError&lt;/code&gt; or &lt;code&gt;UnknownTransactionCommitResult&lt;/code&gt; and reattempt.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use transactions when you're working with multi-document consistency.&lt;/li&gt;
&lt;li&gt;Always wrap your logic inside &lt;code&gt;session.startTransaction() / commitTransaction() / abortTransaction()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Keep transactions short, simple, and used only when necessary.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That’s it! This small trick can prevent big disasters in your backend logic.&lt;/p&gt;

&lt;p&gt;If you liked this, I’ve also written about:&lt;br&gt;
👉 &lt;a href="https://dev.to/arnavsharma2711/redis-beyond-set-and-get-unlocking-the-real-power-of-redis-3g04"&gt;Redis beyond SET and GET&lt;/a&gt;&lt;br&gt;
👉 &lt;a href="https://dev.to/arnavsharma2711/behind-the-scenes-how-nodejs-express-handle-multiple-requests-5h4p"&gt;How Node.js handles concurrent requests&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Let me know if you want a LinkedIn post version or carousel graphic too — happy to make one!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>node</category>
      <category>mongodb</category>
      <category>database</category>
    </item>
  </channel>
</rss>
