<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Atishay Jain</title>
    <description>The latest articles on Forem by Atishay Jain (@atishay_jain).</description>
    <link>https://forem.com/atishay_jain</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1086016%2F6f6b54de-61de-4ddc-b751-d9abe8b5cf55.jpg</url>
      <title>Forem: Atishay Jain</title>
      <link>https://forem.com/atishay_jain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/atishay_jain"/>
    <language>en</language>
    <item>
      <title>What exactly happens when you enter a URL into your Browser</title>
      <dc:creator>Atishay Jain</dc:creator>
      <pubDate>Tue, 18 Jul 2023 10:22:17 +0000</pubDate>
      <link>https://forem.com/atishay_jain/what-exactly-happens-when-you-enter-a-url-into-your-browser-3p35</link>
      <guid>https://forem.com/atishay_jain/what-exactly-happens-when-you-enter-a-url-into-your-browser-3p35</guid>
      <description>&lt;p&gt;Have you ever wondered what happens when you type a website address like google.com or youtube.com into your browser’s address bar and hit enter? Sure, if you are connected to the internet, you are presented with the desired web page. But exactly what goes behind the scenes is what we will be discussing in this blog.&lt;/p&gt;

&lt;h1&gt;
  
  
  Structure
&lt;/h1&gt;

&lt;p&gt;This blog will be divided into two parts, the first part will give a complete high-level overview of the whole process that starts with a client typing in a URL then, then converting the URL to an IP address (using DNS), establishing a connection to share resources and finally rendering the web page.&lt;/p&gt;

&lt;p&gt;In the second part, we will learn about DNS and how it actually resolves the client-provided domain name into the corresponding IP address.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 1: The Overview
&lt;/h1&gt;

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

&lt;p&gt;Let’s just first understand what a URL is-&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;URL stands for Uniform Resource Locator. It is composed of four parts namely- Scheme, Domain, Path and Resource, as shown in the image below.&lt;br&gt;
If you are confused about path and resource, consider a file system and think of path as the directory path and the resource as the actual file (.txt / .cpp file or anything).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QT4ZeJ-F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjyr7uz31cysm315g8nz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QT4ZeJ-F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjyr7uz31cysm315g8nz.png" alt="The four components of a URL" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process starts with the client entering a URL into the browser’s address bar. The browser then parses this URL, which basically means it destructures it into components (see the diagram above). Now that the browser has the domain name extracted, the process of DNS resolution can start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qrSAJzNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjah1oiq1n9e6gg271r6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qrSAJzNm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjah1oiq1n9e6gg271r6.png" alt="Client initiating the process" width="694" height="666"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. DNS Resolution
&lt;/h2&gt;

&lt;p&gt;The machine does not understand English, that’s what we humans understand and thus are provided to type the domain name of whatever site we need to refer to. To fulfill our request, the browser needs to know at exactly what address the web page is stored, and that address must be a bunch of 0’s and 1’s!&lt;/p&gt;

&lt;p&gt;In other words, it is required to convert the domain name into an IP address of the server that currently stores the files to render the required web page. After obtaining the IP, a connection can be established between the client’s machine and the server.&lt;/p&gt;

&lt;p&gt;As soon as we hit enter, the browser first looks into something known as a DNS Cache. A cache is something where recently used information is stored for retrieval to reduce the load on servers. They are of two types-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browser DNS Cache&lt;/strong&gt; — Domain name and IP address mapping stored in the browser. If you are using Chrome, you can see your browser's DNS cache at chrome://net-internals/#dns&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OS DNS Cache&lt;/strong&gt; — You can see the stored IP addresses in your computer by running ipconfig/displaydnson your terminal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yfB4hy4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leqc002r4crtoc7a3vyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yfB4hy4J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leqc002r4crtoc7a3vyw.png" alt="DNS Caches are checked" width="680" height="592"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the value is not found in these caches, the request goes on to the DNS resolver. For now, let’s treat it as a black box that receives a domain name and sends the corresponding IP address back to the browser. The actual working of DNS will be discussed in detail in Part 2 below.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Establishing a Connection
&lt;/h2&gt;

&lt;p&gt;With the IP address obtained from the DNS resolution process, the browser can establish a TCP (Transmission Control Protocol) connection with the web server through a three-way handshake.&lt;/p&gt;

&lt;p&gt;If the website uses HTTPS (secure), an additional step called SSL/TLS handshake occurs to establish a secure connection. You can read more about it &lt;a href="https://www.ssl.com/article/ssl-tls-handshake-overview/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Hsj_q9V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvpqhz0z8z3hcrbbu77u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Hsj_q9V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvpqhz0z8z3hcrbbu77u.png" alt="Image description" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Sending an HTTP Request
&lt;/h2&gt;

&lt;p&gt;Once the connection is established, the browser sends an HTTP (Hypertext Transfer Protocol) request to the web server. The request includes information about the resource you want to access (e.g., the path in the URL) and any additional data if you’re submitting a form or interacting with the website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x-s-rnzA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yoxzqlrvdra8ohxc5bl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x-s-rnzA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yoxzqlrvdra8ohxc5bl.png" alt="Image description" width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Receiving the HTTP Response
&lt;/h2&gt;

&lt;p&gt;After processing the request, the web server sends back an HTTP response to the browser. The response contains an HTTP status code, headers (e.g., Content-Type, Cache-Control), and the requested content itself (e.g., HTML page, images, CSS files).&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Rendering the Web Page
&lt;/h2&gt;

&lt;p&gt;Upon receiving the HTTP response, the browser starts rendering the web page. It interprets the HTML content, loads any additional resources referenced in the HTML (e.g., images, stylesheets, JavaScript files), and executes any JavaScript code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Finally you can interact with your favourite website and all this happens in few seconds, pretty neat isn’t it?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rP3a51vp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qe8yuthamzp3pile2uo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rP3a51vp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qe8yuthamzp3pile2uo.png" alt="Image description" width="522" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 2: DNS Resolution
&lt;/h1&gt;

&lt;p&gt;Now let's expand the black box we created in step 2.&lt;/p&gt;

&lt;p&gt;DNS stands for Domain Name System, it is essentially a distributed and decentralized collection of servers that works together in order to convert the domain name into a corresponding IP address.&lt;/p&gt;

&lt;p&gt;There are mainly four types of servers in DNS -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DNS Resolver&lt;/li&gt;
&lt;li&gt;The Root Server&lt;/li&gt;
&lt;li&gt;Top Level Domain Server (TLD)&lt;/li&gt;
&lt;li&gt;Authoritative Name Server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c_nBNtQr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt2zkkv70yhqzrhs5nry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c_nBNtQr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt2zkkv70yhqzrhs5nry.png" alt="Image description" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will be going through each of their functionality in detail. But before that, we need to know how our computer gets access to this DNS system. So, to answer that let’s learn about ISPs.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. ISP
&lt;/h1&gt;

&lt;p&gt;After a hurtful cache miss in the browser and OS cache storage, the browser turns to an ISP (Internet Service Provider). The ISP looks into its own cached records and if it is not able to find the domain name, it passes the IP address of the DNS Resolver.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some examples of ISP’s are verizon, AT&amp;amp;T, etc.&lt;br&gt;
In India some of the popular ones are Airtel, Reliance Jio, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  2. DNS Resolver
&lt;/h1&gt;

&lt;p&gt;A DNS resolver is anything that returns an IP address corresponding to a domain name, previously our browser, computer, and ISP acted as a DNS resolver. Since we met with cache miss in all the above three, we need to contact a higher authority, a place where we will be definitely able to get our IP address.&lt;/p&gt;

&lt;p&gt;This place is nothing but the network of servers that we call DNS, and the thing that passes our query to this DNS system is the DNS resolver, it basically acts as an entry point to the DNS.&lt;/p&gt;

&lt;p&gt;The address of a DNS resolver is provided by your ISP. But you can also change it by tinkering with the network settings. Changing your DNS can lead to a faster and more secure browsing experience. Maybe now’s a good time to learn what a VPN does (but we will go off-topic, instead learn about it &lt;a href="https://www.fortinet.com/resources/cyberglossary/how-does-vpn-work"&gt;here&lt;/a&gt; :)).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some popular examples are Cloudfare's 1.1.1.1 and Google’s 8.8.8.8&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will be discussing a ‘recursive’ type of DNS where we basically tell the DNS resolver to not come back until it has the IP address.&lt;/p&gt;

&lt;p&gt;There is another type which is called — ‘iterative’. In this, the client makes a series of requests itself until it found a match. In case of a miss, the current server directs the client to try on some other server. Learn more about it &lt;a href="https://www.techtarget.com/whatis/definition/iterative-DNS-query"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Root Server
&lt;/h1&gt;

&lt;p&gt;The root nameserver maintains an up-to-date index of TLD nameservers, each of which handles a specific TLD, such as .com, .org, or .net. The root nameserver also maintains a cache of records, if it is not able to find the queried domain name, it sends back the the address of the appropriate TLD server. (For example, the address of the .in TLD server is sent if the query was for amazon.in).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6rqSPqpd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kluob5vgv4p3llp5knin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6rqSPqpd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kluob5vgv4p3llp5knin.png" alt="Image description" width="800" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The resolver then queries the specified TLD server.&lt;/p&gt;

&lt;p&gt;There are a total 13 sets of these root servers (A-M) which are strategically placed around the globe. They are managed by 12 organization. The map below shows the distribution of these root servers&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YVvLsL8p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2o5uzgz4hyx008dy2zr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YVvLsL8p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2o5uzgz4hyx008dy2zr.png" alt="Image description" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://root-servers.org/"&gt;https://root-servers.org/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Top-Level Domain (TLD) Servers
&lt;/h1&gt;

&lt;p&gt;The TLD servers manage information about domain names under their specific TLDs. When the DNS resolver queries the TLD server for the IP address of the requested domain (e.g., “google.com”), the TLD server either provides the IP address directly(cache system) or refers the resolver to the authoritative name server responsible for that domain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mB1BQBEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x24epoxubyil0wi6g19r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mB1BQBEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x24epoxubyil0wi6g19r.png" alt="Image description" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Authoritative Name Servers
&lt;/h1&gt;

&lt;p&gt;These are the final stop in the DNS resolution process. The authoritative name servers store the DNS records for specific domain names. Each domain typically has at least two authoritative name servers for redundancy and fault tolerance. When the DNS resolver contacts the authoritative name server for the requested domain, it receives the IP address of the server hosting the website associated with that domain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XNRlp5WO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajb8u55rezy7wgjelzjk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XNRlp5WO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajb8u55rezy7wgjelzjk.png" alt="Image description" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  6. IP Address Returned
&lt;/h1&gt;

&lt;p&gt;The client (finally) gets back the IP address from the DNS resolver, which caches the IP address in its local cache for future use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DsHqiyej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqmd7564labbt09g0bvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DsHqiyej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqmd7564labbt09g0bvz.png" alt="Image description" width="560" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2 described in Part 1 is now complete, and we can move on with the rest of the steps as described.&lt;/p&gt;

&lt;p&gt;Notice how caching mechanism is involved in every step of DNS resolution. And how the DNS system is distributed among various servers. This distributed network and the caching mechanisms are necessary in order to make the URL resolution process seamless and efficient.&lt;/p&gt;




&lt;p&gt;If you learned something new, be sure to show your support and check out my other blogs —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Backend Development Primer [Part 1/2]&lt;/li&gt;
&lt;li&gt;The Backend Development Primer [Part 2/2]&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Find me on &lt;a href="https://linkedin.com/in/atishayjain15"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://github.com/Atigit15"&gt;Github&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;All the illustrations in this article are taken from ByteByteGo. Check them out :)&lt;/li&gt;
&lt;li&gt;A short &lt;a href="https://www.youtube.com/watch?v=AlkDbnbv7dk&amp;amp;t=1s"&gt;video&lt;/a&gt; by ByteByteGo (Part 1)&lt;/li&gt;
&lt;li&gt;DNS Resolution explanation by &lt;a href="https://www.youtube.com/watch?v=mpQZVYPuDGU"&gt;PowerCert Animates Videos&lt;/a&gt; (Part 2)&lt;/li&gt;
&lt;li&gt;A short &lt;a href="https://www.youtube.com/watch?v=27r4Bzuj5NQ"&gt;video&lt;/a&gt; by ByteByteGo (Part 2)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.computerhope.com/jargon/d/dns-resolver.htm"&gt;https://www.computerhope.com/jargon/d/dns-resolver.htm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cloudflare.com/learning/dns/what-is-recursive-dns"&gt;https://www.cloudflare.com/learning/dns/what-is-recursive-dns&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>networking</category>
      <category>learning</category>
      <category>design</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Database and Scaling : Backend Development [Part 2/2]</title>
      <dc:creator>Atishay Jain</dc:creator>
      <pubDate>Fri, 07 Jul 2023 08:15:30 +0000</pubDate>
      <link>https://forem.com/atishay_jain/database-and-scaling-backend-development-part-22-38n6</link>
      <guid>https://forem.com/atishay_jain/database-and-scaling-backend-development-part-22-38n6</guid>
      <description>&lt;p&gt;In this article we will continue where we left off. If you haven’t checked the ‘Part 1’ out, I highly recommend to read that first, so you get a basic understanding of how things work at the backend :) Now let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;Authentication and Authorisation&lt;/li&gt;
&lt;li&gt;Performance Optimisation&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Scaling&lt;/li&gt;
&lt;li&gt;Serverless Architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="db"&gt;1. Databases&lt;/h2&gt;

&lt;p&gt;Data persistence is integral to the vast majority of web applications. Being able to understand how to structure, build, and query your own database are important skills for any full stack developer to have.&lt;/p&gt;

&lt;p&gt;Databases are split into “relational” and “non-relational” types of databases, and each handles data and scaling in different manners.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL(Structured query language)&lt;/strong&gt; databases are relational databases that store and manage data using tables. They ensure data integrity, support powerful querying with SQL, and offer scalability, ACID transactions, and data security. Popular implementations include &lt;em&gt;MySQL, PostgreSQL, and Oracle Database&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NoSQL (Not only SQL)&lt;/strong&gt; databases offer flexible data models, scalability, and high performance. They don’t enforce strict schemas (like SQL) and excel in handling large volumes of unstructured data. Popular implementations include &lt;em&gt;MongoDB, Cassandra, and Redis&lt;/em&gt;. They are ideal for real-time data, high-speed transactions, and large-scale analytics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, which DB you chose depends on the type of functionality you want in your application. &lt;a href="https://circleci.com/blog/sql-vs-nosql-databases/"&gt;This&lt;/a&gt; article goes in depth with the difference between the SQL and NoSQL DB’s.&lt;/p&gt;

&lt;h2 id="security"&gt;2. Authentication and Authorisation&lt;/h2&gt;

&lt;p&gt;Are they the same thing ? Definitely not.&lt;/p&gt;

&lt;p&gt;Authentication is the process by which we determine whether a user is who he claims to be. Whereas authorization is the process of verifying what specific applications, files, and data a user has access to. Authorisation always comes after authentication.&lt;/p&gt;

&lt;p&gt;There are various way of providing authentication, some of them being -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classic email and password verification&lt;/li&gt;
&lt;li&gt;Google’s OAuth 2.0 protocol (sign in with google feature)&lt;/li&gt;
&lt;li&gt;JWT tokens. &lt;a href="https://www.youtube.com/watch?v=7nafaH9SddU&amp;amp;ab_channel=TraversyMedia"&gt;Learn More&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Using libraries such as passport and session. &lt;a href="https://levelup.gitconnected.com/everything-you-need-to-know-about-the-passport-local-passport-js-strategy-633bbab6195"&gt;Learn More&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that hashing users' passwords before storing it is a very important step, storing user’s password in plain text in your database is very dangerous flaw. A library called &lt;strong&gt;bcrypt&lt;/strong&gt; helps with hashing and comparing the passwords.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to learn more about authentication, then I recommend reading about &lt;a href="https://www.makeuseof.com/what-is-a-zero-knowledge-proof/"&gt;zero-knowledge proofs&lt;/a&gt;, which is a core concept in authentication.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id="optimisation"&gt;3. Performance Optimisation&lt;/h2&gt;

&lt;p&gt;In today’s fast-paced digital world, a slow-loading website or application can frustrate users and cause them to abandon it altogether. Therefore, it’s crucial to optimize the performance of MERN stack applications to ensure they can handle a high volume of traffic without compromising on speed and performance.&lt;/p&gt;

&lt;p&gt;Performance optimisation can be categorised in to two parts -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code Optimisation&lt;/strong&gt; : This includes techniques like minification of code, lazy loading, server-side rendering, code splitting and load balancing.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Optimisation&lt;/strong&gt; : This includes techniques like indexing, denormalisation, caching, partitioning and database sharding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that these optimisation techniques becomes important when developing enterprise level applications. We will discuss caching and database sharding in detail in the upcoming sections.&lt;/p&gt;

&lt;p&gt;If you want to know more about the techniques mentioned above. Check out this article.&lt;/p&gt;

&lt;h2 id="caching"&gt;4. Caching&lt;/h2&gt;

&lt;p&gt;As discussed above, caching is database optimisation technique. A cache layer or server acts as a secondary storage layer, faster and highly efficient to temporarily store a subset of data which doesn’t change often and is frequently accessed by the client. To maintain it’s small size and freshness, the data items also have an expiration time.&lt;/p&gt;

&lt;p&gt;Two caching paradigms-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cache Aside Pattern&lt;/strong&gt; : Also known as lazy loading, is the most common caching pattern available. Here, the cache is updated after the data is requested. In order to read data from the database, the cache is first checked to determine whether the data is available. If the data is available (also known as a cache hit), the cached data is returned. Otherwise (cache miss), the database is queried for the data and then the cache is the populated with this data.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write-Through Pattern&lt;/strong&gt; : Here, when the application or backend process updates the primary database, the data is also updated in the cache. In case of cache miss, lazy loading pattern is deployed. Here, the chances of a cache miss is reduced but, the size of cache has increased and it’s also storing items which are infrequently accessed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;‘&lt;em&gt;node-cache&lt;/em&gt;’ and ‘&lt;em&gt;memcache&lt;/em&gt;’ are two popular Nodejs packages. The most popular option for caching is using ‘Redis’. Being an in-memory database, its data access operations are faster than any other disk-based database, which makes Redis the perfect choice for caching.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bkF6y7BX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aglmpd1ghoenj1g0r15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bkF6y7BX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aglmpd1ghoenj1g0r15.png" alt="Caching with redis" width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="https://www.mydistributed.systems/2020/07/an-overview-of-storage-engines.html"&gt;this &lt;/a&gt;article for difference between on-disk and in-memory databases.&lt;/p&gt;

&lt;h2 id="scalign"&gt;5. Scaling&lt;/h2&gt;

&lt;p&gt;While developing an application in our local machine we have one server, to which we make a single request at a time. Also, our application acts as a single service. This type of system is called a &lt;strong&gt;monolithic system&lt;/strong&gt; ((0,0,0) in the cube shown below)&lt;/p&gt;

&lt;p&gt;However, an enterprise level application needs to handle multiple requests at the same time from possibly million of users. Surely, there is a need to scale our application so that it can perform in these type scenarios.&lt;/p&gt;

&lt;p&gt;The diagram below shows three dimensions of scaling, let’s discuss each of them in brief below-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5UgKRGTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w43ido9qgiddc85xigu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5UgKRGTl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w43ido9qgiddc85xigu.png" alt="Scale Cube" width="800" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cloning Services&lt;/strong&gt;:
To prevent one server to become swarmed with requests and thus take a lifetime to respond, we create multiple instances of our server listening for the same set of requests. We can implement this in our local machine, we use cluster module which is a inbuilt module in Nodejs, having multiple cores (or CPU’s) by running each instance in each of the core.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cluster&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalCpus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;os&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;cpus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//totalCpus will give total number of CPU's in your machine&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isMaster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;totalCpus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="c1"&gt;// create worker threads&lt;/span&gt;
  &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//each worker thread will execure this&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//This will result in multiple servers listening on the same PORT and also &lt;/span&gt;
&lt;span class="c1"&gt;//connected to the database seperately.&lt;/span&gt;
&lt;span class="c1"&gt;//Output&lt;/span&gt;
&lt;span class="cm"&gt;/* Server running on 3000
   Server running on 3000
   Server running on 3000
   ....8 times, if running on octa-core machine.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out &lt;a href="https://www.youtube.com/watch?v=f6u3xSsHylY&amp;amp;ab_channel=JavascriptProgrammer"&gt;this &lt;/a&gt;video to see the difference in response time of multiple requests while using one versus multiple servers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Sharding&lt;/strong&gt;:
We saw above that we can create multiple server instances, but the problem is that all these server access the same database, thus querying a DB results in a bottleneck in our application’s performance. Database sharding provides the solution for this, using this technique each server is only responsible for only a subset of data.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For example, let’s take the example of Amazon. Let’s assume that amazon servers are running on 3 servers (let’s call them server A,B and C). These servers are designed in such a way that each cater to the request of only a subset of the user. For instance, Servers A handles users whose names' start with ‘A’ to ‘I’. Server B handles ‘J’ to ‘R’ and C handles ‘S’ to ‘Z’. Now suppose of server B requests data fromt he database, the database will now know that it needs to look in that partition that store users’ information with initial letter from ‘J’ to ‘R’&lt;br&gt;
.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thus, data partitioning, as shown in the above example can improve the application’s capacity and availability.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Microservices:&lt;/strong&gt;
Microservices are an architectural and organizational approach to software development where software is composed of small independent services that communicate over well-defined APIs. Where as Z-axis scaling splits things that are similar, Y-axis scaling splits things that are different. At the application tier, Y-axis scaling splits a monolithic application into a set of services. Each service implements a set of related functionality such as order management, customer management etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DmvtPWEU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzrbvdl56l6l1z6icjnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DmvtPWEU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzrbvdl56l6l1z6icjnl.png" alt="Microservices AWS" width="694" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now our multiple server instances can assigned to a different microservices. This way multiple independent functions in our app need not to wait for each other and can run at the same time. Note that how we divide our application into microservices are important and require careful planning.&lt;/p&gt;

&lt;p&gt;Two ways to run your application that has been divided into microservices are using- &lt;strong&gt;container&lt;/strong&gt; or &lt;strong&gt;serverless&lt;/strong&gt;. A platform that provides container service is ‘&lt;em&gt;Amazon elastic container service’&lt;/em&gt;. We will discuss serverless architecture in detail next.&lt;/p&gt;

&lt;p&gt;If you are interested to know more about backend scalability, you can check out ‘&lt;strong&gt;&lt;a href="https://www.oreilly.com/library/view/art-of-scalability/9780134031408/"&gt;The Art of Scalability&lt;/a&gt;&lt;/strong&gt;’. Or you can check out &lt;a href="https://www.youtube.com/playlist?list=PLrwNNiB6YOA3xc1dfQpHuaU8HsKxkEgiQ"&gt;this &lt;/a&gt;youtube playlist.&lt;/p&gt;

&lt;h2 id="serverless"&gt;6. Serverless Architecture&lt;/h2&gt;

&lt;p&gt;As you may have noticed by now, maintaining a server, making it scalable and secured among other things is not an easy job. Thus, by adopting serverless architecture, developers can offload these responsibilities to a third-party provider, enabling them to focus on writing application code.&lt;/p&gt;

&lt;p&gt;Function as a Service (FaaS), a popular type of serverless architecture, allows developers to focus on writing application code.Function as a Service (FaaS), a popular type of serverless architecture, allows developers to focus on writing application code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qD43E6Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ncqpbbhvvrxzyqq0faj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qD43E6Oj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ncqpbbhvvrxzyqq0faj.png" alt="Serverless architecture" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the most popular serverless architectures is Function as a Service (FaaS), where developers write their application code as a set of discrete functions. Each function will perform a specific task when triggered by an event, such as an incoming email or an HTTP request. Developers then deploy their functions, along with their triggers, to a cloud provider account.&lt;/p&gt;

&lt;p&gt;When a function is invoked, the cloud provider either executes the function on a running server, or, if there is no server currently running, it spins up a new server to execute the function. This execution process is abstracted away from the view of developers, who focus on writing and deploying the application code.&lt;/p&gt;

&lt;p&gt;Few of the major FaaS providers are &lt;em&gt;AWS Lambda, Google Cloud Functions and AWS Functions.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.theodinproject.com/lessons/nodejs-introduction-to-mongodb"&gt;https://www.theodinproject.com/lessons/nodejs-introduction-to-mongodb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://circleci.com/blog/sql-vs-nosql-databases/"&gt;https://circleci.com/blog/sql-vs-nosql-databases/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.sailpoint.com/identity-library/difference-between-authentication-and-authorization/"&gt;https://www.sailpoint.com/identity-library/difference-between-authentication-and-authorization/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.logrocket.com/caching-node-js-optimize-app-performance/"&gt;https://blog.logrocket.com/caching-node-js-optimize-app-performance/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/clickysoft/optimizing-mern-stack-performance-techniques-for-high-traffic-applications-628"&gt;https://dev.to/clickysoft/optimizing-mern-stack-performance-techniques-for-high-traffic-applications-628&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.infoq.com/articles/microservices-intro/"&gt;https://www.infoq.com/articles/microservices-intro/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.datadoghq.com/knowledge-center/serverless-architecture/"&gt;https://www.datadoghq.com/knowledge-center/serverless-architecture/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/microservices/"&gt;https://aws.amazon.com/microservices/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Image Source
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover : &lt;a href="https://realpython.com/python-contact-book/"&gt;https://realpython.com/python-contact-book/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.com/blog/design-pattern-mean-mern-stack-performance/"&gt;https://redis.com/blog/design-pattern-mean-mern-stack-performance/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.datadoghq.com/knowledge-center/serverless-architecture/"&gt;https://www.datadoghq.com/knowledge-center/serverless-architecture/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/microservices/"&gt;https://aws.amazon.com/microservices/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://thenewstack.io/from-monolith-to-microservices/"&gt;https://thenewstack.io/from-monolith-to-microservices/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>database</category>
      <category>security</category>
      <category>serverless</category>
    </item>
    <item>
      <title>The Basics : Backend Development [Part 1/2]</title>
      <dc:creator>Atishay Jain</dc:creator>
      <pubDate>Fri, 07 Jul 2023 07:18:02 +0000</pubDate>
      <link>https://forem.com/atishay_jain/the-basics-backend-development-part-12-8gd</link>
      <guid>https://forem.com/atishay_jain/the-basics-backend-development-part-12-8gd</guid>
      <description>&lt;p&gt;This article will include all the major terms you hear when backend development is discussed. You can use this to skim through all the important concepts before an interview ;) &lt;br&gt;
Note that this article is written with the MERN stack in view, but the underlying concepts are universal.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;HTTP&lt;/li&gt;
&lt;li&gt;Middleware Functions&lt;/li&gt;
&lt;li&gt;REST Architecture&lt;/li&gt;
&lt;li&gt;CRUD Operations&lt;/li&gt;
&lt;li&gt;MVC Architecture&lt;/li&gt;
&lt;li&gt;CORS&lt;/li&gt;
&lt;li&gt;Frameworks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="itb"&gt;1. Introduction to Backend&lt;/h2&gt;

&lt;p&gt;The back-end is the code that runs on the server, that receives requests from the clients, and contains the logic to send the appropriate data back to the client. The back-end also includes the database, which will persistently store all of the data for the application.&lt;/p&gt;

&lt;p&gt;The backend is made up of three parts :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The server&lt;/strong&gt; : This is the computer that receives requests. To make your machine a server, install Node.js which is runtime environment for Javascript and listen on desired port number.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The app&lt;/strong&gt; : This is the application running on the server that listens for requests, retrieves information from the database, and sends a response. A framework like express helps in making these apps.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The database&lt;/strong&gt; : Databases are used to organize and persist data. They can be classified into SQL and NoSQL databases. MERN stack follows MongoDB database which is a NoSQL database.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="http"&gt;2. HTTP&lt;/h2&gt;

&lt;p&gt;HTTP stands for &lt;em&gt;Hypertext Transfer Protocol&lt;/em&gt;  and is used to structure requests and responses over the internet. HTTP requires data to be transferred from one point to another over the network. The resources itself are transferred with the help of a TCP connection.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analogy
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Suppose you are at restaurant and you call a waiter for him to take your order. You dictate your order in english and the waiter notes it down, he then makes a track (at the speed of light) to the kitchen and tells the chef to prepare the order. He comes back, while ripping of the track, with the dish or some unfortunate news that the dish can’t be prepared.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lUd4ljEI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/209pwfb6l6j4i8csq5fr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lUd4ljEI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/209pwfb6l6j4i8csq5fr.png" alt="HTTP with analogy" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you are the &lt;em&gt;client&lt;/em&gt;, the waiter the &lt;em&gt;server&lt;/em&gt; and the chef is the &lt;em&gt;database&lt;/em&gt;. To dictate your order you chose english, while the request on web uses &lt;em&gt;HTTP&lt;/em&gt;, the track is the &lt;em&gt;TCP connection&lt;/em&gt; to deliver resources, and whatever the waiter comes back with is the &lt;em&gt;response&lt;/em&gt;. The dish you want to eat is the &lt;em&gt;URL&lt;/em&gt; (uniform resource locator), this tells the server what actually the client needs.&lt;/p&gt;
&lt;h3&gt;
  
  
  How it is different from HTTPS ?
&lt;/h3&gt;

&lt;p&gt;This ‘S’ in HTTPS stands for &lt;em&gt;security&lt;/em&gt;. Coming to the analogy above, if you tell the waiter to include a secret sauce in the dish, it might not be a good idea for the waiter to note it in plain text, ( it can be read by anyone at the track junctions !) To secure sensitive and personal content, some servers may provide an encryption facility.&lt;/p&gt;

&lt;h2 id="middleware"&gt;3. Middleware Functions&lt;/h2&gt;

&lt;p&gt;Middleware is any code that executes between the server receiving a request and sending a response. These functions might modify the request object, query the database, or otherwise process the incoming request.&lt;/p&gt;

&lt;p&gt;In the analogy, the actual preparation of the dish can be thought of a middleware. Middleware functions typically end by passing control to the next (using next()) middleware function, rather than by sending a response. You often see them recieve three parameters — req, res and next.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;//our middleware functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myLogger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LOGGED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myLogger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// code excerpt taken from express documentation&lt;/span&gt;

&lt;span class="c1"&gt;//Output&lt;/span&gt;
&lt;span class="cm"&gt;/* Every time the app receives a request, 
it prints the message “LOGGED” to the terminal.*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2 id="rest"&gt;4. REST Architecture&lt;/h2&gt;

&lt;p&gt;REST, or &lt;em&gt;REpresentational State Transfer&lt;/em&gt;, is an architectural style for building API’s , providing standards between computer systems on the web, making it easier for systems to communicate with each other.&lt;/p&gt;

&lt;p&gt;A RESTful API uses commands to obtain resources. In REST, each resource is identified by a unique URI (Uniform Resource Identifier) and can be manipulated using standard HTTP methods such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GET&lt;/strong&gt; to retrieve a resource;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PUT&lt;/strong&gt; to change the state of or update a resource, which can be an object, file or block;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;POST&lt;/strong&gt; to create that resource; and&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DELETE&lt;/strong&gt; to remove it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kyATpLkq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr93nb9kuwtt59nb6arc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kyATpLkq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr93nb9kuwtt59nb6arc.png" alt="REST api for movie app" width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is based on the idea that the REST APIs are &lt;strong&gt;stateless&lt;/strong&gt;, meaning that calls can be made independently of one another, and each call contains all of the data necessary to complete itself successfully.&lt;/p&gt;

&lt;p&gt;It also &lt;strong&gt;separates the concern&lt;/strong&gt; of server and client, allowing their implementation to be done independently of each other.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By using a REST interface, different clients hit the same REST endpoints, perform the same actions, and receive the same responses.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id="crud"&gt;5. CRUD Operations&lt;/h2&gt;

&lt;p&gt;CRUD stands for &lt;strong&gt;Create, Read, Update and Delete&lt;/strong&gt; operations. Think of a simple blog website, the basic function you want to give your users are — creating a blog, reading blogs of other people, updating content and deleting a blog. It’s crucial for a developer to know and implement these CRUD operations.&lt;/p&gt;

&lt;p&gt;Corresponding to each type of operation there is a different HTTP verb that is included in the request that the client makes. HTTP verbs for CRUD are POST, GET, PUT, and DELETE respectively. Apart from HTTP verbs, a client’s request have certain other fields like — header and accept parameter, a path to resource and optional body content. Read about it here.&lt;/p&gt;

&lt;p&gt;Similarly, server’s response consists of — content type, status code and any data the client requested for (read operation). Client will only accept the data if it’s type is one of the mentioned types in header of the request.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Clients request :
GET http://fashionboutique.com/customers
Accept: application/json
/*
HTTP verb : GET
Resource Locator : fashionboutique.com/customers
Accept files which are application/json format.
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Servers response
Status Code: 200 (OK)
Content-type: application/json
/*
Status code 200 implies that response is successfully generated. 
Content type : the response is in application/json format
followed by the actual customers data
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2 id="mvc"&gt;6. MVC Architecture&lt;/h2&gt;

&lt;p&gt;MVC stands for Model, View, Controller and refers to the architecture of your code. It is a way to organize your application by separating all of the actions into 3 main components: Models, Views and Controllers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iYAfGBTu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9taa6aja0tf3mwx4hqdz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iYAfGBTu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9taa6aja0tf3mwx4hqdz.png" alt="Model View Controller diagram" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Models&lt;/strong&gt; are the basic building blocks of your database. So for every entry in your DB , you’ll create a model that holds the details of that entry. Models define the types of information that get used by your views and controllers. (Mongoose)&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Views&lt;/strong&gt; are the component that generates the UI for your application. It uses data supplied by a controller, plug that data in html with the help of a templating engine and display the desired information.(EJS or React-engine).&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Controllers&lt;/strong&gt; are the components that decide what view to display and what information is going to be put into it. It’s where the main logic of your server resides.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id="cors"&gt;7. CORS&lt;/h2&gt;

&lt;p&gt;The Same Origin Policy is an important security measure that basically says “Only requests from the same origin (the same IP address or URL) should be allowed to access this API”.&lt;/p&gt;

&lt;p&gt;But, we are specifically trying to set up our API so that we can access it from different origins, so to enable that we need to set up &lt;em&gt;Cross-origin resource sharing&lt;/em&gt;, or CORS.&lt;/p&gt;

&lt;p&gt;Setting up CORS is very easy by using a npm library of the same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Simple Usage (Enable All CORS Requests)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/products/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is CORS-enabled for all origins!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2 id="frameworks"&gt;8. Frameworks&lt;/h2&gt;

&lt;p&gt;A framework is just boilerplate code bundled in an organised way. A obvious advantage of using a framework is that you don’t have to repeatedly write the same code. Another is that it gives you the organisation of files from the start according to MVC principles. Some good examples are —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Express&lt;/li&gt;
&lt;li&gt;Django,&lt;/li&gt;
&lt;li&gt;Ember&lt;/li&gt;
&lt;li&gt;Rails and many more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using frameworks can reduce the verbose and provide necessary abstraction to speed up your development .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the ‘Part 2' of this primer to know about how to scale and optimise your application and other cool stuff.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;References :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/article/back-end-architecture"&gt;https://www.codecademy.com/article/back-end-architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/article/http-requests"&gt;https://www.codecademy.com/article/http-requests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/article/what-is-rest"&gt;https://www.codecademy.com/article/what-is-rest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.theodinproject.com/lessons/nodejs-express-102-crud-and-mvc"&gt;https://www.theodinproject.com/lessons/nodejs-express-102-crud-and-mvc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.theodinproject.com/lessons/nodejs-api-basics"&gt;https://www.theodinproject.com/lessons/nodejs-api-basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://expressjs.com/en/resources/middleware/cors.html"&gt;https://expressjs.com/en/resources/middleware/cors.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Image Source :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cover : &lt;a href="https://realpython.com/tutorials/front-end/"&gt;https://realpython.com/tutorials/front-end/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;HTTP : &lt;a href="https://apipheny.io/wp-content/uploads/2020/10/api-waiter-1.png"&gt;https://apipheny.io/wp-content/uploads/2020/10/api-waiter-1.png&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;REST : &lt;a href="https://www.sitepoint.com/creating-restful-apis-express-4/#creating-the-api-for-the-movie-app"&gt;https://www.sitepoint.com/creating-restful-apis-express-4/#creating-the-api-for-the-movie-app&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;MVC : &lt;a href="https://www.interviewbit.com/blog/mvc-architecture/"&gt;https://www.interviewbit.com/blog/mvc-architecture/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>mern</category>
      <category>backend</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
