<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kartik Joshi on Medium]]></title>
        <description><![CDATA[Stories by Kartik Joshi on Medium]]></description>
        <link>https://medium.com/@karthik.joshi103?source=rss-159f9833c07e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DaS5UcXRwYU2uS4z5BcQMQ.jpeg</url>
            <title>Stories by Kartik Joshi on Medium</title>
            <link>https://medium.com/@karthik.joshi103?source=rss-159f9833c07e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 01:23:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karthik.joshi103/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Make Sure AI-Generated Code Is Actually Correct]]></title>
            <link>https://medium.com/@karthik.joshi103/how-to-make-sure-ai-generated-code-is-actually-correct-3c0fae5f0c4f?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c0fae5f0c4f</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[agents]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Fri, 23 Oct 2026 04:49:45 GMT</pubDate>
            <atom:updated>2026-10-29T03:10:42.673Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Guide for AI code generation</strong></h4><ul><li>PR volume went up. line-by-line review didn’t scale with it. The fix is a framework, not reading faster.</li><li>Give AI the same onboarding you’d give a new hire. Design docs, conventions, existing utilities, before it writes a line.</li><li>Split the work across scoped passes. Feature dev, edge cases, performance, security, instead of one shot that tries to do everything.</li><li>Never let it pick a dependency unsupervised. Check what it actually installed before you trust it.</li><li>Tests and a quick manual run aren’t optional, even when the generated code looks clean.</li><li>Calibrate how hard you review based on how well you know the stack fundamentals carry you through the unfamiliar ones.</li></ul><p>The industry has switched to speed mode. As a team, you can now expect a flood of AI-generated code. Development velocity has jumped to rocket speed, and PRs with 50+ file changes are becoming normal. The problem is those PRs are basically unreviewable line by line, and none of that changes who’s accountable. As a developer, you still own whatever code you ship, AI-written or not.</p><p>That’s the real shift. It’s not “AI writes the code now, so review matters less.” It’s the opposite. Code review and validation matter <em>more</em>, because the volume went up and the bottleneck moved from typing speed to judgment. Here’s the framework I use to make sure what AI generates actually follows my system’s architecture, design principles, and best practices no anti-patterns and enough confidence that it’s correct for <em>my</em> codebase, not just correct in general.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8EAFjGGUOHNAoVP4IGQ9dQ.jpeg" /><figcaption>Coding with AI</figcaption></figure><h3>Part 1: Code Generation</h3><h4>Give it the onboarding you’d give a new hire.</h4><p>Whenever a new engineer joins a team, they get some form of knowledge transfer before they touch the codebase. How the product works, how the code is organized, what the conventions are. AI needs the same thing before you hand it a feature.</p><p>That means having an actual reference document. Design patterns in use, the utilities folder and what’s already built, the testing strategy, architecture diagrams, Postman collections, even screenshots of application flows. Without that, AI is guessing at your conventions from whatever file it happens to read next. With it, you’ve given it the same grounding you’d want from a new teammate.</p><h4>Split the work into specialized agents.</h4><p>I use four agents for four different concerns, in this order:</p><ol><li><strong>Feature development:</strong> fulfill the requirement, follow best practices, include tests, respect the existing codebase structure.</li><li><strong>Review / edge-case fixes: </strong>catch what the first pass missed. Null checks, unhandled branches, DRY/YAGNI/KISS violations, duplicate logic, dead code.</li><li><strong>Performance:</strong> now that it works in dev, does it hold up at production scale? Indexes, caching, arrays vs. maps, parallelism where it’s warranted.</li><li><strong>Security:</strong> input validation, authorization checks, known vulnerability patterns, sensitive data exposure.</li></ol><p>By the time code has gone through all four, you have real confidence it’s production-ready, and because each stage is a separate, scoped pass, the change stays feature-gated. removable, and it shouldn’t touch what already works.</p><h4>Pick your path before you start.</h4><p>There are two reasonable ways in:</p><ul><li>If you’re confident about the business requirements, write the tests first, covering the main scenarios, then move into planning mode.</li><li>If you’re not confident the requirements are fully nailed down, or you’re not sure tests written up front would even cover the real scope, skip straight to planning mode and use that conversation to pressure-test the approach.</li></ul><p>Either way, use a stronger model for the planning step. Talk through the approach with AI before writing code. Give it the system’s high-level design and the feature requirements, and actually brainstorm trade-offs rather than accepting the first plan. Once there’s a plan you trust, build it block by block. Asking AI to build an entire feature end-to-end in one shot produces bad code. It loses the thread. Breaking the feature into steps keeps each piece small enough for it to actually focus.</p><h4>Write skills for the mistakes you already know it’ll make</h4><p>You already know the recurring failure modes. AI defaults to useEffect for everything in React, whether or not it&#39;s the right tool. Left alone, it&#39;ll generate more tests than you need, including redundant or low-value ones. So write skills or rules that head those off before they happen. A React-best-practices skill, a &quot;write only the tests this actually needs&quot; rule, whatever matches the patterns you keep correcting by hand. There are open-source examples already out there (Vercel&#39;s being one) to borrow from. The broader point is that if you know AI tends to generate weak code in a specific area, encode the fix as a skill instead of catching it every single time in review.</p><h4>Don’t let it pick your dependencies unsupervised.</h4><p>This one’s easy to miss because the code around the import usually looks fine. AI will reach for a library to solve a problem, and it won’t reliably check whether that library is still maintained, whether it’s the current recommended approach, or whether the API it’s calling even matches the version actually in your package.json. Training data goes stale, and a model will confidently use a method signature from two major versions ago, or pull in some barely-maintained package instead of the one your ecosystem has actually standardized on.</p><p>So treat every new dependency AI introduces as its own review item, not a side effect of the feature. Force it to check the library’s latest official docs before you accept the import, not just generate code that assumes a remembered API. Look at when the package was last published, whether it has open security advisories, and whether there’s a more current or more widely-adopted alternative. This is a small amount of extra friction per dependency, and it’s cheap compared to finding out three months later that half your utils folder is built on something nobody maintains anymore.</p><h4>Understand before you touch.</h4><p>Generating code is only half of it. You’re accountable for it, so you need to actually understand what landed, at least the core logic, even if not every line. Ask AI to explain its own output, and if the reasoning doesn’t hold up, push back and ask for a different approach rather than accepting it because it looks plausible.</p><p>This matters even more when you’re dropped into an unfamiliar codebase and asked to build something. Start by asking AI for a high-level map of the system, and pull in any technical design docs that exist. Once you know roughly where the change belongs, go deeper on the surrounding code before committing to a plan.</p><p>Two habits worth forcing here:</p><ul><li><strong>Check for duplication first.</strong> AI is very good at reproducing something that already exists if there’s already a sortUsersByEmailit&#39;ll happily write another one instead of reusing it. Always ask it to check for an existing implementation before generating something new.</li><li><strong>Do the impact analysis yourself.</strong> If a shared function is being changed, AI is prone to skipping over everywhere else that function is called. That blast-radius check is on you.</li></ul><h3>Part 2: Code Validation</h3><p>Generating code that looks right is the easy part. Proving it actually works is where the real work is.</p><h4>Tests</h4><p>Run the actual suite. unit, integration, e2e, smoke — whatever applies. And make sure whatever AI generates is testable in the first place. Code that’s hard to test is usually a sign the design needs rework, not just the tests.</p><h4>Human validation</h4><p>This is the one that’s easiest to skip and most important not to. AI is very good at producing idiomatic, good-looking code and tests that look thorough in a way that can trap you into trusting it without checking. Run it locally. Hit the API through Postman if it’s a backend change, click through the UI if it’s a frontend one. That minimal manual pass is what actually gives you confidence it works, not just that it compiles and the tests pass.</p><h4>AI-assisted validation</h4><p>Validating through unit tests is one layer. A closer-to-real-world check is having AI write scripts that exercise the feature end-to-end, hitting the API locally, or working against a local database with limited, scoped access if the feature involves integration between systems. If that holds up, that’s a meaningfully stronger signal than tests alone.</p><h4>Calibrate review depth to how well you know the stack</h4><p>When you know the language or framework well, reviewing AI-generated code is pattern matching you already do instinctively. You know a flood of useEffect calls in React is a smell. You know await inside a forEach in Node.js doesn&#39;t actually wait for anything. You catch it immediately because you&#39;ve been burned by it before.</p><p>The harder case is a stack you don’t know well. Say AI hands you Go code that spins up a goroutine for every use case without a WaitGroup anywhere in sight. If you&#39;ve never worked in Go, that won&#39;t jump out at you, and &quot;it works when I ran it&quot; feels like enough confidence when it isn&#39;t. This is where you fall back on fundamentals that transfer across languages regardless of syntax. Anything external or async needs error handling. Multiple if branches are often better expressed as a lookup. Avoid unnecessary nested loops. Be deliberate about dependency injection rather than reaching for it by default. Fundamentals get you most of the way, but every language has its own idioms on top of that, so push AI to justify its choices against official docs and established practices for that language, not just &quot;this runs.&quot; And in parallel, if you&#39;re going to be working in that language long-term, actually learn it. There&#39;s no shortcut around that.</p><h4>Ask for the reasoning every time.</h4><p>Don’t just accept generated code. Ask <em>why</em> it was written that way. That reasoning is also useful material. You can ask AI to self-assess a confidence level for what it produced, and bring that into your own PR descriptions so reviewers know where to look harder.</p><h4>Let tooling carry some of the load.</h4><ul><li><strong>AI code review tools: </strong>GitHub Copilot Code Review, CodeRabbit. Genuinely useful once PR volume goes up, since they catch what a quick human skim would miss.</li><li><strong>Deterministic tools:</strong> Dependabot, SonarQube, and other CI-integrated scanners enforce baseline code quality standards without needing a human in the loop for every check.</li></ul><h3>The bottom line</h3><p>None of this is about slowing AI down. It’s about making sure the thing that got faster at writing code doesn’t outpace the thing that has to stay rigorous. Understanding what shipped and standing behind it. Context up front, scoped agents for different concerns, tests plus a real human pass, and tooling for what can be automated. That’s what turns “AI wrote a lot of code fast” into “a lot of correct code shipped fast.”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c0fae5f0c4f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All you need to know about Redis]]></title>
            <link>https://medium.com/@karthik.joshi103/all-you-need-to-know-about-redis-d8bfb52411dd?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8bfb52411dd</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Tue, 15 Sep 2026 03:01:02 GMT</pubDate>
            <atom:updated>2026-09-15T03:01:02.671Z</atom:updated>
            <content:encoded><![CDATA[<h4>Redis guide for developers</h4><p>We all know Redis is an in-memory database main use case of Redis is caching, but that’s not all about Redis. With Redis, we can do queues, mutex locks, pagination, search, etc. Let’s explore all that in this blog before that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*oFjXhugFtWov1j4IATQdbQ.png" /><figcaption>Redis fastest data platform</figcaption></figure><h3>Let’s start with fundamentals.</h3><p>As we know, Redis is an in-memory database. We can store data in key-value format in RAM, but why not on a hard disk because reading/writing from a hard disk is more work for the CPU.</p><ul><li>CPU needs to do context switching for I/O operations, and it has to go through a lot of layers: CPU -&gt; syscall -&gt; os block layer -&gt; fs-&gt;I/o scheduler -&gt; device driver -&gt; physical medium.</li><li>In hard disks, data is addressable in blocks.</li><li>Even modern NVMe SSDs are significantly slower than RAM for random access</li></ul><p>RAM-based memory</p><ul><li>CPU can easily read the data from memory: CPU -&gt; memory controller -&gt; RAM.</li><li>In RAM, it is byte-addressable.</li><li>Data durability is an issue. Redis solves it by persisting data to disk using mechanisms such as snapshots and AOF (append-only file logs).</li><li>Redis takes advantage of this by keeping its working dataset in memory and using specialized data structures such as hashes, sets, and sorted sets for common access patterns.</li></ul><h4>Capabilities of Redis:</h4><p>Redis has many use cases. The most common is caching. Apart from that, it can be used for several other use cases.</p><ul><li>Caching</li><li>Distributed locks/mutex</li><li>Pagination and ordered queries</li><li>Filtering using secondary indexes</li><li>Search</li><li>Queues and background jobs</li><li>Rate limiting</li></ul><p>The interesting part is that many of these use cases don’t require Redis to behave like a traditional relational database. Instead, we model the problem around the data structures Redis already provides.</p><h4>Caching</h4><p>Caching is a concept where we store a subset of primary data for a period of time in primary memory, which is RAM, for better read speed. It is used in two situations.</p><p><strong>Eviction policies</strong> are used to refresh the data based on standard algorithms because in-memory databases are bound by size. When the capacity is full, data is removed based on these algorithms. The most common algorithms are</p><ol><li>LRU (least recently used).</li><li>LFU (least frequently used).</li></ol><p>TTL expiration and eviction are related but different concepts. A TTL tells Redis when a key should expire. An eviction policy determines what Redis should remove when it needs to free memory.</p><p><strong>Cache invalidation</strong> is considered one of the toughest problem is software because maintaining consistency between the database and the cache is a complex operation, as there are a lot of data modifications, and choosing between speed and data consistency is really hard. If we choose speed, data is inconsistent. If we choose data consistency, the cache is inefficient. It is about finding the right balance.</p><p>There are a few patterns to solve this:</p><ul><li><strong>Write-through: </strong>whenever you perform an update, create, or delete operation, you will do the same operation in Redis also, so that the cache will always be fresh.</li><li><strong>Write-back:</strong> The application writes to the cache first and persists the change to the database asynchronously. This can improve write latency, but introduces durability and failure-recovery concerns because a failure before the database write completes can result in lost updates.</li><li><strong>Cache-aside(lazy loading): </strong>whenever api requests receives a read request, you will first check whether it is present in the cache. If it is, then you will return from the cache. Otherwise, fetch that response from the database or any other system, then store it in the cache and return the response. When data updates, the application typically updates the database and invalidates (deletes) the corresponding cache entry to ensure consistency. Cache-aside doesn’t solve cache invalidation. It gives you a relatively simple strategy for managing it.</li></ul><h4>Mutex-based Locking</h4><p>Imagine two API requests arriving at almost the same time. Both requests want to perform an operation that should happen only once. If both requests execute the critical section simultaneously, we can end up with a race condition. A mutex solves this by allowing only one process to enter the critical section at a time</p><p>In Redis, we can implement a simple lock using an atomic SET with NX and an expiration</p><pre>SET lock:report-processing &lt;unique-token&gt; NX EX 30</pre><p>There are three important parts here:</p><ul><li>NX → create the key only if it doesn&#39;t already exist.</li><li>EX 30 → automatically expire the lock after 30 seconds.</li><li>&lt;unique-token&gt; → uniquely identify the owner of the lock</li></ul><p>If Redis returns OK, we acquired the lock.</p><p>If Redis returnsnil , somebody else already holds it.</p><pre>const token = randomUUID();<br><br>const result = await redisClient.set(<br>  &quot;lock:report-processing&quot;,<br>  token,<br>  {<br>    NX: true,<br>    EX: 30<br>  }<br>);<br><br>if (result === &quot;OK&quot;) {<br>  // We own the lock<br>} else {<br>  // Someone else owns the lock<br>}</pre><h4>Pagination</h4><p>Redis doesn’t have SQL’s LIMIT and OFFSET, but that doesn&#39;t mean pagination is difficult. In fact, Redis sorted sets are a very natural fit for pagination when the result needs to be ordered.</p><p>A sorted set contains:</p><pre>member + score</pre><p>Example:</p><pre>requests:index<br><br>request-101 → 1726311000000<br>request-102 → 1726311200000<br>request-103 → 1726311400000</pre><p>Here, the score can represent updated_at. Now Redis maintains the ordering for us. Redis offers ZRANGE, a range-based query to get the data range-wise</p><pre>ZRANGE requests:index 0 9 REV</pre><p>The above query returns the newest 10 requests.</p><p>Now we just need wire up with this query with client pagination payload.</p><p>Suppose our API receives:</p><pre>{<br>  &quot;page&quot;: 3,<br>  &quot;limit&quot;: 20<br>}</pre><p>We calculate offset:</p><pre>const offset = (page - 1) * limit;</pre><p>Then we can simply use this offset.</p><pre>redis.zRange(<br>  &quot;requests:index&quot;,<br>  offset,<br>  offset + limit - 1,<br>  { REV: true }<br>);</pre><p>And if the UI needs the total number of results:</p><pre>redis.zCard(&quot;requests:index&quot;);</pre><p>So Redis can give us both.</p><h4>Filtering</h4><p>Redis offers a bunch of queries for querying the data. Main filtering happens through indexes</p><p>Suppose every request has:</p><pre>enterprise<br>region<br>business unit<br>status</pre><p>Instead of storing only one sorted set, we maintain indexes such as:</p><pre>requests:enterprise:acme<br>requests:region:india<br>requests:region:us<br>requests:status:pending</pre><p>Each index contains the request IDs as keys and updated_at as the score, for example.</p><pre>requests:region:india<br><br>request-1 → 1000<br>request-2 → 1200<br>request-3 → 1500</pre><p>Now imagine the API asks:</p><pre>enterprise = ACME<br>region = India<br>status = pending</pre><p>Redis sorted sets support intersection operations, including ZINTER and ZINTERSTORE. ZINTERSTORE can combine scores using aggregations such as MAX, MIN, or SUM So the query can conceptually become.</p><pre>enterprise index<br>       ∩<br>region index<br>       ∩<br>status index<br>       ↓<br>matching requests<br>       ↓<br>sort by updated_at<br>       ↓<br>take page</pre><p>In Node. js code:</p><pre> const multi = redisClient.multi();<br><br> const dimensionKeys =<br>    planDimensionKeys(<br>      query,<br>      multi,<br>      tmpPrefix,<br>    );<br><br>multi.zInterStore(<br>  finalKey,<br>  dimensionKeys,<br>  { AGGREGATE: &quot;MAX&quot; }<br>);<br>//combining  filtering with pagination<br>multi.zRange(<br>  finalKey,<br>  offset,<br>  offset + limit - 1,<br>  { REV: true }<br>);<br><br>multi.zCard(finalKey);</pre><p>dimensionKeys are the actual query that handles the filters.</p><pre>type Dimension =<br>  | &quot;enterprise&quot;<br>  | &quot;region&quot;<br>  | &quot;businessUnit&quot;<br>  | &quot;status&quot;<br>  | &quot;submitter&quot;;<br><br>const DIMENSIONS: Dimension[] = [<br>  &quot;enterprise&quot;,<br>  &quot;region&quot;,<br>  &quot;businessUnit&quot;,<br>  &quot;status&quot;,<br>  &quot;submitter&quot;,<br>];<br><br>const TMP_KEY_TTL_SECONDS = 30;<br><br>function dimensionIndexKey(<br>  dimension: Dimension,<br>  value: string,<br>): string {<br>  return `request:index:${dimension}:${value}`;<br>}<br><br>function planDimensionKeys(<br>  query: RequestQuery,<br>  multi: ReturnType&lt;typeof redisClient.multi&gt;,<br>  tmpPrefix: string,<br>): string[] {<br>  const dimensionKeys: string[] = [];<br><br>  for (const dimension of DIMENSIONS) {<br>    const values = query[dimension];<br><br>    if (!values?.length) {<br>      continue;<br>    }<br><br>    const sourceKeys = values.map((value) =&gt;<br>      dimensionIndexKey(dimension, value),<br>    );<br><br>    // dimension = A<br>    //<br>    // Just use the existing index.<br>    if (sourceKeys.length === 1) {<br>      dimensionKeys.push(sourceKeys[0]);<br>      continue;<br>    }<br><br>    // dimension = A OR B OR C<br>    //<br>    // First create:<br>    //<br>    // A<br>    // OR<br>    // B<br>    // OR<br>    // C<br>    //<br>    // using ZUNIONSTORE.<br>    const unionKey =<br>      `${tmpPrefix}:union:${dimension}`;<br><br>    multi.zUnionStore(<br>      unionKey,<br>      sourceKeys,<br>      {<br>        AGGREGATE: &quot;MAX&quot;,<br>      },<br>    );<br><br>    multi.expire(<br>      unionKey,<br>      TMP_KEY_TTL_SECONDS,<br>    );<br><br>    dimensionKeys.push(unionKey);<br>  }<br><br>  return dimensionKeys;<br>}</pre><p>Flow diagram:</p><pre>                    REQUEST INDEXES<br>                         │<br>          ┌──────────────┼──────────────┐<br>          │              │              │<br>      Enterprise       Region         Status<br>          │              │              │<br>       ACME/Foo       IN/US         Pending/Done<br>          │              │              │<br>          ▼              ▼              ▼<br>       ZUNION          ZUNION          index<br>          │              │              │<br>          └──────────────┼──────────────┘<br>                         │<br>                      ZINTER<br>                         │<br>                         ▼<br>                    Final result<br>                         │<br>                    ZRANGE REV<br>                         │<br>                         ▼<br>                    Request IDs<br>                         │<br>                       MGET<br>                         │<br>                         ▼<br>                    API response</pre><h4>Search in Redis</h4><p>As we saw, normal filters are solved by sorted sets along with intersection queries. Along with this, Redis offers Redis Search. Redis Search is not required for every filtering problem.</p><p>If the application only needs a small number of well-known filters, plain Redis Sets or Sorted Sets can work extremely well.</p><p>But once the requirement becomes full-text search, prefix matching, complex boolean queries, numeric filtering, aggregations, or vector search, Redis Search becomes a much more appropriate solution.</p><p>To do full-text search in Redis, we need to create an index based on the schema before that.</p><p>Ex:</p><p>If we want to search something like:</p><pre>request name contains &quot;payment&quot;</pre><p>Now we tell Redis which fields we want to search.</p><pre>FT.CREATE request-idx<br>ON HASH<br>PREFIX 1 &quot;request:&quot;<br>SCHEMA<br>  requestName TEXT<br>  enterprise TAG<br>  region TAG<br>  status TAG<br>  updatedAt NUMERIC SORTABLE</pre><p>The important thing to notice here is the data type</p><pre>requestName → TEXT<br><br>enterprise  → TAG<br>region      → TAG<br>status      → TAG<br><br>updatedAt   → NUMERIC SORTABLE</pre><p>Redis Search supports different field types for different query requirements. TEXT is intended for full-text search, TAG is useful for exact-match values such as categories/statuses, and NUMERIC is used for numeric filtering. fields can also be marked SORTABLE</p><p>Now we can search using:</p><pre>FT.SEARCH request-idx &quot;@requestName:payment&quot;</pre><p>Redis Search searches the requestName field rather than us manually scanning every request.</p><p>Redis Search also supports pagination using limit and offset:</p><pre>FT.SEARCH request-idx \<br>  &quot;@requestName:payment&quot; \<br>  LIMIT 0 20</pre><p>Redis Search also supports sort</p><pre>FT.SEARCH request-idx \<br>  &quot;@status:{pending}&quot; \<br>  SORTBY updatedAt DESC \<br>  LIMIT 0 20</pre><p>Redis Search is included in Redis Open Source starting with Redis 8, so you don’t need to purchase a separate Redis Search license to use it in a self-managed Redis deployment.</p><h4>Redis as Job Queues</h4><p>Redis can also be used as the backend for job queues</p><p>Imagine an API that needs to perform an expensive operation:</p><ul><li>send an email</li><li>generate a report</li><li>process an image</li><li>recalculate leaderboard rankings</li><li>generate notifications</li><li>process a webhook</li></ul><p>We don’t necessarily want the API request to wait for that work to finish.</p><p>Instead, the API can put a job into a queue and return immediately:</p><pre>API Request<br>     │ add job<br>     ▼<br>   Queue<br>     │<br>     ▼<br>   Redis<br>     │ worker picks job<br>     ▼<br>   Worker<br>     │<br>     ▼<br>Process job</pre><p>Example:</p><p>In one of my applications, I use BullMQ with Redis to process leaderboard operations asynchronously.</p><p>For example, recalculating the ranks of thousands of users doesn’t need to happen inside the HTTP request.</p><p>Instead, the API adds a job:</p><pre>await leaderboardQueue.add(<br>  &#39;recalculate-all-ranks&#39;,<br>  {<br>    triggeredBy: &#39;admin&#39;,<br>    reason: &#39;manual-recalculation&#39;,<br>  },<br>);</pre><p>The API can now respond without waiting for all users to be processed.</p><p>A worker consumes the job:</p><pre>@Processor(&#39;leaderboard&#39;)<br>export class LeaderboardProcessor extends WorkerHost {<br><br>  async process(job: Job) {<br>    switch (job.name) {<br><br>      case &#39;recalculate-all-ranks&#39;:<br>        return this.recalculateAllRanks(job);<br><br>      case &#39;update-leaderboard-cache&#39;:<br>        return this.updateLeaderboardCache(job);<br><br>      default:<br>        throw new Error(`Unknown job: ${job.name}`);<br>    }<br>  }<br>}</pre><p>Multiple workers</p><p>One of the biggest advantages of a queue is that the work doesn’t have to be performed by the same application instance that received the request.</p><pre>                 ┌── Worker 1<br>                 │<br>API ──&gt; Redis ───┼── Worker 2<br>                 │<br>                 └── Worker 3</pre><p>We can run multiple workers to process jobs concurrently.</p><p>In my example, I intentionally configured:</p><pre>@Processor(&#39;leaderboard&#39;, {<br>  concurrency: 1,<br>})</pre><p>which means this particular worker processes one job at a time.</p><p>For workloads where jobs are independent, we could increase the concurrency or run multiple worker instances.</p><p>You can find the complete code <a href="https://github.com/kdj309/leetcode-backend/blob/master/src/leaderboard_cache/leadeboard-processor.ts#L16">here</a></p><h4>Rate Limiting using Redis</h4><p>Rate limiting is allowing a client to make only N requests within a particular time window.</p><p>For example:</p><ul><li>100 requests/minute/user</li><li>10 login attempts/minute / IP</li><li>1000 API requests/hour / API key</li></ul><p>Suppose we want to allow 10 requests per minute per user.</p><p>For user 123we create a Redis key:</p><pre>rate-limit:user:123</pre><p>On every request, we will increment the key</p><pre>INCR rate-limit:user:123</pre><p>After the tenth:</p><pre>rate-limit:user:123 = 10</pre><p>If the value becomes greater than 10, reject the request.</p><p>We also give the key a 60-second expiration:</p><pre>EXPIRE rate-limit:user:123 60</pre><p>After 60 seconds, Redis automatically removes the key, and the counter starts again, so in 60 seconds the user can only make 10 requests.</p><p>In Node. js:</p><pre>async function checkRateLimit(<br>  userId: string,<br>) {<br>  const key = `rate-limit:user:${userId}`;<br><br>  const limit = 10;<br>  const windowSeconds = 60;<br><br>  const count = await redis.incr(key);<br><br>  if (count === 1) {<br>    await redis.expire(<br>      key,<br>      windowSeconds,<br>    );<br>  }<br><br>  return {<br>    allowed: count &lt;= limit,<br>    remaining: Math.max(<br>      0,<br>      limit - count,<br>    ),<br>  };<br>}<br><br>//usage<br>const result = await checkRateLimit(userId);<br><br>if (!result.allowed) {<br>  throw new TooManyRequestsException();<br>}</pre><p>Till now, we have seen a lot of use cases. Apart from these, Redis can be used for streams and the pub/sub model. Redis isn’t valuable only because it is fast. Its atomic operations, expiration, and data structures make it useful for solving coordination and data-access problems across distributed applications. Once you understand those primitives, Redis stops being just <em>“the thing we use for caching”</em> and becomes a much more versatile tool for designing scalable applications. refer official <a href="https://redis.io/solutions/">docs</a> for more use cases</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8bfb52411dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All you need to know about Elasticsearch]]></title>
            <link>https://medium.com/@karthik.joshi103/all-you-need-to-know-about-elasticsearch-ec4eb10c2c80?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec4eb10c2c80</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Mon, 20 Jul 2026 04:55:15 GMT</pubDate>
            <atom:updated>2026-07-20T04:55:15.067Z</atom:updated>
            <content:encoded><![CDATA[<h4>Guide for Elasticsearch integration</h4><p>I’ve used Elasticsearch in three different projects now, and for most of that time I treated it like a magic search box. Throw JSON at it, get relevant results back, don’t ask questions. That worked fine until it didn’t, when a search that should’ve matched came back empty, or a field I was sure was a number started throwing type errors.</p><p>Turns out the magic is a pretty logical pipeline once you take it apart. This is me doing that, starting from the cluster and working down to the individual token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gn9J0XcDq33O4a2_mupVGw.jpeg" /></figure><h4>The physical model: nodes, shards, indices</h4><p>Strip away the terminology and Elasticsearch is just a bunch of servers agreeing to work together. Elasticsearch calls a server a <strong>node</strong>, and a group of nodes is a <strong>cluster</strong>. Every node knows about every other node in the cluster, and if a request lands on the wrong one, that node just forwards it to whichever node actually has the data.</p><pre>Cluster<br> ├── Node<br> ├── Node<br> └── Node</pre><p>When you index data, you’re not writing to the database. You’re writing to an <strong>index</strong>, which is really a logical name for a collection of <strong>shards</strong>. Each shard is its own self-contained Lucene index, holding a subset of your documents.</p><pre>Index<br> ├── Shard 1 → Lucene index → documents<br> ├── Shard 2 → Lucene index → documents<br> └── Shard 3 → Lucene index → documents</pre><p>This is the part I used to gloss over: an index isn’t one thing sitting on one machine. It’s shards scattered across nodes, and Lucene is doing the actual searching underneath each shard. Elasticsearch’s job is mostly routing and coordination on top of a pile of Lucene indices.</p><p><strong>Primary and replica shards</strong></p><p>There are two kinds of shards, and they do different jobs.</p><p>A <strong>primary shard</strong> is where a document actually lives. One document belongs to exactly one primary shard. A <strong>replica shard</strong> is just a copy of a primary, sitting on a different node for redundancy and for spreading out read load.</p><p>The part that tripped me up initially: the number of primary shards is fixed the moment you create the index. It’s a decision baked into how documents route to shards, so you can’t just dial it up later. Changing it means moving to a new index, either via_reindex, or via the faster _split and _shrink resize APIs, which copy Lucene segments directly instead of reindexing every document by hand. Replica count is the opposite. It&#39;s a dynamic setting, so you can bump it up or down on a live index whenever you want. Elasticsearch also automatically rebalances shard copies across nodes so the cluster stays highly available even if a node drops.</p><p><strong>Mapping: the schema you didn’t know you had</strong></p><p>Elasticsearch stores JSON documents, but it’s not schemaless the way people assume. <strong>Mapping</strong> is Elasticsearch’s version of a schema for your document fields, and you get it one of two ways.</p><p><strong>Explicit mapping: </strong>you define the field types yourself, so every developer touching the index knows exactly what they’re working with. This is what I reach for once an index leaves the prototyping stage.</p><p><strong>Dynamic mapping: </strong>Elasticsearch looks at the incoming JSON and guesses the data type. If you’ve set up an index template, it’ll match against that instead of guessing blind. Convenient for getting started, less convenient the day a string field silently becomes a date because someone sent a value that looked like one.</p><pre>PUT /products<br>{<br>  &quot;mappings&quot;: {<br>    &quot;properties&quot;: {<br>      &quot;name&quot;:  { &quot;type&quot;: &quot;text&quot; },<br>      &quot;price&quot;: { &quot;type&quot;: &quot;float&quot; },<br>      &quot;in_stock&quot;: { &quot;type&quot;: &quot;boolean&quot; }<br>    }<br>  }<br>}</pre><p><strong>Runtime fields: computing instead of reindexing</strong></p><p><strong>Runtime fields</strong> solve a specific problem. You want a new field on documents that are already indexed, but you don’t want to reindex everything to get it. A runtime field is evaluated at query time instead of being stored, so it shows up in search without touching what’s already on disk.</p><p>There’s a real cost to this. Since the field is computed on the fly from a script, performance is directly proportional to how expensive that script is. Cheap script, cheap field. Anything doing real work, you’ll feel it on search latency.</p><p>You can pull runtime field values straight from _source or from doc values, and you can define runtime fields either in the mapping itself or right inside a search request. One useful wrinkle: if a runtime field has the same name as an actual indexed field, the runtime field wins and overrides it for that query.</p><p><strong>Text analysis: how text becomes searchable</strong></p><p>This is the part that actually explains why full-text search works the way it does, and it’s the part most people never look at until something breaks.</p><p>A text field doesn&#39;t get stored as-is. Before it&#39;s searchable, it goes through <strong>text analysis,</strong> tokenization, and normalization via something called an <strong>analyzer</strong>. Elasticsearch ships with a default analyzer that handles this for you, but you can swap in a built-in one or write your own.</p><p>An analyzer is made of exactly three kinds of components, run in this order:</p><pre>raw text<br>   │<br>   ▼<br>character filters (0 or more)<br>   │<br>   ▼<br>tokenizer (exactly 1)<br>   │<br>   ▼<br>token filters (0 or more)<br>   │<br>   ▼<br>searchable tokens</pre><p><strong>Character filters</strong></p><p>A character filter gets the original text as a raw stream of characters and can add, remove, or transform characters before tokenization even starts. An analyzer can have zero or more of these.</p><p>Example: a character filter that converts Hindu-Arabic numerals into their Arabic-Latin equivalents before the text is tokenized at all.</p><p><strong>Tokenizer</strong></p><p>The tokenizer takes that stream of characters and breaks it up into individual tokens. Unlike character filters and token filters, an analyzer must have <strong>exactly one</strong> tokenizer, no more, no less.</p><p>The simplest example is the whitespace tokenizer:</p><pre>&quot;Hi Hello&quot; → [whitespace tokenizer] → [&quot;Hi&quot;, &quot;Hello&quot;]</pre><p><strong>Token filters</strong></p><p>Once you have tokens, token filters can add, remove, or modify them. An analyzer can have zero or more.</p><pre>&quot;Hi&quot; → [lowercase filter] → &quot;hi&quot;</pre><p>Some token filters go further than editing a token in place. They can inject new tokens entirely, like adding synonyms into the stream.</p><p><strong>Two moments of analysis: index time and search time</strong></p><p>Here’s the bit that actually clicked things into place for me. Text analysis doesn’t happen once. It happens twice. Once when a document is indexed, and once again when a search query runs against a text field. The query gets analyzed too, using the same pipeline.</p><p>Say this gets indexed:</p><pre>&quot;Game of THRONES Winter is Coming&quot;</pre><p>After the default analyzer runs lowercasing, stopword removal, and stemming. what actually lands in the index looks closer to:</p><pre>game throne winter come</pre><p>Later, someone searches winter is coming. That query string goes through the same analysis and comes out as:</p><pre>winter come</pre><p>And that’s what actually gets matched against the indexed tokens. Not the raw query text. This is also why you can configure a different analyzer for search time than the one used at index time, if your use case calls for it.</p><p><strong>Stemming: matching walk, walked, and walking</strong></p><p><strong>Stemming</strong> reduces a word down to its root form, so variants of the same word match each other during a search walking and walked both collapse to walk. Since word structure differs across languages, stemming is language-dependent.</p><p>There are two families of stemmers:</p><ul><li><strong>Algorithmic stemmers</strong> apply a set of rules to strip a word down to its root. An English stemmer, for instance, might just chop off trailing -s or -es from plurals. These are generally preferred over dictionary stemmers because they generalize better.</li><li><strong>Dictionary stemmers</strong> look up the word in a provided dictionary and swap unstemmed variants for the stemmed form found there.</li></ul><p>Default stemming isn’t always right for your data, and it can hurt precision. For that, Elasticsearch lets you define control stemming custom overrides for specific words that should or shouldn’t be stemmed the default way.</p><h4>What Lucene is actually doing underneath</h4><p>Everything above is Elasticsearch’s vocabulary. Underneath all of it is Apache Lucene, a Java library that does the real indexing and searching. Elasticsearch is best thought of as Lucene plus the stuff Lucene doesn’t do on its own. a REST API, clustering, distribution across shards, near-real-time search. Strip that away and every shard is just a Lucene index sitting on disk.</p><p><strong>The inverted index</strong></p><p>Lucene’s core structure is the same one every search engine textbook opens with: the inverted index. Instead of document → words it contains, it flips things around to word → documents that contain it.</p><p>Take a document like:</p><pre>{ &quot;title&quot;: &quot;Notes on distributed systems and search&quot; }</pre><p>After analysis, that title breaks down into terms, and each term gets a posting list of the documents (and positions within them) where it shows up:</p><pre>Term          Posting List<br>─────────────────────────────────────<br>distribut     [doc1:pos2, doc7:pos0]<br>notes         [doc1:pos0, doc3:pos1]<br>search        [doc1:pos5, doc1:pos5]<br>system        [doc1:pos3, doc4:pos2]</pre><p>A search for “distributed systems” gets analyzed the same way the document was, down to the same stems. Lucene looks up distribut and system in the term dictionary, intersects their posting lists to find documents containing both, and scores the matches by default using BM25, which weighs term frequency against how common the term is across the whole index. Rarer, more concentrated terms score higher. That&#39;s the whole trick behind relevance.</p><h4>Running it: Elasticsearch on Docker</h4><p>Enough theory. Here’s the setup I actually use to get a single-node cluster running locally or on a small VPS for a side project.</p><p>Before touching the Compose file, it’s worth checking whether your box can handle it. Elasticsearch isn’t shy about memory, and this is roughly what I look at:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C07ZF5bAQ4mBwVpS6NLK_Q.png" /></figure><p>A 2GB / 1 vCPU / 50GB box clears all three with room to spare, as long as you cap the heap, which the Compose file below does.</p><pre>version: &#39;3.8&#39;<br>services:<br>  elasticsearch:<br>    image: docker.elastic.co/elasticsearch/elasticsearch:8.13.0<br>    container_name: elasticsearch<br>    environment:<br>      - discovery.type=single-node<br>      - ES_JAVA_OPTS=-Xms512m -Xmx512m<br>      - ELASTIC_PASSWORD=yourpassword<br>      - xpack.security.enabled=true<br>      - xpack.security.http.ssl.enabled=false<br>    ports:<br>      - &quot;9200:9200&quot;<br>    volumes:<br>      - esdata:/var/lib/elasticsearch/data<br>    restart: unless-stopped<br>    ulimits:<br>      memlock:<br>        soft: -1<br>        hard: -1<br>      nofile:<br>        soft: 65536<br>        hard: 65536<br>volumes:<br>  esdata:<br>    driver: local</pre><p>discovery.type=single-node tells Elasticsearch not to bother waiting for other nodes to form a cluster. It just becomes a one-node cluster on its own. ES_JAVA_OPTS caps the JVM heap at 512MB on both ends, which is what keeps this fitting inside a 2GB box. Security is on by default in 8.x, so ELASTIC_PASSWORD sets the password for the built-in elastic user. TLS is switched off here purely to keep local setup simple, not something I&#39;d leave off in production.</p><p><strong>Bring it up:</strong></p><pre>mkdir elasticsearch &amp;&amp; cd elasticsearch<br>nano docker-compose.yml<br># paste the config above, save and exit<br>docker compose up -d<br>docker compose logs -f</pre><p>And check that it actually came up:</p><pre>curl -u elastic:yourpassword http://localhost:9200</pre><p>Two API endpoints are worth knowing before you go further. _analyze lets you run any text through an analyzer and see exactly what tokens come out, which is the fastest way to debug a &quot;why didn&#39;t this match&quot; question:</p><pre>GET /_analyze<br>{<br>  &quot;text&quot;: &quot;Software Engineer&quot;,<br>  &quot;analyzer&quot;: &quot;standard&quot;<br>}</pre><p>And _explain on a search request shows you the actual scoring breakdown for why a document ranked where it did, useful once relevance stops matching your intuition.</p><h4>Wiring it into a NestJS app</h4><p>The theory’s useless if you can’t actually call the thing from your API. Here’s how I hook it up.</p><p><strong>One client, not one per request</strong></p><p>The Elasticsearch client keeps its own connection pool under the hood, so creating a new one on every request is just throwing that away and paying the setup cost again. The fix is the usual one: wrap it in a singleton and hand out the same instance everywhere.</p><pre>import { Client } from &#39;@elastic/elasticsearch&#39;;<br>import { config } from &#39;src/config/config&#39;;<br><br>class ElasticsearchClientSingleton {<br>  private static instance: Client | null = null;<br><br>  public static getInstance(): Client {<br>    if (!ElasticsearchClientSingleton.instance) {<br>      ElasticsearchClientSingleton.instance = new Client({<br>        node: config().elasticsearch.node,<br>        auth: {<br>          username: config().elasticsearch.username,<br>          password: config().elasticsearch.password,<br>        },<br>      });<br>    }<br><br>    return ElasticsearchClientSingleton.instance;<br>  }<br><br>  public static resetInstance() {<br>    ElasticsearchClientSingleton.instance = null;<br>  }<br>}</pre><p>Nothing clever here getInstance() builds the client once and returns the cached one on every call after that. The one thing worth calling out is resetInstance(). It exists purely for tests: spin up a fresh client against a test cluster (or a mock) without the previous instance leaking across test files.</p><p><strong>A real query: bool, should, filter, and boost</strong></p><p>I’m building search for a competitive-programming platform, think a LeetCode-style problem list, and the query needs to do two different jobs at once: find problems that match a text query, and narrow by difficulty, without the difficulty filter affecting how results are ranked. That split is exactly what should and filter are for inside a bool query.</p><pre>const filters: ESFilterClause[] = [];<br>if (difficulty) {<br>  filters.push({<br>    term: { difficulty: difficulty.toLowerCase() }<br>  });<br>}<br><br>const shouldClauses: ESShouldClause[] = [];<br>if (query) {<br>  shouldClauses.push({<br>    match_phrase: {<br>      title: { query, boost: 20 }<br>    }<br>  });<br><br>  shouldClauses.push({<br>    match: {<br>      title: { query, boost: 10, operator: &#39;and&#39; }<br>    }<br>  });<br>}<br><br>const boolQuery: ESBoolQuery = {};<br>if (shouldClauses.length &gt; 0) {<br>  boolQuery.should = shouldClauses;<br>  boolQuery.minimum_should_match = 1;<br>}<br>if (filters.length &gt; 0) {<br>  boolQuery.filter = filters;<br>}<br><br>const response = await elasticSearchClient.search({<br>  index: &#39;problems_v3&#39;,<br>  from: (page - 1) * limit,<br>  size: limit,<br>  query: Object.keys(boolQuery).length &gt; 0 ? { bool: boolQuery } : { match_all: {} }<br>});</pre><p>A few decisions in here that aren’t obvious from the code alone:</p><p>filter vs should isn&#39;t just a naming choice filter clauses run in a filter context. They decide yes/no on whether a document matches. They don&#39;t contribute to the relevance score, and Elasticsearch can cache them. Difficulty belongs there: I want &quot;medium&quot; problems excluded or included, not &quot;more medium-ish&quot; problems ranked higher.</p><p>The two should clauses are doing the same search two different ways on purpose, with different weight. match_phrase requires the terms to appear in that exact order, and it&#39;s boosted to 20 if someone&#39;s query matches the title phrase-for-phrase. That&#39;s about as confident a match as you can get. The plain match clause with operator: &#39;and&#39; is more forgiving (all terms present, any order) and gets less weight, at boost 10. It&#39;s a fallback that still ranks below a phrase hit, not a tie.</p><p>minimum_should_match: 1 is doing more work here than it looks like, and it&#39;s worth being exact about why. Elasticsearch&#39;s own default for this value is conditional: if a bool query has should clauses and nothing else, it already defaults to requiring at least one to match. But the instant you add a filter clause, which happens here whenever difficulty is set, that default silently flips to 0, and should clauses become pure scoring bonuses with no bearing on whether a document matches at all. Skip the explicit minimum_should_match: 1 and a difficulty-only search with no matching title would still return every problem at that difficulty, just unranked. Setting it explicitly is what keeps the title query enforced once a filter shows up alongside it. This is a genuinely easy trap to fall into, and it&#39;s bitten people in production before.</p><h4>Where this leaves me</h4><p>None of this changes how you write a search query day to day. But knowing that a text field is really &quot;raw string → character filters → one tokenizer → token filters → tokens,&quot; backed by a flat inverted index rather than anything graph-shaped, and run at both index time and search time, turns half the &quot;why didn&#39;t this match&quot; debugging sessions into thirty-second checks instead of an hour of guessing. So understanding how things work is necessary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec4eb10c2c80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backend from First Principles]]></title>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-4e75bd525157?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e75bd525157</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Mon, 08 Jun 2026 03:11:00 GMT</pubDate>
            <atom:updated>2026-06-08T04:51:19.909Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 6: Concurrency, Parallelism, and the Scaling Ladder</h4><p>This is the last part of the series. We’re ending on the two topics that underpin almost every other decision you’ll make at scale: how your code handles multiple things happening at once, and how you decide when to stop patching a single machine and start adding more.</p><p>Both are more concrete than the typical explanation makes them sound.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3pHVAXn5RkjPYL30O_ZHLQ.png" /><figcaption>Backend from first priniciples</figcaption></figure><h4>The CPU Problem Nobody Talks About(IO vs CPU)</h4><p>Here’s what actually happens when your Node.js server handles a database query:</p><ol><li>Request comes in. You deserialize the HTTP body, which is CPU work.</li><li>You build a query and send it over TCP. From this point, the CPU is sitting idle.</li><li>The database does its thing. Your CPU waits.</li><li>Response arrives. You serialize and send it back to the CPU to work again.</li></ol><p>Step 3 is the interesting one. If you’re handling one request at a time and your DB takes 20ms to respond, your CPU is doing nothing for 20ms. At 100 requests per second, that’s 2 full seconds of wasted CPU per second. The CPU isn’t slow. You’re just not giving it anything to do.</p><p>This is the IO-bound problem. Your bottleneck isn’t compute, it’s waiting. And the solution is concurrency, the ability to start handling something else while you wait.</p><p><strong>Concurrency</strong> is about structurally interleaving tasks so idle time gets used. One CPU can be concurrent.</p><p><strong>Parallelism</strong> is about executing multiple things simultaneously across multiple CPU cores.</p><p>A single-threaded event loop is concurrent but not parallel. Go routines running across 8 CPU cores are both. Your video encoder is burning all cores that’s parallel, not particularly concurrent.</p><p>Which one you need depends on what your code is actually doing.</p><h4>IO-Bound vs CPU-Bound</h4><p><strong>IO-bound tasks:</strong> anything where the CPU waits on something external. HTTP servers, database queries, file reads, queue operations, and calling third-party APIs. The CPU is fast. The bottleneck is the wire.</p><p><strong>CPU-bound tasks:</strong> anything where the CPU is the bottleneck. Video processing, image manipulation, ML model inference, cryptographic operations, search indexing, and heavy mathematical computation. The CPU is genuinely busy, not waiting.</p><p>For IO-bound work, concurrency is what you want. You can handle thousands of concurrent requests on a single core if most of them are just waiting for database responses.</p><p>For CPU-bound work, you need parallelism, multiple cores actually crunching in parallel. A Node.js event loop cannot help you encode a video faster. Worker threads or a separate process can.</p><p>This is why Node.js is fine for API servers and terrible for image processing. And why Go, which gives you real parallelism alongside good concurrency primitives, is popular for workloads that mix both.</p><p>Traditionally, programming languages like C++ and C supported concurrency through os threads. Programming languages like JavaScript and Python support concurrency with the help of the kernel&#39;s ability to constantly listen to the socket file descriptor and perform action whenever there io performed for that descriptor through system calls, for example, epoll, it is known as even driven concurrency model.</p><h4>Let’s see how the JavaScript event loop works.</h4><p>The event loop is not magic. It’s a while loop that checks whether async work is done.</p><p>Under the hood, Node.js uses epoll (on Linux), a kernel-level mechanism for watching file descriptors (sockets, timers, etc.) and getting notified when they’re ready, instead of constantly polling.</p><pre>// What&#39;s happening inside Node.js (simplified)<br>int epoll_fd = epoll_create1(0);<br><br>struct epoll_event event;<br>event.events = EPOLLIN;<br>event.data.fd = socket_fd;<br>epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &amp;event);<br><br>while (true) {<br>  int num_events = epoll_wait(epoll_fd, events, MAX_EVENTS, timeout);<br>  for (int i = 0; i &lt; num_events; i++) {<br>    if (events[i].events &amp; EPOLLIN) {<br>      handle_read(events[i].data.fd); // fire the JS callback<br>    }<br>  }<br>}</pre><p>The epoll_wait call blocks the loop until something is ready. a socket has data, a timer has expired, or a file read is completed. When an event fires, the corresponding JavaScript callback gets queued and executed. The CPU isn&#39;t spinning in a busy loop. It&#39;s parked by the kernel until there&#39;s work to do.</p><p>Now, one of the famous JavaScript interview output-based questions</p><pre>console.log(&#39;1&#39;);<br><br>setTimeout(() =&gt; console.log(&#39;2&#39;), 0);<br><br>Promise.resolve().then(() =&gt; console.log(&#39;3&#39;));<br><br>console.log(&#39;4&#39;);</pre><p>Output: 1, 4, 3, 2.</p><p>Here&#39;s exactly why:</p><p>console.log(&#39;1&#39;) runs synchronously on the V8 call stack. setTimeout registers a timer with Node.js (not V8). Promise.resolve().then(...) queues a microtask inside V8. console.log(&#39;4&#39;) runs synchronously.</p><p>When the call stack empties, V8 drains its microtask queue first, that’s the Promise callback, 3. Only after microtasks are empty does the event loop pull from the task queue the setTimeout callback, 2.</p><p>Two queues. Microtasks (Promises, queueMicrotask) always run before the next task queue item. This is why Promise.resolve() With a 0ms timeout still beats setTimeout(..., 0).</p><h4>Go’s Approach: The GMP Model</h4><p>JavaScript handles concurrency by being single-threaded and clever about waiting. Go takes a different angle. It gives you lightweight user-space threads (goroutines) that the runtime maps onto real OS threads.</p><p>The model has three pieces: <strong>G</strong> (goroutines), <strong>M</strong> (OS threads), and <strong>P</strong> (logical processors).</p><p>Goroutines are cheap: 2KB of stack memory at startup versus ~1MB for an OS thread. You can run hundreds of thousands of them. OS threads are expensive and limited. Go’s runtime keeps the number of M threads roughly equal to CPU cores. P is the scheduler’s unit of work it holds a run queue of goroutines and dispatches them onto an M.</p><pre>// Spawning a goroutine this is all it takes<br>go func() {<br>    result := queryDatabase()<br>    fmt.Println(result)<br>}()<br><br>// HTTP server handling concurrent requests each in its own goroutine<br>http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {<br>    data := fetchFromDB(r.Context())<br>    json.NewEncoder(w).Encode(data)<br>})</pre><p>When a goroutine blocks on IO, the Go runtime parks it and puts a different goroutine on that OS thread. No thread is ever sitting idle waiting for a network response. This is what makes Go efficient for both IO-heavy and CPU-heavy workloads. The runtime figures out the multiplexing for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*hjnJtcuDcEHOdBsCc4rmhQ.png" /><figcaption>Go GMP model</figcaption></figure><p>Python’s asyncio does something similar to Node.js (single-threaded event loop, cooperative multitasking). Java has added virtual threads in recent versions (Project Loom) . The same M: N idea Go has had since the beginning. The trend is clear: blocking OS threads per request doesn’t scale.</p><h4>Choosing: IO or CPU?</h4><p>The decision chart is simpler than it sounds:</p><p>If your task is waiting on a network or disk → concurrency (async/await, event loop, goroutines).</p><p>If your task is burning CPU → parallelism (worker threads, multiple processes, Go routines across cores).</p><p>Most backend services are IO-bound. Node.js, with its event loop, handles them well. If you start doing CPU-heavy work in Node, image resizing, video transcoding, or ML inference, move that work to a worker thread or a separate service. Don’t block the event loop with synchronous computation.</p><h4>The Scaling Ladder</h4><p>The mistake I see most often with scaling is jumping to the solution someone read about: sharding, microservices, Kubernetes. before exhausting the cheaper options. Every step up the ladder adds operational cost. Some steps are irreversible. Climb slowly.<br><strong>1: Single server:</strong></p><pre>Client → App Server → Database</pre><p>Start here. Always. A well-tuned single server can handle more than most teams expect. The operational simplicity is worth money.</p><p><strong>2: Vertical scaling:</strong></p><p>Before adding machines, make the machine bigger. More CPU, more RAM, faster disk. This is cheap, fast, and doesn’t add complexity. The ceiling is higher than people think . A 64-core machine with 256GB RAM handles a lot.</p><p>It does have a ceiling and a single point of failure, which is why it’s rung 2, not the final answer.</p><p><strong>3: Query optimization + indexes:</strong></p><p>The most commonly skipped rung. Before you provision anything, pull up your slow query logs. An unindexed column that gets queried 500 times per second is costing you more than a bigger machine would gain you.</p><pre>-- Add this before you add a server<br>EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;<br>-- If you see &quot;Seq Scan&quot; on a high-frequency query column, you need this<br>CREATE INDEX idx_orders_user_id ON orders(user_id);</pre><p>Indexes are free in terms of infrastructure cost. Fix N+1 queries. Remove unused JOINs. Add pagination. This rung solves most early performance problems.</p><p><strong>4: Horizontal scaling:</strong></p><p>When a single app server is genuinely the bottleneck:</p><pre>Load Balancer<br>          /      |      \<br>       App1    App2    App3<br>                |<br>            Database</pre><p>This is the first real distributed step, and it comes with a cost: your application must be stateless. Sessions, local caches, WebSocket state, anything stored on a specific server breaks as soon as the load balancer sends the next request somewhere else.</p><p>Push state out: sessions into Redis, files into object storage, WebSocket coordination into a pub/sub broker. Then scaling app servers is just adding more identical boxes.</p><p><strong>5: Caching layer:</strong></p><p>When DB reads are the bottleneck, and you’ve already fixed the slow queries:</p><pre>App Server → Redis → Database<br>              ↑<br>         (read hit)</pre><p>Cache the expensive, frequently-read, slow-to-change data. Not everything. Know your hit ratio. below 80%, you’re adding complexity without much benefit.</p><p>Don’t forget the thundering herd problem from Part 5, multiple servers simultaneously missing cache and hammering the DB. Use cache locking and jittered TTLs.</p><p><strong>6: Read replicas:</strong></p><p>For read-heavy workloads where even cache misses are too frequent:</p><pre>App Servers<br>              ↓<br>       Primary DB (writes)<br>         /          \<br>  Read Replica    Read Replica</pre><p>Writes go to the primary. Reads spread across replicas. Replication lag is usually milliseconds fine for most reads. For reads that must see the freshest data (immediately after a write), route them to primary for a short window.</p><p><strong>7: Background jobs:</strong></p><p>Operations that don’t need to complete email, notifications, report generation, and image processing should not block the request.</p><pre>// Instead of doing this synchronously<br>app.post(&#39;/export&#39;, async (req, res) =&gt; {<br>  const report = await generateLargeReport(req.body); // blocks for 30s<br>  res.send(report);<br>});<br>// Do this<br>app.post(&#39;/export&#39;, async (req, res) =&gt; {<br>  const jobId = await reportQueue.add(&#39;generate&#39;, req.body);<br>  res.json({ jobId, status: &#39;processing&#39; }); // returns immediately<br>});</pre><p>BullMQ, SQS, RabbitMQ, Kafka, pick one that fits your scale. The queue also gives you retry logic, dead letter handling, and rate limiting for free.</p><p><strong>8: CDN for static assets:</strong></p><p>Images, JS bundles, and CSS don’t belong on your app server. A CDN serves them from a Point of Presence close to the user, cutting the round-trip from 200ms to 15ms.</p><p>Move static assets off the origin. CloudFront, Cloudflare, and Fastly all work. Your app servers stop burning bandwidth on content that never changes.</p><p><strong>9: Connection pooling:</strong></p><p>When you start seeing connection errors or high connection overhead:</p><pre>App servers (many connections)<br>        ↓<br>   PgBouncer (pools)<br>        ↓<br>  PostgreSQL (fewer, stable connections)</pre><p>PgBouncer sits between your app and database. It maintains a small pool of real database connections and multiplexes many application connections through them. Postgres has a hard limit on concurrent connections. PgBouncer lets you run 200 app server connections while keeping 20 actual DB connections.</p><p><strong>10: Database sharding:</strong></p><p>This is the serious step. When a single database, even with replicas, can’t handle the data volume:</p><pre>App Server<br>        /     |     \<br>  Shard 1  Shard 2  Shard 3<br>  (1-1M)  (1M-2M)  (2M-3M)</pre><p>You partition rows across multiple database instances by some key user ID range, hash, or geographic region. Queries only touch the relevant shard instead of scanning the full dataset.</p><p>The operational cost is real. Cross-shard JOINs become application-level problems. Transactions that span shards lose database-level atomicity. Schema changes need to be deployed to every shard. Don’t shard until you’ve genuinely exhausted rungs 1–9. Most companies never need to.</p><p><strong>11: Edge computing:</strong></p><p>For truly global products, you can push lightweight logic to the CDN layer, Cloudflare Workers, or Vercel Edge Functions. These run in V8 isolates close to the user and handle things like auth validation, A/B routing, and request rewriting without touching your origin.</p><p>They’re stateless and constrained, with no long-running operations, no direct DB access. But for the tasks they fit, they cut latency from 200ms to under 10ms.</p><p><strong>12: Microservices (maybe):</strong></p><p>The most over-applied rung.</p><p>Microservices solve real problems: teams that can’t ship independently because of a coupled codebase, services with different infrastructure needs (CPU-heavy ML vs. lightweight API), and failure isolation between critical and non-critical parts of the system.</p><p>They create real problems too: distributed tracing across N services, cross-service transactions without database-level guarantees, N deployment pipelines, N monitoring configurations, and authentication that has to work across service boundaries.</p><p>A well-structured monolith with clear module boundaries handles most of what microservices promise. If two features can be deployed independently from a monolith, there’s no team velocity reason to split. If a service genuinely needs to scale 100x more than the rest of the system, split it. Otherwise, the complexity cost isn’t worth it yet.</p><p>Many companies that microserviced early are quietly consolidating. Shopify runs on a monolith. Stack Overflow served millions of users on a handful of servers for years.</p><h4>The One Rule That Ties This Together</h4><p>Scale is easy to add. It’s hard to remove.</p><p>Every rung on that ladder is another thing to operate, monitor, debug, and explain to new engineers. Once you introduce Kafka, you own Kafka. Once you shard your database, you’re sharded forever.</p><p>The teams that build well aren’t the ones that anticipate the most scale. They’re the ones that measure accurately, fix the actual bottleneck, and resist the urge to pre-optimise for problems they don’t have yet.</p><p>Start simple. Observe. Apply the smallest fix that moves the number. Repeat.</p><h4>Wrapping Up the Series</h4><p>Six parts. I didn’t expect it to get this long when I started writing.</p><p>We started at HTTP, the protocol that carries every request you’ll ever handle. Walked through databases and how they actually store and retrieve data. Caching, queues, and the tradeoffs they introduce. Search, error handling, and the operational glue (config, logging, graceful shutdown) that keeps a service running. Security is not a checklist but a mindset about boundaries. Performance engineering and where latency actually comes from. And now concurrency, parallelism, and the scaling ladder.</p><p>None of these topics is closed. Every one of them has a decade of depth behind it. But you now have the mental models to read deeper on any of them and, more importantly, to recognize which one actually applies to the problem in front of you.</p><p>That recognition is the hard-won part. It doesn’t come from reading the patterns. It comes from misapplying a few of them first.</p><h4>References and Further Reading</h4><p><strong>Concurrency &amp; Event Loop</strong></p><ul><li><a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick">Node.js Event Loop, Timers, and process.nextTick</a>, officially goes deeper into the phases</li><li><a href="https://docs.libuv.org/en/v1.x/">libuv documentation</a>, the async I/O library underneath Node.js</li><li><a href="https://man7.org/linux/man-pages/man7/epoll.7.html">epoll man page</a>: the kernel mechanism behind it all</li></ul><p><strong>Go Concurrency</strong></p><ul><li><a href="https://go.dev/ref/mem">The Go Memory Model</a> official spec on how goroutine memory works</li><li><a href="https://morsmachine.dk/go-scheduler">Go scheduler deep dive, Morsmachine</a> is still the best single explanation of GMP</li><li><a href="https://go.dev/doc/effective_go#concurrency">Effective Go Concurrency</a></li></ul><p><strong>Databases &amp; Scaling</strong></p><ul><li><a href="https://www.postgresql.org/docs/current/indexes-types.html">PostgreSQL Index Types</a></li><li><a href="https://www.pgbouncer.org/usage.html">PgBouncer documentation</a></li><li><a href="https://aws.amazon.com/rds/features/read-replicas/">AWS read replica documentation</a></li><li><a href="https://vitess.io/docs/21.0/overview/whatisvitess/">Vitess YouTube’s MySQL sharding solution</a></li></ul><p><strong>Caching</strong></p><ul><li><a href="https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/">Redis documentation data persistence</a></li><li><a href="https://docs.aws.amazon.com/AmazonElastiCache/latest/dg/BestPractices.html">AWS ElastiCache best practices</a></li></ul><p><strong>Architecture Patterns</strong></p><ul><li><a href="https://12factor.net/">The Twelve-Factor App</a> stateless services, config, and disposability</li><li><a href="https://martinfowler.com/books/eaa.html">Martin Fowler Patterns of Enterprise Application Architecture</a></li><li><a href="https://docs.bullmq.io/">BullMQ documentation</a></li></ul><p><strong>Further Series Reading</strong></p><ul><li>Parts 1–5 of this series cover HTTP internals, databases, caching, search, security, and performance in detail. Start from Part 1 if any of the concepts here feel unfamiliar.</li></ul><p>Part 6 of Backend from First Principles. Thanks for reading the whole series. Questions and disagreements welcome in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e75bd525157" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backend from first principles]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/@karthik.joshi103/backend-from-first-principles-882f4fc806ea?source=rss-159f9833c07e------2"><img src="https://cdn-images-1.medium.com/max/1376/1*3pHVAXn5RkjPYL30O_ZHLQ.png" width="1376"></a></p><p class="medium-feed-snippet">Part 5(Performance Engineering)</p><p class="medium-feed-link"><a href="https://medium.com/@karthik.joshi103/backend-from-first-principles-882f4fc806ea?source=rss-159f9833c07e------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-882f4fc806ea?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/882f4fc806ea</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Tue, 19 May 2026 03:01:06 GMT</pubDate>
            <atom:updated>2026-05-20T03:00:44.397Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Backend from first principles(Security)]]></title>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-security-a5fc9bf8385a?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5fc9bf8385a</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Mon, 11 May 2026 03:01:03 GMT</pubDate>
            <atom:updated>2026-05-11T03:51:17.349Z</atom:updated>
            <content:encoded><![CDATA[<h3>Backend from first principles</h3><h4>Part 4</h4><p>Security is the topic every engineer nods along to in planning meetings and then underinvests in until something goes wrong.</p><p>The mental model that actually helps: every attack is an attacker executing something outside the defined boundary of your system. Your job is to know where your boundaries are and defend them not assume that most inputs will be reasonable, not assume that your internal network is safe, not assume that a logged-in user is acting in good faith.</p><p>That last point is the one that bites people most. “False assumption” is the real attack surface. The more your code assumes, the more gaps an attacker can exploit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E4Id6xyAMDPX7G-pFYY5zw.png" /></figure><h4>SQL Injection</h4><p>SQL injection is old. It’s also still in the OWASP Top 10 every year, which tells you something.</p><p>The attack is simple: if you build queries by concatenating strings, an attacker sends SQL instead of data, and your database executes it. The fix is equally simple: use parameterized queries. Never build SQL with string interpolation.</p><pre>// DON&#39;T vulnerable<br>const query = `SELECT * FROM users WHERE email = &#39;${email}&#39;`;<br><br>// DO parameterized<br>const result = await db.query(<br>  &#39;SELECT * FROM users WHERE email = $1&#39;,<br>  [email]<br>);</pre><p>with parameterised query user input is treated as a literal value ,not executable sql even if someone passed DROP table users; it get passed to the database as string not parsed as a command.</p><p>a few things to also lock down:</p><ol><li>validate and sanitise all inputs before they reach query</li><li>restrict your db user’s permission means your app user shouldn&#39;t have drop table privileges.</li></ol><h4>Command Injection:</h4><p>Same idea different target. If your backend takes user input and passes it to the shell command you’ve handed the attacker OS-level access.</p><pre>// DON&#39;T letting user input touch exec()<br>import { exec } from &#39;child_process&#39;;<br>exec(`convert ${req.body.filename} output.pdf`); // never do this<br><br>// DO use libraries, validate input, avoid shell<br>import sharp from &#39;sharp&#39;;<br>const safeName = path.basename(req.body.filename); // strip path traversal<br>await sharp(`/uploads/${safeName}`).toFile(&#39;output.pdf&#39;);</pre><p>If you genuinely need to run a shell command with user-provided input, use execFile with arguments as an array (no shell interpolation) and validate the input against a strict allowlist before that.</p><h4>Authentication</h4><p>Authentication is one of those things where the cost of getting it wrong is enormous and the right answer is almost always “use an existing solution.”</p><p>Auth0, Supertokens, Clerk these handle the hard parts: OAuth 2.0 flows, SAML federation, session revocation, MFA, refresh token rotation. Building this from scratch is a meaningful engineering project with a long tail of edge cases, and most enterprise will not do that they will relay on Oauth solutions.</p><p>but if you are startup they charge you what matters to you that is daily active users if you have more they will charge more so it’s always best to build your own auth system with minimal third party logins with strong checks like:</p><ul><li>Check verified boolean from Oauth return response</li><li>Don’t include multiple integration analyse your user market according that user behaviour pattern choose the integration</li><li>Use stable identifiers like sub id instead of email to uniquely map the user in db so that if user changes email in the third party platform the system can update user email by adding verification layer</li></ul><p>That said, when you do need to build it yourself, here’s what actually matters.</p><h4>Password storage</h4><p>Plain text: obviously not. MD5 or SHA256: also no fast hash functions can be brute-forced at billions of guesses per second with a GPU.</p><p>The right answer is a slow hashing algorithm designed specifically to be expensive: <strong>Argon2</strong> , bcrypt, or scrypt.</p><pre>import argon2 from &#39;argon2&#39;;<br><br>// On registration<br>const hash = await argon2.hash(password, {<br>  type: argon2.argon2id,<br>  memoryCost: 65536,  // 64MB makes GPU attacks expensive<br>  timeCost: 3,<br>  parallelism: 4,<br>});<br><br>// On login<br>const valid = await argon2.verify(hash, inputPassword);</pre><p>One point to highlight If hashing happens on the server, an attacker can send 10,000 “Login” requests per second. Your server will spend all its CPU power trying to hash those 10,000 passwords, effectively crashing your site. By moving the heavy work to the client, you force the <strong>attacker</strong> to use their own CPU power to generate those hashes, making a high-speed attack much more expensive</p><p>To do this securely, you must use a multi-stage hashing approach:</p><ul><li><strong>Client Side:</strong> Hash the password (e.g., using Argon2) to handle the heavy CPU lifting with web workers without blocking the main thread</li><li><strong>Server Side:</strong> Take the incoming client hash and hash it again (even with a fast algorithm like SHA-256) before comparing it to your database.</li><li>This ensures that the “secret” stored in your database is different from what travels over the network.</li></ul><h4>Token strategy (stateless)</h4><p>Access token + refresh token is the standard. Short-lived access tokens (15 minutes) limit the damage window if one is stolen. Long-lived refresh tokens stay in an httpOnly cookie and are used only to get new access tokens.</p><pre>// Access token short-lived, in memory on client<br>const accessToken = jwt.sign(<br>  { userId: user.id, role: user.role }, // no PII in payload<br>  process.env.JWT_SECRET,<br>  { expiresIn: &#39;15m&#39; }<br>);<br><br>// Refresh token long-lived, stored httpOnly<br>const refreshToken = jwt.sign(<br>  { userId: user.id, tokenVersion: user.tokenVersion },<br>  process.env.REFRESH_SECRET,<br>  { expiresIn: &#39;7d&#39; }<br>);<br><br>res.cookie(&#39;refresh_token&#39;, refreshToken, {<br>  httpOnly: true,<br>  secure: true,<br>  sameSite: &#39;strict&#39;,<br>  maxAge: 7 * 24 * 60 * 60 * 1000,<br>});</pre><p>What goes in the JWT payload: user ID, role, anything needed for authorization. What doesn’t go in the payload: email, name, phone the payload is base64-encoded, not encrypted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*VA9NK_4im6Zv_fCGuyphsA.png" /></figure><h4>Rate limiting on auth endpoints</h4><p>Login endpoints get hit hard in credential stuffing attacks. Rate limiting is not optional.</p><pre>import rateLimit from &#39;express-rate-limit&#39;;<br><br>const loginLimiter = rateLimit({<br>  windowMs: 15 * 60 * 1000, // 15 minutes<br>  max: 10,                   // 10 attempts per IP<br>  message: { error: &#39;Too many attempts, try again later&#39; },<br>  standardHeaders: true,<br>});<br><br>app.post(&#39;/auth/login&#39;, loginLimiter, loginHandler);</pre><p>Layer this with user-level limits too (not just IP) a distributed attack from many IPs still shouldn’t let one account get hammered.</p><h4>Authorization (and BOLA)</h4><p>Authentication answers “who are you?” Authorization answers “what are you allowed to do?” Most security bugs I’ve seen are authorization bugs, not authentication bugs.</p><p>The check that teams most often skip: resource-level ownership. You verify the user is logged in. You verify the user has the right role. You forget to verify the user owns the specific resource they’re accessing.That’s Broken Object Level Authorization (BOLA)</p><pre>// BAD only checks that user is authenticated<br>app.get(&#39;/orders/:id&#39;, auth, async (req, res) =&gt; {<br>  const order = await Order.findById(req.params.id);<br>  return res.json(order); // returns any user&#39;s order<br>});<br><br>// GOOD checks ownership<br>app.get(&#39;/orders/:id&#39;, auth, async (req, res) =&gt; {<br>  const order = await Order.findOne({<br>    _id: req.params.id,<br>    userId: req.user.id, // must belong to THIS user<br>  });<br><br>  if (!order) return res.status(404).json({ error: &#39;Not found&#39; });<br>  return res.json(order);<br>});</pre><p>Notice the 404, not 403. This is intentional. A 403 tells the attacker the resource exists and they just don’t have access a useful signal for probing. Returning 404 for unauthorized resources gives away nothing.</p><p>The broader principle: default deny. Every endpoint should require explicit permission to be accessible. If the middleware doesn’t explicitly grant access, the request should fail.</p><pre>// NestJS guard requires both authentication and the right role<br>@UseGuards(JwtAuthGuard, RolesGuard)<br>@Roles(&#39;admin&#39;)<br>@Delete(&#39;/users/:id&#39;)<br>async deleteUser(@Param(&#39;id&#39;) id: string, @Request() req) {<br>  // Resource ownership check still needed even with role guard<br>  if (req.user.role !== &#39;superadmin&#39; &amp;&amp; req.user.id !== id) {<br>    throw new ForbiddenException();<br>  }<br>  return this.usersService.delete(id);<br>}</pre><h4>XSS</h4><p>Cross-site scripting happens when user-provided content gets rendered in the browser as HTML meaning an attacker can inject a script tag, and that script runs with full access to the page’s cookies, tokens, and DOM.</p><p>The primary defence is server-side sanitisation before storing or rendering any user HTML.</p><pre> import DOMPurify from &#39;isomorphic-dompurify&#39;;<br><br>// Sanitize before storing<br>const cleanHTML = DOMPurify.sanitize(req.body.content, {<br>  ALLOWED_TAGS: [&#39;b&#39;, &#39;i&#39;, &#39;em&#39;, &#39;strong&#39;, &#39;p&#39;, &#39;ul&#39;, &#39;li&#39;],<br>  ALLOWED_ATTR: [],<br>});<br><br>await Post.create({ content: cleanHTML, authorId: req.user.id });</pre><p>The second layer is a Content Security Policy header that tells the browser which scripts are allowed to run:</p><pre>app.use((req, res, next) =&gt; {<br>  res.setHeader(&#39;Content-Security-Policy&#39;, [<br>    &quot;default-src &#39;self&#39;&quot;,<br>    &quot;script-src &#39;self&#39;&quot;,           // no inline scripts<br>    &quot;style-src &#39;self&#39; &#39;unsafe-inline&#39;&quot;,<br>    &quot;img-src &#39;self&#39; data: https:&quot;,<br>    &quot;frame-ancestors &#39;none&#39;&quot;,      // prevents iframe embedding<br>  ].join(&#39;; &#39;));<br>  next();<br>});</pre><h4>Security Misconfiguration</h4><p>This is less a specific attack and more a category of “things developers do that hand attackers easy wins”</p><p>The most common ones:</p><ul><li>Pushing .env files to GitHub (use git-secrets or pre-commit hooks)</li><li>Running debug log level in production (leaks stack traces, query details, internal paths)</li><li>Leaving default credentials on databases.</li></ul><pre>// Validate environment at startup log nothing sensitive<br>const requiredEnvVars = [&#39;DATABASE_URL&#39;, &#39;JWT_SECRET&#39;, &#39;REDIS_URL&#39;];<br>const missing = requiredEnvVars.filter(k =&gt; !process.env[k]);<br>if (missing.length) {<br>  throw new Error(`Missing env vars: ${missing.join(&#39;, &#39;)}`); // names only, not values<br>}<br><br>// Log level from environment<br>const logLevel = process.env.NODE_ENV === &#39;production&#39; ? &#39;warn&#39; : &#39;debug&#39;;</pre><h4>Defense in Depth</h4><p>No single layer holds. The point of layered security is that an attacker who gets through one layer hits another.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*bHgYN41wGfDrlDwDAkgCrA.png" /></figure><p>A reasonable stack looks like this:</p><ul><li><strong>Network layer</strong>: firewall rules, VPC isolation, private subnets for databases</li><li><strong>Application layer</strong>: input validation, parameterized queries, sanitization</li><li><strong>Auth layer</strong>: strong hashing, short-lived tokens, rate limiting, MFA</li><li><strong>Authorization layer</strong>: role checks + ownership checks on every resource</li><li><strong>Output layer</strong>: CSP headers, sanitized error responses (no stack traces to clients)</li><li><strong>Monitoring layer</strong>: anomaly detection, alert on repeated 401s, suspicious query patterns</li><li>Use API Gateways and proxies to route into your backend endpoints with proper health checks and security configurations to prevent abuse early</li></ul><p>When one layer fails and at some point, something fails the others limit the blast radius.</p><p>Part 5 will cover performance: profiling, N+1 queries, connection pooling, and what to reach for before you scale horizontally. Drop questions below if anything here needs more detail.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5fc9bf8385a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backend from first principles]]></title>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-c49ee9073358?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c49ee9073358</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Mon, 06 Apr 2026 03:01:01 GMT</pubDate>
            <atom:updated>2026-05-19T03:43:11.139Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 3</h4><h4>Backend from First Principles: Part 3: Search, Errors, Config, Logs, and Dying Gracefully</h4><p>If you missed Part 1 and Part 2, I covered HTTP internals, databases, caching, and queues. This one picks up from there and gets into some of the less glamorous but genuinely critical parts of building backend systems, the stuff that separates a demo that works from a service that holds up.</p><p>We’re covering six areas: full-text search, error handling, config management, logging and observability, and graceful shutdown. Each of these is easy to get roughly right and hard to get exactly right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3pHVAXn5RkjPYL30O_ZHLQ.png" /></figure><h4>Full-Text Search</h4><p>SQL’s The LIKE &#39;%keyword%&#39; query works. It just works slowly, and it has no idea what you actually meant.</p><p>Real search engines flip the data model. Instead of scanning every row and checking if it contains your term, they build an <strong>inverted index</strong> upfront, a map from each word to every document that contains it, along with where and how often. When you search, you look up the word in the index rather than scanning everything.</p><pre>Index entry for &quot;machine&quot;:<br>  → &quot;Introduction to Machine Learning&quot; (pages 2, 88, 108 — score: high)<br>  → &quot;Art of Cooking&quot; (page 20 — score: low)</pre><p>That relevance score isn’t arbitrary. Elasticsearch uses <strong>BM25</strong> (Best Match 25), a ranking function that weights term frequency against term rarity across all documents. A word appearing on 3 pages of a book specifically <em>about</em> that word ranks higher than the same word buried once in an unrelated chapter.</p><p>Elasticsearch wraps Apache Lucene to give you this on top of things like:</p><ul><li><strong>Fuzzy search: </strong>matches “machin” or “machien” to “machine.”</li><li><strong>Field boosting: </strong>a match in the title scores higher than a match in the description</li><li><strong>Analyzers: </strong>tokenization, stemming, stopword removal, all configurable</li></ul><p>Here’s what a basic boosted query looks like in Elasticsearch:</p><pre>{<br>  &quot;query&quot;: {<br>    &quot;multi_match&quot;: {<br>      &quot;query&quot;: &quot;machine learning&quot;,<br>      &quot;fields&quot;: [&quot;title^3&quot;, &quot;subtitle^2&quot;, &quot;description^1&quot;, &quot;content&quot;],<br>      &quot;fuzziness&quot;: &quot;AUTO&quot;<br>    }<br>  }<br>}</pre><p>The ^3 means the title field gets 3x the weight. This is how you make &quot;Introduction to Machine Learning&quot; outrank &quot;Art of Cooking (which mentions machine learning once in a footnote).&quot;</p><p><strong>Cons of Elasticsearch</strong></p><ul><li>Elasticsearch provides eventual consistency, so if you want to search on new data, you have to index that data, and making it searchable takes time.</li><li>Elasticsearch doesn’t offer durability, which means that when you add a new node to an Elasticsearch cluster, it rebalances shards across all nodes. During rebalancing, you can lose data if the cluster goes down. PostgreSQL and MySQL have WAL and replication logs to recover from. Elasticsearch has no such guarantee on writes in flight during rebalancing.</li></ul><p>PostgreSQL has full-text search built in, too, via tsvector tsquery. It&#39;s solid for a moderate scale. But if you need faceted search, autocomplete, or complex relevance tuning, Elasticsearch is worth the operational overhead.</p><pre>-- PostgreSQL full-text search<br>SELECT title, ts_rank(search_vector, query) AS rank<br>FROM books, to_tsquery(&#39;machine &amp; learning&#39;) query<br>WHERE search_vector @@ query<br>ORDER BY rank DESC;</pre><h4>Error Handling</h4><p>Most error-handling code I’ve seen falls into one of two traps: either catch everything at the top and swallow details, or let specific errors bubble up inconsistently and surprise you in production. Neither is great.</p><p>Errors come from different places and need different handling strategies.</p><p><strong>The Error Taxonomy</strong></p><p><strong>Logic errors</strong>: wrong algorithm, missed edge case, misread requirement. The fix is mostly upstream: write a solution doc before writing code. Explicitly list edge cases before you touch the keyboard. Boring advice, but it works.</p><p><strong>Database error: </strong>connection failures, schema mismatches, type conflicts. A few things help:</p><ul><li>Validate inputs with DTOs <em>before</em> they reach the database layer</li><li>Run health checks on your DB connection at startup and periodically after</li><li>Use parameterized queries to avoid injection and also avoid a whole class of runtime type errors</li><li>Use migration up/down scripts so you can roll back schema changes</li></ul><pre>// DTOs stop bad data before it hits your repo layer<br>class CreateUserDto {<br>  @IsEmail()<br>  email: string;<br>  @IsString()<br>  @MinLength(8)<br>  password: string;<br>}</pre><p><strong>External service errors: </strong>third-party APIs fail. They rate-limit. They time out. Your code needs to assume this will happen, not treat it as exceptional.</p><pre>// Exponential backoff retry<br>async function callWithRetry&lt;T&gt;(<br>  fn: () =&gt; Promise&lt;T&gt;,<br>  maxRetries = 3<br>): Promise&lt;T&gt; {<br>  for (let attempt = 0; attempt &lt; maxRetries; attempt++) {<br>    try {<br>      return await fn();<br>    } catch (err) {<br>      if (attempt === maxRetries - 1) throw err;<br>      const delay = Math.pow(2, attempt) * 100; <br>      await new Promise(res =&gt; setTimeout(res, delay));<br>    }<br>  }<br>  throw new Error(&#39;unreachable&#39;);<br>}</pre><p><strong>Input validation errors: </strong>wrong format, missing field, invalid email. These should fail early and fail clearly with a 400, not crash somewhere in the middle.</p><p><strong>Config errors: </strong>missing API key, bad connection string. If your app can’t start because a required env variable isn’t set, it should tell you that <em>at startup</em>, not crash mid-request when it first tries to use the config.</p><pre>// Validate env at startup, not lazily<br>function validateConfig() {<br>  const required = [&#39;DATABASE_URL&#39;, &#39;REDIS_URL&#39;, &#39;JWT_SECRET&#39;, &#39;STRIPE_KEY&#39;];<br>  const missing = required.filter(key =&gt; !process.env[key]);<br>  if (missing.length &gt; 0) {<br>    throw new Error(`Missing required env variables: ${missing.join(&#39;, &#39;)}`);<br>  }<br>}<br>validateConfig(); // called before server.listen()</pre><p><strong>Error Flow in a Typical backend App</strong></p><pre>Request<br>  │<br>  ▼<br>Validation Pipe ──── 400 Bad Request (invalid payload)<br>  │<br>  ▼<br>Guard / Auth ──────── 401 / 403<br>  │<br>  ▼<br>Controller<br>  │<br>  ▼<br>Service ───────────── Business logic errors thrown here<br>  │<br>  ▼<br>Repository ────────── DB errors thrown here<br>  │<br>  ▼<br>[All errors bubble up]<br>  │<br>  ▼<br>Global Exception Filter ──── Sanitizes &amp; logs, sends clean HTTP response</pre><p>The global exception filter is where you catch everything that slipped through. It sanitizes the error (no stack traces, no DB schema info, no API keys in the response) and logs with context.</p><pre>@Catch()<br>export class GlobalExceptionFilter implements ExceptionFilter {<br>  catch(exception: unknown, host: ArgumentsHost) {<br>    const ctx = host.switchToHttp();<br>    const response = ctx.getResponse&lt;Response&gt;();<br>    const correlationId = ctx.getRequest().headers[&#39;x-correlation-id&#39;];<br>    const status = exception instanceof HttpException<br>      ? exception.getStatus()<br>      : 500;<br>    logger.error({<br>      correlationId,<br>      error: exception instanceof Error ? exception.message : &#39;Unknown error&#39;,<br>      stack: exception instanceof Error ? exception.stack : undefined,<br>    });<br>    response.status(status).json({<br>      statusCode: status,<br>      correlationId,<br>      message: status &gt;= 500 ? &#39;Internal server error&#39; : (exception as HttpException).message,<br>    });<br>  }<br>}</pre><p><strong>Circuit Breaker</strong></p><p>If a downstream service starts failing, retrying indefinitely is going to make things worse; you’ll pile up threads, exhaust connection pools, and cascade the failure.</p><p>The circuit breaker pattern handles this. In normal operation, it’s <strong>closed</strong> (requests pass through). When the error rate crosses a threshold, it triggers <strong>open</strong> requests to fail immediately without even attempting the call. After a timeout, it goes <strong>half-open</strong> and lets a test request through to see if the service recovered.</p><pre>Closed ──[error rate &gt; threshold]──► Open<br>  ▲                                    │<br>  │                              [timeout expires]<br>  │                                    ▼<br>  └──[test request succeeds]──── Half-Open</pre><pre>// Using opossum circuit breaker<br>import CircuitBreaker from &#39;opossum&#39;;<br>const breaker = new CircuitBreaker(callPaymentService, {<br>  timeout: 3000,<br>  errorThresholdPercentage: 50,<br>  resetTimeout: 30000,<br>});<br>breaker.fallback(() =&gt; ({ queued: true })); // queue for later if open</pre><p>Async job failures are a different problem. If a BullMQ job fails after retries, it goes to the <strong>dead letter queue. </strong>You don’t lose the work, and you don’t block the main queue. You can inspect, replay, or alert on it later.</p><h4>Config Management</h4><p>Config is boring until something goes wrong in production because someone hardcoded a staging API key.</p><p>There are three places where config lives:</p><ol><li><strong>.env files: </strong>fine locally, fine in staging with care</li><li><strong>YAML/JSON/TOML files: </strong>better for structured, non-secret config (feature flags, tuning params)</li><li><strong>Secret vaults</strong> (AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager): the right answer for anything sensitive in production</li></ol><pre>Config sensitivity guide:<br>  PORT, LOG_LEVEL, POOL_SIZE    → env file, fine to commit<br>  DB_URL (no creds), REDIS_URL  → env file, don&#39;t commit<br>  DB_PASSWORD, API_KEYS         → secret vault<br>  JWT_SECRET                    → secret vault, rotate regularly</pre><p>Feature flags deserve their own mention. They’re not secrets; they’re toggles that let you control feature rollout without a deploy. You can A/B test, roll out by region or membership tier, and kill a broken feature instantly.</p><pre>// Simple feature flag check<br>const flagsConfig = {<br>  newCheckoutFlow: process.env.FLAG_NEW_CHECKOUT === &#39;true&#39;,<br>  betaSearch: process.env.FLAG_BETA_SEARCH === &#39;true&#39;,<br>};<br>if (flagsConfig.newCheckoutFlow &amp;&amp; user.isPremium) {<br>  return newCheckout(cart);<br>}<br>return legacyCheckout(cart);</pre><p>Docker gives you two distinct moments to inject config: <strong>build-time</strong> (ARG) and <strong>run-time</strong> (ENV). Build-time args get baked into the image, don&#39;t put secrets there. Runtime env is injected at container start, which is what you want for anything sensitive or environment-specific.</p><pre># Build arg — OK for non-sensitive build metadata<br>ARG NODE_ENV=production<br>ENV NODE_ENV=$NODE_ENV<br># Secrets come from your orchestrator at runtime, not the Dockerfile<br># e.g., Kubernetes mounts them from a Secret or ConfigMap<br></pre><h4>Logging, Monitoring, and Observability</h4><p>The difference between “something is broken” and “here’s exactly what broke, when, and why” is how good your observability setup is.</p><p>There are three things to instrument: <strong>logs</strong>, <strong>metrics</strong>, and <strong>traces</strong>. They answer different questions.</p><ul><li><strong>Logs: </strong>what happened (user events, errors, state changes)</li><li><strong>Metrics: </strong>how the system is behaving (CPU, memory, request latency, error rate)</li><li><strong>Traces: </strong>where time was spent across services for a specific request</li></ul><p><strong>Log Levels</strong></p><pre>DEBUG  → local dev only, verbose, never in production<br>INFO   → normal significant events: user signed in, job completed<br>WARN   → recoverable issues that need attention: rate limit hit, retry triggered<br>ERROR  → system-responsible failures: DB query failed, API timeout<br>FATAL  → service is going down</pre><p>The key discipline: don’t log at ERROR for things the user caused (wrong password = WARN, not ERROR). It pollutes your alert channels with noise.</p><p><strong>Structured vs Unstructured</strong></p><p>Unstructured logs look like this:</p><pre>[2024-01-15 14:23:01] User login failed for email: wrong password</pre><p>Structured (JSON) logs look like this:</p><pre>{<br>  &quot;timestamp&quot;: &quot;2024-01-15T14:23:01Z&quot;,<br>  &quot;level&quot;: &quot;warn&quot;,<br>  &quot;event&quot;: &quot;login_failed&quot;,<br>  &quot;userId&quot;: &quot;usr_abc123&quot;,<br>  &quot;correlationId&quot;: &quot;req_xyz789&quot;,<br>  &quot;reason&quot;: &quot;invalid_password&quot;<br>}</pre><p>The second one is searchable in Splunk, Grafana, CloudWatch, whatever you’re using. The first one is readable by humans. Use structured in production, add pretty-printing for local dev.</p><p>Notice I logged userId, not email. Emails are PII; log the ID, look up the email when you need it for investigation. Logging API keys or passwords is obviously bad, but logging emails quietly collects PII into your log aggregation system.</p><p><strong>Correlation IDs</strong></p><p>When a request hits your API gateway, generate a UUID. Pass it in a header (X-Correlation-ID) to every downstream service and include it in every log line. When something goes wrong, you can trace the entire lifecycle of that specific request across every service without guessing.</p><pre>// Express middleware<br>app.use((req, res, next) =&gt; {<br>  req.correlationId = req.headers[&#39;x-correlation-id&#39;] as string <br>    || randomUUID();<br>  res.setHeader(&#39;X-Correlation-ID&#39;, req.correlationId);<br>  next();<br>});<br>logger.info({<br>  event: &#39;payment_processed&#39;,<br>  correlationId: req.correlationId,<br>  userId: req.user.id,<br>  amount: payment.amount,<br>});</pre><p><strong>What to Alert On</strong></p><p>Not everything needs a page. Be deliberate:</p><pre>5xx error rate &gt; 1% for 2 minutes    → page on-call<br>Response P99 &gt; 2 seconds             → page on-call<br>Queue depth &gt; 10,000                 → alert team channel<br>Memory utilization &gt; 80%             → alert team channel<br>Scheduled job failed                 → alert team channel<br>Single 5xx                           → log, no alert</pre><p>Alert fatigue is real. If the team gets paged for things that don’t need immediate human action, they’ll start ignoring alerts.</p><h4>Graceful Shutdown</h4><p>This one trips up a lot of teams because it looks simple until a payment gets dropped.</p><p>The naive shutdown: process gets a kill signal, process dies, in-flight requests get cut off. Anyone who was mid-transaction just lost their work, maybe got charged without confirmation, maybe got their data half-written.</p><p>The graceful version adds a few steps between “signal received” and “process exits”:</p><pre>SIGTERM received<br>  │<br>  ├─ Stop accepting new HTTP connections<br>  │<br>  ├─ Wait for in-flight requests to complete<br>  │<br>  ├─ Drain active DB transactions<br>  │<br>  ├─ Close Redis/WebSocket/queue connections<br>  │<br>  └─ Exit cleanly</pre><p>There are three signals to know about:</p><ul><li><strong>SIGINT: </strong>Ctrl+C in your terminal. Soft, handleable.</li><li><strong>SIGTERM: </strong>sent by Kubernetes or another process saying, “Please shut down.” Soft, handleable.</li><li><strong>SIGKILL: </strong>hard kill. You cannot catch it, you cannot ignore it. If your graceful shutdown takes too long, Kubernetes will send SIGKILL after a timeout (default 30s).</li></ul><pre>const server = app.listen(3000);<br>async function gracefulShutdown(signal: string) {<br>  console.log(`${signal} received, shutting down...`);<br>  // 1. Stop accepting new connections<br>  server.close(async () =&gt; {<br>    // 2. Close DB pool<br>    await db.pool.end();<br>    // 3. Close Redis<br>    await redis.quit();<br>    // 4. Drain queue (stop processing, let active jobs finish)<br>    await queue.close();<br>    console.log(&#39;Shutdown complete&#39;);<br>    process.exit(0);<br>  });<br>  setTimeout(() =&gt; {<br>    console.error(&#39;Shutdown timed out, forcing exit&#39;);<br>    process.exit(1);<br>  }, 25000); // less than Kubernetes&#39; 30s<br>}<br>process.on(&#39;SIGTERM&#39;, () =&gt; gracefulShutdown(&#39;SIGTERM&#39;));<br>process.on(&#39;SIGINT&#39;, () =&gt; gracefulShutdown(&#39;SIGINT&#39;));</pre><p>The timeout on the line setTimeout(..., 25000) is important. If you&#39;re waiting on an external service that&#39;s also down, your graceful shutdown could hang forever. You want to force-exit within Kubernetes&#39; termination grace period, or it&#39;ll send SIGKILL anyway, and you lose the &quot;graceful&quot; part.</p><p>Kubernetes uses health checks to decide whether to route traffic to your pod. If your readiness probe fails, it pulls the pod from the load balancer, allowing you to deliberately fail a readiness check as the first step of a shutdown, which stops new requests immediately without abruptly closing the socket.</p><pre>let isShuttingDown = false;<br>// Kubernetes readiness probe<br>app.get(&#39;/health/ready&#39;, (req, res) =&gt; {<br>  if (isShuttingDown) {<br>    return res.status(503).json({ status: &#39;shutting_down&#39; });<br>  }<br>  res.json({ status: &#39;ready&#39; });<br>});<br>process.on(&#39;SIGTERM&#39;, async () =&gt; {<br>  isShuttingDown = true; // Kubernetes stops sending traffic<br>  await new Promise(res =&gt; setTimeout(res, 5000)); // drain inflight<br>  // ... rest of shutdown<br>});</pre><p>None of these topics exists in isolation. Elasticsearch errors need the same correlation ID infrastructure as your HTTP errors. Config validation failures at startup are 100x cheaper than runtime crashes. Graceful shutdown is useless if you don’t know it fired (that’s a log). It all connects.</p><p>Part 4 will cover security and performance engineering: Drop any questions in the comments, especially if something here looks different from how your team does it. I’m curious what tradeoffs people are making.</p><p>Parts 1 and 2 covered HTTP internals, databases, caching, and message queues.<br><a href="https://medium.com/@karthik.joshi103/backend-from-first-principles-036209a3049c">Part 1</a>, <a href="https://medium.com/@karthik.joshi103/backend-from-first-principles-91eaf3720e38">Part 2</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c49ee9073358" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backend from first principles]]></title>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-91eaf3720e38?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/91eaf3720e38</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Mon, 30 Mar 2026 03:01:00 GMT</pubDate>
            <atom:updated>2026-05-19T03:45:21.884Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part2(REST, Databases, Caching, and Background Jobs)</h4><p>Part 1 was the introduction. From part 2, concepts become more interesting and important. If you missed part 1, check it out <a href="https://medium.com/@karthik.joshi103/backend-from-first-principles-036209a3049c">here</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3pHVAXn5RkjPYL30O_ZHLQ.png" /></figure><h4><strong>REST and Backend API Design Fundamentals</strong></h4><p>In the initial days, Tim Berners Lee built the World Wide Web (WWW) to share resources between networks through HTML format and HTTP protocol, but it was accessible to a very limited range of systems within CERN. When CERN made it public, it got popular, and the scale increased, and people started highlighting small issues at that time.</p><p>Tim had worked on three main technologies, that is, URL(unique address to resource).HTTP (allows for the retrieval of linked resources from across the web), and HTML.</p><p><a href="https://info.cern.ch/hypertext/WWW/"><em>www architecture by tim birners lee</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mVmJs0NcuBxkBxc55RfEWw.png" /><figcaption>WWW Flow</figcaption></figure><p>Every single hyperlink click opened a brand new TCP connection. It solved universal way to name, link, and retrieve documents across all those machines.</p><p>Later, Roy Fielding published the paper on the constraints of www then both these colleagues collaborated and released a standard way for sharing resources over the network and browser through the REST architecture rest means representation state transfer, and it solved 6 constraints.</p><p>1. <strong>Client-server:</strong> The browser and HTTP server were separate processes that communicated over a wire. It was there on the web that’s why CERN released it’s webbrowser, and after many other browsers were built, such mosaic etc.</p><p>2. <strong>Cache:</strong> Responses must label themselves as cacheable or not</p><p>3. <strong>Layered systems:</strong> A client cannot tell whether it’s talking to the origin server or an intermediary. This constraint enabled the infrastructure of the modern internet, later proxies got in the 90s, and then CDNs</p><p>4. <strong>Uniform interface:</strong></p><p>. Everything addressable has a URI.</p><p>. Never transfer the resource itself. You transfer a representation of it.</p><p>. The server must respond with a message that includes enough metadata to describe how to process it.</p><p>5. <strong>Stateless:</strong> Each HTTP request must contain all the information needed to understand it. The server holds no session state between requests.</p><p>6. <strong>Code on demand</strong><br>Before that, SOAP (Simple Object Access Protocol) was there. SOAP is essentially a remote procedure call (RPC) over HTTP using XML as the envelope. It wraps every remote call in XML, sends it over HTTP, and defines a strict contract upfront using another XML format called WSDL.</p><p>SOAP was using only the POST method, and it was very rigid. Later, HTTP evolved with caching, but SOAP didn’t evolve with http that’s led people to use the REST style.</p><p>One of the main highlights of the rest architecture was representing resources in different formats like JSON, HTML, and <br>idempotency, which means the system became more robust and error-free when side effects/effects caused by GET, PUT, PATCH, and DELETE will not change if we change the payload. Otherwise, it will return the same response, but the POST method is non-idempotent, where we can expect different results with the same payload, which is the main reason why only GET responses are cached.</p><p><strong>REST API Design Standards</strong></p><ul><li>resource is generally plural, like books, users, etc</li></ul><p>Ex: <a href="https://api.domain/v1/resource">https://api.domain/v1/resource</a></p><ul><li>/ indicates a hierarchical relationship between the resources</li></ul><p>Ex: /authors/books</p><ul><li>For single resource identification, such as an ID/name, can be used</li></ul><p>Ex: /books/:bookid</p><ul><li>GET methods do not support a request payload. People use request query parameters to get specific resources.</li></ul><p>Ex: /books?genre=comedy</p><ul><li>Request URL can indicate a nested relationship</li></ul><p>Ex: /users/userid/posts/postid/comments</p><h4>Database</h4><p>In the early days, data was stored on magnetic tapes as flat files. Every application owned its own files, defined its own formats, and wrote its own code to read them. The deeper problem was that the data was coupled to programs. If you wanted to reorganize how data was stored on disk, every program that touched that data had to be rewritten. Data and logic were not separated at all. <a href="https://en.wikipedia.org/wiki/Edgar_F._Codd">Edgar F. Codd</a> is considered the father of RDBMS, as he published a <a href="https://dl.acm.org/doi/10.1145/362384.362685">paper</a> on RDBMS that led the whole industry to evolve in the 70’s, but before that, <a href="https://en.wikipedia.org/wiki/Charles_Bachman">Charles Bachman</a> developed the first DBMS known as the Integrated Data Store.</p><p><strong>The short gist of the paper was</strong></p><p><strong>First</strong>: data should be represented as simple flat tables (relations) of rows and columns, nothing else. No pointers, no navigation, no hierarchy. Just tables.</p><p><strong>Second, and more important</strong>: there should be a complete separation between the logical view of data (tables and their relationships) and the physical storage on disk. The application declares what it wants. The database engine figures out how to get it. This is called data independence, and it was the direct solution to the coupling problem that plagued IDS and IMS.</p><p><strong>How does an actual RDBMS work internally?</strong></p><p>Storage layer: pages and the heap</p><p>Data is stored on disk in fixed-size blocks called pages. A table is just a collection of pages. Each page contains a header (metadata about the page), a slot array (pointers to where each row starts within the page), and the actual row data packed from the bottom up. This structure is called a heap file. Rows are stored in no particular order, just wherever there’s space.</p><p>When you insert a row, the engine finds a page with enough free space, writes the row, and updates the slot array. When you delete, it marks the slot as dead but doesn’t immediately reclaim space; a background process (vacuum in PostgreSQL, purge in InnoDB) does that later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QDCRleore-NT6Utg4DnvIA.png" /><figcaption>page</figcaption></figure><p>The B-Tree index</p><p>Sequential scans of heap files are catastrophically slow for large tables. The solution is a B-Tree index — a self-balancing tree structure where leaf nodes contain the actual indexed values plus pointers (page number + slot number) back to the heap rows.</p><p>When you query WHERE id = 42, the engine walks the B-Tree in O(log n) time to find the leaf node containing 42, reads the pointer, goes directly to that page and slot in the heap, and returns the row</p><p><strong>How does query execution happen?</strong></p><p>It happens in four stages.</p><ol><li><strong>Parsing:</strong> Parsing converts your SQL text into an abstract syntax tree.</li></ol><p>ex:</p><p>SQL</p><pre>SELECT <br>    c.customer_name,<br>    c.email,<br>    SUM(o.total_amount) as total_spent,<br>    COUNT(o.order_id) as order_count,<br>    AVG(o.total_amount) as avg_order_value<br>FROM customers c<br>INNER JOIN orders o ON c.customer_id = o.customer_id<br>WHERE o.order_date &gt;= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)<br>GROUP BY c.customer_id, c.customer_name, c.email<br>HAVING total_spent &gt; 1000<br>ORDER BY total_spent DESC<br>LIMIT 50;</pre><p>AST</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZBy6LMcrz5ztRS9yzIA7Rw.png" /></figure><p>Check <a href="https://docs.sqlparser.com/demos/ast-explorer/#ast-visualization">this</a> cool SQL parser tool.</p><p>2.<strong> Semantic analysis:</strong> It is a process that checks that the tables and columns actually exist.</p><p>3.<strong> Query optimizer:</strong> It takes the logical plan and generates multiple possible physical execution plans. Should it use the index or do a full scan? Should it join table A to B or B to A? Should it hash-join or nested-loop join? It estimates the cost of each plan using statistics it maintains about table sizes and value distributions, and picks the cheapest one.</p><p>4.<strong> Execution:</strong> It runs the chosen plan, pulling rows through a pipeline of operators. Cost-based optimization was one of the toughest problems System R (IBM’s prototype) pioneered.</p><p><strong>RDBMS, one of the main concepts, is ACID, which handles transaction management</strong></p><p><em>What happens when two users modify the same data simultaneously, or when the power goes out mid-write?</em></p><ul><li>ACID Atomicity (a transaction either fully commits or fully rolls back), Consistency (the database moves from one valid state to another), Isolation (concurrent transactions don’t see each other’s partial work), Durability (once committed, data survives crashes).</li><li>Before any change is made to a data page, the change is written to an append-only log on disk first(<a href="https://www.postgresql.org/docs/current/wal-intro.html">WAL log</a>). If the system crashes, on restart, the engine replays the log to recover committed transactions and rolls back incomplete ones.</li><li>The data pages themselves are written lazily in the background. This is how durability is achieved without syncing every page to disk on every write.</li><li>Isolation is handled by locking (early systems). Modern systems use MVCC (Multi-Version Concurrency Control)<strong>. </strong>In MVCC, each row has a version chain. A writer creates a new version rather than overwriting the old one. Readers see a consistent snapshot of the database as of their transaction start time without blocking writers.</li></ul><p><strong>Other Databases(NoSQL databases)</strong></p><ul><li><strong>Document databases : </strong>(MongoDB, 2009) came from the observation that many applications naturally work with self-contained documents, a user profile with nested addresses and preferences, and forcing this into normalized tables with joins added unnecessary complexity and latency.</li><li><strong>Column-family:</strong> stores (Cassandra, 2008 from Facebook; BigTable, 2004 Google) came from the need to write at massive scale across multiple data centers with no single point of failure.</li><li><strong>Graph databases:</strong> (Neo4j, 2007) came from the observation that highly connected data, social networks, fraud detection, and recommendation engines are expressed naturally as nodes and edges but require enormous numbers of joins in SQL, each one expensive.</li><li><strong>Time-series databases</strong> (InfluxDB) came from metrics and IoT append-only time-ordered data, where the query patterns are completely different from transactional queries.</li></ul><p>At the end, all databases are solving the gap between how humans want to think about data and how machines actually store it with a different approach.</p><p><strong>Database concepts used in backend development</strong></p><ul><li>Databases provide different data types for handling data like int, ch,charvar,uuid, text, double, timestamp, etc. To identify the uniqueness of the colum primary key is used, which can be used as foriegn key for referencing with other table and while referencing with other tables.</li><li>Using DDL and DML scripts for DB migrations, it is known as up/down migration, which means we have write DB up commands that modify or create new tables as per the requirement, and in the down step, we have write inverse of that, which means deletion of those newly created tables so that we can rollback to the previous version.</li><li>Referential Integrity: It is a database constraint which makes sure the relation between columns stays consistent. It prevents orphan records, ensuring every foreign key value corresponds to an existing row in the referenced table, thus maintaining data accuracy and structural reliability through Cascade Rules. If a parent record is updated/deleted, actions can be automatically cascaded to child tables (e.g., CASCADE, SET NULL, NO ACTION).</li><li>Constraints: We can give constraints for a specific column, like not null, to avoid null values.</li><li>Index: An index is a special lookup table where, along with the index location of that row, is stored in the lookup table. When performing queries, it will directly fetch that row by using that indexed column, which is used to improve the query performance.</li><li>Triggers: Triggers are like jobs that execute after a certain action, for example, after updating the table. Instead of changing the updatedAt field, we can create a trigger that runs after the update operation.</li><li>Views: Views are the virtual table that the result set of a SQL statement table it presents data stored in the underlying base tables dynamically when it is queried.</li></ul><h4>Caching</h4><p>Caching is a concept where we store a subset of primary data for a period of time in primary memory, which is RAM, for better read speed. It is used in two situations.</p><ol><li>Resource-intensive computations.</li><li>Huge data handling.</li><li>Frequent reads than writes.</li></ol><p>The caching concept is used in different applications and in different software, browsers, API, etc.</p><p>a.<strong> DNS:</strong> DNS heavily relies on caching because resolving the IP address of a domain is a resource-intensive task. It maintains cache at os level, browsers have their own DNS cache, root server have their own cache, top-level servers have their own cache. If it is not found in any server, it will fetch from the name server.</p><p>b. <strong>Hardware:</strong> In computer systems, there are three levels of cache (l1,l2,l3, L3). The CPU intelligently stores the data in these caches from RAM.</p><p>c. <strong>Browser: </strong>Browsers cache the static response from the server through the cache header through HTTP.</p><p>d. <strong>CDN: </strong>Global cdn provdiers which offers edge location support. They use caching based on user location in the edge node. The request is routed to the nearest CDN, and the first CDN will check if the response is present in the edge CDN server. If it is not, it will get the data from the main CDN, and stores in the edge CDN, and return the data. Behind the scenes, A<a href="https://www.cloudflare.com/en-gb/learning/cdn/glossary/anycast-network/">nycast IP</a> is used here.</p><p>e. <strong>API:</strong> In backend systems, in-memory database systems like Redis, Memcache, and elastic cache are used to store complex database query results for a period of time to reduce database load and improve the api latency, and it is used when we know that data will not change frequently to avoid database queries. In-memory databases store the data in key-value pairs. The key is the unique ID, and the data can be anything. When we restart or power off, data is deleted to handle this scenario; cache systems store data in secondary storage between intervals.</p><p><strong>Eviction policies</strong> are used for refreshing the data based on standard algorithms because in-memory databases are bound to size. When the capacity is full, data is removed based on these algorithms. The most common algorithms are</p><ol><li>Lru(least recently used).</li><li>Lfu(least frequently used).</li><li>Ttl(which has the latest ttl duration).</li></ol><p><strong>Cache invalidation</strong> is considered one of the toughest problem is software because maintaining the consistency between the database and the cache is a complex operation, as there are a lot of data modifications, and choosing between speed and data consistency is really hard. If we choose speed, data is inconsistent; if we choose data consistency, the cache is inefficient. It is about finding the right balance.</p><p>There are a few patterns to solve this:</p><ul><li><strong>Write-through: </strong>whenever you perform an update, create, or delete operation, you will do the same operation in Redis, also, so that the cache will always be fresh.</li><li><strong>Write-back:</strong> here, the Redis cache is updated first in the background, asynchronously, data base is updated, but if the Redis update fails, data loss can happen.</li><li><strong>Cache-aside(lazy loading): </strong>whenever api requests receives any read request, you will first check whether it is present in the cache. If it is, then you will return from cache; otherwise, fetch that response from the database or any other systems, then store that in cache and return the response. When data updates, the application typically updates the database and invalidates (deletes) the corresponding cache entry to ensure consistency. In most scenarios, cache-aside will solve the invalidation problem.</li></ul><pre>// Cache-aside pseudocode<br>if (redis.exists(key)) return redis.get(key)<br>data = await db.query(sql)<br>redis.setex(key, 300, data)  // 5min TTL<br>return data</pre><p>Common examples/scenarios where cache is used:</p><p>Twitter timeline response is a resource-intensive computation, so it will cache the response.</p><p>User Profiles response: User Profile is generally not updated frequently.</p><h4><strong>Background Jobs + Task Queue</strong></h4><p>In software development, asynchronous processing significantly shapes the system architecture. Background jobs take the main place. These Background jobs will execute in a separate process and reduce the load on the main process in the request-response life cycle. How we structure/architect the implementation of background job execution shapes the scaling part of the system.</p><p>Consider the scenario where we handle tasks synchronously: for user signup, the user is prompted to submit email + password along with user information. After successful user creation, we generally verify the user by authenticating with the verification code sent via email. If this process is synchronous, suppose user creation is successful. Still, the third-party email service is down, and in the same request response lifecycle, we are handling the email job in that case, even though the user is a successful sign-up. Still, we send the 500 code saying something went wrong, which is a bad user experience because the user is created successfully. Still, only verification of email failed, the user received that the whole operation failed, which frustrates the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QvnhZr6jO15fqtTfA153Pg.jpeg" /><figcaption>Synchronous signup flow</figcaption></figure><p>If it is asynchronous operation than what happens is after user signup we send that check your email for verification link but in background we will enqueue the verification email job to queue and consumer will pickup that job and execute that job this is gives user better preemptive user experience if that job fails we will have some retry mechanism using retry strategy such as exponential backoff to handle the error whenever job executed successfully consumer will send the acknowledgement to queue so that it can remove that job from queue so that we should not loss thar task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*oLCdTnTnWvlFZsAyKXLtTQ.png" /><figcaption>Asynchronous signup flow</figcaption></figure><p>In background task execution, three systems will be involved</p><p>1. A job that is formed by the application layer, which will be known as the producer</p><p>2. Broker/Queue a queue system which will hold these jobs until some consumer picks the job</p><p>3. Consumer who will execute that job</p><p>There are typically 4 kinds of background jobs, such as</p><ol><li><strong>One-off tasks:</strong> these jobs have a single responsibility, and it is executed once the primary task, e.g., sending a welcome email.</li><li><strong>Recurring jobs:</strong> these jobs will execute again and again periodically in a fixed interval. These are also known as cron jobs.<br>ex: sending weekly/monthly reports.</li><li><strong>Recurrent jobs:</strong> these jobs will have some hierarchy, which means they will independently call another task. For example, in LMS systems, once a teacher uploads videos, the video is processed for generating bit rate formats of that video, and after that, a thumbnail is generated, and after that, transcriptions are generated. Each task is an individual job.</li><li><strong>Batch tasks:</strong> these are jobs executed batchwise, for example, when a user uploads multiple files in the background, those files are handled as a batch</li></ol><p><strong>General considerations for background jobs handling</strong></p><ul><li>Keep the jobs small with a single responsibility.</li><li>Proper error handling with a robust retry mechanism using a dead letter queue to handle failed jobs.</li><li>Proper monitoring for analysing how many tasks failed, were successful, pending, and the duration of the task.</li><li>Rate limiting: when a user performs a certain action multiple times, handling rate limiting of the third system is crucial for task management.</li><li>idempotency: make sure tasks are idempotent, which means if we perform some action, if that fails, we should be able to rollback to the previous version, so that if another consumer tries to perform that task, it should execute that task successfully.</li></ul><p>To handle the queue, there are a lot of systems available, such as BullMQ and RabbitMQ. These systems will provide robust options and scaling for handling task management at scale.</p><p><strong>Push notifications as background jobs.</strong></p><p>Push notifications are the perfect example of background jobs; mobile application systems don’t directly control the push notifications, it goes through these steps.</p><ol><li><strong>User Permission:</strong> The app requests permission from the user to send notifications.</li><li><strong>Device Token Generation:</strong> If the user grants permission, the app registers with the platform’s push notification service (<a href="https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns">APNs</a> for iOS, <a href="https://firebase.google.com/docs/cloud-messaging">FCM</a> for Android) and receives a unique device token.</li><li><strong>Token Transmission:</strong> The app sends this device token to the developer’s backend server, which stores it, ensuring each message reaches the right device.</li><li><strong>Sending Notification:</strong> When the developer wants to send a notification, their server prepares a payload (the message content) and sends it to the respective push notification service (APNs or FCM), including the device token and authentication credentials.</li><li><strong>Delivery:</strong> The push notification service authenticates the request and routes the notification to the target device, where the operating system handles its display according to user settings.</li></ol><p>If any system tries to send push notifications multiple times, Google and Apple systems will block to avoid spamming.</p><p>Part 2 is done. I included a brief history so that we can understand how the system evolved over the years. Computer scientists have done decades of research on complex systems, and understanding from scratch is very important.<br><strong>Part 3</strong>: Search, Error Handling, Config Management, Logging, Monitoring, Graceful Shutdown.</p><p><strong>Quick question</strong>: What’s your biggest production headache?</p><p>Share below 👇</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91eaf3720e38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Backend from first principles]]></title>
            <link>https://medium.com/@karthik.joshi103/backend-from-first-principles-036209a3049c?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/036209a3049c</guid>
            <category><![CDATA[first-principles-thinking]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Fri, 20 Mar 2026 03:13:54 GMT</pubDate>
            <atom:updated>2026-05-19T03:46:42.723Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part 1: Backend Foundations</h4><p>Backend engineering is like a dealer who talks to the other party and transfers the information to the client. scope of backend engineering, moving beyond simple CRUD APIs to focus on building reliable, scalable, fault-tolerant, and maintainable systems. Developing backend through libraries without understanding how it works under the hood is dangerous. Here are the key reasons to avoid this approach and focus on first principles:</p><ul><li>Lack of Transferability: If a developer only knows a framework, their knowledge cannot easily be applied to a different language or ecosystem if they need to switch technologies for performance or business reasons</li><li>Missing Underlying Concepts: Frameworks often hide the underlying network protocols, database interactions, and server behaviors. Understanding these fundamentals is crucial for building reliable, scalable, and efficient systems.</li><li>Inefficient Problem Solving: Looking at backend problems only through the lens of a particular framework limits a developer’s ability to see the “big picture.”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3pHVAXn5RkjPYL30O_ZHLQ.png" /></figure><h4>Let’s first understand what a backend system is.</h4><p>A backend is a computer (server) that listens for HTTP, WebSocket, or gRPC requests over the internet (typically on ports 80 or 443). Its primary job is to serve content (static files like images/JS or dynamic JSON data) and accept data from clients. Backends are crucial for securely managing data and business logic.</p><p><strong>Why Not Put Backend Logic in the Frontend?</strong></p><ol><li>Security: Browsers are sandboxed environments restricted from accessing file systems or environment variables.</li><li>CORS Policy: Restricts calling external APIs from different domains.</li><li>Database Management: Backends use connection pools to efficiently handle thousands of database requests, which browsers cannot do.</li><li>Computing Power: Servers can be scaled up to handle heavy logic, whereas client devices have limited resources.</li></ol><p><strong>How does the request travel in the network?</strong></p><p>1. From Domain → IP (DNS Magic)</p><p>When you type google.com:</p><ol><li>Browser cache → hit? Done.</li><li>No cache → ask TLD server (.com)</li><li>Root servers → find authoritative name servers</li><li>Get IP → connect to the actual Google server</li></ol><p>Max hops: 30. More = page not found.</p><p>google.com → [browser cache] → [.com TLD] → [Google NS] → 142.250.190.78</p><p>2. Finding the Nearest Server</p><p>Google servers = distributed worldwide (Netherlands, US, etc.).</p><p>Browser → external IP → network hops → nearest Google server.</p><p>Your laptop (192.168.1.100) <br> → ISP → multiple routers (hops) <br> → google.com server in Mumbai</p><p>3. Server Does Its Job</p><p>Server receives request → serves:</p><ul><li>Static files: HTML, CSS, JS, images</li><li>Browser renders → pixels on screen</li><li>Server definition: Takes requests → gives responses.</li></ul><p>4. Packets: Internet’s True Currency</p><p>All data = packets</p><pre>Packet = [Your data] + [metadata]<br>- Source IP/port<br>- Destination IP/port  <br>- Sequence number<br>- Checksum</pre><p><strong>Protocols wrap packets</strong>:</p><ul><li>HTTP/HTTPS (web)</li><li>FTP (files)</li><li>TELNET (remote access)</li></ul><h4>HTTP Routing:</h4><p>Routing Function: While HTTP methods define the <em>what</em> (action/intent), routing defines the <em>where</em> (the resource path).</p><p>Static vs. Dynamic Routes:</p><ul><li>Static Routes: Fixed paths like /api/books that always return the same type of data.</li><li>Dynamic Routes: Paths containing variables, such as /api/users/:idallowing the server to extract specific parameters (like a user ID) to perform custom logic.</li></ul><p>Query Parameters: Key-value pairs appended to a URL (e.g., ?search=value) used to send metadata in GET requests, which do not have a body.</p><p>Nested Routes: A practice of nesting resources within a route to define specific relationships, such as /api/users/123/posts/456 to fetch a specific post from a specific user.</p><p>Versioning and Deprecation: Using versions in URLs (e.g., /v1/, /v2/) to manage changes in API structure without breaking existing client applications.</p><p>Catch-All Route: A fallback mechanism (often denoted as /*) to handle requests that do not match any defined routes, providing a user-friendly &#39;Not Found&#39; message.</p><h4>Serialization and Deserialization:</h4><p>The client sends the request to server request will be binary as we know client code will be handled by javascript and server code can be in java, go, rust any server-side language they need some process to convert the incoming request to language specific data structures that is handled by libraries or language by default provide that wrapper methods example in javascript we do response.json() for converting the readable response stream to json that is known as deserilization.</p><ul><li>The Problem of Language Agnosticism: When a client (e.g., <em>JavaScript</em> in a browser) communicates with a server (e.g., <em>Rust</em> or <em>Node.js</em>), they use different data types and structures. To understand each other, they need a common format.</li><li>The Solution: Common Standards: Serialization is the process of converting data from a language-specific format into a common, serialized format (like <em>JSON</em>) for transmission. Deserialization is the reverse process, converting the received standard format back into a language-specific object.</li><li>JSON: <em>JSON</em> (JavaScript Object Notation) is highlighted as the most popular text-based serialization standard, known for being human-readable and supported by nearly all programming languages.</li></ul><h4>CORS: Normal vs Preflight Requests</h4><p>The browser maintains the security of web pages. If one web page of a different origin is sending a request to another web app of a different origin, then the browser doesn’t allow resource sharing between them because of security concerns.</p><p>If that request-sending page needs the resources, then the resource-sharing app should permit it by allowing that site, which is checked by sending a preflight request first before the actual request, which helps the browser to verify the resource mechanism if resources sending site includes an Access-Control-Allow-Origin HTTP header as that site URL, then it is permitted to share the resources.</p><p>Certain requests don’t always send preflight requests because some requests don’t need to verify the access-control-allow-origin. After all, it is settled as *, which means any origin is allowed for resource sharing, but some requests need to verify the access-control-allow-origin(POST and PUT) to maintain security.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*gh_KFqW1sC0JdO3hsNfDtQ.gif" /><figcaption>CORS Request flow</figcaption></figure><p>CORS trips everyone up at first. Two types:</p><ol><li>Normal requests: Simple GET with standard headers.</li><li>Preflight: Browser sends OPTIONS api first for determining the necessary headers for–POST/PUT methods.</li></ol><p>Server responds 204 No Content → browser proceeds.</p><p><strong>Headers that matter</strong>:</p><ul><li>Request: <br>If-None-Match, If-Modified-Since</li><li>Response: ETag, Cache-Control: max-age=...</li></ul><p>Common codes reminder:</p><ul><li>1xx: Informational</li><li>2xx: Success</li><li>3xx: Redirect</li><li>4xx: Client error</li><li>5xx: Server error</li></ul><p><em>(CORS preflight flow — browser OPTIONS → server 204 → actual request)</em></p><h4><strong>Authentication and </strong>Authorization</h4><p>Authentication has evolved from stateful to stateless and then to stateless authentication providers.</p><p><strong>State Full Authentication</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IFW_2FbqD5_Z_j8sJgUGDg.png" /><figcaption>Statefull authentication</figcaption></figure><p><strong>Stateless authentication</strong></p><p>JWT was introduced, making the system stateless and supporting authentication by storing encrypted user information in a token that can be shared with multiple services to provide distributed-system security, but it is harder to control individual access. Refresh tokens help in this scenario, but since we store a single secret for verification, it is still limited.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wr_T4HI-TTx5CL37k1-XuA.png" /><figcaption>Stateless authentication</figcaption></figure><p><strong>JWT</strong>: Stateless (token = encrypted user info).<br><strong>Problem</strong>: Hard to revoke. <br><strong> Fix: </strong>refresh tokens.</p><p><strong>Where to keep the JWT tokens?</strong></p><p>The answer is cookies, but make sure that cookies have these security standards</p><p><strong>Cookies</strong>: Secure (HttpOnly, SameSite, Secure flags).</p><p><strong>Saml:</strong></p><p>It all started with an XML-based authorization system, but it was mostly used of enterperise workspace integration for employees.</p><p><strong>Oauth</strong></p><p>Later systems became interdependent, meaning one system wanted to access another’s resources. Initially, they used a naive approach of password sharing. However, they soon realised there was no standard way to access on behalf of a user. This led to the evolution of OAuth, which involves three systems: the access verifier, the application that needs access, and the access invoker (user). With user consent, the application authorises the resource server to access specific resources on the user&#39;s behalf. This makes the system robust. Later, integration moved to both client and server-side, leading to OAuth 2, which provided different flows for various application types.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_qgvawD4NDYP7GDPi7DufA.jpeg" /><figcaption>Oauth1.0 Flow</figcaption></figure><p><strong>OAuth2</strong>: Resource owner (you) → client (app) → server.</p><p>The OAuth 2.0 merges phases 1–3 into just A→D, which is why 2.0 feels much simpler — it trades the cryptographic signing requirement for TLS as the security layer.</p><p><a href="https://datatracker.ietf.org/doc/html/rfc6749"><em>(OAuth2 flow: consent → code → token → API access)</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DPv5nUbJvXnGskL7BMH5rA.png" /><figcaption>Oauth2.0 Flow chart</figcaption></figure><p><strong>Flows:</strong> Different flows depend on the application type. A major difference is that in implicit flow, the client application can directly get the access token without an authorization grant</p><ul><li>Auth code (server-side)</li><li>Implicit (client-side)</li><li>Device code (IoT)</li></ul><p>OAuth 2.0 mostly solved the authorization problem, so that on behalf of the user, clients can access the user&#39;s resources with the user’s consent.</p><p><strong>Open ID:</strong> Open ID used OAuth2.0 as a base to solve the authentication problem, that is, solving who the requesting user is. The identity provider handles user registration for user identity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SaTYw_sFqEP1P8DjvCMt2w.png" /><figcaption>OPEN ID flow</figcaption></figure><p><strong>The ID token is the heart of it.</strong> It’s a signed JWT from the identity provider containing claims like sub (a stable user ID), email, name, iat (issued at), exp (expiry), and aud (your client ID). Because it&#39;s a JWT, your app can verify the signature locally without making another network call — that&#39;s the key performance win over the old approach of just calling /userinfo every time.</p><p><strong>A concrete mental model</strong> — “Sign in with Google” is OIDC:</p><ul><li>The access_token Let your app call Google APIs (authorization — OAuth 2.0&#39;s job)</li><li>The id_token tells your app &quot;this is Kartik, their Google ID is 12345&quot; (authentication — OIDC&#39;s job)</li></ul><p>The OpenID Connect protocol designs are open to encourage an open ecosystem of IDPs. While the leading IDPs are currently large cloud service providers, such as Google and Microsoft, OpenID Connect supports many OP types for websites, applications, clients, and devices.</p><p><a href="https://openid.net/developers/how-connect-works/"><em>OpenID: OAuth + ID token (JWT with identity).</em></a></p><p><strong>RBAC:</strong> It is a service/application-level authorization strategy for controlling resources at a granular level based on user roles. separating role-based mechanism from OpenID system is more preferred as we don’t have much more control over the role specially critical resources</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79irXUr3ttSlQqtsVvxv0g.png" /><figcaption>Authorization and Authentication Statck</figcaption></figure><h4>Validation &amp; Transformations</h4><p>In frontend and backend, validation is very Important when it comes to security. In typical API development, logic is divided into the service layer, the controller layer, and the repository layer. The service layer is the layer that contains all the core business logic. The controller layer contains all HTTP routing and response bodies, and the repository layer contains service-level api’s. When it comes to validation, even though from the client side, there is a validation, server-side validation is necessary, as it talks to external systems or db there are three types of validations</p><p><strong>3 Types</strong>:</p><ol><li><strong>Type</strong>: It is about checking whether the expected value has the expected type, like number/string, etc. Libraries like Joi and zod are most popular for payload type checking.</li><li><strong>Semantic</strong>: These are the validations where we check if the given value makes semantically sense.<br>Ex: DOB not in the future, age 1–120.</li><li><strong>Syntactic</strong>: It is validating the syntax of the value, like whether the value is an email or not.<br>Ex: Valid email/phone format.</li></ol><p><strong>Transformation</strong>: Transformation is a process where we modify the incoming request payload to the expected form for communicating with the database.<br>Ex: all query params by default come as strings, but we will transform those to integers depending on our implementation</p><p>Both validation and transformations happen in middleware in most of the systems.</p><h4>Layers: Controllers/Services/Repos/Middleware</h4><p>In the backend or coding in general, keeping things segregated helps a lot with this responsibility separation concept, where each layer has its own responsibility for handling the request.</p><ul><li><strong>Controller</strong>: It will handle all HTTP request parts like parsing, Serialization, transformations, validations, so that data is without any issues and ready to proceed to the next business logic layer.</li><li><strong>Service</strong>: Business logic.</li><li><strong>Repo</strong>: DB ops (queries, connections).</li><li><strong>Middleware</strong>: It is like reusable function that helps to remove redundant function calls rather execute on the fly and pass controll to next function or handler Middleware can be used in handling security, logging, authentication, transformations, validations, global error handling ideally global error handling would be last middle that will catch all errors returns the response thare are some middleware libraries like cors that is usallu configured at first to handle cors check and parsing libraries like body parser these are crucial Middleware handlers.</li></ul><p><strong>Request context</strong>: All languages provide a request context, which means we can store some data specific to that request to handle necessary operations like authorization. We can store some metadata related to user identity and request origin that can be used in middlewares to perform user/request-specific operations.</p><p>Each topic has the potential to be a separate blog, with much research and engineering involved in these. I tried to keep it short. <strong><br>Covered</strong>: CORS → Middleware layers. Full HTTP foundation.</p><p><strong>Part 2</strong>: REST design → Databases → Caching → Background jobs.</p><p>What’s the backend concept that tripped you up the most while learning?</p><p>Drop it comments👇</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=036209a3049c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Frontend First Principles: Why React/Vue/Svelte Feel Familiar]]></title>
            <link>https://javascript.plainenglish.io/frontend-first-principles-why-react-vue-svelte-feel-familiar-41aa19930f5a?source=rss-159f9833c07e------2</link>
            <guid isPermaLink="false">https://medium.com/p/41aa19930f5a</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[svelte]]></category>
            <dc:creator><![CDATA[Kartik Joshi]]></dc:creator>
            <pubDate>Thu, 15 Jan 2026 03:02:49 GMT</pubDate>
            <atom:updated>2026-01-17T08:23:52.033Z</atom:updated>
            <content:encoded><![CDATA[<h4>Part(2/2)</h4><p>In the Previous Part, we discussed core concepts like reactivity, components, states, and routing. If you have missed it, check it out <a href="https://medium.com/@karthik.joshi103/frontend-first-principles-why-react-vue-svelte-feel-familiar-7c7e8b4813cb">here</a>.</p><p>In this section, let’s explore the remaining core concepts from first-principles thinking, styling, data fetching/caching, and performance optimization in React, Vue, and Svelte.</p><p>If you are not a member check <a href="https://medium.com/@karthik.joshi103/frontend-first-principles-why-react-vue-svelte-feel-familiar-41aa19930f5a?sk=63e68d6f475f04f2a0fa7edab1cf77b3">this</a> out</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zhm0bnTlBSAxVJSkgIP9uw.png" /><figcaption>Frontend From First Principles</figcaption></figure><h3>Styling</h3><p>It is mostly independent; there are multiple options for styling inline, CSS files, and SCSS. But the approach used to solve CSS composition is specific to the framework; some rely on Single File Components, and some use a build step.</p><h4>React</h4><p>React doesn’t have a built-in mechanism to handle CSS composition. JSX can be used for inline styling. Other than that, import CSS styles from CSS files, but normal CSS files don’t promise unique class names, and if some other component uses the same className, to tackle that, CSS modules, SCSS are used apart from those third-party styling libraries, such as Tailwind, Styled component got popular. In the latest version style tag is introduced, through which inline styling becomes easier for developers. React provides useTransition and startTransition hooks for handling smooth ui animations.</p><pre>//inline css<br>function Button(){<br>  return &lt;button style={{color:&#39;red&#39;}}&gt;&lt;/button&gt;<br>}<br><br>//style.module.css<br>.btn{<br>  color:red;<br>  padding:1rem;<br>  outline:none;<br>  background:white;<br>}<br>//importing styles<br>import style from &#39;./style.module.css&#39;;<br>function Button(){<br>  return &lt;button className={style.btn}&gt;&lt;/button&gt;<br>}</pre><h4>Vue</h4><p>Vue follows SFC components, which offer reactive binding for class and style. You can conditionally apply styles based on a reactive state. Additionally, it uses scoped styles on the style tag, which makes CSS applied to that specific component. Vue also has preprocessor libraries. Apart from Vue, it offers a transition component for handling animations.</p><pre>&lt;script setup&gt;<br>import { ref } from &#39;vue&#39;<br>const greeting = ref(&#39;Hello World!&#39;)<br>const isActive = ref(true)<br>const hasError = ref(false)<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;p class=&quot;greeting&quot;&gt;{{ greeting }}&lt;/p&gt;<br>  &lt;div<br>  class=&quot;static&quot;<br>  :class=&quot;{ active: isActive, &#39;text-danger&#39;: hasError }&quot;<br>&gt;&lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;style&gt;<br>.greeting {<br>  color: red;<br>  font-weight: bold;<br>}<br>.static{<br>  padding:1rem;<br>}<br>.text-danger{<br>  color:red;<br>}<br>.active{<br>  border:1px solid black;<br>}<br>&lt;/style&gt;</pre><p>It handles CSS composition by creating a unique data attribute for each component, ensuring that CSS is scoped to that component only.</p><p>Which is handled <a href="https://github.com/vuejs/core/blob/aac7e1898907445c8f89b22047a9bfcf0a6e91b8/packages/compiler-sfc/src/compileStyle.ts#L80">here</a>.</p><h4>Svelte</h4><p>Svelte also follows a similar approach to Vue by default; styles declared in the style tag are scoped to that particular component by adding a unique className depending on the hash of the filename. If you want to declare it as global, we can use: global for that. Svelte also offers transitions for smoother animations.</p><pre>//scoped styles<br>&lt;style&gt;<br> p {<br>  /* this will only affect &lt;p&gt; elements in this component */<br>  color: burlywood;<br> }<br>&lt;/style&gt;<br><br>//global styles<br>&lt;style&gt;<br> :global {<br>  /* applies to every &lt;div&gt; in your application */<br>  div { ... }<br><br>  /* applies to every &lt;p&gt; in your application */<br>  p { ... }<br> }<br><br> .a :global {<br>  /* applies to every `.b .c .d` element, in any component,<br>     that is inside an `.a` element in this component */<br>  .b .c .d {...}<br> }<br>&lt;/style&gt;<br><br>//transitions<br>&lt;script&gt;<br> import { fade } from &#39;svelte/transition&#39;;<br><br> let visible = $state(false);<br>&lt;/script&gt;<br><br>&lt;button onclick={() =&gt; visible = !visible}&gt;toggle&lt;/button&gt;<br><br>{#if visible}<br> &lt;div transition:fade&gt;fades in and out&lt;/div&gt;<br>{/if}</pre><h3>Data Fetching</h3><p>Data Fetching is independent of frameworks; most frameworks handle it as a side effect using fetch/axios. Apart from that, there are libraries like react-query that provide a way to handle common errors and loading states, and caching lets us explore how it works.</p><h4>React</h4><p>In React, to handle side effects, there is the useEffect hook, which will execute after the DOM is painted. Fetching data is also a side effect.</p><pre>import {useEffect,useState} from &#39;react&#39;<br>function App(){<br>  const [users,setUsers]=useState([]);<br>  const fetchGithubUsers=async ()=&gt;{<br>    try {<br>      const response=await fetch(&#39;https://api.github.com/users&#39;);<br>      const users=await response.json();<br>      return users;<br>    }catch(error){<br>      throw error;<br>    }<br>  }<br>  useEffect(()=&gt;{<br>      async function startFetching() {<br>        setUsers(null);<br>        try {<br>          const result = await fetchGithubUsers();<br>          if (!ignore) {<br>            setUsers(result);<br>           }<br>          }catch(error){<br>            console.error(error.message)<br>          }<br>       <br>      }<br><br>    let ignore = false;<br>    startFetching();<br>    return () =&gt; {<br>      ignore = true;<br>    }<br>  })<br>return users?.map((user) =&gt; (<br>  &lt;h1 key={user.id}&gt;{user.login}&lt;/h1&gt;<br>));<br>}</pre><p>There is a cache function provided by React, which lets you cache server data or heavy computations, but it is only available in React server components.</p><h4>Vue</h4><p>Vue also offers a similar lifecycle method, onMounted, which can be used for performing data fetching similar to React Query. There is a Vue query that provides a more robust solution.</p><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;div v-if=&quot;loading&quot;&gt;Loading posts...&lt;/div&gt;<br>    &lt;div v-else-if=&quot;error&quot;&gt;Error: {{ error.message }}&lt;/div&gt;<br>    &lt;ul v-else&gt;<br>      &lt;li v-for=&quot;post in posts&quot; :key=&quot;post.id&quot;&gt;{{ post.title }}&lt;/li&gt;<br>    &lt;/ul&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script setup&gt;<br>import { ref, onMounted } from &#39;vue&#39;;<br>import axios from &#39;axios&#39;; // Or use native fetch<br><br>const posts = ref([]);<br>const error = ref(null);<br>const loading = ref(true);<br><br>const fetchPosts = async () =&gt; {<br>  try {<br>    // Example using Axios:<br>    const response = await axios.get(&#39;jsonplaceholder.typicode.com/posts&#39;);<br>    posts.value = response.data;<br>  } catch (err) {<br>    error.value = err;<br>  } finally {<br>    loading.value = false;<br>  }<br>};<br><br>onMounted(() =&gt; {<br>  fetchPosts();<br>});<br>&lt;/script&gt;</pre><h4>Svelte</h4><p>Svelte offers two ways for data fetching: client-side and server-side. Client-side through the onMount method and server-side through the load function from the global file.</p><p><strong>Client Side Data Fetching</strong></p><pre>&lt;script&gt;<br>  import { onMount } from &#39;svelte&#39;;<br><br>  let posts = [];<br><br>  onMount(async () =&gt; {<br>    const response = await fetch(&#39;https://jsonplaceholder.typicode.com/posts&#39;);<br>    posts = await response.json();<br>  });<br>&lt;/script&gt;<br><br>{#if posts.length &gt; 0}<br>  {#each posts as post}<br>    &lt;p&gt;{post.title}&lt;/p&gt;<br>  {/each}<br>{:else}<br>  &lt;p&gt;Loading posts...&lt;/p&gt;<br>{/if}</pre><p><strong>Server Side Data Fetching</strong></p><pre>// src/routes/posts/+page.js<br>export async function load({ fetch }) {<br>  const response = await fetch(&#39;https://jsonplaceholder.typicode.com/posts&#39;);<br>  if (response.ok) {<br>    const posts = await response.json();<br>    return { posts }; // Data is returned as a prop named &#39;data&#39; in the component<br>  }<br>  throw new Error(&#39;Could not fetch posts&#39;);<br>}<br><br><br>&lt;!-- src/routes/posts/+page.svelte --&gt;<br>&lt;script&gt;<br>  export let data;<br>&lt;/script&gt;<br><br>&lt;h1&gt;Posts&lt;/h1&gt;<br><br>{#await data.postsPromise}<br>  &lt;p&gt;Loading posts...&lt;/p&gt;<br>{:then posts}<br>  {#each posts as post}<br>    &lt;p&gt;{post.title}&lt;/p&gt;<br>  {/each}<br>{:catch error}<br>  &lt;p style=&quot;color: red&quot;&gt;{error.message}&lt;/p&gt;<br>{/await}</pre><p>Apart from the same, tanstack query provides a <a href="https://tanstack.com/query/latest/docs/framework/svelte/overview">Svelte</a> wrapper thanks to TannerLinsley.</p><p>React Query uses a reducer pattern for data updates, and for re-rendering, it uses the observer pattern for notifying components as subscribers. It uses an abort controller for cancelling the request and a map-based structure for handling the cache.</p><p>If you see the reducer pattern <a href="https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L607">here</a>, you can see reducer-based updates. map-based handling for <a href="https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L92">cache</a>.</p><h3>Performance Optimization</h3><p>Optimizing web apps is very critical for user experience, so all major frameworks have lots of ways to optimize at different levels, like caching, memoization, code-splitting, preloading, etc. Let’s explore how different frameworks handle performance.</p><p><strong>React</strong></p><p>In React key part for optimization is preventing as many re-renders as we can, which can be done by structuring the state carefully and using memoization techniques such as useMemo,useCallback, React.memo before React 19. In the latest React, React-compiler does that automatically and intelligently.</p><p>Decisions like</p><ol><li>When to use ref vs state?</li><li>Which level to store the state, and where and when to access the state?</li><li>How to get the derived state out of the original state?</li><li>When not to use useEffect?</li><li>Rendering thousands of nodes, but as the user only sees what the user can see at a time, is that necessary?</li></ol><p><strong>useMemo:</strong></p><p>useMemo is a memoization hook that memoizes the values, and it only updates when its dependency changes. It helps in memoizing complex computing logic. How it does it is using the Double buffering technique, that is, React maintains two fiber trees, i.e, currentTree and workInProgress tree. During the updating phase, it checks whether the dependency has changed, which is stored in memoizeState of the tree through objectIs check, and if it has changed, it recomputes the value by calling the callback function and updates the currentTree</p><p>If you see <a href="https://github.com/facebook/react/blob/5aec1b2a8d1bcafb43a7ddb64dc37eacad081bea/packages/react-reconciler/src/ReactFiberHooks.js#L2917">this</a> code, you will know how it initializes during the mounting phase, and <a href="https://github.com/facebook/react/blob/5aec1b2a8d1bcafb43a7ddb64dc37eacad081bea/packages/react-reconciler/src/ReactFiberHooks.js#L2936">this</a> code how it updates during the rerendering phase.</p><p><strong>useCallBack:</strong></p><p>useCallback is pretty similar to useMemo. The main difference it stores the callback function itself in the useMemo hook; nextCreate is the callback that is getting called.</p><pre>const nextValue = nextCreate();</pre><p>But in useCallback, the callback is saved.</p><pre>function mountCallback&lt;T&gt;(callback: T, deps: Array&lt;mixed&gt; | void | null): T {<br>  const hook = mountWorkInProgressHook();<br>  const nextDeps = deps === undefined ? null : deps;<br>  hook.memoizedState = [callback, nextDeps];<br>  return callback;<br>}</pre><p><strong>React.memo:</strong></p><p>React.memo stores the ReactComponent itself, and if previous props are not similar to current props, it will render by replacing the child in the workInProgress tree; otherwise, it will use the memoized component behind the scenes. React will tag each component with a type so that it can act accordingly.</p><p><a href="https://github.com/facebook/react/blob/5aec1b2a8d1bcafb43a7ddb64dc37eacad081bea/packages/react/src/ReactMemo.js#L12">This</a> is where the memo method is exposed, along with the component display name, which is getting initialized.</p><p><a href="https://github.com/facebook/react/blob/5aec1b2a8d1bcafb43a7ddb64dc37eacad081bea/packages/react-reconciler/src/ReactFiberBeginWork.js#L471">This</a> is where the memoized component updation is handled. The main part is.</p><pre>if (!hasScheduledUpdateOrContext) {<br>    // This will be the props with resolved defaultProps,<br>    // unlike current.memoizedProps which will be the unresolved ones.<br>    const prevProps = currentChild.memoizedProps;<br>    // Default to shallow comparison<br>    let compare = Component.compare;<br>    compare = compare !== null ? compare : shallowEqual;<br>    if (compare(prevProps, nextProps) &amp;&amp; current.ref === workInProgress.ref) {<br>      return bailoutOnAlreadyFinishedWork(current, workInProgress, renderLanes);<br>    }<br>  }<br>  // React DevTools reads this flag.<br>  workInProgress.flags |= PerformedWork;<br>  const newChild = createWorkInProgress(currentChild, nextProps);<br>  newChild.ref = workInProgress.ref;<br>  newChild.return = workInProgress;<br>  workInProgress.child = newChild;<br>  return newChild;</pre><p><strong>lazy loading:</strong></p><p>Lazy loading is a technique to asynchronously load the component on demand, which is very critical for reducing client-side bundle size, where there are hundreds of components. The way that is handled is through promises, transitions, dynamic import, and error catching.</p><p>React.lazy expects the callback that returns the promise, so handle that promise. React internally creates an initializer <a href="https://github.com/facebook/react/blob/5aec1b2a8d1bcafb43a7ddb64dc37eacad081bea/packages/react/src/ReactLazy.js#L70">here</a>, and based on the promise status, whether that is resolved, rejected, or pending, it handles the component.</p><p>Apart from these, React offers transitions that allow you to mark a state transition as non-blocking and allow other updates to interrupt it <strong>useDeferredValue</strong> hook to defer updating a non-critical part of the UI and let other parts update first.</p><p>If the page load is still not solved, you can think of other renderings, like SSR using Next.js or others.</p><h4>Vue</h4><p>Similar to React, Vue also offers various tools to handle performance, such as <strong>defineAsyncComponent</strong> for lazy loading/code splitting. Directives such as v-once and v-memo for preventing re-rendering by memoization, apart from Vue, also support different rendering techniques like SSR and SSG for more optimization.</p><p><strong>V-once:</strong></p><p>V-once directive lets you define update only once, never update in subsequent render cycles. It does that by storing the value in cache, also preventing computing. You can check the implementation <a href="https://github.com/vuejs/core/blob/623bfb29a23c36bf935e7faefa64d1baa69d465c/packages/compiler-core/src/transforms/vOnce.ts#L8">here</a>.</p><p><strong>V-memo:</strong></p><p>V-memo is like useMemo, it conditionally memoizes the subtree based on dependency changes, and behind it checks the dependency changes through a shallow object.is like check then if dependency not changed it returns cached version, otherwise creates new vnode</p><p><a href="https://github.com/vuejs/core/blob/623bfb29a23c36bf935e7faefa64d1baa69d465c/packages/runtime-core/src/helpers/withMemo.ts#L4">This</a> is the helper used for the memo directive, <a href="https://github.com/vuejs/core/blob/623bfb29a23c36bf935e7faefa64d1baa69d465c/packages/compiler-core/src/transforms/vMemo.ts#L4">where the</a> actual invocation occurs.</p><p><strong>defineAsyncComponent:</strong></p><p>Vue offers <strong>defineAsyncComponent, </strong>which can be used to load components, which reduces the client-side bundle size dynamically</p><pre>import { defineAsyncComponent } from &#39;vue&#39;<br><br>// Simple async component<br>const AsyncComp = defineAsyncComponent(() =&gt; <br>  import(&#39;./components/MyComponent.vue&#39;)<br>)<br><br>// With options<br>const AsyncComp = defineAsyncComponent({<br>  loader: () =&gt; import(&#39;./components/MyComponent.vue&#39;),<br>  loadingComponent: LoadingSpinner,<br>  errorComponent: ErrorDisplay,<br>  delay: 200,<br>  timeout: 3000<br>})</pre><p>Unlike React, in Vue, it wraps the imported component in a default export, and it retries on error. Also, it offers suspend mode; if it is true, it uses a promise; otherwise, setTimeout delay, and Vue provides built-in error handling, with more options for code splitting compared to React.</p><p><a href="https://github.com/vuejs/core/blob/623bfb29a23c36bf935e7faefa64d1baa69d465c/packages/runtime-core/src/apiAsyncComponent.ts#L4">This</a> is the api for <strong>asyncComponent</strong></p><h3>Svelte</h3><p>SvelteKit offers load functions and enhanced fetch to reduce network waterfall, also supports a built-in image library(@sveltejs/enhanced-img) for handling images, and its Link tag provides an option for preloading, which improves performance. If you are using SvelteKit, by default route-based code splitting, and under the hood, it uses the Vite bundler for that. If you are not using SvelteKit, then you have to use dynamic imports for code splitting.</p><p><strong>Route-based code splitting:</strong></p><p>Svelte first creates the manifest for routes <a href="https://github.com/sveltejs/kit/blob/6cf9491ccdc4e6af8801e681c017c9627e9bf3d6/packages/kit/src/core/sync/create_manifest_data/index.js#L21">here</a> at build time, where it nodes and routes of the application, and at the code generation phase, it defines the code structure to dynamically load component-based router pattern matching through import() and at different runtime like client side all nodes of the route are fetched parrally using promise all to assigned to route object which is done <a href="https://github.com/sveltejs/kit/blob/6cf9491ccdc4e6af8801e681c017c9627e9bf3d6/packages/kit/src/runtime/client/client.js#L1518">here</a> and here hover/touch preload also handled vite is used <a href="https://github.com/sveltejs/kit/blob/6cf9491ccdc4e6af8801e681c017c9627e9bf3d6/packages/kit/src/exports/vite/index.js#L136">here</a> for manual chunking for each route.</p><p><strong>load and enhanced fetch:</strong></p><p>Enhanced fetch is a native fetch function with caching ability + dependency tracking to handle SSR. You can see the implementation <a href="https://github.com/sveltejs/kit/blob/81cd545dd78b60ee994a04d8abacb58bf63584d7/packages/kit/src/runtime/client/fetcher.js#L34">here</a>. Load functions run BEFORE navigation completes, so data is ready when components render — eliminating loading spinners and request waterfalls!</p><p><strong>enhanced-img:</strong></p><p>Svelte Kit provides a package for handling images, which reduces the CLS(cumulative layout shift) by attaching width and height, also uses picture element, and the image is converted to an optimized format with fallback handling for unsupported browsers. You can see the implementation <a href="https://github.com/sveltejs/kit/blob/81cd545dd78b60ee994a04d8abacb58bf63584d7/packages/enhanced-img/src/index.js#L9">here</a>.</p><p>Frameworks agnostic optimizing techniques can be used, such as</p><ol><li>Removing event listeners using abort controller.</li><li>Using CDN for static assets.</li><li>Parrallel api loading.</li><li>Virtualization techniques.</li><li>Using Best Coding Patterns great resource is <a href="https://www.patterns.dev/">patterns.dev</a></li></ol><h3>Conclusion</h3><p>You’ve now got the full mental model:</p><p>Part 1: UI=state, components, data flow, routing<br>Part 2: Styling, data fetching, performance</p><p>Build a mini app using <em>only</em> these concepts in vanilla JS. Then port to your favorite framework. Notice how much easier it feels?.</p><p>Next framework switch? You’ll spend days, not months.</p><p>If you missed part 1, check <a href="https://medium.com/@karthik.joshi103/frontend-first-principles-why-react-vue-svelte-feel-familiar-7c7e8b4813cb">this</a> out.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41aa19930f5a" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/frontend-first-principles-why-react-vue-svelte-feel-familiar-41aa19930f5a">Frontend First Principles: Why React/Vue/Svelte Feel Familiar</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>