<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Saurav Jha]]></title><description><![CDATA[Documenting my engineering learnings.]]></description><link>https://srvjha.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 16:01:07 GMT</lastBuildDate><atom:link href="https://srvjha.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[What Actually Happens When You Send a Message to ChatGPT?]]></title><description><![CDATA[You type a question, hit enter, and a few seconds later you have an answer that reads like a human wrote it. It feels a little like magic. It isn't. It's…]]></description><link>https://srvjha.hashnode.dev/what-actually-happens-when-you-send-a-message-to-chatgpt</link><guid isPermaLink="true">https://srvjha.hashnode.dev/what-actually-happens-when-you-send-a-message-to-chatgpt</guid><category><![CDATA[chatgpt]]></category><category><![CDATA[openai]]></category><category><![CDATA[gpt]]></category><category><![CDATA[Prompt]]></category><category><![CDATA[Tokenization]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[engineering]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Wed, 01 Jul 2026 08:52:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/2b9f5e2e-6bc7-405c-ac9f-befe4d1c03e4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You type a question, hit enter, and a few seconds later you have an answer that reads like a human wrote it. It feels a little like magic. It isn't. It's math, at a scale that's hard to picture, running on an architecture that changed AI completely. Let's break down what's actually happening under the hood.</p>
<h2>What is an LLM?</h2>
<p>LLM stands for <strong>Large Language Model</strong>, a model trained on a large amount of language data, built by <strong>ML engineers</strong> who feed it huge volumes of text so it can learn the patterns in how we write and speak.</p>
<p><strong>Here's the problem LLMs actually solve</strong>. Before this generation of models, getting a computer to work with language meant writing rigid rules, if this word appears do this, match this pattern do that. That approach breaks the moment language does anything even slightly unexpected, which is most of the time. LLMs don't work off fixed rules. They learn statistical patterns from data, which means they generalize to sentences they've never seen before.</p>
<p>You already know the popular examples: <strong>GPT-4o from OpenAI</strong>, <strong>Claude from Anthropic</strong>, <strong>Gemini from Google</strong>, <strong>Llama from Meta</strong>.</p>
<p><strong>One thing worth being clear on early</strong>: an LLM is trained on a fixed dataset up to a certain point. It isn't pulling live answers from an infinite, ever-updating pool. Once training is done, it generates responses based on the patterns it picked up during that training, not by browsing the internet in real time.</p>
<h2>What Happens When You Send a Message to ChatGPT?</h2>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/43a24b75-e608-41e2-bab5-418b436fd30f.png" alt="" style="display:block;margin:0 auto" />

<p>Say you type "Hi Cutie 🙂" and hit send. It feels instant, but a few distinct steps happen in that gap.</p>
<p><strong>Processing your message.</strong> The moment you hit send, your text gets converted into a form the model can actually work with (more on that in the tokenization section below). It doesn't stay as plain English for long.</p>
<p><strong>Generating a response.</strong> This is the part people get wrong most often. The model isn't looking up your question in some giant database and returning a matching answer. It's predicting, one token at a time, what the most likely next piece of text should be, given everything that came before <strong>it. It does this over and over, token by token, until it decides the response is complete.</strong></p>
<h2>Tokenization</h2>
<p>A token is a <strong>chunk of text</strong>. Depending on the tokenizer, that chunk could be a whole word, part of a word, or in some cases a single character.</p>
<p>Tokenization exists because a model can't realistically hold a fixed entry for every possible word in every language, including words people haven't invented yet. Breaking text into smaller reusable pieces lets the model handle rare words, typos, and made up terms by combining fragments it already knows, instead of failing on anything outside a fixed dictionary.</p>
<blockquote>
<p>Words and tokens are not the same thing, and that trips people up constantly. A short common word like "the" is usually a single token. But something like "unbelievable" might get split into pieces like "un", "believ", and "able". Even a name like "ChatGPT" can get broken into several tokens rather than staying as one unit.</p>
</blockquote>
<p>Once the text is broken into tokens, each token gets converted into a <strong>vector embedding</strong>, a list of numbers that represents that token's meaning. This is the step that actually gives the numbers significance. Words with similar meaning end up positioned close to each other in that numerical space, which is what lets the model reason about meaning instead of just symbols.</p>
<h2>Transformers</h2>
<p>The Transformer is the architecture almost every modern LLM is built on. It was introduced in a 2017 paper, and it's genuinely the reason the field moved as fast as it did afterward.</p>
<p>Before Transformers, models processed text sequentially, one word after another, <strong>using architectures like RNNs</strong>. That's slow, and it makes it hard for the model to connect something at the start of a long passage with something much later in it. Transformers process the whole sequence at once and use a mechanism called self-attention to directly relate every word to every other word, regardless of how far apart they are.</p>
<p>Two ideas inside the Transformer do most of the heavy lifting for understanding language:</p>
<p><strong>Positional encoding.</strong> Because Transformers process everything in parallel instead of one word at a time, they lose the natural sense of order that a sentence has. Positional encoding adds information about where each token sits in the sequence, so "dog bites man" and "man bites dog" don't get treated as the same thing just because they contain identical words.</p>
<p><strong>Self-attention.</strong> This is what lets the model figure out meaning from context rather than from the word alone. Take "Ruchi learned <strong>Python</strong> in college" versus "The <strong>python</strong> slithered through the grass." Same word, completely different meaning. Self-attention lets the model weigh every other word in the sentence when interpreting one word, so it picks up on "learned" and "college" to know the first is a programming language, and "slithered" and "grass" to know the second is a snake.</p>
<p>That combination, parallel processing plus attention across the full sequence, is what made it possible to train on massive datasets efficiently while still capturing long range context. That's exactly what language needs to be understood properly. GPT, Claude, Gemini, Llama, nearly every major model today is a variant of this same architecture, just at different scales with different training choices layered on top.</p>
<h2>The Finishing Touches: Softmax, Temperature, and Add &amp; Norm</h2>
<p>There are a few more pieces working quietly in the background that decide how the model actually picks its next word, and how it stays stable while doing all this across dozens of stacked layers.</p>
<p><strong>Softmax.</strong> The model's raw output is just a score for every possible next token, numbers, not probabilities. Softmax converts that list into an actual probability distribution that adds up to 100%, so instead of "token A scored higher than token B," you get "token A: 62% chance, token B: 20%." That's what turns raw scoring into something the model can actually sample a next token from.</p>
<p><strong>Temperature.</strong> The dial for how safe or adventurous that sampling is. Low temperature sticks close to the highest-probability token, predictable, but can feel repetitive. High temperature flattens the distribution so lower-ranked tokens get a real shot too, more variety, less consistency. That's why the same prompt at high temperature gives you differently worded answers each time, while low temperature gives close to the same one. Neither setting is "correct," it depends on whether you want reliability or variety.</p>
<p><strong>Add &amp; Norm.</strong> Right after self-attention, and again after the feed-forward step, every Transformer block does two things. "Add" is a residual connection: the block's input gets added back to its output, like a relay runner who's allowed to tweak the baton slightly but must hand back something recognizable, not a different object. This keeps information from diluting as it passes through layer after layer. "Norm" rescales the numbers afterward to keep them in a stable range, without it, stacking dozens of these blocks makes training unstable or makes it stop working entirely. Less a nice-to-have, more a load-bearing wall.</p>
<h2>Transformer Visualizer</h2>
<p>If you want to see this flow instead of just reading about it, <strong>I built a small visualizer that walks through tokenization, embeddings, and next-token prediction step by step</strong>: <a href="http://transformer-visualizer-omega.vercel.app">transformer-visualizer-omega.vercel.app</a>.</p>
<p>It uses a GPT-style decoder-only setup with live next-token prediction, so you can actually watch a prompt turn into tokens and then into a generated response instead of just imagining it.</p>
<h2>Summary</h2>
<ul>
<li><p>An LLM is trained on a fixed dataset and generates replies by predicting the next token repeatedly, not by looking up or copying from the internet.</p>
</li>
<li><p>Before any of that prediction can happen, your text has to be broken into tokens and converted into numerical vector embeddings, since computers only ever work with numbers.</p>
</li>
<li><p>The Transformer architecture processes an entire sequence in parallel and uses self-attention so every word can directly relate to every other word, which is why it replaced older sequential models like RNNs.</p>
</li>
<li><p>Positional encoding keeps word order intact despite parallel processing, and Add &amp; Norm keeps information stable as it passes through dozens of stacked layers.</p>
</li>
<li><p>Softmax turns the model's raw scores into real probabilities, and temperature decides whether the model plays it safe with the top choice or takes more risks with lower-probability tokens.</p>
</li>
</ul>
<p><strong>Resources where I learned from :</strong></p>
<ul>
<li><p><a href="https://arxiv.org/abs/1706.03762">Attention Is All You Need</a>, the original Transformer paper by Vaswani et al.</p>
</li>
<li><p><a href="https://jalammar.github.io/illustrated-transformer/">The Illustrated Transformer</a> by Jay Alammar, for the visual intuition behind attention and the encoder-decoder flow</p>
</li>
<li><p><a href="https://platform.openai.com/tokenizer">OpenAI's Tokenizer tool</a>, to see real tokenization behavior on actual text</p>
</li>
<li><p><a href="https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them">What are tokens and how to count them</a>, OpenAI's own explainer on tokens and context limits</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How OIDC and OAuth Actually Works Under the Hood ?]]></title><description><![CDATA[Most developers use "Login with Google" every day but have no idea what happens under the hood. This blogs covers all.]]></description><link>https://srvjha.hashnode.dev/how-oidc-and-oauth-actually-works-under-the-hood</link><guid isPermaLink="true">https://srvjha.hashnode.dev/how-oidc-and-oauth-actually-works-under-the-hood</guid><category><![CDATA[authentication]]></category><category><![CDATA[OIDC]]></category><category><![CDATA[oauth]]></category><category><![CDATA[Google]]></category><category><![CDATA[backend]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Wed, 22 Apr 2026 08:09:27 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/38ededbb-921a-4292-bb10-9df593a1892d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There is a good chance you have used "Login with Google" a hundred times without thinking twice about what is happening behind the scenes. And if you are building something, you have probably copy-pasted some auth library and called it a day. This blog is for the people who want to actually understand what is going on from the very beginning, from the problems that forced these systems to exist, to <strong>why OAuth and OIDC are designed the way they are</strong>.</p>
<p>We are going to go step by step.</p>
<h2>First, Let's Talk About Auth in General</h2>
<p>Authentication is basically the system figuring out <em>who you are</em>.</p>
<p>There are two broad approaches:</p>
<ul>
<li><p><strong>Stateless (Token-based)</strong> — The client holds a token. The server trusts the token. No server-side session storage needed.</p>
</li>
<li><p><strong>Stateful (Session-based)</strong> — The server creates a session and stores it. The client just holds a session ID.</p>
</li>
</ul>
<p>Most modern systems go stateless with JWTs. You log in, get a token, attach it to every request, and the server decodes it to know who you are.</p>
<h2>Symmetric Authentication: The Simple Starting Point</h2>
<p>When you are building a small app, the simplest thing you can do is <strong>symmetric JWT auth</strong>. There is one secret key (<code>JWT_SECRET</code>), and it does two jobs:</p>
<ol>
<li><p><strong>Create</strong> the token (sign it)</p>
</li>
<li><p><strong>Verify</strong> the token (decode it)</p>
</li>
</ol>
<p>Same key. Both sides. Simple.</p>
<p>This works perfectly fine when you have a single server. The key lives in your <code>.env</code>, you sign tokens on login, you verify them on protected routes, done.</p>
<p>But here is where it starts to fall apart.</p>
<h2>The Monolith Problem</h2>
<p>In a monolith, everything runs inside one server. Auth service, upload service, payment service all of them are just different routes inside the same Express app. The <code>JWT_SECRET</code> is in one place, and every part of the application can access it directly.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/24b9212f-9a68-4a09-ba76-87bd63497169.png" alt="" style="display:block;margin:0 auto" />

<p>The problem is not the secret sharing here. The problem is the architecture itself.</p>
<p>If auth goes down, <strong><em>everything</em> goes down</strong>. If one route has a bug that crashes the process, the entire server is dead. There is no isolation. You cannot independently scale the part of your system that is getting hammered with traffic. If your upload service suddenly gets 10x the load, you cannot just spin up more upload servers without also spinning up more copies of your entire application.</p>
<p>This is the core argument for microservices.</p>
<h2>Microservice Architecture: Different Servers, Different Services</h2>
<p>The idea is simple: split services into separate servers.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/aeba7dd3-1c31-4d61-9af0-2727a3dedd5a.png" alt="" style="display:block;margin:0 auto" />

<p>Your main application runs at <code>srvjha.in</code>. Now if auth crashes, uploads still work. If uploads get flooded with traffic, you scale <em>that</em> service independently without touching anything else.</p>
<p>This is genuinely better. But it introduces a new problem that is not obvious until you sit down and think about it.</p>
<h2>The Big Problem with Microservices and Auth</h2>
<p>Here is the scenario. A user wants to upload a file.</p>
<ol>
<li><p>User hits the upload server with a token.</p>
</li>
<li><p>Upload server needs to verify that token.</p>
</li>
<li><p>To verify a JWT, you need the <code>JWT_SECRET</code>.</p>
</li>
</ol>
<p>So now your upload server also needs <code>JWT_SECRET</code>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/4f40fc5c-d38c-49f7-9bf0-ed9a59cfee56.png" alt="" style="display:block;margin:0 auto" />

<p>Fine. Add it to the upload server's <code>.env</code>. But now imagine you have 10 different microservices. Payment service, notification service, analytics service, recommendation service... they all need to verify tokens. They all need the secret.</p>
<p>The problems pile up fast:</p>
<ul>
<li><p><strong>You have to share the secret with every single service.</strong> That means 10+ places where this secret exists. The more places a secret lives, the more chances it has to leak.</p>
</li>
<li><p><strong>If the secret rotates</strong> (which it should, periodically), you have to update it in every single service simultaneously. That is a deployment nightmare.</p>
</li>
<li><p><strong>Any service getting compromised means the attacker can now forge tokens for your entire system.</strong></p>
</li>
</ul>
<p>Sharing a secret widely is not security. It is a time bomb.</p>
<h3>The Other Approach Internal Auth Calls</h3>
<p>Instead of giving every service the secret, what if each service just <em>asked</em> the auth server to verify the token?</p>
<p>The upload server gets a token → instead of verifying it locally, it makes an internal network call to auth server → auth server verifies and returns the user info → upload server proceeds.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/d4407cd6-1ded-4d45-87c9-7fa7cac4a490.png" alt="" style="display:block;margin:0 auto" />

<p>This is actually better from a secret management standpoint. Only the auth server holds the secret. But now you have a different problem.</p>
<p>Every single request to any service triggers an internal call to the auth server. If you have high traffic, the <strong>auth server becomes a bottleneck</strong>. It gets overwhelmed. It becomes the single point of failure you were trying to avoid in the first place.</p>
<p>So now you have two real problems:</p>
<ul>
<li><p><strong>Problem 1:</strong> Can't give JWT secret to every service</p>
</li>
<li><p><strong>Problem 2:</strong> Internal verification calls can overwhelm the auth server</p>
</li>
</ul>
<p>This is exactly the problem Google, Microsoft, and every large tech company ran into. They all had multiple services. They all needed a way to handle auth across them without creating these bottlenecks.</p>
<h3>How Google Thinks About This: Single Sign-On</h3>
<p>Think about Google's product suite. YouTube, Gmail, Drive, Maps, Play Store. When you are logged into Gmail and you open Youtube in a new tab, you are already logged in. You did not log in again. That is SSO(Single Sign-On).</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/52073268-9dde-4b0e-849d-4d054a956188.png" alt="" style="display:block;margin:0 auto" />

<p>Under the hood, all of these services go through a central authentication service at <code>accounts.google.com</code>. But this auth service does not verify every token on every request. That would be exactly Problem 2.</p>
<p>The solution they built is based on a different cryptographic approach: <strong>Asymmetric Authentication</strong>.</p>
<h2>Asymmetric Authentication:</h2>
<p>The Real Solution</p>
<p>Instead of one secret key, you have two:</p>
<ul>
<li><p><strong>Private Key</strong> — Used to <em>create</em> (sign) the token. Stays with the auth server. Never shared.</p>
</li>
<li><p><strong>Public Key</strong> — Used to <em>verify</em> the token. Can be shared with literally anyone. It is public.</p>
</li>
</ul>
<p>The math behind this is elegant: the public key can verify that a token was signed by the matching private key, but it cannot be used to <em>create</em> new tokens. It is a one-way street.</p>
<p>So the formula becomes:</p>
<pre><code class="language-javascript">Token + Public Key = UserInfo  (verification, anyone can do this)
Payload + Private Key = Token  (signing, only auth server can do this)
</code></pre>
<p>Think of it like currency notes. Anyone can look at a note and verify it is genuine (public key). But only the mint can print new ones (private key).</p>
<p>Now the flow looks like this:</p>
<ul>
<li><p>Auth server signs tokens with its private key.</p>
</li>
<li><p>Every other service gets the public key.</p>
</li>
<li><p>They can verify tokens locally without calling the auth server.</p>
</li>
<li><p>If an attacker gets the public key — so what? They cannot forge tokens with it.</p>
</li>
</ul>
<p>This solves Problem 1 (no sensitive secret to protect) and Problem 2 (no central bottleneck for verification).</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/4dbcdc82-4a09-45d0-a0e7-699d26e25778.png" alt="" style="display:block;margin:0 auto" />

<h3>But There Is Still a Distribution Problem</h3>
<p>How do all these services get the public key? And more importantly, when the auth server rotates its keys (which it should, regularly), how do services get the <em>updated</em> public key?</p>
<p>You need a standardized way to distribute public keys. And this is where a standard protocol starts to make sense.</p>
<h2>OIDC : OpenID Connect</h2>
<p>A bunch of companies (Google, Microsoft, and others) sat down and agreed on a standardized way to build authentication services. The result is <strong>OIDC — OpenID Connect</strong>.</p>
<p>The other protocol you may hear about is <strong>SAML</strong>, which works on XML. OIDC works on JSON and is far more common in modern systems.</p>
<p>When you build an auth service that follows OIDC, you have to expose a specific endpoint:</p>
<pre><code class="language-shell">/.well-known/openid-configuration
</code></pre>
<p>This is called the <strong>Service Discovery</strong> endpoint. Hit it, and you get back a JSON document describing everything about the auth service. You can see this live right now at <code>accounts.google.com/.well-known/openid-configuration</code>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/a76756d9-a07c-4d34-89ed-378182d83b93.png" alt="" style="display:block;margin:0 auto" />

<p>That JSON contains keys like:</p>
<ul>
<li><p><code>issuer</code> — the base URL of the auth service</p>
</li>
<li><p><code>authorization_endpoint</code> — where users go to log in</p>
</li>
<li><p><code>token_endpoint</code> — where tokens are issued</p>
</li>
<li><p><code>jwks_uri</code> — where to get the public keys</p>
</li>
</ul>
<p>That last one, <code>jwks_uri</code>, points to the <strong>JSON Web Key Set (JWKS)</strong> endpoint. This is where any service can fetch the current public keys. Automated. Standardized. Services can even poll this endpoint periodically to pick up rotated keys without any manual intervention.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/2273250c-0118-4fd1-9c8c-9f01c442922c.png" alt="" style="display:block;margin:0 auto" />

<p>This solves the key distribution problem cleanly.</p>
<h3>The Remaining Problem with OIDC Alone</h3>
<p>OIDC by default works for <em>internal</em> services. Google's own products : YouTube, Gmail, Drive can use OIDC to authenticate users through <code>accounts.google.com</code> because they are Google's own services.</p>
<p>But what if you are building <code>srvjha.in</code> — an external application — and you want your users to log in with their Google account?</p>
<p>When your app tries to redirect users to the Google login page without any prior setup, Google blocks it. From Google's perspective, it has no idea who <code>srvjha.in</code> is. Why should it trust this random URL with its users' credentials?</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/e9cef183-62ea-4f18-9d11-ad4ceae78228.png" alt="" style="display:block;margin:0 auto" />

<p>This is where OAuth comes in.</p>
<hr />
<h2>OAuth The Trust Handshake Before OIDC</h2>
<p>OAuth is not a replacement for OIDC. It is the layer that comes <em>before</em> OIDC. It is how external applications earn Google's trust so they can then use OIDC to authenticate users.</p>
<p>The process starts with the <strong>Developer Console</strong>.</p>
<p>You go to Google's developer console and register your application. You fill out a form:</p>
<ul>
<li><p>App Name</p>
</li>
<li><p>Your URL (<code>srvjha.in</code>)</p>
</li>
<li><p>Privacy Policy URL</p>
</li>
<li><p>Terms &amp; Conditions URL</p>
</li>
<li><p>What user data you need</p>
</li>
<li><p>Your hosted URL</p>
</li>
<li><p><strong>Redirect URI</strong> (<code>srvjha.in/api/redirect-uri</code>)</p>
</li>
</ul>
<p>Google reviews this (or auto-approves for basic access) and gives you back:</p>
<pre><code class="language-javascript">client_id
client_secret
</code></pre>
<p>Now <code>srvjha.in</code> is a <em>recognized</em> application in Google's system. It has an identity. When your app redirects users to Google's login page with <code>?client_id=your_id</code> in the URL, Google recognizes it and trusts the request.</p>
<h3>Lets go through a complete OAuth + OIDC Flow</h3>
<p>What happens when a user clicks "<strong>Login with Google</strong>" on your app:</p>
<p><strong>Step 1 — Service Discovery</strong></p>
<p>Your app hits <code>accounts.google.com/.well-known/openid-configuration</code> and gets back the JSON with all the important URLs, including <code>authorization_url</code>.</p>
<p><strong>Step 2 — Redirect to Google Login</strong></p>
<p>Your app redirects the user to <code>authorization_url?client_id=your_client_id</code>. Google sees the client_id, recognizes your app, and shows the user Google's login page.</p>
<p><strong>Step 3 — User Logs In and Consents</strong></p>
<p>User logs in with their Google credentials. Google shows a consent form — "srvjha.in wants access to your name and email. Allow?" User clicks Allow.</p>
<p><strong>Step 4 —</strong></p>
<h3><strong>The Short Code</strong></h3>
<p>Here is an interesting design decision. After consent, Google needs to send something back to your app. The naive approach would be to redirect to your URI with the actual token in the URL:</p>
<pre><code class="language-plaintext">srvjha.in/api/redirect-uri?token=actual_token
</code></pre>
<p>But <strong>tokens in URLs are dangerous</strong>. URLs get logged in browser history, server logs, proxy logs. A token sitting in a URL is a <strong>Man-in-the-Middle attack</strong> waiting to happen. Anyone intercepting the network traffic or reading logs gets a valid token.</p>
<p>So instead, Google sends a <strong>short-lived code</strong> (valid for about 1 minute) in the URL:</p>
<pre><code class="language-javascript">srvjha.in/api/redirect-uri?code=short_code
</code></pre>
<p>This code is useless on its own.</p>
<p><strong>Step 5 — Code for Token Exchange</strong></p>
<p>Your backend takes this <code>short_code</code> and combines it with your <code>client_secret</code>, then makes a server-to-server call to Google's <code>token_endpoint</code>:</p>
<pre><code class="language-javascript">short_code + client_secret → token_endpoint → actual Token
</code></pre>
<p>This exchange happens server-to-server, not through the browser. It never appears in a URL. The <code>client_secret</code> never leaves your server. Even if someone intercepts the <code>short_code</code> from the redirect URL, they cannot exchange it without the <code>client_secret</code>.</p>
<p>This is why the <code>client_secret</code> matters — it is the proof that the entity completing the exchange is actually your registered application and not some attacker who intercepted the code.</p>
<p><strong>Step 6 — You Have a Token</strong></p>
<p>Your server now has a proper JWT, signed by Google's private key. Any service in your system can verify this token using Google's public keys (fetched from <code>jwks_uri</code>). No calls back to Google needed for every request. No shared secrets across services.</p>
<h2>Putting It All Together</h2>
<p>Let us summarize the whole thing:</p>
<p><strong>Symmetric JWT</strong> works fine for a single server. One key, simple, fast. Falls apart when you add multiple services.</p>
<p><strong>Asymmetric JWT</strong> solves the multi-service problem. Auth server holds the private key, everyone else gets the public key. But you still need a standardized way to distribute and rotate those public keys.</p>
<p><strong>OIDC</strong> is that standard. It defines the <code>/.well-known/openid-configuration</code> endpoint, the JWKS endpoint for public key distribution, and a consistent contract for how auth services expose their functionality. If you build your auth service following OIDC, any other system knows exactly how to interact with it.</p>
<p><strong>OAuth</strong> is the trust layer on top of OIDC that allows external applications (applications that are not part of your organization) to use your OIDC endpoints. You register your app, get a <code>client_id</code> and <code>client_secret</code>, and now Google (or any OAuth provider) knows who you are and can safely let your users authenticate through them.</p>
<p>The final flow:</p>
<pre><code class="language-javascript">OAuth  →  establishes trust between your app and the identity provider
OIDC   →  handles the actual token exchange and user info retrieval
</code></pre>
<p>OAuth enables the relationship. OIDC does the work.</p>
<h2>One Last Thing :</h2>
<p>Why the Short Code Pattern Is Clever</p>
<p>The two-step code-then-token exchange is worth dwelling on for a second because it is a genuinely elegant security design.</p>
<p>The problem it solves is: how do you get a sensitive token from Google's servers to your backend without ever exposing it in a URL?</p>
<p>The answer is: you do not send the token through the browser at all. You send a throwaway code through the browser (where it might get logged or intercepted), and then you do the real exchange server-to-server, authenticated with your <code>client_secret</code>. Even if someone steals the code, it expires in a minute, and without your secret, they cannot complete the exchange.</p>
<p>This pattern sometimes called the <strong>Authorization Code Flow</strong> is the standard way OAuth works and the reason it has held up as a security model for so long.</p>
<h2>Examples:</h2>
<p>When you use a library like <code>passport.js</code>, <code>next-auth</code>, <code>better-auth</code>, or any other auth abstraction, this is what it is doing under the hood. It is hitting the discovery endpoint, redirecting to the authorization URL with your client_id, handling the redirect with the code, exchanging it for a token, and then giving you the user object.</p>
<p>The library abstracts the steps, but the steps are always the same. Understanding them means you know what to look at when something goes wrong, you know why your redirect URI needs to match exactly, and you understand why rotating your client_secret is a security practice and not just overhead.</p>
]]></content:encoded></item><item><title><![CDATA[Joins in SQL Explained.]]></title><description><![CDATA[In relational databases, data is intentionally split across multiple tables to avoid duplication and maintain consistency. This concept is called…]]></description><link>https://srvjha.hashnode.dev/joins-in-sql-explained</link><guid isPermaLink="true">https://srvjha.hashnode.dev/joins-in-sql-explained</guid><category><![CDATA[Databases]]></category><category><![CDATA[joins]]></category><category><![CDATA[SQL]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sun, 12 Apr 2026 16:53:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/dd179be9-7990-4659-a42c-3db6d9d83493.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What are SQL Joins and Why do we need it ?</h2>
<p>In relational databases, data is intentionally split across multiple tables to avoid duplication and maintain consistency. This concept is called normalization.</p>
<p>Let's understand normalization to understand why do we need joins.</p>
<p>Suppose a single table storing orders:</p>
<p><strong>Orders Table</strong></p>
<table>
<thead>
<tr>
<th>order_id</th>
<th>customer_name</th>
<th>customer_email</th>
<th>product_name</th>
<th>product_price</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>Saurav</td>
<td><a href="mailto:saurav@mail.com">saurav@mail.com</a></td>
<td>Laptop</td>
<td>70000</td>
</tr>
<tr>
<td>2</td>
<td>Saurav</td>
<td><a href="mailto:saurav@mail.com">saurav@mail.com</a></td>
<td>Mouse</td>
<td>500</td>
</tr>
<tr>
<td>3</td>
<td>Rahul</td>
<td><a href="mailto:rahul@mail.com">rahul@mail.com</a></td>
<td>Laptop</td>
<td>70000</td>
</tr>
</tbody></table>
<p>Did you see any problem here ?</p>
<p>If not then god might save your company working you are in ;)</p>
<ol>
<li><p><strong>Data Duplication:</strong> "Saurav's name and email" &amp; "Laptop and its price" are repeated multiple times.</p>
</li>
<li><p><strong>Update Anomaly:</strong> If Saurav changes email → you must update every row Miss one → inconsistent data</p>
</li>
<li><p><strong>Insert Anomaly:</strong> Want to add a new customer without an order? → Not possible without inserting fake order data</p>
</li>
<li><p><strong>Delete Anomaly:</strong> If you delete all Saurav’s orders → you lose customer info entirely</p>
</li>
</ol>
<p>This is the problem normalization solves by separating the data to avoid this anomalies.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/ef543795-63d7-4129-8ee1-12093ee8ef59.png" alt="" style="display:block;margin:0 auto" />

<p>Now lets back to our main topic,</p>
<p>We have successfully splited the data but now the question is how we can retrieve the related data from different tables ?</p>
<p>This is where SQL Joins come in.</p>
<h3>What is a JOIN?</h3>
<p>A <strong>JOIN</strong> is used to combine rows from two or more tables based on a relationship between them, typically defined using keys (like primary key and foreign key).</p>
<p>Instead of storing all data in one place, we store it in separate tables and use joins to reconstruct the complete information when needed.</p>
<h3>Types of Joins</h3>
<p>Let’s understand joins using a simple example.</p>
<p>Imagine you are building a movie booking system.</p>
<p>You have two tables:</p>
<h3>1. <code>users</code></h3>
<pre><code class="language-sql">id | name
---------
1  | Saurav
2  | Amit
3  | Neha
</code></pre>
<h3>2. <code>bookings</code></h3>
<pre><code class="language-sql">id | user_id | movie
----------------------
1  |    1    | Dhurandhar
2  |    1    | Hera Pheri
3  |    2    | Dhamaal
</code></pre>
<p>Now suppose you want to answer:</p>
<blockquote>
<p>“Which user booked which movie?”</p>
</blockquote>
<p>This requires combining both tables.</p>
<h2>1. INNER JOIN</h2>
<h3>What it does</h3>
<p>Returns only the data where there is a match in both tables.</p>
<h3>Query</h3>
<pre><code class="language-sql">SELECT u.name, b.movie
FROM users u
INNER JOIN bookings b ON u.id = b.user_id;
</code></pre>
<h3>Result</h3>
<pre><code class="language-text">Saurav | Dhurandhar
Saurav | Hera Pheri
Amit   | Dhamaal
</code></pre>
<h3>Explanation</h3>
<ul>
<li><p>Neha is missing because she has no booking.</p>
</li>
<li><p>Only matching rows are returned.</p>
</li>
</ul>
<h3>Analogy</h3>
<p>Think of two lists:</p>
<ul>
<li><p>People who signed up</p>
</li>
<li><p>People who bought tickets</p>
</li>
</ul>
<p>INNER JOIN shows only people who did both.</p>
<hr />
<h2>2. LEFT JOIN</h2>
<h3>What it does</h3>
<p>Returns all rows from the left table, even if there is no match in the right table.</p>
<h3>Query</h3>
<pre><code class="language-sql">SELECT u.name, b.movie
FROM users u
LEFT JOIN bookings b ON u.id = b.user_id;
</code></pre>
<h3>Result</h3>
<pre><code class="language-text">Saurav | Dhurandhar
Saurav | Hera Pheri
Amit   | Dhamaal
Neha   | NULL
</code></pre>
<h3>Explanation</h3>
<ul>
<li><p>Neha appears, even though she didn’t book anything.</p>
</li>
<li><p>Her movie is NULL because no match exists.</p>
</li>
</ul>
<h3>Analogy</h3>
<p>You have a list of all users. You try to attach booking info if it exists. If not, you still keep the user.</p>
<hr />
<h2>3. RIGHT JOIN</h2>
<h3>What it does</h3>
<p>Returns all rows from the right table, even if there is no match in the left table.</p>
<h3>Query</h3>
<pre><code class="language-sql">SELECT u.name, b.movie
FROM users u
RIGHT JOIN bookings b ON u.id = b.user_id;
</code></pre>
<h3>Result</h3>
<pre><code class="language-text">Saurav | Dhurandhar
Saurav | Hera Pheri
Amit   | Dhamaal
</code></pre>
<h3>Explanation</h3>
<ul>
<li><p>All bookings are shown.</p>
</li>
<li><p>If a booking had no user (rare case), user would be NULL.</p>
</li>
</ul>
<h3>Analogy</h3>
<p>You care about all bookings. Users are attached if available.</p>
<h2>4. FULL JOIN</h2>
<h3>What it does</h3>
<p>Returns all rows from both tables, whether they match or not.</p>
<h3>Query</h3>
<pre><code class="language-sql">SELECT u.name, b.movie
FROM users u
FULL JOIN bookings b ON u.id = b.user_id;
</code></pre>
<h3>Result</h3>
<pre><code class="language-text">Saurav | Dhurandhar
Saurav | Heri Pheri
Amit   | Dhamaal
Neha   | NULL
</code></pre>
<p>(If there were bookings without users, those would also appear with NULL user.)</p>
<h3>Analogy</h3>
<p>Combine both lists completely:</p>
<ul>
<li><p>All users</p>
</li>
<li><p>All bookings</p>
</li>
</ul>
<p>Even if they don’t match.</p>
<h2>5. CROSS JOIN</h2>
<h3>What it does</h3>
<p>Returns every possible combination of rows from both tables.</p>
<h3>Query</h3>
<pre><code class="language-sql">SELECT u.name, b.movie
FROM users u
CROSS JOIN bookings b;
</code></pre>
<h3>Result</h3>
<pre><code class="language-text">Saurav | Dhurandhar
Saurav | Hera Pheri
Saurav | Dhamaal
Amit   | Dhurandhar
Amit   | Hera Pheri
Amit   | Dhamaal
Neha   | Dhurandhar
Neha   | Hera Pheri
Neha   | Dhamaal
</code></pre>
<h3>Explanation</h3>
<ul>
<li>Every user is paired with every booking.</li>
</ul>
<h3>Analogy</h3>
<p>Everyone is matched with every possible option.</p>
<h2>Summary</h2>
<table>
<thead>
<tr>
<th>Join Type</th>
<th>What it returns</th>
</tr>
</thead>
<tbody><tr>
<td>INNER JOIN</td>
<td>Only matching rows</td>
</tr>
<tr>
<td>LEFT JOIN</td>
<td>All left + matching right</td>
</tr>
<tr>
<td>RIGHT JOIN</td>
<td>All right + matching left</td>
</tr>
<tr>
<td>FULL JOIN</td>
<td>Everything from both sides</td>
</tr>
<tr>
<td>CROSS JOIN</td>
<td>All combinations</td>
</tr>
</tbody></table>
<h2>Final Thought</h2>
<p>Joins are not just about syntax. They reflect how you think about relationships:</p>
<ul>
<li><p>Do you want only matches? → INNER JOIN</p>
</li>
<li><p>Do you want everything from one side? → LEFT JOIN</p>
</li>
<li><p>Do you want everything from both? → FULL JOIN</p>
</li>
</ul>
<p>Understanding this clearly will help you write correct queries and design better systems.</p>
]]></content:encoded></item><item><title><![CDATA[All you need to know about SQL Queries]]></title><description><![CDATA[In this article, we will cover how to write SQL commands, along with a few tips and things to keep in mind while writing queries, and how to write…]]></description><link>https://srvjha.hashnode.dev/all-you-need-to-know-about-sql-queries</link><guid isPermaLink="true">https://srvjha.hashnode.dev/all-you-need-to-know-about-sql-queries</guid><category><![CDATA[Databases]]></category><category><![CDATA[sql Queries]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sun, 05 Apr 2026 09:40:47 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/5d6c85ca-1005-4729-a67e-a8dadbb97fcc.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we will cover how to write SQL commands, along with a few tips and things to keep in mind while writing queries, and how to write optimized queries.</p>
<p>SQL commands are categorized into different sublanguages such as DDL, DQL, and DML.</p>
<p>Let’s understand.</p>
<h2>DDL (Data Definition Language)</h2>
<p>Whenever we are talking about the <strong>structure of data</strong>, like creating, modifying, or deleting tables, we are dealing with DDL.</p>
<p>It is not about actual data inside the table, but about how the data is structured. Things like defining tables, altering columns, or deleting tables come under DDL.</p>
<p>Commands in DDL include:</p>
<ul>
<li><p><code>CREATE</code></p>
</li>
<li><p><code>ALTER</code></p>
</li>
<li><p><code>DROP</code></p>
</li>
<li><p><code>TRUNCATE</code></p>
</li>
</ul>
<p>So whenever you are defining or changing the structure of your database, you are using DDL.</p>
<h2>Building you first table</h2>
<p>Let’s create a table:</p>
<pre><code class="language-sql">CREATE TABLE matrimony (
  id SERIAL PRIMARY KEY,
  first_name VARCHAR(100) NOT NULL,
  last_name VARCHAR(100),
  email VARCHAR(322) UNIQUE NOT NULL,
  phone_number VARCHAR(10) UNIQUE,
  country_code VARCHAR(4),
  gender VARCHAR(20) CHECK (gender IN ('male','female','prefer not to say')),
  age INT CHECK(age &gt; 21),
  no_of_rejections INT DEFAULT 0 CHECK (no_of_rejections &gt;= 0),
  enrollment_date DATE DEFAULT CURRENT_DATE
);
</code></pre>
<h2>Understanding the Keywords</h2>
<ul>
<li><p><code>SERIAL</code> → auto-increment integer (handled internally using a sequence)</p>
</li>
<li><p><code>PRIMARY KEY</code> → uniquely identifies each row + creates index</p>
</li>
<li><p><code>VARCHAR(n)</code> → variable-length string (max n)</p>
</li>
<li><p><code>UNIQUE</code> → no duplicate values allowed</p>
</li>
<li><p><code>NOT NULL</code> → field must have a value</p>
</li>
<li><p><code>DEFAULT</code> → assigns value automatically if not provided</p>
</li>
<li><p><code>CHECK</code> → constraint to validate data</p>
</li>
</ul>
<h2>Important Insight (this is gold for interviews)</h2>
<blockquote>
<p>Always think in terms of <strong>bytes and disk I/O</strong>, not just data types.</p>
</blockquote>
<ul>
<li><p>Every column → consumes storage</p>
</li>
<li><p>More bytes → more disk reads → slower queries</p>
</li>
<li><p>Fixed types (INT) are faster for computation</p>
</li>
<li><p>Variable types (VARCHAR) are flexible .</p>
</li>
</ul>
<p>So choice is:</p>
<blockquote>
<p><strong>Correctness first, then optimization</strong></p>
</blockquote>
<h2>How <code>id SERIAL</code> auto-increments internally</h2>
<p>When you use:</p>
<pre><code class="language-sql">id SERIAL PRIMARY KEY
</code></pre>
<p>PostgreSQL actually does:</p>
<ol>
<li><p>Creates a hidden sequence <code>matrimony_id_seq</code></p>
</li>
<li><p>Sets default: <code>DEFAULT nextval('matrimony_id_seq')</code></p>
</li>
<li><p>On every insert: Database calls nextval() gets next number and assigns it to id</p>
</li>
</ol>
<p>So internally:</p>
<p>It’s not magic it’s a sequence generator.</p>
<h2>DDL Operations</h2>
<h3>ALTER (modify structure)</h3>
<p>Used when you want to change schema without deleting table.</p>
<p>Examples:</p>
<pre><code class="language-sql">ALTER TABLE matrimony ADD COLUMN city VARCHAR(50); // add

ALTER TABLE matrimony RENAME COLUMN first_name TO fname; // rename
</code></pre>
<h3>TRUNCATE vs DROP (very important difference)</h3>
<p><strong>TRUNCATE</strong></p>
<pre><code class="language-sql">TRUNCATE TABLE matrimony; 
</code></pre>
<ul>
<li><p>Removes all data</p>
</li>
<li><p>Keeps table structure</p>
</li>
<li><p>Very fast (no row-by-row delete)</p>
</li>
<li><p>Minimal logging</p>
</li>
</ul>
<blockquote>
<p>In Disk level it deallocates data pages quickly and doesn’t scan each row .</p>
</blockquote>
<p><strong>DROP</strong></p>
<pre><code class="language-sql">DROP TABLE matrimony; 
</code></pre>
<ul>
<li><p>Deletes entire table + structure</p>
</li>
<li><p>Everything is gone</p>
</li>
</ul>
<blockquote>
<p>In Disk level it removes metadata + data files and frees storage completely</p>
</blockquote>
<h3>When to use what</h3>
<ul>
<li><p>Use TRUNCATE → when you want empty table but keep structure</p>
</li>
<li><p>Use DROP → when you don’t need table at all</p>
</li>
<li><p>Use ALTER → when structure needs change Final Insight (this connects everything)</p>
</li>
</ul>
<p>All these operations ultimately affect disk layout:</p>
<p>CREATE → allocates structure on disk ALTER → modifies metadata + sometimes rewrites data TRUNCATE → clears data pages efficiently DROP → removes everything</p>
<p>So always think:</p>
<p>SQL is just syntax, but real work is happening on disk.</p>
<h2>DML (Data Manipulation Language)</h2>
<p>Whenever we are talking about the <strong>actual data inside the table</strong>, like inserting, updating, or deleting records, we are dealing with DML.</p>
<p>It is not about the structure, but about the data itself. So whenever you add new data, modify existing data, or remove data from a table, that comes under DML.</p>
<p>Commands in DML include:</p>
<ul>
<li><p><code>INSERT</code></p>
</li>
<li><p><code>UPDATE</code></p>
</li>
<li><p><code>DELETE</code></p>
</li>
</ul>
<pre><code class="language-sql">INSERT INTO matrimony (first_name, email, age)
VALUES ('Saurav', 'saurav@gmail.com', 25);
</code></pre>
<p>Here we are inserting actual data into the table.</p>
<pre><code class="language-sql">UPDATE matrimony
SET age = 26
WHERE email = 'saurav@gmail.com';
</code></pre>
<p>Here we are modifying existing data.</p>
<pre><code class="language-sql">DELETE FROM matrimony
WHERE email = 'saurav@gmail.com';
</code></pre>
<p>Here we are removing data from the table.</p>
<h3>Important insight</h3>
<p>DML operations are <strong>expensive compared to reads</strong> because they directly affect disk.</p>
<ul>
<li><p>INSERT → writes new data</p>
</li>
<li><p>UPDATE → reads + writes (can rewrite rows)</p>
</li>
<li><p>DELETE → marks data as deleted (later cleaned by database)</p>
</li>
</ul>
<p>So always be careful with:</p>
<ul>
<li><p>missing <code>WHERE</code> clause</p>
</li>
<li><p>large updates/deletes</p>
</li>
</ul>
<h2>DQL (Data Query Language)</h2>
<p>Whenever we are talking about reading or fetching data, we are dealing with DQL.</p>
<p>This is the most commonly used part of SQL because most of the time applications are reading data.</p>
<p>Command in DQL:</p>
<ul>
<li><code>SELECT</code></li>
</ul>
<h2>Think of this in your matrimony system</h2>
<p>Your table = users looking for matches<br />Each row = one person profile</p>
<p>Now SQL is how you <strong>query these profiles</strong>.</p>
<h2>Filtering (WHERE) — find specific profiles</h2>
<pre><code class="language-sql">SELECT * FROM matrimony
WHERE age &gt; 21 AND gender = 'female';
</code></pre>
<p>Meaning:</p>
<p>You are saying:</p>
<blockquote>
<p>“Give me profiles where age is greater than 21 and gender is female”</p>
</blockquote>
<p>Without <code>WHERE</code>, database will scan all profiles → more disk reads.</p>
<h2>Pagination (LIMIT &amp; OFFSET)</h2>
<pre><code class="language-sql">SELECT * FROM matrimony
LIMIT 10 OFFSET 20;
</code></pre>
<h3>What it means:</h3>
<ul>
<li><p><code>LIMIT 10</code> → give me 10 profiles</p>
</li>
<li><p><code>OFFSET 20</code> → skip first 20 profiles</p>
</li>
</ul>
<p>Imagine users are scrolling profiles:</p>
<ul>
<li><p>Page 1 → first 10 profiles</p>
<pre><code class="language-sql">LIMIT 10 OFFSET 0
</code></pre>
</li>
<li><p>Page 2 → next 10 profiles</p>
<pre><code class="language-sql">LIMIT 10 OFFSET 10
</code></pre>
</li>
</ul>
<p><strong>OFFSET = how many profiles to skip</strong></p>
<h2>Aggregation (GROUP BY)</h2>
<pre><code class="language-sql">SELECT gender, COUNT(*)
FROM matrimony
GROUP BY gender;
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>“How many male, female, and others are there?”</p>
</blockquote>
<p>Database groups profiles and then counts.</p>
<h3>More examples</h3>
<pre><code class="language-sql">SELECT AVG(age) FROM matrimony;
</code></pre>
<p>→ average age of users</p>
<pre><code class="language-sql">SELECT MAX(age) FROM matrimony;
</code></pre>
<p>→ oldest user</p>
<h2>HAVING — filter after grouping</h2>
<pre><code class="language-sql">SELECT gender, COUNT(*)
FROM matrimony
GROUP BY gender
HAVING COUNT(*) &gt; 10;
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>“Show only those gender groups where count is more than 10”</p>
</blockquote>
<h3>Simple difference</h3>
<ul>
<li><p><code>WHERE</code> → filters rows</p>
</li>
<li><p><code>HAVING</code> → filters grouped results</p>
</li>
</ul>
<h2>Pattern Matching (LIKE)</h2>
<pre><code class="language-sql">SELECT * FROM matrimony
WHERE first_name LIKE 'Sa%';
</code></pre>
<p>Meaning:</p>
<blockquote>
<p>“Find profiles where name starts with ‘Sa’”</p>
</blockquote>
<h3>Examples</h3>
<ul>
<li><p><code>'S%'</code> → starts with S</p>
</li>
<li><p><code>'%v'</code> → ends with v</p>
</li>
<li><p><code>'%au%'</code> → contains "au"</p>
</li>
<li><p><code>'__u%'</code> -&gt; third letter "u"</p>
</li>
</ul>
<h3>Tips and Tricks (Important for Writing Better SQL)</h3>
<h3>1. Using Alias (creating columns on the fly)</h3>
<pre><code class="language-sql">SELECT first_name, age, (age + 5) AS future_age
FROM matrimony;
</code></pre>
<ul>
<li><p><code>(age + 5)</code> is computed on the fly</p>
</li>
<li><p><code>AS future_age</code> creates a temporary column in the result</p>
</li>
</ul>
<h3>2. Alias for Table</h3>
<pre><code class="language-sql">SELECT m.first_name, m.age
FROM matrimony m;
</code></pre>
<ul>
<li><p>Makes queries shorter and cleaner</p>
</li>
<li><p>Very useful in joins</p>
</li>
</ul>
<h3>3. Single Quote vs Double Quote</h3>
<ul>
<li><p><code>' '</code> → string/value</p>
</li>
<li><p><code>" "</code> → column/table name (identifier, especially in PostgreSQL)</p>
</li>
</ul>
<pre><code class="language-sql">SELECT * FROM matrimony
WHERE first_name = 'Saurav';
</code></pre>
<pre><code class="language-sql">SELECT "first_name" FROM matrimony;
</code></pre>
<h3>4. Avoid SELECT *</h3>
<pre><code class="language-sql">SELECT * FROM matrimony;
</code></pre>
<p>Better:</p>
<pre><code class="language-sql">SELECT first_name, age FROM matrimony;
</code></pre>
<ul>
<li><p>Reduces disk reads</p>
</li>
<li><p>Improves performance</p>
</li>
</ul>
<h3>5. Always Use WHERE Carefully</h3>
<pre><code class="language-sql">UPDATE matrimony SET age = 30;
</code></pre>
<p>This updates the entire table ❌</p>
<pre><code class="language-sql">UPDATE matrimony
SET age = 30
WHERE id = 1;
</code></pre>
<ul>
<li>Missing WHERE = dangerous</li>
</ul>
<h3>6. Indexing</h3>
<pre><code class="language-sql">CREATE INDEX idx_email ON matrimony(email);
</code></pre>
<ul>
<li><p>Without index → full table scan</p>
</li>
<li><p>With index → faster lookup</p>
</li>
</ul>
<h3>7. Use LIMIT While Testing</h3>
<pre><code class="language-sql">SELECT * FROM matrimony LIMIT 10;
</code></pre>
<ul>
<li>Prevents loading large data</li>
</ul>
<h3>8. Filter Early</h3>
<pre><code class="language-sql">SELECT * FROM matrimony WHERE age &gt; 21;
</code></pre>
<ul>
<li>Reduces unnecessary data scan</li>
</ul>
<h3>9. Be Careful with LIKE</h3>
<pre><code class="language-sql">SELECT * FROM matrimony
WHERE first_name LIKE '%au%';
</code></pre>
<ul>
<li><code>%</code> at the start → cannot use index → slow</li>
</ul>
<p>Better:</p>
<pre><code class="language-sql">SELECT * FROM matrimony
WHERE first_name LIKE 'Sa%';
</code></pre>
<h3>10. COUNT Can Be Expensive</h3>
<pre><code class="language-sql">SELECT COUNT(*) FROM matrimony;
</code></pre>
<ul>
<li>On large tables, scans data → can be slow</li>
</ul>
<h2>Final Thoughts</h2>
<ul>
<li><p>SQL is not just about writing queries, it’s about understanding how data flows and how efficiently you interact with it.</p>
</li>
<li><p>The better you think in terms of data and disk, the better your queries and systems will perform.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Your Data Is Not Actually Stored in a Database]]></title><description><![CDATA[Understanding the layer between your app and disk.]]></description><link>https://srvjha.hashnode.dev/your-data-is-not-actually-stored-in-a-database</link><guid isPermaLink="true">https://srvjha.hashnode.dev/your-data-is-not-actually-stored-in-a-database</guid><category><![CDATA[Databases]]></category><category><![CDATA[SQL]]></category><category><![CDATA[NoSQL]]></category><category><![CDATA[server]]></category><category><![CDATA[disk]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sun, 05 Apr 2026 08:06:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/d02ff618-e6aa-4058-a75b-007214219f50.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>While you may wonder why I wrote this title, but you will get the point once you read the whole article. So whenever we talk about databases, one thing that comes to our mind is that to store data we need some kind of database, but it's not actually correct. I will prove that in this article.</p>
<h2>Where does the data go actually?</h2>
<p>Actually all the mutation (write) or query (read) happens on your hard disk or SSD. And this operation is one of the most critical operations for data handling. Almost all your optimizations revolve around this, because disk I/O is expensive compared to memory operations.</p>
<h2>Why do we need a database?</h2>
<p>You might be thinking, can't a server directly write or read from disk I/O? Technically yes, but the key concept here is that communicating with disk requires handling very low-level operations, as disk ultimately understands data in binary form (0s and 1s). While it is possible to do this, it becomes very complex because you have to manage caching, handle efficient data access patterns, ensure consistency, and write highly optimized low-level code, which is very hard and error-prone. So instead, we use a software which acts as a middle layer known as a database.</p>
<h2>What is database ?</h2>
<p>It is a software which supports an easy language to communicate with disk and also provides features like indexing, caching, and other optimization techniques to efficiently write and read data. So the main role of a database is to manage how data is stored and accessed, while the actual data is always persisted on disk.</p>
<blockquote>
<p>Key Insight: A database does not directly control the disk hardware, it works through the operating system which actually performs the disk operations. The database decides how and when data should be written or read, applies optimizations like buffering and logging, and structures the data in a way that makes access efficient. So even though the data is stored on disk, the intelligence of how it is handled comes from the database.</p>
</blockquote>
<p>Now let's understand what are the ways to query data, means the language the database uses to query data.</p>
<h3>Type of Databases</h3>
<p>Two of the most broadly used databases are SQL and NoSQL, so we often get confused about why they exist, which problem they solve, and why both are required in the first place.</p>
<blockquote>
<p>There are more databases like vector DB, object-oriented DB, etc., but for this article we will explore SQL and NoSQL because they are the most commonly used ones and are generally used as primary databases. Almost every system needs either SQL or NoSQL as a core data store. The rest of the databases are usually use-case specific they solve a particular part of a problem rather than acting as the primary system of record.</p>
</blockquote>
<p>So let’s understand this at a high level with an analogy.</p>
<p>I guess you are aware of <strong>JavaScript and TypeScript</strong>, so whenever these terms come to mind, what is the key difference between them?</p>
<p>JavaScript is <strong>loosely</strong> typed. What I mean is you can store different kinds of values without enforcing a strict structure. For example:</p>
<pre><code class="language-plaintext">const yourFuture = { income: "1LPA", maritalStatus: "single" }
yourFuture.status = "Dark"
</code></pre>
<p>So here pause for a second and think will the object allow the <code>status</code> to be added? The answer is yes. That means there is no strict structure enforced at compile time. It is flexible and allows changes easily.</p>
<p>NoSQL is just like this. It allows flexible data models where the structure can evolve over time. You are not forced to strictly define the schema upfront, which makes it useful when your data shape is not fixed or keeps changing. However, this flexibility comes with trade-offs, and ensuring consistency and correctness often becomes the responsibility of the application.</p>
<p>Now what is TypeScript? It’s a <strong>superset of JavaScript</strong>, meaning JavaScript with types. Here everything follows a defined structure, and you cannot freely add properties that are not part of that structure.</p>
<pre><code class="language-plaintext">type Future = {
  income: string
  maritalStatus: string
}

const yourFuture: Future = {
  income: "1LPA",
  maritalStatus: "single"
}

yourFuture.status = "Dark" // Error
</code></pre>
<p>In this case, it will not work because <code>yourFuture</code> is strictly defined to only have <code>income</code> and <code>maritalStatus</code>. You cannot add anything outside of that structure. This enforces consistency.</p>
<p>This is similar to <strong>SQL databases like PostgreSQL</strong>, where the schema is defined in advance, relationships are structured, and constraints help maintain data integrity. SQL systems are designed to provide strong guarantees around correctness, consistency, and reliability of data.</p>
<p>On the other hand, <strong>NoSQL databases like MongoDB</strong> are designed with flexibility and scalability in mind. They allow dynamic schemas and are well-suited for handling large volumes of data or rapidly evolving requirements.</p>
<p><mark class="bg-yellow-200 dark:bg-yellow-500/30">One important thing to understand here is that this is not about one being better than the other. It is about trade-offs. SQL systems prioritize structure and consistency, while NoSQL systems prioritize flexibility and ease of scaling. Modern databases often incorporate features from both worlds, but the core ideas still remain.</mark></p>
<p>Now let’s understand both of them with real world usecase.</p>
<h2>SQL</h2>
<p>For applications like banking or stock markets, the most important thing is correct handling of data. They cannot allow data to be unstructured or inconsistent. They need strictness, because even a small mistake can lead to serious business loses.</p>
<p>These databases are usually very large, so modifying the structure frequently is not a good idea. Also, there are complex relationships between data, and they often require joins across multiple tables, so having a well-defined structure becomes very important.</p>
<p>So <strong>SQL (Structured Query Language)</strong> is designed for this kind of use case. It enforces a predefined schema, meaning the structure of data is decided before storing it. This helps maintain consistency across the system.</p>
<p>SQL databases like PostgreSQL follow <strong>ACID principles</strong>, which are very important for reliability:</p>
<ul>
<li><p>Atomicity means a transaction either fully completes or fully fails, there is no partial state.</p>
</li>
<li><p>Consistency ensures that data always remains valid according to defined rules and constraints.</p>
</li>
<li><p>Isolation means multiple transactions can run without affecting each other incorrectly.</p>
</li>
<li><p>Durability ensures that once data is written, it will not be lost even in case of failures.</p>
</li>
</ul>
<p>This makes SQL databases suitable for systems where correctness is more important than flexibility.</p>
<h2>NoSQL</h2>
<p>For applications like social media, real-time analytics, content platforms, or systems handling large-scale user data(Discord), the requirement is different. Here, <strong>the data is not always strictly structured</strong>, and it keeps evolving over time. In such cases, strict schemas can slow down development and make changes harder.</p>
<p>These systems also deal with a huge amount of data and traffic, so scaling becomes very important. They need databases that can <strong>handle high read/write throughput</strong> and can scale horizontally across multiple machines easily.</p>
<p>So NoSQL databases are designed for this kind of use case. Instead of enforcing a fixed schema, they allow flexible data models, where each record can have a slightly different structure. This makes it easier to evolve the system without constantly changing the database schema.</p>
<p>NoSQL databases like MongoDB do not strictly follow the same ACID guarantees as traditional SQL systems. Instead, many of them follow a model closer to <strong>eventual consistency,</strong> where data may not be immediately consistent across all nodes but will become consistent over time. This trade-off helps in achieving better performance and scalability.</p>
<p>Unlike SQL, where relationships are handled using joins, NoSQL databases often avoid complex joins. Instead, data is usually denormalized, meaning related data is stored together to reduce the need for multiple queries. This improves read performance but can introduce some duplication.</p>
<p><strong>One important thing to understand is that flexibility does not mean no rules. You still need to design your data carefully, but the responsibility of maintaining structure and consistency often shifts more towards the application rather than the database.</strong></p>
<h2>Summary</h2>
<ul>
<li><p>Data is ultimately stored on disk, and all reads/writes are disk I/O operations, which are the main performance bottleneck.</p>
</li>
<li><p>A database is a software layer that manages how data is stored and accessed, handling complexity like caching, indexing, and consistency.</p>
</li>
<li><p>SQL and NoSQL exist because of different trade-offs, not because one is better than the other.</p>
</li>
<li><p>SQL databases like PostgreSQL focus on structure, consistency, and strong guarantees (ACID).</p>
</li>
<li><p>NoSQL databases like MongoDB focus on flexibility, scalability, and handling large, evolving data.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[System Design : Consistent Hashing]]></title><description><![CDATA[System Design: Consistent Hashing]]></description><link>https://srvjha.hashnode.dev/system-design-consistent-hashing</link><guid isPermaLink="true">https://srvjha.hashnode.dev/system-design-consistent-hashing</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[ChaiCohort]]></category><category><![CDATA[System Design]]></category><category><![CDATA[consistent hashing]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Mon, 23 Mar 2026 13:37:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/e04a999c-90c4-46c3-917f-3117c9faa148.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>Why do we need Consistent Hashing ?</h2>
<p>Imagine you are building a bookMyShow system which is used to book tickets. Initially what would be your system?</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/8fb6b37b-8756-4bc0-81da-30cdbed3eea1.png" alt="" style="display:block;margin:0 auto" />

<p>All user requests (search, booking, payments) go through the server and hit a single database.</p>
<p>Now as your system scales, the single database becomes a bottleneck.</p>
<ul>
<li><p><strong>Writes</strong>: All booking operations (seat locking, payments) go to one database → means server overwhelms, disk I/O limits, locking issues.</p>
</li>
<li><p><strong>Reads</strong>: High traffic for browsing movies, checking availability → even reads can overwhelm a single node.</p>
</li>
</ul>
<p>Even though reads can be improved using caching or replicas, eventually a single database cannot handle the load.</p>
<p>To solve this, we do <strong>horizontal scaling</strong>. Instead of one database, we now have multiple database servers.</p>
<p>Now data is distributed across these databases instead of storing everything in one place.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/7b78fe03-6970-4418-9425-2e85779e8bc0.png" alt="" style="display:block;margin:0 auto" />

<h3>Hashing</h3>
<p>Now the important question is:</p>
<blockquote>
<p>How do we decide which data goes to which database?</p>
</blockquote>
<p>There should be a mechanism to determine where a specific piece of data is stored so that we can fetch it correctly. For this, we use a shard key (eg: <code>userid</code>) and apply a hash function on it to determine the index of the server.</p>
<pre><code class="language-javascript">serverIndex = hash(key) % N, where N is the size of the server pool.
</code></pre>
<p>This gives us a <strong>deterministic mapping</strong>, meaning the same key will always go to the same server.</p>
<p>So this architecture looks good and works well for distributing load across multiple servers.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/05c2c32e-61cf-4096-b0a2-91305ba12fc6.png" alt="" style="display:block;margin:0 auto" />

<h3>The rehasing problem</h3>
<p><em>This approach works well when the size of servers is fixed and data is distributed evenly.</em></p>
<p>But there is some problems if size is not fixed…</p>
<p>As the scale grew more we added an another shard to distribute the load further now our hash function will give the same hash value for the key but now our <strong>number of servers udpated to 4</strong> so now modulo values will be changed.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/05a9a606-0d26-490e-9540-e1479cdbba9b.png" alt="" style="display:block;margin:0 auto" />

<p>This means that the mapping of keys to servers changes.</p>
<p>From the above figure, we can see that:</p>
<ul>
<li><p>Most of the keys now point to different servers</p>
</li>
<li><p>Only a few keys remain on the same shard</p>
</li>
</ul>
<p>In other words:</p>
<blockquote>
<p>A small change in the number of servers causes a large portion of the data to be remapped.</p>
</blockquote>
<h3>Why is this a problem?</h3>
<p>To reflect this new mapping, we now need to:</p>
<ul>
<li><p>Move large amounts of data across servers</p>
</li>
<li><p>Rebalance the entire system</p>
</li>
<li><p>Update routing logic</p>
</li>
</ul>
<p>This leads to:</p>
<ul>
<li><p>High network overhead</p>
</li>
<li><p>Increased computation</p>
</li>
<li><p>Possible downtime or performance degradation</p>
</li>
</ul>
<p>So this hashing is not consistent thats why the concept of consistent hashing came into picture.</p>
<h2>What is Consistent Hashing ?</h2>
<p>Its a technique used in distributed systems where data is distributed across multiple servers efficiently. With this approach the data movement is minimized only subsets of keys needs to be remapped.</p>
<h2>How Consistent Hashing works ?</h2>
<h3>Hash Servers</h3>
<p>Arrangement of servers is in circular space called hash ring, the server is mapped onto the ring via server name or ip , let's understand with an example how the server is mapped in the ring.</p>
<ul>
<li><p>Create Hash Ring with fixed points(servers) for example lets take 1000</p>
</li>
<li><p>Now we place 3 servers in our ring using function on server ip and suppose this is point where they will mapped (300,500,900)</p>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/9a1dbed5-fb1d-4595-bb21-d7e9abd991ec.png" alt="" style="display:block;margin:0 auto" />

<h3>Hash Keys</h3>
<p>Place the keys by using same function but here we will not do the modulo will directly place the hash value of keys in the hash ring.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/227a7471-1b44-457e-8882-221112fbf557.png" alt="" style="display:block;margin:0 auto" />

<h3><strong>Server lookup</strong></h3>
<p>Now to determine which server the key is stored on we move in clockwise direction so k0(key0) is stored in server1 and this is how the server lookup is done</p>
<h3>Adding and removing server</h3>
<p>When we add new node (server 4) and it is placed somewhere on the ring, Only the keys between the new server and its previous neighbor need to move. All other keys remain untouched. Similarly, when a server is removed only the keys handled by that server are reassigned</p>
<blockquote>
<p>This ensures minimal data movement compared to modulo hashing.</p>
</blockquote>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/0096b446-d8b9-4947-a941-ec8613bdacd5.png" alt="" style="display:block;margin:0 auto" />

<p>So far so good we successfully minimized the data movement. Now there is some bottlenecks in this architecture as well</p>
<p>Can you identify any problem from the below diagram</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/0c617eb6-6d79-4ae0-9270-685171665f37.png" alt="" style="display:block;margin:0 auto" />

<h3>Problem with Basic Consistent Hashing</h3>
<p>If not then let's discuss from the diagram you can see all keys(k0,k1 and k2) data are stored in server 1 and server 2, server 3 are empty and untouched so here hotspot problem will arise all load will go to the server 1 because following clockwise rotation that is the first server for all keys so the problem arises with this approach is</p>
<ul>
<li><p>non-uniform key distribution on the ring</p>
</li>
<li><p>it is impossible to keep the same size of partitions on the ring for all servers considering a server can be added or removed</p>
</li>
</ul>
<h3>How do we solve this problem ?</h3>
<h3>Virtual Nodes</h3>
<p>What we gonna do is we till take suppose 3 random sparse points from hash ring and we will map the server addresses on the hash ring <strong>keep in mind that</strong> no server is added just their address will be added there so that when any key moves clockwise if they found any server address they will be stored to that particular server see below diagram for better understanding</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/b80a7d8e-b6e9-4e09-994a-4a0b47245579.png" alt="" style="display:block;margin:0 auto" />

<h2>Summary</h2>
<ul>
<li><p>Single database becomes a bottleneck as system scales (reads + writes)</p>
</li>
<li><p>Horizontal scaling introduces multiple database shards</p>
</li>
<li><p>Modulo hashing (<code>hash(key) % N</code>) distributes data across shards</p>
</li>
<li><p>Works well only when number of servers is fixed</p>
</li>
<li><p>Adding/removing servers causes most keys to be remapped</p>
</li>
<li><p>Leads to heavy data movement, overhead, and instability</p>
</li>
<li><p>Consistent hashing maps servers and keys on a hash ring</p>
</li>
<li><p>Only a small subset of keys move when servers change</p>
</li>
<li><p>Improves scalability and stability</p>
</li>
<li><p>Virtual nodes ensure better load distribution and reduce hotspots</p>
</li>
</ul>
<h3>References</h3>
<ul>
<li><p><a href="https://bytebytego.com/courses/system-design-interview/design-consistent-hashing">https://bytebytego.com/courses/system-design-interview/design-consistent-hashing</a></p>
</li>
<li><p><a href="https://www.hellointerview.com/learn/system-design/core-concepts/consistent-hashing">https://www.hellointerview.com/learn/system-design/core-concepts/consistent-hashing</a></p>
</li>
<li><p><a href="https://docs.aws.amazon.com/wellarchitected/latest/reducing-scope-of-impact-with-cell-based-architecture/consistent-hashing.html">https://docs.aws.amazon.com/wellarchitected/latest/reducing-scope-of-impact-with-cell-based-architecture/consistent-hashing.html</a></p>
</li>
<li><p><a href="https://highscalability.com/consistent-hashing-algorithm/">https://highscalability.com/consistent-hashing-algorithm/</a></p>
</li>
<li><p><a href="https://arpitbhayani.me/blogs/consistent-hashing/">https://arpitbhayani.me/blogs/consistent-hashing/</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[System Design: Understanding Kafka Architecture]]></title><description><![CDATA[Learn how Kafka ensures ordered event streaming while enabling parallel processing using topics, partitions, producers, and consumer groups.]]></description><link>https://srvjha.hashnode.dev/system-design-understanding-kafka-architecture</link><guid isPermaLink="true">https://srvjha.hashnode.dev/system-design-understanding-kafka-architecture</guid><category><![CDATA[System Design]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[kafka]]></category><category><![CDATA[queue]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Mon, 16 Mar 2026 09:55:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/cc76567f-78ad-4f79-a71d-4b76da9c70b1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Suppose you are watching a cricket match <strong>IND vs AUS, World Cup Final 2023 :(</strong> . While checking the live score, you expect updates to appear in the correct sequence.</p>
<p>For example:</p>
<pre><code class="language-javascript">After 2 overs  → IND 10/0
After 5 overs  → IND 45/0
After 8 overs  → IND 50/2
</code></pre>
<p>Now imagine if the updates appeared like this instead:</p>
<pre><code class="language-javascript">After 5 overs → IND 45/0
After 2 overs → IND 10/0
After 8 overs → IND 50/2
</code></pre>
<p>This would mean the <strong>events(live scores) are arriving out of order</strong>, which creates confusion for users.</p>
<p>So now think that millions of people are watching the match live on platforms like Cricbuzz. The score updates are processed by <strong>multiple servers running in a distributed system</strong>. Despite this complexity, the score almost always appears in the correct order.</p>
<p><strong>Simple Analogy to understand multiple servers running in a distributed system :</strong></p>
<p>Imagine a restaurant where thousands of customers arrive at once.<br />If only <strong>one waiter</strong> handled all the orders, everything would become slow and chaotic.</p>
<p>Instead, the restaurant uses <strong>many waiters working together</strong> so orders can be processed faster.</p>
<p>Similarly, apps like Cricbuzz use <strong>many computers working together</strong> to handle millions of live score requests.</p>
<p>So the question is:</p>
<p><strong>How does Cricbuzz ensure that live score updates always appear in the correct order?</strong></p>
<p>This is where tools like <strong>Apache Kafka</strong> come in.</p>
<h2>What is Kafka?</h2>
<p>Kafka is a <strong>distributed event streaming platform</strong> designed to handle large amounts of real-time data.</p>
<p>In simple terms, it acts like a <strong>central pipeline for events</strong>.</p>
<p>Whenever something happens in a system, for example:</p>
<ul>
<li><p>a cricket ball is bowled</p>
</li>
<li><p>a run is scored</p>
</li>
<li><p>a wicket falls</p>
</li>
</ul>
<p>that event is sent to Kafka.</p>
<p>Kafka then:</p>
<ol>
<li><p><strong>stores the events in order</strong></p>
</li>
<li><p><strong>processes them very quickly</strong></p>
</li>
<li><p><strong>delivers them to multiple services that need the data</strong></p>
</li>
</ol>
<p>This allows platforms like Cricbuzz to process <strong>millions of live score updates in real time while keeping the sequence correct</strong>.</p>
<h2>How kafka works under the hood ?</h2>
<p>Its mainly consists of three important parts:</p>
<h3>1. Topics</h3>
<p>A <strong>topic</strong> is simply a name for a type of event.</p>
<p>For example, in a cricket scoring system we might have a topic like <code>score_update</code></p>
<p>Every time something happens in the match a run, a wicket, message related to the score is sent to this topic.</p>
<p>You can think of a topic like a <strong>timeline</strong> where related events are recorded.</p>
<h3>2. Producers</h3>
<p>A <strong>producer</strong> is the system that <strong>sends messages to Kafka</strong>.</p>
<p>In our example, the scoring system that records each ball could act as a producer.</p>
<p>Whenever a ball is completed, it sends a message like this:</p>
<pre><code class="language-javascript">Message {
  topic: "score_update",
  body: {
    over: 5.3,
    runs: 4,
    batsman: "Rohit"
  }
}
</code></pre>
<p>Kafka receives this message and stores it inside the <code>score_update</code> topic.</p>
<h3>3. Partitions (for scalability)</h3>
<p>Now imagine millions of people checking the score at the same time.</p>
<p>One machine processing all the messages would become slow.</p>
<p>So Kafka <strong>splits a topic into multiple partitions</strong>.</p>
<p>Each partition can be processed by a different machine, which allows the system to handle a large amount of data efficiently.</p>
<p>Example:</p>
<p><strong>topic</strong> : <code>score_update</code></p>
<p><strong>[ partition 0 , partition 1, partition 2]</strong></p>
<p>Messages are distributed across these partitions.</p>
<p>Kafka often uses a partition key (such as match_id) so that all events for the same match go to the same partition. This ensures that ball-by-ball updates remain in order.</p>
<blockquote>
<p>Partition = ordered sequence of messages</p>
</blockquote>
<h3>4. Consumers</h3>
<p>A <strong>consumer</strong> is a system that <strong>reads events from Kafka</strong>.</p>
<p>For example:</p>
<ul>
<li><p>a service that updates the live score API</p>
</li>
<li><p>a service that sends notifications</p>
</li>
<li><p>a service that calculates match statistics</p>
</li>
</ul>
<p>Consumers subscribe to a topic to read its events.</p>
<p>When they connect to Kafka, they also provide a <strong>consumer group id</strong>.</p>
<p>Example:</p>
<p><strong>consumerGroupId</strong>: "SEND_LIVE_SCORE"<br /><strong>topic</strong>: "score_update"</p>
<p>The consumer group tells Kafka that these consumers belong to the <strong>same processing group</strong>.</p>
<p>Kafka then distributes the partitions among the consumers in that group so that the work is shared.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/d9167cbc-9e60-4ff5-8926-58b4ba6b3098.png" alt="" style="display:block;margin:0 auto" />

<h3>Key Takeaways</h3>
<ul>
<li><p>Every message is available to all consumer groups.</p>
</li>
<li><p>Inside each consumer group, partitions are assigned to consumers so that only one consumer processes messages from a partition.</p>
</li>
</ul>
<p>This is why <strong>Kafka</strong> is used in systems where maintaining <strong>event order</strong> while enabling <strong>parallel processing across multiple machines</strong> is critical.</p>
]]></content:encoded></item><item><title><![CDATA[System Design : Message Queue (RabbitMQ)]]></title><description><![CDATA[System Design: Message Queues]]></description><link>https://srvjha.hashnode.dev/system-design-message-queue-rabbitmq</link><guid isPermaLink="true">https://srvjha.hashnode.dev/system-design-message-queue-rabbitmq</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[System Design]]></category><category><![CDATA[queue]]></category><category><![CDATA[message queue]]></category><category><![CDATA[rabbitmq]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Mon, 09 Mar 2026 14:57:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/750df4e5-8400-47f7-8e06-014bcd8dda60.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is a Queue?</h2>
<p>A queue is a fundamental concept in software engineering used to manage tasks or messages that need to be processed in a specific order <strong>(FIFO)</strong>. It provides a mechanism for decoupling the components of a system, allowing them to operate independently and asynchronously.</p>
<p><strong>Analogy</strong> : Imagine a line of customers waiting to order at a McDonald's counter.</p>
<p>When customers arrive, they join the line at the back. The customer who has been waiting the longest gets served first, while new customers must wait their turn. No one can skip ahead in the line.</p>
<p>This is exactly how a <strong>queue works in software</strong>. Tasks enter the queue one by one, and the system processes them in the same order they arrived ensuring fairness and predictable execution.</p>
<h2>Why Message Queues Exist ?</h2>
<p>Let's imagine how youtube system design would look like if there was no message queue means they needed to perform synchronous operation</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/f47b9789-ff58-4e09-ade0-4db573a12c9c.png" alt="" style="display:block;margin:0 auto" />

<p>Each service waits for the next one to finish before returning a response.</p>
<p>That means the <strong>original client request stays open the entire time</strong> until the whole pipeline finishes.</p>
<h3>Problems with Synchronous Processing</h3>
<ul>
<li><p><strong>The Slow Response :</strong> Video processing is heavy. Converting formats and compressing video can take seconds or even minutes depending on the file size.</p>
</li>
<li><p><strong>Tight Coupling Between Services</strong> : In a synchronous pipeline, every service depends directly on the next one.If <strong>one service goes down</strong>, the entire chain breaks.</p>
</li>
<li><p><strong>Cascading Failures</strong>: Synchronous chains can cause <strong>cascading failures</strong>.</p>
</li>
</ul>
<p>Imagine this situation:</p>
<ul>
<li><p>Compress service becomes slow</p>
</li>
<li><p>Format service starts waiting longer</p>
</li>
<li><p>Upload service keeps connections open</p>
</li>
<li><p>Threads start piling up</p>
</li>
<li><p>Eventually the whole system becomes overloaded</p>
</li>
</ul>
<p>One slow service can cause the <strong>entire pipeline to degrade</strong>.</p>
<p>This is a common failure pattern in distributed systems.</p>
<ul>
<li><strong>Resource Exhaustion:</strong></li>
</ul>
<p>Because requests stay open for a long time:</p>
<ul>
<li><p>API threads stay occupied</p>
</li>
<li><p>HTTP connections remain open</p>
</li>
<li><p>Memory usage increases</p>
</li>
</ul>
<p>If thousands of users upload videos simultaneously, the server could easily run out of resources.</p>
<p>Now you know the problem lets understand how message queue helps make the processing asynchronous.</p>
<h2>What is a Message Queue?</h2>
<p>A message queue is a specific type of queue used in distributed systems for inter-process communication. It acts as an intermediary between different services or applications, allowing them to send and receive messages without direct knowledge of each other.</p>
<h3>Working Principle of Message Queues:</h3>
<ul>
<li><p><strong>Producer/Publisher:</strong> A component that creates and sends messages to the message queue.</p>
</li>
<li><p><strong>Consumer/Subscriber:</strong> A component that retrieves and processes messages from the message queue.</p>
</li>
<li><p><strong>Decoupling:</strong> Message queues decouple producers and consumers, meaning they don't need to be available at the same time or know each other's direct addresses. This enhances system resilience and scalability.</p>
</li>
<li><p><strong>Asynchronous Processing:</strong> Messages are processed asynchronously, allowing the producer to continue its work without waiting for the consumer to complete the message processing.</p>
</li>
<li><p><strong>Buffering:</strong> Message queues act as a buffer, storing messages when consumers are busy or offline, and delivering them when they become available. This prevents system overload and data loss.</p>
</li>
</ul>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/8fd2c3ab-a9cc-4d3f-94a7-424b7980277d.png" alt="" style="display:block;margin:0 auto" />

<h2>How RabbitMQ Works ?</h2>
<p>RabbitMQ is an open-source, distributed message queue written in Erlang that supports various communication protocols, primarily the Advanced Message Queuing Protocol (AMQP). It was designed to solve the "spaghetti mesh" architecture problem where every client directly talks to every other client.</p>
<h3>Core Components of RabbitMQ:</h3>
<ol>
<li><p><strong>RabbitMQ Server (Broker):</strong></p>
<ul>
<li><p>This is the central component that receives messages from producers and delivers them to consumers.</p>
</li>
<li><p>It listens on port 5672 by default.</p>
</li>
<li><p>It uses a two-way, bi-directional TCP connection for communication with clients, leveraging the AMQP protocol.</p>
</li>
<li><p>The server <em>pushes</em> messages to consumers when they are ready.</p>
</li>
</ul>
</li>
<li><p><strong>Publisher (Producer):</strong></p>
<ul>
<li><p>An application that sends messages to the RabbitMQ server.</p>
</li>
<li><p>It establishes a stateful, two-way TCP connection with the server.</p>
</li>
<li><p>Publishers declare the queue they intend to publish to (even if RabbitMQ internally routes it via an exchange).</p>
</li>
</ul>
</li>
<li><p><strong>Consumer (Subscriber):</strong></p>
<ul>
<li><p>An application that receives and processes messages from the RabbitMQ server.</p>
</li>
<li><p>It also establishes a stateful, two-way TCP connection with the server.</p>
</li>
<li><p>Consumers express interest in a specific queue and the server pushes messages to them.</p>
</li>
</ul>
</li>
<li><p><strong>AMQP (Advanced Message Queuing Protocol):</strong></p>
<ul>
<li><p>The primary protocol used by RabbitMQ for communication.</p>
</li>
<li><p>It defines the format of messages and the rules for their exchange between the server and clients.</p>
</li>
<li><p>AMQP connections are stateful and bi-directional.</p>
</li>
</ul>
</li>
<li><p><strong>Channels:</strong></p>
<ul>
<li><p>An abstraction on top of a single TCP connection. Multiple channels can be opened over a single TCP connection, allowing for concurrent communication and multiplexing.</p>
</li>
<li><p>Channels are similar in concept to HTTP/2 streams, where multiple concurrent requests/responses are tagged with a unique ID and sent over a single connection.</p>
</li>
</ul>
</li>
<li><p><strong>Queues:</strong></p>
<ul>
<li><p>Named buffers within the RabbitMQ server where messages are stored.</p>
</li>
<li><p>Publishers send messages to queues, and consumers retrieve messages from them.</p>
</li>
<li><p>Queues can be <em>durable</em> (persisted to disk) or non-durable.</p>
</li>
</ul>
</li>
<li><p><strong>Exchanges:</strong></p>
<ul>
<li><p>Receive messages from producers and route them to one or more queues based on predefined rules (e.g., direct, fanout, topic).</p>
</li>
<li><p>While publishers typically specify a queue name, messages are internally routed via an exchange, providing an additional layer of abstraction.</p>
</li>
</ul>
</li>
</ol>
<h2>Exchange Types in RabbitMQ</h2>
<p>In RabbitMQ, producers do not send messages directly to queues. Instead, messages are first sent to an <strong>exchange</strong> (router), which decides how to route them to one or more queues.</p>
<p>RabbitMQ supports several exchange types.</p>
<h3>Direct Exchange</h3>
<p>A direct exchange routes messages to a queue based on an <strong>exact routing key match</strong>.</p>
<p>For example, if a message has the routing key <code>video.compress</code>, it will only be delivered to queues that are bound with the same key.</p>
<p>This is commonly used when messages need to be delivered to <strong>specific services</strong>.</p>
<h3>Fanout Exchange</h3>
<p>A fanout exchange <strong>broadcasts messages to all queues</strong> bound to it, ignoring routing keys.</p>
<p>This is useful when multiple services need to react to the same event.</p>
<p>Example:</p>
<ul>
<li><p>send notifications</p>
</li>
<li><p>update analytics</p>
</li>
<li><p>trigger logging</p>
</li>
</ul>
<h3>Topic Exchange</h3>
<p>A topic exchange allows routing based on <strong>pattern matchine</strong></p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/2c8fc2d6-00d6-417e-b926-9daaf6dd72ba.png" alt="" style="display:block;margin:0 auto" />

<h3>Message Delivery and Acknowledgement:</h3>
<ul>
<li><p>RabbitMQ pushes messages to consumers.</p>
</li>
<li><p>Consumers are responsible for acknowledging messages after successful processing. This informs the server to remove the message from the queue.</p>
</li>
<li><p>If a consumer fails to acknowledge a message (e.g., due to a crash), RabbitMQ can redeliver the message to another available consumer, ensuring "at least once" delivery guarantee.</p>
</li>
</ul>
<h3>Pros and Cons of RabbitMQ:</h3>
<p><strong>Pros:</strong></p>
<ul>
<li><p><strong>Decoupling:</strong> Effectively decouples system components.</p>
</li>
<li><p><strong>Asynchronous Processing:</strong> Enables non-blocking operations.</p>
</li>
<li><p><strong>Reliable Delivery:</strong> Offers mechanisms like acknowledgments to ensure messages are processed.</p>
</li>
<li><p><strong>Channels:</strong> Efficiently multiplexes communication over a single TCP connection, reducing overhead.</p>
</li>
<li><p><strong>Mature Technology:</strong> Widely adopted and well-supported.</p>
</li>
</ul>
<p><strong>Cons:</strong></p>
<ul>
<li><p><strong>Complexity:</strong> Can be complex to set up and manage due to its many components and concepts.</p>
</li>
<li><p><strong>Push Model Scalability:</strong> The push model to consumers can be challenging to scale, as it's hard to guarantee that consumers won't be overwhelmed if the server pushes too many messages at once.</p>
</li>
<li><p><strong>Guarantees:</strong> While it offers "at least once" delivery, achieving "exactly once" delivery is very complex and often requires additional logic on the application side.</p>
</li>
</ul>
<h3>RabbitMQ in Practice:</h3>
<ul>
<li><p>Spinning up a RabbitMQ server can be easily done using Docker containers.</p>
</li>
<li><p>Node.js (and other languages) has official AMQP client libraries (e.g., <code>amqplib</code>) to interact with RabbitMQ, allowing developers to create publishers and consumers.</p>
</li>
<li><p>Cloud-based RabbitMQ services (like CloudAMQP) simplify deployment and management.</p>
</li>
</ul>
<p>RabbitMQ is particularly well-suited for asynchronous job systems where publishers submit tasks and consumers pick them up for processing.</p>
<h2>References:</h2>
<ul>
<li><p><a href="https://www.youtube.com/watch?v=e03c3CIGtYU&amp;t=6362s">https://www.youtube.com/watch?v=e03c3CIGtYU&amp;t=6362s</a></p>
</li>
<li><p><a href="https://www.cloudamqp.com/blog/part1-rabbitmq-for-beginners-what-is-rabbitmq.html">https://www.cloudamqp.com/blog/part1-rabbitmq-for-beginners-what-is-rabbitmq.html</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[System Design: Scaling Databases to Millions – Complete Journey
]]></title><description><![CDATA[System Design: Scaling Databases to Millions]]></description><link>https://srvjha.hashnode.dev/system-design-scaling-databases-to-millions-complete-journey</link><guid isPermaLink="true">https://srvjha.hashnode.dev/system-design-scaling-databases-to-millions-complete-journey</guid><category><![CDATA[System Design]]></category><category><![CDATA[Databases]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Mon, 02 Mar 2026 14:08:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/e8cdc9dc-b811-484c-b2da-5d26e648b4b9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For any fast-growing system, one of the major problems it needs to solve is scaling. The system must be capable of handling lots of concurrent users. A crash is not an option. That can lead to major economic losses and reputation damage. So how do we prevent it?</p>
<p>Let’s design a basic system.</p>
<h2>Designing a Scalable Application Layer</h2>
<p>We’ll start with a load balancer that distributes traffic across four servers.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/ff3723e8-0e88-45b3-a3f1-9200a361dce2.png" alt="" style="display:block;margin:0 auto" />

<h3>Stateful vs Stateless Approach</h3>
<p>Now we have an option: we can make our servers stateful. Sounds simple, right?</p>
<p>But here’s the problem. Suppose some user’s session data is stored on Server 1. The next time that same user visits the page, how will the load balancer know that their data is on Server 1? Without any special logic, the load balancer could send them to Server 3. That’s a problem.</p>
<p>One solution is to use sticky sessions. The load balancer can apply rules — for example, round-robin with session affinity — so that the same user keeps getting routed to the same server.</p>
<blockquote>
<p>Sticky sessions don’t actually store your data inside the load balancer. That’s a common misunderstanding. The load balancer isn’t holding your session state. It simply remembers that “User A” should always be routed to “Server 1.” It binds the client to a specific server.</p>
</blockquote>
<p>Does that solve it?</p>
<p>Not entirely.</p>
<p>What happens if that specific server crashes? All the session data tied to it is gone. Now your stateful approach starts breaking down.</p>
<p>So maybe making the application layer stateful isn’t the best idea.</p>
<p>Instead, we keep our servers stateless.</p>
<p>But then the big question: where does the data go?</p>
<p>For that, we introduce a separate machine dedicated to storing data called database.</p>
<p>Now we have a single database instance. All backend servers are stateless and fetch data from this database. That means every read and every write goes to this one database.</p>
<p>But dont you think if all reads and writes are on single database it will overwhelms the server and make it slow ? Yes. That becomes a bottleneck.</p>
<p>So to prevent this bottleneck what can we do ?</p>
<h3>Vertical Scaling</h3>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/c472eb6b-d433-4eb0-8af9-c67261b95f6f.png" alt="" style="display:block;margin:0 auto" />

<p>We can implement <strong>Vertical Scaling</strong> by adding more resources (means adding more CPU, more RAM, better SSDs) to it .</p>
<p>Or can we add another database server to handle the load will it work ? the answer is <strong>No</strong> because that would lead to the problem of <strong>Data Inconsistency.</strong></p>
<p>So vertical scaling sounds good to prevent this bottleneck it will now work fast and we will keep scaling up our database as our business grows (as the number of reads/writes will be more now)</p>
<p>But there are trade-offs:</p>
<ol>
<li><p>Cost - high-end machines are expensive.</p>
</li>
<li><p>Downtime - as there will more traffic and as we scale up there is some delay while adding more resources , during that period there will be business losses. We can prevent it out by <strong>preplanning</strong> means we can <strong>prewarm</strong> our server in advance before any traffic surge but what if the timing isn't right :(</p>
</li>
</ol>
<p>So to conclude vertical scaling is good solution but there is some tradeoffs as well also other than this bottleneck which we tried to prevent one of the major problem that exist even after preventing this bottleneck is the <strong>Single Point of Failure</strong> if the single instance is down everything crashes.</p>
<p>So what we do now ?</p>
<h3>Horizontal Scaling - DB Replication</h3>
<p>Instead of making one machine stronger, we add more machines.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/2c156059-15c2-4ef0-a524-f3db079ce836.png" alt="" style="display:block;margin:0 auto" />

<p>We introduce replication.</p>
<p>We will add multiple instances of replica(clone) where our primary database will be the master and others will be the replicas which will have the sole purpose of read so all the read will be done by the replicas called read replicas and all writes will be on the master database , this architecture is called <strong>primary replica</strong> or <strong>master slave architecture.</strong></p>
<p>This way we will reduce the load on <strong>primary</strong> and we will periodically sync the master wth the replicas to keep it sync with primary (there will be some delay while syncing) so this way we have created distributed system to handle reads and writes.</p>
<p><strong>Trade-offs :</strong></p>
<ol>
<li><p>Cost increases (more machines).</p>
</li>
<li><p>Storage consumption increases.</p>
</li>
<li><p>Data is not always real-time on replicas due to replication lag.</p>
</li>
</ol>
<p>To solve the data Realtime problem what we can do is let the application decide which type of data needs to Realtime that part we can managed by reading with primary so its Realtime</p>
<p><strong>For example</strong> : Instagram views can be read through the read replica as delay tradeoff we can do doesn't matter but an update to caption cannot be lagged so there we use the primary to read in Realtime.</p>
<p>Also, if the primary goes down, one of the replicas can be promoted to primary.</p>
<p>Replication helps when reads are the bottleneck.</p>
<p>But if write traffic becomes very high, replication alone is not enough because all writes still go to one primary :(</p>
<p>So now we will implement an even better technique.</p>
<h2>Sharding</h2>
<p>Sharding is a technique where the database is splited into multiple instances of independent database instances called shards.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/a376b922-7a10-45e0-ae19-23cf232218d9.png" alt="" style="display:block;margin:0 auto" />

<p>So what we do here we split data across the shards which will unique to each server (subset of data) and we will do an indexing to it.</p>
<p>To decide which shard stores which data, we use a shard key.</p>
<h3>Sharding Key</h3>
<p>A sharding key is a field or combination of fields (indexed) in a database table that determines the distribution of data across multiple shards</p>
<p>Now to determine which data is stored in which shard we will implement a hash function</p>
<h3>Hash Function</h3>
<p>Its an algorithm where it will take the input of userId and it will return the index of the shard where the data is present.</p>
<pre><code class="language-javascript">let userId = 1;
let numOfServers = 3;

function getShardIndex(userId){
   return userId % numOfServers // hash value
}

const index = getShardIndex(userId);
write(db[index])
read(db(index])
</code></pre>
<p>this way now our data will be consistent and if you think the read / write operation will overwhelm any shard we can create read replicas of that particular shard this way we did horizontal scaling on this level too. so this is now more optimized</p>
<p>Now have you observe any problem here ?</p>
<p><strong>Trade-offs :</strong></p>
<ol>
<li><p><strong>Resharding :</strong> If we add a new server <code>numOfServers</code> changes from 3 to 4.Now the hash calculation changes. Data that was on shard 1 may now belong to shard 2. To prevent this we have to do data movement which is quite an expensive overhead and there is risk of data loss as well to resolve this issue we have <strong>consistent hashing</strong> which i will cover in another blog.</p>
</li>
<li><p><strong>Expensive Cross-Shard Joins :</strong> If a query needs data from multiple shards, the application must fetch from each shard and merge results. Joins become expensive and complex.</p>
</li>
<li><p><strong>Celebrity problem :</strong> This is also known as the <strong>Hotspot Key Problem.</strong> Imagine data for Virat Kohli , talwiinder and karan aujla all end up on the same shard. So on Instagram, that database shard would be overwhelmed with the read operations. To solve this problem, we may need to allocate a shard for each celebrity. Each shard might even require further partition.</p>
</li>
</ol>
<p>but overall this is great architecture, some companies accept this trade-off.</p>
<p><strong>Every system design has trade-offs.</strong></p>
<h2>Summary</h2>
<p>Scaling a database is not about jumping to complex architectures from day one. It’s about evolving with your bottlenecks.</p>
<p>We start with a single database.<br />When it becomes slow, we try vertical scaling.<br />When reads increase, we introduce replication.<br />When writes become the bottleneck, we move to sharding.</p>
<p>Each solution solves one problem and introduces new trade-offs like cost, complexity, replication lag, resharding overhead, and hotspot issues.</p>
<p>There is no perfect architecture. There is only the right architecture for your current scale.</p>
<p><strong>Good system design is not about overengineering early</strong>.<br />It’s about identifying bottlenecks, understanding trade-offs, and scaling deliberately.</p>
]]></content:encoded></item><item><title><![CDATA[System Design: Thundering Herd Problem in Distributed Systems]]></title><description><![CDATA[What is Thundering herd problem and how to prevent it in distributed systems]]></description><link>https://srvjha.hashnode.dev/thundering-herd-problem</link><guid isPermaLink="true">https://srvjha.hashnode.dev/thundering-herd-problem</guid><category><![CDATA[System Design]]></category><category><![CDATA[thundering-herd]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[server]]></category><category><![CDATA[backend]]></category><category><![CDATA[Databases]]></category><category><![CDATA[Load Balancing]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Tue, 24 Feb 2026 18:07:34 GMT</pubDate><enclosure url="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/65e07704cce87d7f9a130137/3ba44ea0-2f6e-4d52-9380-6bf4528afd69.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2>What is Thundering Herd Problem ?</h2>
<p>When CBSE 12th board result website announces that results will be out <strong>exactly</strong> at 10 AM on particular day. Before 10 AM, thousands of students are waiting and refreshing the page.</p>
<p>The moment the result goes live, all students send requests to the server at the <strong>same time</strong>. Instead of traffic increasing gradually, the system suddenly goes from almost no requests to thousands of requests in a split second. This sudden spike overwhelms the backend causing crashes.</p>
<p>This situation where large amount of clients simultaneously requests the resource after it becomes available called <strong>Thundering Herd Problem</strong>.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/3b7a4256-c66a-480d-a518-ba4a4248bfe6.png" alt="" style="display:block;margin:0 auto" />

<h3>Technical Deep Dive</h3>
<p>When a <strong>large number of processes or threads are waiting</strong> for the <strong>same</strong> event, and when that event happens they all wake up and attempt to acquire the <strong>same</strong> resource at the <strong>same</strong> time. This creates sudden spikes in load, leading to bottlenecks, high latency, wasted work, and system crashes.</p>
<p>After the processes wake up, they all demand the resource and a decision must be made as to which process can continue. After the decision is made the remaining processes are put back to sleep, only to wake up again to request access to the resource.</p>
<h3>How thundering herd occurs during cache expiry or cache miss</h3>
<p>When a large number of users request the same data, the backend first checks the cache.<br />If the data is present, it is returned immediately without touching the database. However, when the cache entry expires (TTL ends) or the cache is empty (cold start), all concurrent requests observe a cache miss.</p>
<p>Instead of a single request rebuilding the cache, thousands of requests simultaneously fall through to the database and attempt to fetch the same data.</p>
<p>This sudden spike overwhelms the database causing slowdowns, timeouts, or complete system failure.</p>
<p>This behavior is known as a <strong>cache stampede</strong>.</p>
<h3>Impact on System</h3>
<p><strong>Unexpected CPU spikes</strong><br />The system jumps from handling mostly cache reads to processing thousands of expensive database queries at once.</p>
<p><strong>Increased latency</strong><br />Requests queue up behind overloaded thread pools and database connections, causing response times to grow rapidly.</p>
<p><strong>Resource waste</strong><br />The same data is recomputed or fetched thousands of times even though only one result is needed, wasting CPU, memory, and network bandwidth.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/575488d4-cf9d-4336-b502-b42839323e29.png" alt="" style="display:block;margin:0 auto" />

<h3>Are normal spike and thundering herd the same problem ?</h3>
<p>While you might wander after reading about till now that thundering herd is normal spike problem but its not correct, well they do result is sudden surge of traffic but fundamentally they are very different in terms of the pattern of their spike.</p>
<p><strong>Normal Spike :</strong> Here the increase in traffic is often occurs at different time like breaking news , sales etc. It is often handled by autoscaling, load balancer.</p>
<ul>
<li><strong>Analogy</strong> : A busy day at a coffee shop with a long line forming over 30 minutes</li>
</ul>
<p><strong>Thundering Herd :</strong> A specific scenario where a massive number of requests hit a system simultaneously, often because a cache has expired or a resource has become available, causing all waiting processes to rush for it at once. Requires specific mitigation strategies to prevent it.</p>
<ul>
<li><strong>Analogy:</strong> The coffee shop door is locked until 9:00 AM, and 500 people try to push through the door at exactly 9:00:00 AM, breaking the door.</li>
</ul>
<h2>Some Well Known Case Studies</h2>
<p>In 2010, Facebook experienced a multi-hour outage triggered by a configuration change to a persistent storage value. The new configuration was interpreted as invalid by cache clients across the infrastructure. Each client attempted to “repair” the invalid configuration by querying backend database clusters, generating hundreds of thousands of queries per second and causing widespread database failures.</p>
<h2><strong>Failure Sequence</strong></h2>
<ol>
<li><p><strong>Initial condition</strong>: Configuration value cached across all application servers</p>
</li>
<li><p><strong>Trigger</strong>: Configuration updated to value that failed client-side validation</p>
</li>
<li><p><strong>Amplification</strong>: Each server detected invalid value and attempted database repair query</p>
</li>
<li><p><strong>Cascading failure</strong>: Database clusters overwhelmed by synchronized query load</p>
</li>
<li><p><strong>Recovery impediment</strong>: Cache invalidation loops prevented normal service restoration</p>
</li>
</ol>
<h2>How to prevent thundering problem ?</h2>
<p>To prevent this we have few strategies that we can follow to avoid such problem</p>
<ul>
<li><p><strong>Request coalescing</strong> : It Prevents thundering herd by ensuring that when many identical requests arrive during a cache miss, only one request fetches the data while others wait for the result.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/c30bce32-558d-483a-8d4f-1d5ed272d1ac.png" alt="" style="display:block;margin:0 auto" />

<ul>
<li><p><strong>Mutex</strong> : This mechanism that allows only one process or thread to access a shared resource at a time, while others wait. This prevents duplicate work, race conditions, and data corruption. It is also called request coalescing lock.</p>
</li>
<li><p><strong>State while invalidate</strong> : Serve the old (stale) cached data immediately while refreshing the data in the background. Once the refresh completes, the cache is updated for future requests.</p>
<img src="https://cdn.hashnode.com/uploads/covers/65e07704cce87d7f9a130137/62a86606-3244-45bb-8661-8dc9b83ad932.png" alt="" style="display:block;margin:0 auto" /></li>
</ul>
</li>
</ul>
<p>Like this there are plenty more strategies to prevent the thundering herd Problem.</p>
<h2>Summary</h2>
<p>So to summarize what we learnt so far is that the thundering herd problem happens when many requests react to the same event at once and overwhelm a shared resource, often during cache expiry or service recovery. Techniques like request coalescing, mutex locks, and stale-while-revalidate help prevent duplicate work and keep systems stable under spikes.</p>
<p>Hope you learned something useful from this post :) feel free to share it with your friends!</p>
<h3>References:</h3>
<ul>
<li><p><a href="https://python.plainenglish.io/the-thundering-herd-problem-cc6c5b83fcd2">https://python.plainenglish.io/the-thundering-herd-problem-cc6c5b83fcd2</a></p>
</li>
<li><p><a href="https://news.ycombinator.com/item?id=1722213">https://news.ycombinator.com/item?id=1722213</a></p>
</li>
<li><p><a href="https://encore.dev/blog/thundering-herd-problem">https://encore.dev/blog/thundering-herd-problem</a></p>
</li>
<li><p><a href="https://medium.com/@venkteshsubramaniam/the-thundering-herd-distributed-systems-rate-limiting-9128d20e1f00">https://medium.com/@venkteshsubramaniam/the-thundering-herd-distributed-systems-rate-limiting-9128d20e1f00</a></p>
</li>
<li><p><a href="https://medium.com/@codebuster/the-thundering-herd-problem-fae5260228c6">https://medium.com/@codebuster/the-thundering-herd-problem-fae5260228c6</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[How a Leaky Abstraction Led to an RCE in React Server Components]]></title><description><![CDATA[Learn how a flaw in React Server Components led to a remote code execution vulnerability, its mechanics, and the fix implemented]]></description><link>https://srvjha.hashnode.dev/how-a-leaky-abstraction-led-to-an-rce-in-react-server-components</link><guid isPermaLink="true">https://srvjha.hashnode.dev/how-a-leaky-abstraction-led-to-an-rce-in-react-server-components</guid><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sun, 21 Dec 2025 14:17:23 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1766326822210/b9baee0e-f6c7-48c4-a678-82bbe84cb1f0.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When RSC (React Server Components) were introduced, they significantly changed the landscape of React and modern web engineering. Writing backend or server-side code became much easier, and this approach appeared to solve long-standing SSR problems. Overall, the change looked very promising.</p>
<p>Next.js, one of the most popular React frameworks, adopted RSC early, which further accelerated its popularity.</p>
<p>At a high level, RSC works by serving static output directly to the browser, while dynamic updates are handled through hydration, which injects JavaScript into the HTML. This approach worked well until November 29th, when <strong>Lachlan Davidson</strong> reported a security vulnerability in React that exposed a critical abstraction flaw in React Server Components.</p>
<h2 id="heading-what-was-the-vulnerability">What was the vulnerability?</h2>
<p>CVE (<strong>Critical Security Vulnerability)</strong> was found in react that allowed unauthenticated <mark>remote code execution</mark> by exploiting the crafted input payloads being send to the React Server Components (RSC).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766324015787/0073b49b-d782-4400-9a8d-7bf15f863764.png" alt="source: CVE React " class="image--center mx-auto" /></p>
<p>The <strong>critical severity</strong> rating highlights how serious the situation was. Importantly, even if an application does <strong>not explicitly implement any React Server Function endpoints</strong>, it may still be vulnerable as long as the version supports React Server Components.</p>
<p>For a detailed technical report and official disclosure, refer to the CVE record here:<br /><a target="_blank" href="https://www.cve.org/CVERecord?id=CVE-2025-55182">https://www.cve.org/CVERecord?id=CVE-2025-55182</a></p>
<h2 id="heading-digging-deeper-into-the-vulnerability">Digging Deeper Into the Vulnerability</h2>
<p>As an engineer, I got curious about how Lachlan discovered this issue, so I dig deeper. What I found is that the vulnerability lies in <strong>react</strong> <strong>flight protocol</strong>.</p>
<h3 id="heading-what-is-the-react-flight-protocol">What Is the React Flight Protocol?</h3>
<p>The React Flight protocol is the mechanism react uses to <strong>send data in particular format between the server and the browser</strong> when using React Server Components (RSC) and Server Functions.</p>
<p>Instead of sending plain HTML or simple JSON, React sends a <strong>stream of structured data</strong> that describes:</p>
<ul>
<li><p>what components to render,</p>
</li>
<li><p>what data they need,</p>
</li>
<li><p>and how they connect to each other.</p>
</li>
</ul>
<p>This allows React to build complex UIs very quickly and efficiently.</p>
<h3 id="heading-why-does-react-need-flight">Why Does React Need Flight?</h3>
<p>The best way to think about React Server Components is as a <strong><mark>BFF (Backend for Frontend)</mark></strong> layer.</p>
<p>Rather than the frontend making multiple API calls and stitching everything together, React asks the server for <em>exactly the UI it needs</em>, in a single round trip. The server then streams back data that matches the <strong>shape of the component tree</strong>.</p>
<p>The Flight protocol is what makes this streaming possible.</p>
<p>Simple Analogy to understand :</p>
<p>Think of Flight like ordering food at a <strong>South Indian restaurant</strong>.</p>
<p>You don’t order each item separately:</p>
<ul>
<li><p>“Bring rice”</p>
</li>
<li><p>“Now bring sambar”</p>
</li>
<li><p>“Now bring chutney”</p>
</li>
</ul>
<p>Instead, you order a <strong>thali</strong>.</p>
<p>As soon as something is ready, it’s served to your table you don’t wait for everything.</p>
<p>Flight works the same way:</p>
<ul>
<li><p>React sends the browser a “UI”</p>
</li>
<li><p>Some components arrive immediately</p>
</li>
<li><p>Some arrive later when data is ready</p>
</li>
<li><p>The UI keeps rendering smoothly in the meantime</p>
</li>
</ul>
<h3 id="heading-where-things-went-wrong">Where Things Went Wrong ?</h3>
<p>Now imagine if someone could <strong>slip a fake instruction into the kitchen order</strong>.</p>
<p>Instead of “add extra sambar”, the note secretly says:<br />“Show the secret ingredients” or “Fire the kitchen”.</p>
<p>Because the kitchen <strong>trusts the order slip</strong>, it follows the instruction without questioning it.</p>
<p>That’s essentially what happened here.</p>
<p>The Flight protocol trusted incoming payloads too much. By crafting a malicious Flight payload, an attacker could <strong>break React’s abstraction layer</strong> and make the server execute unintended code leading to <strong>remote code execution</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766325595581/f687aa76-ccfb-4412-a81e-a42dc6fa4421.png" alt class="image--center mx-auto" /></p>
<p>This is what the malicious payload looked like. I’ll write a separate blog explaining this payload in detail, but at a high level, here’s what was happening.</p>
<p>The exploit relied on <strong>duck typing</strong> and JavaScript’s flexible object behavior. By carefully shaping the payload, the attacker was able to <strong>manipulate how React interpreted internal fields</strong> inside the Flight response.</p>
<p>Specifically, the payload injected a controlled value into the internal <code>_prefix</code> field. This prefix was later used by React in a context where it was <strong>assumed to be safe</strong>, but was instead treated as executable logic.</p>
<h2 id="heading-how-they-fixed-it">How they fixed it ?</h2>
<p>The fix patch added strict <code>hasOwnProperty</code> checks everywhere React reads values from the Flight payload.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766325874095/fd15e35a-ccc0-4400-9fe1-6afb891f1e2f.png" alt class="image--center mx-auto" /></p>
<p>In simple words:</p>
<blockquote>
<p>React now checks:<br />“Does this property <em>actually belong</em> to this object,<br />or is it being inherited from parent object?”</p>
</blockquote>
<p>If the property is not directly owned by the object, React refuses to use it.</p>
<p>This single check blocks:</p>
<ul>
<li><p>access to <code>then</code>,</p>
</li>
<li><p>access to <code>constructor</code>,</p>
</li>
<li><p>access to anything inherited via prototypes.</p>
</li>
</ul>
<p>And once those are blocked, <strong>code execution becomes impossible</strong>.</p>
<h3 id="heading-the-big-lesson">The Big Lesson</h3>
<p>This vulnerability wasn’t about React being “unsafe”.</p>
<p>It was about a <strong>leaky abstraction</strong>:</p>
<ul>
<li><p>a powerful protocol (Flight),</p>
</li>
<li><p>handling untrusted input,</p>
</li>
<li><p>without strict boundary checks.</p>
</li>
</ul>
<p>The fix wasn’t complex but it was <strong>fundamental</strong>.</p>
<p>Sometimes security isn’t about adding more code.<br />It’s about adding the <strong>right guardrail at the right boundary</strong>.</p>
<h2 id="heading-references-and-learning-resources">References and Learning Resources</h2>
<ul>
<li><p><a target="_blank" href="https://react.dev/blog/2025/12/03/critical-security-vulnerability-in-react-server-components">https://react.dev/blog/2025/12/03/critical-security-vulnerability-in-react-server-components</a></p>
</li>
<li><p><a target="_blank" href="https://x.com/rauchg/status/1997362942929440937">https://x.com/rauchg/status/1997362942929440937</a></p>
</li>
<li><p><a target="_blank" href="https://react2shell.com/">https://react2shell.com/</a></p>
</li>
<li><p><a target="_blank" href="https://github.com/facebook/react/commit/bbed0b0ee64b89353a40d6313037bbc80221bc3d#diff-80176ba145466a6b4b583b8c2a66d815d792b3b50adb8acb39e75afa856daea1R921">github fix pr</a></p>
</li>
<li><p><a target="_blank" href="https://x.com/rauchg/status/1997362942929440937">https://x.com/rauchg/status/1997362942929440937</a></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Mastering the DOM: A Hands-On Guide with Examples]]></title><description><![CDATA[Learn DOM manipulation techniques with real-world examples and challenges, from event listeners to browser APIs. Boost your JavaScript skills!]]></description><link>https://srvjha.hashnode.dev/mastering-the-dom-a-hands-on-guide-with-examples</link><guid isPermaLink="true">https://srvjha.hashnode.dev/mastering-the-dom-a-hands-on-guide-with-examples</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[HTML]]></category><category><![CDATA[CSS]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[DOM]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Fri, 28 Feb 2025 22:24:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740781345850/f4d86242-57f2-42c8-8aa5-06c4d3003f85.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, I will share my learnings from completing DOM challenges. This will help you understand how the DOM works and introduce you to commonly used methods and event listeners for DOM manipulation. Let’s get started!</p>
<h2 id="heading-what-is-dom">What is DOM ?</h2>
<p>The DOM (Document Object Model) is a representation of the HTML structure of a webpage. However, the main problem of HTML is that it cannot add interactivity or dynamic behavior to the webpage. For example, if there's a button on the webpage, HTML by itself cannot handle what happens when the button is clicked.</p>
<p>This is where the DOM acts as a bridge between the webpage and scripts like JavaScript, allowing us to manipulate the page dynamically. With JavaScript, we can modify the DOM by adding, removing, or updating elements in response to user interactions, making the webpage more interactive and functional.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1740745873306/62b25b7d-2fb4-4db0-a215-7e8b782f43aa.png" alt class="image--center mx-auto" /></p>
<p>Now that we understand the DOM, let's explore the methods and events I used while completing the DOM challenges.</p>
<h2 id="heading-addeventlistener">addEventListener()</h2>
<p>addEventListener is a method in javascript that is used to attach event handler to an element. It listens for a specified event like “click”, ”button” and runs a function when the event occurs</p>
<pre><code class="lang-javascript">element.addEventListener(event, <span class="hljs-function"><span class="hljs-keyword">function</span>, <span class="hljs-title">useCapture</span>);</span>
</code></pre>
<ul>
<li><p>event : the type of event (input,button… etc)</p>
</li>
<li><p>function : the function event that occurs</p>
</li>
<li><p>useCapture (optional) : A boolean value that determines event propagation (default is false)</p>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> button = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"myButton"</span>);

button.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
  alert(<span class="hljs-string">"Button clicked!"</span>);
});
<span class="hljs-comment">//  when the button with id="myButton" is clicked, an alert pops up</span>
</code></pre>
<h2 id="heading-real-time-dom-manipulation">Real Time DOM Manipulation</h2>
<p>In one of the challenges, I had to update the DOM in real-time as the user typed into an input field. If no value was entered, the text "Not Provided" should be displayed by default. To achieve this, I explored the <code>input</code> event.</p>
<p>The <code>input</code> event fires whenever the value of an <code>&lt;input&gt;</code> field changes due to user interaction, such as typing in a text box. Unlike the <code>change</code> event, which only triggers when the input loses focus, the <code>input</code> event updates the DOM instantly as the user types.</p>
<pre><code class="lang-javascript"> name.addEventListener(<span class="hljs-string">"input"</span>,<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-keyword">if</span>(name.value.length &gt; <span class="hljs-number">0</span>){
        displayName.innerText = name.value
    }
    <span class="hljs-keyword">else</span> {
        displayName.innerText = <span class="hljs-string">"Not Provided"</span>
    }
 })
</code></pre>
<ul>
<li><p>Every time the user types in the input field, the event listener updates the <code>displayName</code> content in real-time.</p>
</li>
<li><p>If the input field is empty or contains only spaces, <code>"Not Provided"</code> is displayed instead.</p>
</li>
</ul>
<h2 id="heading-creating-and-appending-elements">Creating and Appending Elements</h2>
<p>One of the most powerful features of JavaScript is its ability to <strong>dynamically create and insert elements</strong> into the DOM. This allows us to build interactive user interfaces</p>
<h3 id="heading-1-creating-elements-with-documentcreateelement"><strong>1. Creating Elements with</strong> <code>document.createElement</code></h3>
<p>In JavaScript, we use the <code>document.createElement("li")</code> method to create a new element.</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">const</span> li = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">"li"</span>);
  li.classList.add(<span class="hljs-string">"task-item"</span>)
</code></pre>
<p>here what i did is i have created a element li where i have add class name task-item using classlist.add property but this is just the element created there is no change in webpage yet so what we have to do is now use appendChild method</p>
<h3 id="heading-2-appending-elements-with-appendchild">2. Appending Elements with <code>appendChild</code></h3>
<p>Once we create an element, we need to attach it to an existing part of the DOM. The <code>appendChild()</code> method is used to insert the new element as a <strong>child</strong> of another element.</p>
<pre><code class="lang-javascript"> tasks.appendChild(li)
</code></pre>
<p>suppose now when we have to remove the li item from the cart so now we have to manipulate the dom such that our li element gets removed from the tasks element</p>
<h3 id="heading-3-removing-elements-with-removechild">3. Removing Elements with <code>removeChild</code></h3>
<pre><code class="lang-javascript">deleteButton.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
    taskList.removeChild(li);  <span class="hljs-comment">// Removes the &lt;li&gt; when the delete button is clicked</span>
});
</code></pre>
<h2 id="heading-using-browser-apis-to-manipulate-the-dom"><strong>Using Browser APIs to Manipulate the DOM</strong></h2>
<p>Modern web browsers provide built-in <strong>APIs</strong> that allow us to interact with the <strong>DOM dynamically</strong>. One such API is the <code>setInterval</code> function, which helps us execute code repeatedly after a specified time interval.</p>
<h3 id="heading-image-carousel-with-auto-play"><strong>Image Carousel with Auto-Play</strong></h3>
<p>One of the challenges I worked on involved building an <strong>image carousel</strong> with an autoplay feature that moves to the next image every 5 seconds. This required the use of <code>setInterval</code> to switch images automatically and <code>clearInterval</code> to stop autoplay when needed.</p>
<h3 id="heading-understanding-setinterval-for-autoplay"><strong>Understanding</strong> <code>setInterval</code> for Autoplay</h3>
<p>The <code>setInterval(callback, time)</code> method allows us to repeatedly execute a function after a given time interval (in milliseconds).</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callTimer</span>(<span class="hljs-params">time</span>)</span>{
  <span class="hljs-built_in">clearInterval</span>(interval)
  interval = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-keyword">if</span>(time &gt; <span class="hljs-number">0</span>)
    {
      timer.innerText= <span class="hljs-string">`Next slide in <span class="hljs-subst">${time}</span>s`</span>
      time--;
    }
    <span class="hljs-keyword">else</span> 
    {
      <span class="hljs-built_in">clearInterval</span>(interval)
      timer.innerText= <span class="hljs-string">``</span>   
     <span class="hljs-keyword">if</span>(i&lt;images.length<span class="hljs-number">-1</span>){
      nextBtn.click()   
     }
     <span class="hljs-keyword">else</span> {
      previousBtn.click()     
     }
     callTimer(<span class="hljs-number">5</span>)    
    }
  },<span class="hljs-number">1000</span>)
}
</code></pre>
<h3 id="heading-clearing-the-interval-clearinterval"><strong>Clearing the Interval (</strong><code>clearInterval</code>)</h3>
<p>To <strong>stop autoplay</strong>, we use <code>clearInterval(intervalId)</code>. This was needed when manually navigating images or pausing the carousel.</p>
<h2 id="heading-working-with-arrays-to-manage-state-in-the-shopping-cart"><strong>Working with Arrays to Manage State in the Shopping Cart</strong></h2>
<p>One of the most important aspects of this shopping cart implementation was effectively managing the cart’s state using <strong>arrays</strong>. Instead of directly modifying the DOM for every action, I stored all cart items in an array called <code>allCartItems</code>, which acted as the <strong>single source of truth</strong> for the cart's data.</p>
<p>This approach allowed me to manipulate the cart dynamically and update the UI efficiently. Here’s how I managed different cart operations using array methods:</p>
<h3 id="heading-finding-items-in-the-cart-find"><strong>Finding Items in the Cart (</strong><code>find()</code>)</h3>
<p>Before adding an item to the cart, I needed to check if it was already present. To do this, I used the <code>find()</code> method, which searches the array for an item that matches a given condition.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> existingItem = allCartItems.find(<span class="hljs-function"><span class="hljs-params">cartItem</span> =&gt;</span> cartItem.name === item);
<span class="hljs-keyword">if</span>(existingItem){
      existingItem.quantity++;
     <span class="hljs-keyword">return</span> updateCart();
   }
</code></pre>
<p><strong>Why?</strong></p>
<ul>
<li><p>This prevents duplicate entries.</p>
</li>
<li><p>If an item already exists, I just increase its quantity instead of adding it again.</p>
</li>
</ul>
<p>💡 <em>This ensures that the cart reflects real-world behavior, where the same product is grouped instead of being added multiple times separately.</em></p>
<h3 id="heading-adding-new-items-push"><strong>Adding New Items (</strong><code>push()</code>)</h3>
<p>If the item was not found in the cart, I created a new object representing the item and <strong>pushed</strong> it into the array:</p>
<pre><code class="lang-javascript">  <span class="hljs-keyword">let</span> newItem = {
    <span class="hljs-attr">name</span>: item,
    <span class="hljs-attr">price</span>: price,
    <span class="hljs-attr">quantity</span>: <span class="hljs-number">1</span>
};

allCartItems.push(newItem);
updateCart();
</code></pre>
<p><strong>Why?</strong></p>
<ul>
<li><p>This makes it easy to store multiple items in a structured way.</p>
</li>
<li><p>Each item is represented as an object containing <code>name</code>, <code>price</code>, and <code>quantity</code>.</p>
</li>
</ul>
<p>💡 <em>Using objects in arrays made it easy to manage item details dynamically.</em></p>
<h3 id="heading-updating-item-quantity-map"><strong>Updating Item Quantity (</strong><code>map()</code>)</h3>
<p>When a user increases or decreases the quantity of an item, instead of searching through the DOM, I updated the state in the array:</p>
<pre><code class="lang-javascript"> incrementQuantity.addEventListener(<span class="hljs-string">"click"</span>, <span class="hljs-function">() =&gt;</span> {
       cartItem.quantity++;
       updateCart();
   });
</code></pre>
<h3 id="heading-removing-items-splice"><strong>Removing Items (</strong><code>splice()</code>)</h3>
<p>When the user removes an item, I used the <code>splice()</code> method to delete it from the array based on its index:</p>
<pre><code class="lang-javascript">allCartItems.splice(index, <span class="hljs-number">1</span>);
updateCart();
</code></pre>
<p><strong>Why?</strong></p>
<ul>
<li><p>The <code>splice()</code> method removes the item at the specified index, preventing unnecessary looping.</p>
</li>
<li><p>The cart updates instantly, reflecting the correct items.</p>
</li>
</ul>
<p>💡 <em>This made removing items more efficient instead of looping through the entire array and filtering it manually.</em></p>
<h2 id="heading-what-i-learned-from-memory-card-game"><strong>What I Learned From Memory Card Game</strong></h2>
<p>This was a challenging tasks but i got to learned about different concepts of js i had refere some website and yt tutorial for learning how to built the memory card game</p>
<p>Visit this link for your reference: <a target="_blank" href="https://www.geeksforgeeks.org/build-a-memory-card-game-using-html-css-and-javascript/">https://www.geeksforgeeks.org/build-a-memory-card-game-using-html-css-and-javascript/</a></p>
<h3 id="heading-shuffling-cards-using-sort"><strong>Shuffling Cards Using</strong> <code>sort()</code></h3>
<p>To make the game challenging, I randomized the card order using <code>sort()</code> with a random comparator:</p>
<pre><code class="lang-javascript">cardValuesCopy.sort(<span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">Math</span>.random() - <span class="hljs-number">0.5</span>); <span class="hljs-comment">// little hack but fisher yates algo is used most often</span>
</code></pre>
<ul>
<li>This provides an equal probability of swapping positions, ensuring the cards are shuffled each time the game starts.</li>
</ul>
<h3 id="heading-game-logic-matching-cards"><strong>Game Logic: Matching Cards</strong></h3>
<p>To check if the flipped cards match, I compared their inner text values:</p>
<pre><code class="lang-javascript">jsCopyEditfunction checkForMatch(){
  <span class="hljs-keyword">if</span>(firstCard.children[<span class="hljs-number">1</span>].innerText !== secondCard.children[<span class="hljs-number">1</span>].innerText){
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      firstCard.classList.remove(<span class="hljs-string">"flipped"</span>);
      secondCard.classList.remove(<span class="hljs-string">"flipped"</span>);
      resetBoard();
    }, <span class="hljs-number">500</span>);
  } <span class="hljs-keyword">else</span> {
    winnerMoves++;
    <span class="hljs-keyword">if</span> (winnerMoves === <span class="hljs-number">8</span>) { 
      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
        alert(<span class="hljs-string">`Congratulations🔥 You won the game with <span class="hljs-subst">${moves.innerText}</span> moves and <span class="hljs-subst">${time.innerText}</span> taken`</span>);
        restartGame();
      }, <span class="hljs-number">300</span>);
    }
    resetBoard();
  }
}
</code></pre>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>These DOM challenges have truly helped me <strong>level up my JavaScript skills</strong>. I explored various <strong>methods, properties, event listeners, and DOM manipulation techniques</strong>, which deepened my understanding of how the browser dynamically updates the UI. From handling user interactions to working with timers, arrays, and state management, each challenge has enhanced my problem-solving abilities.</p>
<p>You can check out the <strong>GitHub repository</strong> and <strong>live demo</strong> of these projects here:</p>
<p>🔗 <strong>Live Link</strong>: <a target="_blank" href="https://srvjha.github.io/DOM-Challenges">https://srvjha.github.io/DOM-Challenges</a></p>
<p>🔗 <strong>Github Repo Link</strong>: <a target="_blank" href="https://github.com/srvjha/DOM-Challenges">https://github.com/srvjha/DOM-Challenges</a></p>
<p>If you <strong>learned something new</strong>, do <strong>give it a like</strong> and <strong>share it with your peers</strong>! 🚀</p>
]]></content:encoded></item><item><title><![CDATA[Building JavaScript Polyfills Yourself]]></title><description><![CDATA[Learn to build JavaScript polyfills, understand their purpose, and approach common interview questions with step-by-step implementations and examples]]></description><link>https://srvjha.hashnode.dev/building-javascript-polyfills-yourself</link><guid isPermaLink="true">https://srvjha.hashnode.dev/building-javascript-polyfills-yourself</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[polyfills]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Mon, 10 Feb 2025 20:41:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1739219956026/b9601745-c228-4173-afee-adcd5f584304.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article we will understand what is the exact meaning of polyfill and also will cover how to approach the polyfill question in interview and will implement the code with dry run . Let dive into it ! 🚀</p>
<h2 id="heading-why-polyfill">Why Polyfill ?</h2>
<p>Suppose you are developing your own website where you are using some array methods, such as the <code>filter</code> method. Now, what if a user accesses your website using a browser that does not support this method natively? Since you cannot predict which browsers will support certain features, this could lead to compatibility issues.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739215045585/3c433925-1cd4-4d59-ab1f-687447438dae.png" alt class="image--center mx-auto" /></p>
<p><mark>To prevent such problems,</mark> companies create their own versions of these methods, known as <strong>polyfills</strong>. These custom implementations ensure that if a built-in method is unavailable or unsupported, the polyfill seamlessly takes over, allowing the code to execute without errors. This approach helps maintain functionality across different browsers and ensures a smooth user experience.</p>
<h2 id="heading-what-is-polyfills">What is Polyfills ?</h2>
<p>Now that you understand why polyfills are necessary, here’s the MDN definition to help you in interviews:</p>
<blockquote>
<p>A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.</p>
</blockquote>
<h2 id="heading-lets-implement-polyfills-for-some-popular-array-methods">Lets Implement Polyfills for some popular Array Methods</h2>
<p>Before implementing a <strong>polyfill</strong>, break it down into the following steps:</p>
<p>✅ <strong>Read the method signature</strong> (What is expected? Kya nature hai uska?)<br />✅ <strong>Understand the input</strong> (What type of data does it accept?)<br />✅ <strong>Check the parameters</strong> (How many parameters does it take? What are they?)<br />✅ <strong>Define the output</strong> (What should the function return?)<br />✅ <strong>Determine the return type</strong> (Does it return an array, object, boolean, etc.?)<br />✅ <strong>Check if it’s iterable</strong> (Does it work with loops or higher-order functions?)</p>
<h3 id="heading-map">map()</h3>
<p><code>map()</code> method returns a new array where each element is the result of applying a callback function to every element of the original array.</p>
<p>To create a polyfill for map method first we need to break it down to the points which defines the core logic of map method</p>
<ul>
<li><p>Read Signature : New Array return | input - userFn ( its a callback function)</p>
</li>
<li><p>Iterate over the array</p>
</li>
<li><p>the callback function contains value and the index of the elements</p>
</li>
</ul>
<p>Now that we understand what is required of us Lets Code it</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(!<span class="hljs-built_in">Array</span>.prototype.myMap){
    <span class="hljs-built_in">Array</span>.prototype.myMap = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">userfn</span>)</span>{
        <span class="hljs-keyword">const</span> newArray = [];
        <span class="hljs-keyword">const</span> currentArray = <span class="hljs-built_in">this</span> <span class="hljs-comment">// yeh jis context mein hota usko point krta hai</span>
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>;i&lt;currentArray.length;i++){
           newArray.push(userfn(currentArray[i],i));
        }
        <span class="hljs-keyword">return</span> newArray;
    }
}

<span class="hljs-keyword">const</span> testMap = [<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>];
<span class="hljs-keyword">const</span> giveMultiples  = testMap.myMap(<span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>e*<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Custom Map: "</span>,giveMultiples)
</code></pre>
<p>To create a polyfill method Array.prototype.&lt;method-name&gt; is used where we define our function</p>
<ul>
<li><p>now inside that function we create a new Array as we noted in the signature that it returns new array</p>
</li>
<li><p>so we have created a new array so after that we have created a const variable that store <code>this</code> which referes to current context which is array in this case</p>
</li>
<li><p>then we simply run a for loop pushing the userfn with value and index</p>
</li>
<li><p>and finally return the new array</p>
</li>
</ul>
<p>simple right 😁 don’t worry let understand with a small dry run</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739217593223/1de0adf3-b512-42cd-a330-c6de5d27eb0e.jpeg" alt class="image--center mx-auto" /></p>
<p>This is how we create a polyfill for the <code>map</code> method. Try to understand the flow, and apologies for my handwriting! 😅</p>
<h3 id="heading-here-is-the-other-methods-that-is-widely-asked-in-interview">Here is the other methods that is widely asked in interview:</h3>
<p>1️⃣ <strong>map ✔</strong><br />2️⃣ <strong>reduce</strong><br />3️⃣ <strong>filter</strong><br />4️⃣ <strong>forEach</strong><br />5️⃣ <strong>some</strong><br />6️⃣ <strong>find</strong><br />7️⃣ <strong>fill</strong><br />8️⃣ <strong>flat</strong><br />9️⃣ <strong>flatMap</strong><br />🔟 <strong>includes</strong><br />1️⃣1️⃣ <strong>at</strong><br />1️⃣2️⃣ <strong>indexOf</strong><br />1️⃣3️⃣ <strong>push</strong><br />1️⃣4️⃣ <strong>pop</strong><br />1️⃣5️⃣ <strong>unshift</strong><br />1️⃣6️⃣ <strong>shift</strong></p>
<p>Try to create it on your own, and if you face any difficulties, I've got you covered! I've created a repository where you can find solutions for these polyfills. 🚀</p>
<p>Here is the link : <a target="_blank" href="https://github.com/srvjha/polyfills">https://github.com/srvjha/polyfills</a></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Now that you’ve understood how polyfills work, how to break them down step by step, and how to implement them and i have also demonstrated you with a dry run, Now the more you code, the better you'll grasp the logic behind these methods.</p>
<p>If you found this helpful, feel free to like ❤ and share your thoughts.🚀</p>
]]></content:encoded></item><item><title><![CDATA[JavaScript Array Lessons from Life😊]]></title><description><![CDATA[JavaScript array methods reflect life's growth and success, offering insights through relatable coding lessons]]></description><link>https://srvjha.hashnode.dev/javascript-array-lessons-from-life</link><guid isPermaLink="true">https://srvjha.hashnode.dev/javascript-array-lessons-from-life</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[chai-code ]]></category><category><![CDATA[Life lessons]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Wed, 05 Feb 2025 19:34:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738784371502/0498481e-49bd-4401-ac41-0878f04c78ae.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we are going to learn 10 JS methods that we can relate to our life experiences. so lets dive into it….</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1738752560158/57eef58a-8542-431e-b8b2-5babdce8a81b.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-push-method">push() Method</h2>
<p>In life, to be successful, you need to push certain values into your ‘life’ array (your box), like discipline, consistency, hard work, and gratitude.</p>
<p>Similarly, in JavaScript, the <code>push()</code> method adds elements to the end of an array. Now, don’t get confused when I say 'end'—because everything starts from emptiness. Here, 'end' is actually the beginning😅. Once you push the first value, it wouldn’t be fair if another value got ahead of it. To maintain fairness, new values are always added at the end.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> successfullLife = [<span class="hljs-string">"discipline"</span>,<span class="hljs-string">"consistency"</span>,<span class="hljs-string">"hardwork"</span>,<span class="hljs-string">"gratitude"</span>]
<span class="hljs-comment">// here suppose i want to add humbleness to my successfull life then i use push method so i will do</span>
successfullLife.push(<span class="hljs-string">"humbleness"</span>)
<span class="hljs-built_in">console</span>.log(successfullLife);
<span class="hljs-comment">// output :- ["discipline","consistency","hardwork","gratitude","humbleness"]</span>
</code></pre>
<h2 id="heading-pop-method">pop() Method</h2>
<p>Now this method is very interesting In life there is always a time when you have to pop out the things that hold onto habits that don’t serve us (like procrastination, self-doubt, or negativity).</p>
<p>Using <code>pop()</code>, we remove the last bad habit that’s holding us back.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> badHabits = [<span class="hljs-string">"procrastination"</span>, <span class="hljs-string">"self-doubt"</span>, <span class="hljs-string">"overthinking"</span>];
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Before:"</span>, badHabits);
<span class="hljs-comment">// "Output" : ["procrastination", "self-doubt", "overthinking"];</span>

badHabits.pop();  <span class="hljs-comment">// Removes "overthinking"</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"After:"</span>, badHabits);
<span class="hljs-comment">// "After" : ["procrastination", "self-doubt"];</span>

<span class="hljs-comment">// Also to get the popped value store it inside the variable</span>
<span class="hljs-keyword">let</span> removedHabit  = badHabits.pop(); 
<span class="hljs-built_in">console</span>.log(removedHabit) <span class="hljs-comment">// "self-doubt"</span>
</code></pre>
<blockquote>
<p><em>"Life is like an array. To grow, sometimes you need to</em> <code>push()</code> good habits and <code>pop()</code> the things that no longer serve you." 🚀</p>
</blockquote>
<h2 id="heading-unshift-method">unshift() Method</h2>
<p>To achieve any goal, you need to break it down into smaller steps and prioritize them in order of importance. Similarly, in JavaScript, the <code>unshift</code> method adds an element to the beginning of an array, just like how high-priority tasks should come first in a plan.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// goal is to make a project</span>
<span class="hljs-comment">// let suppose i prepare the array</span>
<span class="hljs-keyword">let</span> tasks = [<span class="hljs-string">"Test the app"</span>, <span class="hljs-string">"Deploy the app"</span>];
<span class="hljs-built_in">console</span>.log(tasks);
<span class="hljs-comment">// Output: ["Test the app", "Deploy the app"]</span>
<span class="hljs-comment">/*
Now, you realize that "Plan the project" should come first, 
as it's the foundation for everything else.
You use unshift to add it at the beginning:
*/</span>
tasks.unshift(<span class="hljs-string">"Plan the project"</span>);
<span class="hljs-built_in">console</span>.log(tasks);
<span class="hljs-comment">// Output: ["Plan the project", "Test the app", "Deploy the app"]</span>
</code></pre>
<h2 id="heading-shift-method">shift() Method</h2>
<p><strong>In Life</strong> sometimes, we grow certain beliefs, habits, or people that were once important.<br /><strong>In JavaScript:</strong> <code>shift()</code> removes the <strong>first</strong> element of an array</p>
<p>When we’re young, we might focus too much on <strong>seeking validation</strong> from others. But as we mature, we realize it's not necessary, so we <strong>remove it</strong> from our mindset.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mindset = [<span class="hljs-string">"seeking validation"</span>, <span class="hljs-string">"confidence"</span>, <span class="hljs-string">"growth"</span>];

mindset.shift();  <span class="hljs-comment">// Removing "seeking validation"</span>
<span class="hljs-built_in">console</span>.log(mindset);
<span class="hljs-comment">// ["confidence", "growth"]</span>
</code></pre>
<h2 id="heading-flat-method">flat() Method</h2>
<p>Sometimes, life feels complicated and cluttered. In those moments, you just need to <strong>calm down and simplify things</strong> , just like how JavaScript uses the <code>flat()</code> method to flatten nested arrays, making them easier to manage.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> life = [
  [<span class="hljs-string">"Work"</span>, <span class="hljs-string">"Meetings"</span>], 
  [<span class="hljs-string">"Family"</span>, [<span class="hljs-string">"Spending time"</span>, <span class="hljs-string">"Helping parents"</span>]], 
  [<span class="hljs-string">"Health"</span>, [<span class="hljs-string">"Exercise"</span>, <span class="hljs-string">"Healthy eating"</span>]], 
  [<span class="hljs-string">"Personal growth"</span>, [<span class="hljs-string">"Learning new skills"</span>]]
];
<span class="hljs-keyword">let</span> simplifiedLife = life.flat();
<span class="hljs-built_in">console</span>.log(simplifiedLife);
<span class="hljs-comment">//['Work', 'Meetings', 'Family', Array(2), 'Health', Array(2), 'Personal growth', Array(1)]</span>
<span class="hljs-comment">/*
here () is empty means it will flat out the nested array what at what depth that 
is now known so it flats for only depth 1 so we can specify that
*/</span>
 simplifiedLife = life.flat(<span class="hljs-number">2</span>);
<span class="hljs-comment">//['Work', 'Meetings', 'Family', 'Spending time', 'Helping parents', 'Health', 'Exercise', 'Healthy eating', 'Personal growth', 'Learning new skills']</span>
</code></pre>
<h2 id="heading-includes-method">includes() method</h2>
<p>When you apply for a job and submit your resume, modern ATS (Applicant Tracking System) software scans it to check whether it includes specific keywords related to the job.</p>
<p>Similarly, in JavaScript, the <code>includes()</code> method checks whether an array or string contains a specific value.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> resume = [<span class="hljs-string">"HTML"</span>,<span class="hljs-string">"CSS"</span>,<span class="hljs-string">"JAVASCRIPT"</span>];
<span class="hljs-built_in">console</span>.log(resume.includes(<span class="hljs-string">"JAVASCRIPT"</span>);) <span class="hljs-comment">// true;</span>
<span class="hljs-built_in">console</span>.log(resume.includes(<span class="hljs-string">"REACT"</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<h2 id="heading-splice-method">splice() Method</h2>
<p>To improve us as a being we often need to either remove certain habits or replace them with something that add values to our life ,</p>
<p>In JavaScript there is array method names <code>splice()</code> that changes the contents of an array by removing or replacing existing elements and/or adding new elements in place (Uski Jagah)</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> habits = [<span class="hljs-string">"Pray"</span>,<span class="hljs-string">"Workout"</span>,<span class="hljs-string">"Study"</span>,<span class="hljs-string">"Eating Junk Foods"</span>,<span class="hljs-string">"Watching Insta Reels"</span>]
<span class="hljs-comment">// lets see how we can insert new habit</span>
habits.splice(<span class="hljs-number">2</span>,<span class="hljs-number">0</span>,<span class="hljs-string">"Toucing Parents Feet"</span>) <span class="hljs-comment">// (startIndex,deletcount,item1);</span>
<span class="hljs-built_in">console</span>.log(habits)
<span class="hljs-comment">//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Eating Junk Foods', 'Watching Insta Reels']</span>

<span class="hljs-comment">// lets remove the habit</span>
habits.splice(<span class="hljs-number">5</span>,<span class="hljs-number">1</span>)
<span class="hljs-built_in">console</span>.log(habits)
<span class="hljs-comment">//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Eating Junk Foods']</span>

<span class="hljs-comment">// lets replace one habit</span>
habits.splice(<span class="hljs-number">4</span>,<span class="hljs-number">1</span>,<span class="hljs-string">"Eating Healthy Food"</span>)
<span class="hljs-built_in">console</span>.log(habits)
<span class="hljs-comment">//['Pray', 'Workout', 'Toucing Parents Feet', 'Study', 'Healthy Food']</span>
</code></pre>
<h2 id="heading-reverse-method">reverse() method</h2>
<p>Life often moves <strong>forward</strong>, and we focus on the present and future. But sometimes, we need to <strong>look back</strong> at where we started—to reflect on our past experiences, lessons, and growth.</p>
<p>This is similar to how <code>reverse()</code> in JavaScript flips the order of elements in an array, making us reverse things from the <strong>end to the beginning</strong>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> lifeJourney = [<span class="hljs-string">"Struggles"</span>, <span class="hljs-string">"Learning"</span>, <span class="hljs-string">"Small Wins"</span>, <span class="hljs-string">"Success"</span>];
lifeJourney.reverse();
<span class="hljs-built_in">console</span>.log(lifeJourney); 
<span class="hljs-comment">// Output: ["Success", "Small Wins", "Learning", "Struggles"]</span>
</code></pre>
<h2 id="heading-find-method">find() Method</h2>
<p>Imagine you’re swiping through <strong>Tinder 😁</strong>, looking for <strong>the perfect match</strong>. You have a list of potential people, but you're only interested in the first person who <strong>has a cute dog</strong> (because you love dogs!).</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> tinderProfiles = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"AngelPriya"</span>, <span class="hljs-attr">hasDog</span>: <span class="hljs-literal">true</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Sonam Gupta"</span>, <span class="hljs-attr">hasDog</span>: <span class="hljs-literal">false</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Ava Adams"</span>, <span class="hljs-attr">hasDog</span>: <span class="hljs-literal">true</span> }
];

<span class="hljs-keyword">const</span> matchedProfile = tinderProfiles.find(<span class="hljs-function">(<span class="hljs-params">profile</span>)=&gt;</span>profile.hasDog === <span class="hljs-literal">true</span>)
<span class="hljs-built_in">console</span>.log(matchedProfile);
<span class="hljs-comment">// "AngelPriya" -- &gt; returned the first matching profile with dog</span>
</code></pre>
<h2 id="heading-filter-method">filter() Method</h2>
<p>To be successful, one of the key factors is your circle the group of people you surround yourself with. The people around you have a huge impact on your success. That’s why filtering out the good and bad influences in your circle is so important.</p>
<p>Similarly, in JavaScript, there's an array method called <code>filter()</code> that allows you to filter out elements based on certain conditions, keeping only those that are important or relevant</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> socialCircle = [
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Amit"</span>, <span class="hljs-attr">isPositive</span>: <span class="hljs-literal">true</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Raj"</span>, <span class="hljs-attr">isPositive</span>: <span class="hljs-literal">false</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Priya"</span>, <span class="hljs-attr">isPositive</span>: <span class="hljs-literal">true</span> },
  { <span class="hljs-attr">name</span>: <span class="hljs-string">"Neha"</span>, <span class="hljs-attr">isPositive</span>: <span class="hljs-literal">false</span> }
];

<span class="hljs-keyword">let</span> positiveCircle = socialCircle.filter(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> person.isPositive === <span class="hljs-literal">true</span>);

<span class="hljs-built_in">console</span>.log(positiveCircle);
<span class="hljs-comment">// Output: [ { name: 'Amit', isPositive: true }, { name: 'Priya', isPositive: true } ]</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>JavaScript array methods teaches us more than just coding, they reflect life!. Life, like arrays, is about how we handle what’s in front of us. Keep coding and keep growing! 💻💡.</p>
<p>Hope you understand it ,do give it a like ❤ and also share with your fellow coders.</p>
]]></content:encoded></item><item><title><![CDATA[Guide On HTTP Status Codes]]></title><description><![CDATA[Understand HTTP status codes for efficient REST API interaction. Learn success, error, and redirection codes essential for developers]]></description><link>https://srvjha.hashnode.dev/guide-on-http-status-codes</link><guid isPermaLink="true">https://srvjha.hashnode.dev/guide-on-http-status-codes</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[http]]></category><category><![CDATA[Developer]]></category><category><![CDATA[internet]]></category><category><![CDATA[HTTP Status]]></category><category><![CDATA[code]]></category><category><![CDATA[Browsers]]></category><category><![CDATA[server]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sat, 18 Jan 2025 11:15:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737198857608/a373256b-82d5-419b-8aa8-c6111847a830.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Whenever the client (browser) sends a REST (GET, POST…) request to the server, the server responds back, generally with a JSON response that includes some kind of status code. We will learn about these status codes, but they reflect the state of the response the server sends back. So, what is it, and how does it work?</p>
<h2 id="heading-what-is-http-status-code">What is HTTP Status Code ?</h2>
<p>HTTP response status codes show if a specific HTTP request was completed successfully or not. The responses are divided into five groups.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737197983553/333597ce-6422-434e-bfdf-0d6d62f3192a.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-common-http-status-codes-every-developer-should-know-when-building-rest-endpoints">Common HTTP Status Codes Every Developer Should Know When Building REST Endpoints</h3>
<h4 id="heading-1xx-informational"><strong>1xx: Informational</strong></h4>
<ul>
<li><p><strong>100 Continue</strong><br />  The server has received the request headers and is waiting for the rest of the request. It’s a signal to proceed.</p>
<p>  <strong><em><mark>Its like You call a friend and ask, “Should I tell you the details now?” They respond, “Go ahead, I’m listening.”<br />The server is saying, “I’ve got the initial request, you can send more information.”</mark></em></strong></p>
</li>
</ul>
<h4 id="heading-2xx-success"><strong>2xx: Success</strong></h4>
<ul>
<li><p><strong>200 OK</strong><br />  Everything went smoothly. The server successfully processed the request, and the response contains the requested data.</p>
<p>  <strong><em><mark>This code is like when you ask a friend for a pen and he hands you immediately</mark></em></strong></p>
</li>
<li><p><strong>201 Created</strong><br />  A new resource has been successfully created. For example, after creating a new user account, the server might return this.</p>
<p>  <strong><em><mark>You ask a friend to bake a cake, and they say, “Done! Here’s the cake you wanted.”</mark></em></strong></p>
</li>
</ul>
<h4 id="heading-3xx-redirection"><strong>3xx: Redirection</strong></h4>
<ul>
<li><p><strong>301 Moved Permanently</strong><br />  The requested resource has been permanently moved to a new URL. Browsers will automatically redirect to the new location.</p>
<p>  <strong><em><mark>You ask for your friend at their old house, but someone tells you, “They’ve moved to a new address permanently.</mark></em></strong></p>
</li>
<li><p><strong>302 Found</strong><br />  Temporarily redirects the user to another URL, but the original URL should still be used for future requests.</p>
<p>  <strong><em><mark>You go to a restaurant for pizza, but they say, “Today, we’re serving it at our other branch just for today.</mark></em></strong></p>
</li>
</ul>
<h4 id="heading-4xx-client-errors"><strong>4xx: Client Errors</strong></h4>
<ul>
<li><p><strong>400 Bad Request</strong><br />  The server couldn’t understand the request due to invalid syntax. Double-check your input or request format.</p>
<p>  <strong><em><mark>You try to book a movie ticket but provide an invalid date like February 30th. The system says, “I don’t understand this</mark></em></strong></p>
</li>
<li><p><strong>401 Unauthorized</strong><br />  Authentication is required to access the resource. For example, you might need to log in before viewing a page.</p>
<p>  <strong><em><mark>You try to enter a club without a valid ID, and the bouncer says, “No entry without proof of identity.</mark></em></strong></p>
</li>
<li><p><strong>403 Forbidden</strong><br />  The server understood the request but refuses to fulfill it. This often occurs when you lack necessary permissions.</p>
<p>  <strong><em><mark>When you are a “green flag” but still she rejects you</mark></em></strong></p>
</li>
<li><p><strong>404 Not Found</strong><br />  The server couldn’t find the requested resource. It might be due to a broken link or an incorrect URL.</p>
<p>  <strong><em><mark>You ask for your favorite book at a library, but the librarian says, “Sorry, we don’t have that here.</mark></em></strong></p>
</li>
</ul>
<h4 id="heading-5xx-server-errors"><strong>5xx: Server Errors</strong></h4>
<ul>
<li><p><strong>500 Internal Server Error</strong><br />  The server encountered an error it couldn’t handle. It’s usually an issue on the server side.</p>
<p>  <strong><em><mark>You’re trying to fix a car, but something inside breaks, and you can’t continue</mark></em></strong></p>
</li>
<li><p><strong>503 Service Unavailable</strong><br />  The server is temporarily unable to handle the request. This might be due to maintenance or high traffic.</p>
<p>  <strong><em><mark>When you call her , but her phone says, “I’m busy right now, try again later.</mark></em></strong></p>
</li>
</ul>
<h3 id="heading-to-learn-about-more-status-code-kindly-refer-this-site-all-status-codeshttpsdevelopermozillaorgen-usdocswebhttpstatus">To learn about more status code kindly refer this site : <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status">All status codes</a></h3>
<h2 id="heading-conclusion">Conclusion</h2>
<p><strong>That’s its!</strong> Knowing your HTTP status codes is a game-changer when building REST endpoints. They help you understand what’s going wrong and how to fix it. Whether it’s a simple success (200 OK) or a server meltdown (500 Error), these codes give you the clues you need to keep things running smoothly. Keep them in mind next time you're coding!</p>
<p>If you got something valuable from it do give it a like ❤ and share your thought on the comments</p>
<h3 id="heading-connect-with-me">Connect with Me 👇</h3>
<p><a target="_blank" href="https://github.com/srvjha"><strong><em>Github</em></strong></a></p>
<p><a target="_blank" href="https://www.linkedin.com/in/saurav-jha-a30362196/"><strong><em>LinkedIn</em></strong></a></p>
]]></content:encoded></item><item><title><![CDATA[How does TCP 3-Way Handshake work ?]]></title><description><![CDATA[Learn how the TCP 3-Way Handshake establishes reliable connections for secure, ordered data transmission between devices]]></description><link>https://srvjha.hashnode.dev/how-does-tcp-3-way-handshake-work</link><guid isPermaLink="true">https://srvjha.hashnode.dev/how-does-tcp-3-way-handshake-work</guid><category><![CDATA[TCP]]></category><category><![CDATA[tcp/ip-model]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[TCP Handshake]]></category><category><![CDATA[internet]]></category><category><![CDATA[protocols]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Sat, 18 Jan 2025 08:55:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737190452812/7659651d-f2b6-440f-9da4-88e79c4057af.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Whenever there is some exchange of information between two hardware/software one thing that everyone is aiming for is reliability means that the data has sent successfully without any loss of piece of data in between irrespective of time the main focus is ensuring that the data is pass on properly while also maintaining security throughout the process. Here comes the role TCP 3 way Handshake that ensures all this , In this article we will explore all this so lets dive into it</p>
<h2 id="heading-what-is-tcp-transmission-control-protocol">What is TCP ? (Transmission Control Protocol)</h2>
<p>Before understanding the 3-way handshake, let’s quickly brush up on TCP. Transmission Control Protocol, as the name suggests, is a protocol that ensures reliability over speed, and it works on the principle of the 3-way handshake.</p>
<p>Now, you might be wondering, why is this 3-way handshake even required? Let’s break it down. Whenever there’s an exchange of data between two hardware/software, the main goal is to ensure that the data transfers in the correct order.</p>
<p>Wait, what does "correct order" even mean? The data is sent in the form of packets, and these packets are essentially pieces of data with sequence numbers. Sending them sequentially ensures that the data is received in the right order, maintaining reliability. That’s where the 3-way handshake comes in—it’s the process that ensures reliability during data transmission.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737186965170/ffd14891-ab4b-4191-ba1a-986830664794.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-how-3-way-handshake-works">How 3 Way Handshake Works ?</h2>
<p>Suppose you're on a Zoom meeting, and the first thing you say is, "Hey, can you hear me?" The other person replies, "Yes, I can hear you. Can you hear me?" Notice how the first person confirmed they can hear but also asked, "Can you hear me?" in return. To that, you replied, "Yes, I can hear you." This way, in 3 steps a reliable connection is built. Now, let's merge this analogy with the concept.</p>
<p>So here are the 3 steps :</p>
<ol>
<li><p><strong>SYN (Synchronize)</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737187904304/024738f9-d6c2-404c-8849-cbb969e6adb4.png" alt class="image--center mx-auto" /></p>
<p> The client sends a <mark>SYN </mark> (Synchronize) message to the server, requesting a connection. This SYN message also includes a sequence number, which determines the order of data packets to be sent.</p>
</li>
<li><p><strong>SYN(Synchronize) + ACK(Acknowledgement)</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737188451106/04ac5872-f697-4b52-b84c-58d3695da713.png" alt class="image--center mx-auto" /></p>
<p> Now the Server responds back with <mark>SYN </mark> (Synchronize) means it also wants to make a connection with the client and sends <mark>ACK</mark>(Acknowledgment) telling that i have received you information</p>
</li>
<li><p><strong>ACK (Acknowledgement)</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737188744516/8ba50890-c974-4cae-a434-44f1924ea6ef.png" alt class="image--center mx-auto" /></p>
<p> The client also acknowledges <mark>ACK </mark> the server's response, indicating that it has received the SYN-ACK. This final acknowledgment completes the handshake and signals that a reliable connection has been established. At this point, both the client and server are ready to exchange data.</p>
</li>
</ol>
<h2 id="heading-how-tcp-handles-packet-loss-during-transmission">How TCP handles packet loss during transmission ?</h2>
<p>Lets first understand the problem here whenever suppose there is data packets transfer happening there number of things can happen that can lead to packet loss</p>
<ul>
<li><p><strong>Network Congestion</strong>: When too much data flows through the network, routers may drop packets to handle the load.</p>
</li>
<li><p><strong>Hardware Issues</strong>: Faulty cables, routers, or switches can cause packet loss.</p>
</li>
<li><p><strong>Signal Interference</strong>: In wireless networks, interference from other devices can lead to packet loss.</p>
</li>
<li><p><strong>Routing Errors</strong>: Incorrect or unstable routing can cause packets to be lost in transit.</p>
</li>
<li><p><strong>Software Bugs</strong>: Malfunctions in network devices or protocols may result in dropped packets</p>
</li>
</ul>
<p>Now the question comes to your mind how the server know or client know there is a packet loss happened so after a packet is sent, the receiver sends an acknowledgment (ACK) back to the sender, confirming that the packet was received successfully. If the sender doesn't receive an ACK within a certain timeout period, it assumes the packet was lost. so don’t worry it is which is known for reliability so here are the ways to handle packet loss</p>
<ul>
<li><strong>Retransmissions</strong>:<br />  When the sender detects packet loss (due to a timeout or duplicate ACKs), it retransmits the lost packet. This ensures that the missing data is delivered.</li>
</ul>
<ul>
<li><p><strong>Timeouts</strong>:<br />  TCP uses a <strong>retransmission timeout (RTO)</strong>. If an ACK for a packet isn't received within the RTO period, the sender retransmits the packet. TCP dynamically adjusts this timeout based on network conditions.</p>
</li>
<li><p><strong>Duplicate ACKs</strong>:<br />  If the receiver detects a missing packet, it continues to send duplicate ACKs for the last successfully received packet. When the sender gets <strong>three duplicate ACKs</strong>, it triggers <strong>fast retransmission</strong>—resending the missing packet without waiting for the timeout.</p>
</li>
<li><p><strong>Flow Control</strong>:<br />  TCP uses a sliding window protocol to manage the amount of data sent before waiting for an acknowledgment. If packet loss is detected, the window size may be reduced to prevent overwhelming the network.</p>
</li>
<li><p><strong>Congestion Control</strong>:<br />  TCP assumes packet loss is often due to network congestion <mark>(this is just a jargon word it is basically a </mark> <strong><mark>Network traffic jam)</mark></strong>. It uses algorithms like <strong>Slow Start</strong>, <strong>Congestion Avoidance</strong>, <strong>Fast Retransmit</strong>, and <strong>Fast Recovery</strong> to adjust the sending rate and avoid further congestion:</p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>To wrap it up, the 3-way handshake is like a quick check to make sure both sides are ready before sending data. It makes sure everything is in order, and no info gets messed up along the way. I hope you understand the concept much simpler way then what you learned till now</p>
<p>If you got something valuable from it do give it a like ❤</p>
<h3 id="heading-connect-with-me">Connect with Me 👇</h3>
<p><a target="_blank" href="https://github.com/srvjha"><strong><em>Github</em></strong></a></p>
<p><a target="_blank" href="https://www.linkedin.com/in/saurav-jha-a30362196/"><strong><em>LinkedIn</em></strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Quick Guide: Understanding the OSI Model and Network Protocols]]></title><description><![CDATA[Learn about the OSI Model layers and network protocols for efficient computer communication through this detailed guide]]></description><link>https://srvjha.hashnode.dev/quick-guide-understanding-the-osi-model-and-network-protocols</link><guid isPermaLink="true">https://srvjha.hashnode.dev/quick-guide-understanding-the-osi-model-and-network-protocols</guid><category><![CDATA[OSI Model]]></category><category><![CDATA[network protocols]]></category><category><![CDATA[internet]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Fri, 17 Jan 2025 20:51:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737146999047/0fea8d05-197e-4984-b046-65eb49655192.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The OSI Model helps standardize the way computer systems send information to each other. In this article, you will learn about the seven layers of the OSI Model, their purposes, and the network protocols used by the model to send and receive data across different devices.</p>
<h2 id="heading-what-is-osi-model">What is OSI Model ?</h2>
<p>OSI Model consists of 7 layers</p>
<p>Here layer means way of categorizing different functionalities of a network.</p>
<p>In OSI model this layers are categorized based on most tangible(Physical) to least tangible, All the details and inner workings of all the other layers are hidden from the end user.</p>
<p>How to remember all the names of the layers? Easy 😎</p>
<ul>
<li><p><strong>Please</strong>: <strong>Physical Layer</strong></p>
</li>
<li><p><strong>Don't</strong>: <strong>Data Link Layer</strong></p>
</li>
<li><p><strong>Nag</strong>: <strong>Network Layer</strong></p>
</li>
<li><p><strong>The</strong>: <strong>Transport Layer</strong></p>
</li>
<li><p><strong>Smart</strong>: <strong>Session Layer</strong></p>
</li>
<li><p><strong>Pretty</strong>: <strong>Presentation Layer</strong></p>
</li>
<li><p><strong>Aunt</strong>: <strong>Application Layer</strong></p>
</li>
</ul>
<h3 id="heading-physical-layer">Physical Layer</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737143436752/6d455c7a-0772-45af-9b83-cf0e653ca00e.png" alt class="image--center mx-auto" /></p>
<p>The Physical Layer is like the foundation of a building. It’s responsible for the basic connection between devices, whether using cables, wires, or wireless signals like Wi-Fi. It works with data in its simplest form—tiny bits (0s and 1s).</p>
<p>When sending data, this layer ensures the bits travel from one device to another. When receiving data, it takes the signals (like light, electricity, or radio waves), turns them into bits, and passes them to the next layer to make sense of it. Devices like hubs, repeaters, modems, and cables work at this layer to keep the connection going.</p>
<h3 id="heading-data-link-layer">Data Link Layer</h3>
<p>This Layer defines how data is formatted for transmission, how much data can flow between nodes, for how long, and what to do when errors are detected in this flow. Ok making it more simpler first it defines the format of how the data will be send to the next layer ensuring its error free below the flowchart that would clear you the concept</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737144942756/5bb624d4-b684-44f2-8c5b-337b22c7a00a.png" alt class="image--center mx-auto" /></p>
<p>This flowchart appears to represent how data flows between two devices in a network using the OSI model .Here's a explanation of what is happening:</p>
<ol>
<li><p><strong>Data Flow (Left to Right):</strong></p>
<ul>
<li><p><strong>H2 (Header):</strong> This represents additional information added to the data by the current layer before passing it to the next layer. For instance, the Transport Layer might add port numbers, and the Network Layer might add IP addresses.</p>
</li>
<li><p><strong>Data:</strong> This is the actual content or payload being transmitted.</p>
</li>
<li><p><strong>T2 (Trailer):</strong> This is added information, usually at the end of the data, for things like error checking or ensuring proper delivery.</p>
</li>
</ul>
</li>
<li><p><strong>Sending Side (Left):</strong><br /> The process starts at the top layer and moves down through the layers. Each layer may add its own <strong>H2 (Header)</strong> and <strong>T2 (Trailer)</strong> to the data as it prepares it for transmission.</p>
</li>
<li><p><strong>Receiving Side (Right):</strong><br /> When the data reaches the receiving device, it moves upward through the layers. Each layer processes the data, removes its <strong>H2 (Header)</strong> and <strong>T2 (Trailer)</strong>, and passes the cleaned data to the next layer until it reaches the top layer.</p>
</li>
</ol>
<p><strong><em>Headers and trailers are like envelopes for data. They carry instructions or metadata needed to ensure proper delivery.</em></strong></p>
<h3 id="heading-network-layer">Network Layer</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737145229024/7ce5e1a0-f408-4797-83fa-5685c42bd7ae.png" alt class="image--center mx-auto" /></p>
<p>This layer determine which route is suitable from source to destination. This function of the network layer is known as routing ,It acts like a GPS for data, determining the best route for it to travel across different networks using routers. It assigns unique IP addresses <strong>(logical addressing</strong>) to ensure data reaches the correct destination. The layer breaks data into <strong>packets</strong>, adds <strong>headers</strong> with source and destination IPs, and handles errors like packet loss to ensure successful delivery.</p>
<h3 id="heading-transport-layer">Transport Layer</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737145666621/bfa061af-b45a-4fa0-8d1e-b97412c3e8f0.png" alt class="image--center mx-auto" /></p>
<p>The <strong>Transport Layer</strong> ensures reliable data transfer between devices. It breaks data into segments, manages flow control to prevent overload, and provides error detection and correction. Protocols like TCP ensure reliability through 3 way handshake, while UDP focuses on faster, connectionless communication.</p>
<h3 id="heading-session-layer">Session Layer</h3>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20230405124947/communication.jpg" alt="Communication in Session Layer" /></p>
<p>The <strong>Session Layer</strong> is like meeting organizer. It is responsible for setting up, managing , and ending communication sessions between devices.</p>
<h4 id="heading-key-functions">Key Functions:</h4>
<ul>
<li><p><strong>Establishes Sessions:</strong> Starts a session for communication between devices.</p>
</li>
<li><p><strong>Maintains Sessions:</strong> Keeps the session active and handles interruptions.</p>
</li>
<li><p><strong>Ends Sessions:</strong> Closes the session once the communication is complete.</p>
</li>
<li><p><strong>Synchronization:</strong> Adds checkpoints during data transfer so it can resume from the last point in case of interruptions.</p>
</li>
</ul>
<p><strong><em>Think of it like a phone call. The session starts when you dial and connect, stays active while you talk, and ends when you hang up.</em></strong></p>
<h3 id="heading-presentation-layer">Presentation Layer</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737146025721/79290377-4d05-43b4-a3c3-0a6b798f0070.png" alt class="image--center mx-auto" /></p>
<p>The Presentation Layer functions as a translator, ensuring smooth communication between systems by formatting data into standard formats like text or images. It secures transmissions through encryption and decryption while also compressing data to improve transmission speed and efficiency. These functions enable compatibility and optimized communication across different systems.</p>
<h3 id="heading-application-layer">Application Layer</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737146219867/61be05e9-ccbd-465a-89f7-f2d27b0b02ec.png" alt class="image--center mx-auto" /></p>
<p>The <strong>Application Layer</strong> is the user-facing layer where communication starts. It provides services like file transfer, email, and web browsing.</p>
<h4 id="heading-key-functions-1">Key Functions:</h4>
<ul>
<li><p><strong>Network Services:</strong> Interfaces with applications like browsers or email clients.</p>
</li>
<li><p><strong>User Interaction:</strong> Enables users to send or receive data.</p>
</li>
</ul>
<p><strong>Examples:</strong><br />HTTP (web browsing), FTP (file transfer), SMTP (email).</p>
<p><strong><em>It’s like the apps on your phone—your direct way of interacting with the system and the network.</em></strong></p>
<h2 id="heading-list-of-protocols-for-each-osi-layer">List of protocols for each OSI layer:</h2>
<ol>
<li><p><strong>Physical Layer</strong>: Ethernet, USB, Bluetooth, DSL.</p>
</li>
<li><p><strong>Data Link Layer</strong>: MAC, ARP, PPP, Wi-Fi (IEEE 802.11).</p>
</li>
<li><p><strong>Network Layer</strong>: IP (IPv4/IPv6), ICMP, IGMP.</p>
</li>
<li><p><strong>Transport Layer</strong>: TCP, UDP.</p>
</li>
<li><p><strong>Session Layer</strong>: NetBIOS, PPTP, RPC.</p>
</li>
<li><p><strong>Presentation Layer</strong>: SSL/TLS, JPEG, MPEG.</p>
</li>
<li><p><strong>Application Layer</strong>: HTTP, FTP, SMTP, DNS, POP3.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>If you have reached here that means now you have understood how each OSI layer plays a unique role in this process, from physical transmission to application services, creating an efficient and secure system for data exchange. Understanding these protocols helps in building and maintaining reliable networks.</p>
<p>If you liked my content, do give it a like ❤️ and share it with your friends! 🚀</p>
<h3 id="heading-connect-with-me">Connect with Me 👇</h3>
<p><a target="_blank" href="https://github.com/srvjha"><strong><em>Github</em></strong></a></p>
<p><a target="_blank" href="https://www.linkedin.com/in/saurav-jha-a30362196/"><strong><em>LinkedIn</em></strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Client-Side Vs Server-Side : A Complete Guide For Beginners.]]></title><description><![CDATA[Client-side runs on the user's device; server-side runs on a distant server. Includes real-life analogies and communication concepts]]></description><link>https://srvjha.hashnode.dev/client-side-vs-server-side-a-complete-guide-for-beginners</link><guid isPermaLink="true">https://srvjha.hashnode.dev/client-side-vs-server-side-a-complete-guide-for-beginners</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Client Server Architecture]]></category><category><![CDATA[internet]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Fri, 17 Jan 2025 12:30:32 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737112982602/ae7755ee-f1fe-47d1-8b3f-4f4e65e6124c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Often when you heard terms like client and server it hard to distinguish what is the key difference between them because both are software or hardware components that communicate over a network to exchange information. But what's the key difference between them? So in this article you will get a clear picture of how it works and its architecture , will also a real life based analogies to get a complete grasp of it ! 🌐</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737112504444/988de930-0945-4d95-8f2f-faab4d5e28f2.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-what-is-client">What is Client ?</h2>
<p>Client is a device or application that request resources from the server, basically it means like in our browser when you requests a url from server the browser is called as client but the client can be of following types :</p>
<ul>
<li><p><strong>Web Browser</strong>: Requesting web pages from a web server (e.g., Chrome loading YouTube).</p>
</li>
<li><p><strong>Mobile App</strong>: Asking for data from an API.</p>
</li>
<li><p><strong>Workstation</strong>: Connecting to a shared printer.</p>
</li>
</ul>
<p>In short, the client is <strong>initiating the conversation</strong> to get something done.</p>
<h2 id="heading-what-is-server">What is Server ?</h2>
<p>It <strong>provides resources</strong> to a client. Think of it as the "provider" that fulfills the client’s requests. Some examples include:</p>
<ul>
<li><p><strong>Web Server</strong>: Hosting websites (e.g., Google’s servers delivering search results).</p>
</li>
<li><p><strong>Database Server</strong>: Storing and serving data.</p>
</li>
<li><p><strong>Game Server</strong>: Handling multiplayer connections and game states.</p>
</li>
</ul>
<p>Like when you do a freelance work the client specifes you certain requirement in its client and you are the provider that fulfills the reuirement so you are the server that serves the response</p>
<h2 id="heading-how-do-clients-and-servers-communicate-with-each-other"><strong>How Do Clients and Servers Communicate with Each Other?</strong></h2>
<p>The client and the server are the first two obvious participants in client-server communication. Lets understand few keywords that is most often whenever we talk about the communication</p>
<ul>
<li><p><strong>Requests</strong>: Requests are sent from the client to the server to provide information. For example when you want to visit onlyFans you will login using credentials.</p>
</li>
<li><p><strong>Response</strong>: A server's response to a client request is sent to the client in the form of a message.</p>
</li>
<li><p><strong>Service</strong>: A service is a specific task that the server makes available to the client, such as an <strong>weather API</strong> that provides real-time weather data.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737110659552/17434e50-a56f-45f5-ac8c-0488a2d50465.png" alt class="image--center mx-auto" /></p>
<p>Here don’t get confused when i wrote the REST APIs, Modern web services are often based on the REST architecture, which relies on the HTTP protocol and returns data in the JSON format.</p>
<p>In a client-server setup, <strong>clients</strong> handle tasks like user interaction, while <strong>servers</strong> manage heavy-duty work like data storage or processing. Communication between them follows rules set by protocols like <strong>TCP/IP</strong>, ensuring smooth data exchange.</p>
<ul>
<li><p><strong>TCP</strong> keeps the connection alive during a message exchange, handles data packets, and fixes issues like lost packets.</p>
</li>
<li><p>When many requests hit the server, it uses a <strong>priority-based scheduler</strong> to handle them efficiently.</p>
</li>
<li><p>Access to the server can be restricted with <strong>client access control tools</strong> to maintain security.</p>
</li>
</ul>
<p>In short, servers provide resources (like files or data), while clients request and use them.</p>
<h2 id="heading-real-life-scenarios-to-understand-client-sever-communication"><strong>Real-life Scenarios to understand Client Sever Communication</strong></h2>
<p>In your childhood When you wanted anything let say badminton so badminton here is the requested query and you are the client and lets understand each step after this</p>
<ul>
<li><p><strong>The Client:</strong> You, as a child, are the one making the request (e.g., "Papa, mujhe badminton😇").</p>
</li>
<li><p><strong>The API:</strong> Your mother acts as the API, interpreting your request and passing it to your father in a way he understands.</p>
</li>
<li><p><strong>The Server:</strong> Your father is the server who processes the request. He decides whether he can fulfill it or not.</p>
</li>
<li><p><strong>The Request:</strong> "I want Badminton!" is your <strong>GET request</strong>.</p>
</li>
<li><p><strong>The Response:</strong></p>
<ul>
<li><p>If your father agrees, he sends back a <strong>200 (OK)</strong> response along with the badminton.</p>
</li>
<li><p>If badminton is not available or he says no, you get a <strong>404 (Not Found)</strong> or even a <strong>403 (Forbidden)</strong>! 😅</p>
</li>
</ul>
</li>
<li><p><strong>The Endpoints:</strong> Your mother (API) ensures communication between you (client) and your father (server), so you don't directly interact with the backend logic!</p>
</li>
</ul>
<p>This way, your childhood wish fulfillment was a perfect example of a client-server relationship. Cool, right? 😎</p>
<h2 id="heading-fun-fact-peer-to-peer-p2p-vs-client-server">Fun Fact: Peer-to-Peer (P2P) vs Client-Server 🌀</h2>
<p>In a <strong>peer-to-peer (P2P)</strong> model, every device can act as both a client and a server. Think of file-sharing systems like BitTorrent. While the client-server model is hierarchical, P2P is decentralized.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>To sum up, the <strong>client</strong> is the requester, and the <strong>server</strong> is the provider. They communicate seamlessly to enable everything from browsing the web to playing multiplayer games. With this understanding, the next time you hear "client" and "server," you’ll know exactly who’s doing what!</p>
<p>If you got something valuable from it do give it a like ❤ as it motivates me write more articles</p>
<h3 id="heading-connect-with-me">Connect with Me 👇</h3>
<p><a target="_blank" href="https://github.com/srvjha"><strong><em>Github</em></strong></a></p>
<p><a target="_blank" href="https://www.linkedin.com/in/saurav-jha-a30362196/"><strong><em>LinkedIn</em></strong></a></p>
]]></content:encoded></item><item><title><![CDATA[How DNS Hierarchy Works: Exploring Root to Resolution]]></title><description><![CDATA[Explore the DNS system from root to resolution, understanding its hierarchical structure and crucial role in internet functionality]]></description><link>https://srvjha.hashnode.dev/how-dns-hierarchy-works-exploring-root-to-resolution</link><guid isPermaLink="true">https://srvjha.hashnode.dev/how-dns-hierarchy-works-exploring-root-to-resolution</guid><category><![CDATA[dns]]></category><category><![CDATA[dns resolver]]></category><category><![CDATA[internet]]></category><category><![CDATA[flowcharts]]></category><category><![CDATA[Google]]></category><category><![CDATA[caching]]></category><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Fri, 17 Jan 2025 06:01:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1737094079231/01aac91c-6ea3-4499-848d-9702a0d02cdc.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever thought ? what happens when you enter a query in that search bar ,Most of us don’t! But behind the scenes, a whole lot happens in the blink of an eye—within just milliseconds. ⚡ From your request to the response, there’s are lots of process at work, ensuring you get what you’re looking for.</p>
<p>In this article you will understood how important the role of DNS is in the world of internet and will go in depth of its hierarchical architecture</p>
<h2 id="heading-what-is-the-need-of-dns">What is the need of DNS?</h2>
<p>Before diving into what DNS (Domain Name System) is and how it works, let’s first understand why we need it.</p>
<p>DNS is essentially the <strong>internet’s address book</strong> 📖. Just like your phone’s contact list, where every contact has a name linked to a number, DNS maps complex IP addresses to domain names that are easier to read, write, and remember.</p>
<h3 id="heading-1-easier-access-to-websites">1. <strong>Easier Access to Websites</strong> 🖥️</h3>
<p>Instead of remembering complex IP addresses (like <code>192.168.1.1</code>), you can simply type user-friendly domain names like <a target="_blank" href="http://www.google.com">www.google.com</a>.</p>
<h3 id="heading-2-seamless-communication">2. <strong>Seamless Communication</strong> 🌐</h3>
<p>DNS translates human-readable domain names into machine-readable IP addresses so devices can locate and connect to websites or services.</p>
<h3 id="heading-3-scalability">3. <strong>Scalability</strong> 📈</h3>
<p>With millions of websites and services online, DNS helps efficiently manage the vast network by distributing queries across multiple servers, <strong>don’t worry about this point will go in detail how it works</strong></p>
<h2 id="heading-types-of-domain"><strong>Types of Domain</strong></h2>
<p>There are various kinds of domains:</p>
<ul>
<li><p><strong>Generic Domains:</strong> .com(commercial), .edu(educational), .mil(military), .org(nonprofit organization), .net(similar to commercial) all these are generic domains.</p>
</li>
<li><p><strong>Country Domain:</strong> .in (India) .us .uk</p>
</li>
<li><p><strong>Inverse Domain:</strong> if we want to know what is the domain name of the website. IP to domain name mapping. So DNS can provide both the mapping for example to find the IP addresses of <a target="_blank" href="https://www.google.com">google.com</a> then we have to type</p>
</li>
</ul>
<pre><code class="lang-bash">nslookup www.google.com
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737062920211/ad9baed4-c850-401e-bb90-a7b295937a1e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737064055382/dcba3ad2-564a-48ab-b30a-7a226df015e4.png" alt class="image--center mx-auto" /></p>
<p>Now lets understand how dns works as you have seen in the diagram when you request any url it first goes to the resolving nameserver</p>
<h3 id="heading-resolving-nameserver">Resolving Nameserver</h3>
<p><strong>Resolving nameserver</strong> is a type of DNS server responsible for finding the IP address associated with a domain name on behalf of a client (like your computer or browser). It's an intermediary between your device and the DNS system.</p>
<h3 id="heading-root-nameserver">Root Nameserver</h3>
<p><strong>Root nameserver</strong> is the top-most server in the DNS (Domain Name System) hierarchy. It is the first step in translating (resolving) a human-readable domain name (like <a target="_blank" href="http://www.youtube.com">www.youtube.com</a>) into a machine-readable IP address.</p>
<p>There are 13 logical root nameservers available across the globe</p>
<p>To know more about this 13 root nameservers Visit <a target="_blank" href="https://www.iana.org/domains/root/servers">https://www.iana.org/domains/root/servers</a></p>
<h3 id="heading-tld-nameserver">TLD Nameserver</h3>
<p><strong>TLD (Top-Level Domain) nameserver</strong> is a type of DNS server responsible for managing the second level in the DNS hierarchy. It handles requests for domain names that belong to a specific <strong>Top-Level Domain</strong>, such as <code>.com</code>, <code>.org</code>, <code>.net</code>, <code>.edu</code>, or country-specific TLDs like <code>.in</code>, <code>.uk</code>, or <code>.jp</code>.</p>
<h3 id="heading-ans-nameserver">ANS Nameserver</h3>
<p>An <strong>authoritative nameserver</strong> is the final stop in the DNS resolution process. It holds the actual DNS records for a domain, such as its IP address. This server is responsible for responding to DNS queries with the definitive answer about the domain.</p>
<h3 id="heading-dns-workflow-after-this-you-will-be-a-dns-pro">DNS Workflow (After this you will be a DNS Pro! 🚀)</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1737066996918/a761b85c-da37-4e5b-86db-23b08ec9b0da.png" alt class="image--center mx-auto" /></p>
<p>All the steps are self explanatory and also we have discussed this steps in details and i have explained the workflow step by step in this flowchart as well.</p>
<p>Now there some additonal DNS info you should know let dive into that</p>
<h3 id="heading-dns-lookup">DNS Lookup 📖</h3>
<p>In most simpler terms dns lookup is like finding the number in the contact list</p>
<p>When you type a website name (like <a target="_blank" href="http://www.google.com"><code>www.google.com</code></a>) into your browser:</p>
<ol>
<li><p>The browser asks the DNS system: <strong>"What's the IP address of this website?"</strong></p>
</li>
<li><p>The DNS system responds with the IP address (e.g., 142.250.190.78), which tells your browser where to find the website.</p>
</li>
</ol>
<h3 id="heading-dns-resolver">DNS Resolver 🌐</h3>
<p>A <strong>DNS Resolver</strong> is like a guide that helps your computer find the IP address of a website you want to visit.</p>
<p>Here's how it works:</p>
<ol>
<li><p>When you type a domain name (like <a target="_blank" href="http://www.google.com"><code>www.google.com</code></a>) in your browser, your computer doesn't know the IP address.</p>
</li>
<li><p>The <strong>DNS Resolver</strong> takes your request and starts the process of finding the IP address by contacting different DNS servers (like Root, TLD, and Authoritative servers). Refer the above flowchart you wil get better idea .</p>
</li>
<li><p>Once it gets the IP address, the resolver sends it back to your browser so it can load the website. 🚀</p>
</li>
</ol>
<h3 id="heading-dns-caching">DNS Caching 🗂️</h3>
<p>When your browser visits a website (e.g., <a target="_blank" href="http://google.com"><strong>google.com</strong></a>), it needs to find the address (IP address) of that website. DNS caching stores this address temporarily on your computer or network. Next time you visit the same site, your browser uses the cached address instead of asking the DNS server again, making things faster! ⚡🚀</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In short DNS provides unquie identification to ip address make it simpler for user to visit websites.🌐 Instead of remembering complex numbers, you just type the website name! It then follows a structured process to find the address from DNS servers through 3-4 steps. 🧩 As you've understood from the explanation above, DNS works like a pro behind the scenes to connect you to your favorite sites seamlessly.</p>
<p>I hope you've now mastered what DNS is all about! 💡 Keep exploring more about it. If you liked my content, do give it a like ❤️ and share it with your friends! 🚀</p>
<h3 id="heading-connect-with-me">Connect with Me 👇</h3>
<p><a target="_blank" href="https://github.com/srvjha"><strong><em>Github</em></strong></a></p>
<p><a target="_blank" href="https://www.linkedin.com/in/saurav-jha-a30362196/"><strong><em>LinkedIn</em></strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Complete Guide on Working of Internet]]></title><description><![CDATA[Discover how the internet works, from physical connections to data routing, protocols, servers, and real-time communication. Perfect for beginners]]></description><link>https://srvjha.hashnode.dev/complete-guide-on-working-of-internet</link><guid isPermaLink="true">https://srvjha.hashnode.dev/complete-guide-on-working-of-internet</guid><category><![CDATA[internet]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[http]]></category><category><![CDATA[https]]></category><category><![CDATA[dns]]></category><category><![CDATA[Browsers]]></category><category><![CDATA[data]]></category><dc:creator><![CDATA[Saurav Jha]]></dc:creator><pubDate>Wed, 15 Jan 2025 21:44:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1736977220149/925e27f2-efd5-4dba-9029-357d2d39e884.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Have you ever wondered what happens in the background when you send a WhatsApp message to your friend? 🤔📱</strong><br />How does it happen so easily that we don’t even notice the number of things happening in <strong>milliseconds</strong> during the transfer of data? 🌐⚡ If you’re thinking about all this, you’re absolutely in the <strong>right place! 🛤️✨</strong></p>
<p><strong>Let’s explore this in this article. 🚀💡</strong></p>
<p>This guide is for <strong>absolute beginners</strong>, so don’t worry if you don’t have any prior knowledge—we’ve got you covered. 🙌📘</p>
<h2 id="heading-what-is-internet"><strong>What is Internet? 🌐</strong></h2>
<p>Before we jump into the internet, let’s first understand what a <strong>network</strong> is and how it’s related to the internet. 🤝<br />A <strong>network</strong> is basically a collection of devices (like computers, routers, etc.) connected to each other to share information or resources. 🖥️📡 You can think of a computer network like your <strong>cohort group in Discord</strong> 🧑‍🤝‍🧑💬, where everyone shares and accesses each other's blogs.</p>
<p>The <strong>internet</strong>, on the other hand, is a <strong>vast collection of networks</strong> connected to each other. 🌍✨ In fact, the word "internet" comes from the concept of <strong>interconnected networks. 🔗</strong></p>
<h2 id="heading-working-of-internet">Working of Internet</h2>
<p><strong>Now that we understand the basic concept of the internet, let’s dive deeper into how these connections actually work. 🌐🔍</strong><br />When I mentioned the <strong>connection of devices</strong>, you might wonder what happens <strong>under the hood</strong> of this connection.💻 Let’s break it down <strong>step by step</strong>: 🛠️👇</p>
<h3 id="heading-physical-connections">Physical Connections</h3>
<p>Computer connect with each other via :</p>
<p><strong>Wires and Cables:</strong></p>
<ul>
<li><p><strong>Ethernet cables</strong> are used for smaller nearby distances that connects devices directly to network for fast and stable connection.</p>
</li>
<li><p><strong>Fiber-optic cables</strong> are used for longer distances that transmits data as light signals, enabling high-speed data transfer.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240926173133/Difference-between-Fiber-optic-cable-and-Copper-wire.webp" alt="Difference between Fiber optic cable and Copper wire - GeeksforGeeks" /></p>
<p><strong>Radio Waves:</strong></p>
<ul>
<li><p><strong>Wi-Fi</strong>: A wireless technology that allows devices to connect to the internet within a certain range using radio waves.</p>
</li>
<li><p><strong>Cellular Networks</strong>: Mobile data connects devices through 4G/5G networks, using radio waves to communicate with cell towers.</p>
</li>
</ul>
<p><img src="https://www.rfwireless-world.com/images/cellular-data-network-vs-wifi-data-network.webp" alt="cellular data vs wifi | Difference between cellular and wifi" class="image--center mx-auto" /></p>
<p>These connections form the foundation of how devices communicate with each other.🤝</p>
<h2 id="heading-now-we-will-understand-the-flow-of-how-the-internet-works">Now we will understand the flow of how the internet works. 🌐➡️📶</h2>
<h3 id="heading-internet-protocols"><strong>Internet Protocols 🖧</strong></h3>
<p>Every device on the internet communicates using a set of rules called <strong>protocols</strong>:</p>
<ul>
<li><p><strong>TCP/IP (Transmission Control Protocol/Internet Protocol):</strong></p>
<ul>
<li><p>Ensures <strong>reliability</strong> during the transfer of data packets. ✅</p>
</li>
<li><p>Splits data into <strong>small packets</strong> for transmission. 📦</p>
</li>
</ul>
</li>
<li><p><strong>HTTP/HTTPS (HyperText Transfer Protocol):</strong></p>
<ul>
<li><p>Used for browsing websites. 🌐</p>
</li>
<li><p><strong>HTTPS</strong> adds <strong>encryption</strong> for secure communication. 🔒</p>
</li>
</ul>
</li>
</ul>
<p>🔗 <strong>Learn more about protocols here:</strong> <a target="_blank" href="https://www.geeksforgeeks.org/types-of-internet-protocols/">GeeksforGeeks - Types of Internet Protocols</a></p>
<h3 id="heading-addresses-and-domain-names"><strong>Addresses and Domain Names 🏷️</strong></h3>
<p>Each device is assigned a unique identifier known as an <strong>IP address</strong> (like <code>192.168.0.1</code>). To make it easier for humans to access resources:</p>
<ul>
<li><p><strong>Domain Names</strong> (e.g., <a target="_blank" href="http://www.google.com"><code>www.google.com</code></a>) act as the <strong>contact list</strong> app in your phone. 📞</p>
</li>
<li><p><strong>DNS (Domain Name System)</strong> converts these names into <strong>IP addresses</strong> so devices know where to send requests. 🔄</p>
</li>
</ul>
<h3 id="heading-routing-data"><strong>Routing Data 🚀</strong></h3>
<p>Once the connection is established:</p>
<ol>
<li><p>The browser knows the <strong>address</strong> to send the data. It prepares the data along with metadata (<strong>extra information</strong>) and divides it into packets. ✂️📦</p>
</li>
<li><p>Packets are routed via multiple devices like:</p>
<ul>
<li><p><strong>Routers:</strong> Direct packets between networks, ensuring they follow the optimal path. 🛣️</p>
</li>
<li><p><strong>Switches:</strong> Manage packet transfer within a local network. 🔀</p>
</li>
</ul>
</li>
<li><p>Packets might pass through <strong>undersea cables</strong>, satellites, or multiple networks to reach their destination. 🌊🛰️</p>
</li>
</ol>
<h3 id="heading-servers-and-data-processing"><strong>Servers and Data Processing 💾</strong></h3>
<p>When packets reach their destination:</p>
<ul>
<li><p><strong>Servers</strong> (powerful computers) process the request. For example:</p>
<ul>
<li><p>A <strong>web server</strong> retrieves and sends a webpage. 🌐📤</p>
</li>
<li><p>A <strong>database server</strong> fetches or updates data. 📊</p>
</li>
</ul>
</li>
<li><p>The server then sends the response back as packets. 📨</p>
</li>
</ul>
<h3 id="heading-receiving-and-rendering-data"><strong>Receiving and Rendering Data 📥</strong></h3>
<p>The packets travel back to your device via the same or a different path:</p>
<ol>
<li><p>Your device <strong>reassembles</strong> the packets into their original form. 🛠️</p>
</li>
<li><p>Applications (e.g., a browser or video player) interpret the data:</p>
<ul>
<li><p>A <strong>web browser</strong> renders HTML, CSS, and JavaScript into a <strong>viewable webpage</strong>. 🖥️📄</p>
</li>
<li><p>A <strong>video player</strong> streams the video content. 🎥📺</p>
</li>
</ul>
</li>
</ol>
<h3 id="heading-bonus-info"><strong>Bonus Info 🎁</strong></h3>
<ul>
<li><p><strong>Real-Time Communication:</strong><br />  For activities like <strong>video streaming</strong> or <strong>online gaming</strong>:</p>
<ul>
<li>Data is sent and received continuously in real-time using protocols like <strong>UDP (User Datagram Protocol)</strong>, which prioritizes speed over reliability. 🕒⚡</li>
</ul>
</li>
<li><p><strong>Security and Encryption:</strong><br />  To ensure <strong>secure communication</strong>:</p>
<ul>
<li><p>Encryption (like <strong>SSL/TLS</strong>) protects sensitive data from interception. 🔐</p>
</li>
<li><p>Protocols like <strong>HTTPS</strong>, VPNs, and firewalls help safeguard information. 🛡️</p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-internet-workflow-for-query-search">Internet Workflow for Query Search</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1736973779739/13827c1a-653f-4f63-ae4b-70ee3e0770b7.png" alt class="image--center mx-auto" /></p>
<p>You must be wondering why the data packets travels to the nearest data centers ?</p>
<p>Google has data centers all over the world to:</p>
<ol>
<li><p><strong>Reduce Latency</strong>:</p>
<ul>
<li>A nearby server means the request doesn't have to travel far, making it faster.</li>
</ul>
</li>
<li><p><strong>Load Balancing</strong>:</p>
<ul>
<li>Google ensures traffic is distributed efficiently across their servers to avoid overloading any one data center.</li>
</ul>
</li>
<li><p>Caching:</p>
<ul>
<li>Many Google data centers have cached copies of popular queries and results. If the data you’re requesting is already available at the nearest data center, it can respond immediately, avoiding unnecessary delays.</li>
</ul>
</li>
</ol>
<h3 id="heading-what-happens-at-the-nearest-data-center"><strong>What Happens at the Nearest Data Center?</strong></h3>
<ul>
<li><p>The <strong>nearest data center</strong> receives your request and processes it.</p>
<ul>
<li>If the result for your query (e.g., "chaicode") is already <strong>cached</strong> at that data center, it immediately sends back the response.</li>
</ul>
</li>
</ul>
<p>If it isn’t cached, the data center might:</p>
<ul>
<li><p>Forward your request to another Google server where the full search index is stored.</p>
</li>
<li><p>Or fetch the necessary data from Google’s <strong>core infrastructure</strong> and send it back to you.</p>
</li>
</ul>
<p><strong><em>Imagine you’re ordering pizza: 🍕📦</em></strong></p>
<ul>
<li><p><em>You place your order at the</em> <strong><em>nearest branch</em></strong> <em>of a pizza chain (this is the</em> <strong><em>nearest data center</em></strong>*). 🏠📡*</p>
</li>
<li><p><em>If the branch already has the pizza type you want ready (<strong><strong>cached</strong></strong>), they immediately deliver it to you. 🚴‍♂️⚡</em></p>
</li>
<li><p><em>If they don’t have it, they contact the</em> <strong><em>main kitchen</em></strong> <em>(<strong><strong>core Google server</strong></strong>) to prepare the pizza, then deliver it to you via the</em> <strong><em>local branch</em></strong>*. 🏭➡️🏠🍕</p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>If you have reached here means now you know everything related to the internet how it works its workflow you’re now equipped with internet more effectively. Keep exploring, as the digital world has endless opportunities waiting for you! 🌐🚀</p>
<p>If you liked this article do give it a like and share with your friends❤</p>
<h3 id="heading-connect-with-me-on">Connect with me on 👇</h3>
<p><a target="_blank" href="https://x.com/J_srv001">LinkedIn</a></p>
<p><a target="_blank" href="https://x.com/J_srv001">Twitter</a></p>
]]></content:encoded></item></channel></rss>