<?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 Mojammel Haque Bhuiyan on Medium]]></title>
        <description><![CDATA[Stories by Mojammel Haque Bhuiyan on Medium]]></description>
        <link>https://medium.com/@mhbhuiyan?source=rss-518ac2752afd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GYNUPzKuF4dxDFCG6pDgsQ.jpeg</url>
            <title>Stories by Mojammel Haque Bhuiyan on Medium</title>
            <link>https://medium.com/@mhbhuiyan?source=rss-518ac2752afd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:38:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mhbhuiyan/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[Inside TCP: The Protocol That Keeps the Internet Reliable]]></title>
            <link>https://levelup.gitconnected.com/inside-tcp-the-protocol-that-keeps-the-internet-reliable-05fccecdfbd4?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/05fccecdfbd4</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[networking]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Wed, 01 Apr 2026 17:35:03 GMT</pubDate>
            <atom:updated>2026-04-02T19:31:40.722Z</atom:updated>
            <content:encoded><![CDATA[<p>The internet feels instant — you click a link, a page loads, you send a message and it arrives. But underneath all of that, devices are constantly breaking data into small pieces, sending them across unpredictable networks, and reassembling them perfectly on the other side. The protocol that makes this reliable is TCP. This article breaks down exactly how it works — from establishing a connection to every field inside a TCP segment.</p><p><strong>TCP</strong> stands for <strong>Transmission Control Protocol</strong>. <br>TCP handles the process of transmitting data. So, the Transport layer is responsible to transmit data from source to destination.</p><blockquote>A protocol is a <strong>set of rules</strong> that define how data is communicated between devices.</blockquote><h3>TCP communication steps:</h3><ol><li><strong>Connect</strong> → <em>3-way handshake</em></li><li><strong>Transfer Data<br></strong>After the connection is established: <br>- TCP splits data into packets<br>- Assigns sequence numbers<br>- Waits for ACKs<br>- Retransmits any lost packets.</li><li><strong>Disconnect</strong> → <em>4-step termination<br></em>4 step need to terminate the connection, because TCP is full-duplex (both sides send independently). So each side must close separately.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tzt-YEFLdh_-ll_BSo-BCg.png" /><figcaption>TCP communication steps</figcaption></figure><p>In every single request sender send <strong>TCP segment</strong> (Header + Data) to the receiver.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ecOEJIEfiSh1d1os9x5-KA.png" /><figcaption><strong>TCP segment</strong></figcaption></figure><h3><strong>Port</strong></h3><p>A port is a <strong>logical number</strong> used to identify a specific application or service on a computer.</p><p><strong>:: How many ports are possible?</strong></p><p>Ports are based on a <strong>16-bit </strong>number in TCP (also in UDP). <br>Total possible ports — 2¹⁶ = 65536. So, the range is — 0 to 65535.</p><p>Every network communication has 2 ports:</p><ul><li><strong>Source Port</strong> → sender application</li><li><strong>Destination Port</strong> → receiver application</li></ul><p><strong>:: How many applications can run at a time?</strong></p><p>In theory: Up to one app per port. So, Max ≈ 65535 apps using TCP at same time (practically much less).</p><h4>Types of Ports:</h4><ol><li><strong>Well-Known Ports</strong> (0–1023)<br>- Reserved for standard services<br>- Managed by Internet Assigned Numbers Authority<br>- Examples: 80 → HTTP, 443 → HTTPS, 22 → SSH</li><li><strong>Registered Ports</strong> (1024–49151)<br>- Used by applications<br>- Can be registered<br>- Examples: 3306 → MySQL, 5432 → PostgreSQL</li><li><strong>Dynamic / Private Ports</strong> (49152–65535)<br>- Used temporarily by clients<br>- Also called <strong>ephemeral ports<br>- </strong>Example: Browser automatically picks one</li></ol><p>When our app (browser, Go program, etc.) creates a connection using TCP we didn’t specify a source port <strong>OS</strong> automatically assigns an ephemeral port.</p><p>OS looks at the ephemeral port range and picks a port that is:</p><ul><li>Not currently in use</li><li>Available for that protocol</li></ul><h3>Sequence Number</h3><p>Ports tell us where data should go. But once a connection is established and data starts flowing, TCP needs a way to keep track of what was sent and in what order — that’s where sequence numbers come in.</p><p>A sequence number is a number assigned to each byte (or segment) of data sent in TCP. It represents the position of this data in the full stream.</p><h4>Example</h4><p>If client sends: “Hello World”<br>TCP might send:</p><ul><li>Packet 1 → Seq = 1000 → “Hello”</li><li>Packet 2 → Seq = 1005 → “ World”</li></ul><p>Why 1005? Because:</p><ul><li>“Hello” = 5 bytes</li><li>Next sequence = 1000 + 5</li></ul><p><strong>:: Why do we need Sequence Number?</strong></p><ol><li><strong>Maintain Order</strong><br>Packets can arrive out of order. Sequence number helps reassemble data correctly.</li><li><strong>Detect Missing Data<br></strong>If a sequence is missing receiver knows something is lost.</li><li><strong>Enable Retransmission<br></strong>Sender can resend only missing parts.</li></ol><blockquote>Without sequence numbers TCP cannot guarantee <strong>ordered and reliable delivery.</strong></blockquote><p><strong>:: How is Initial Sequence Number (ISN) selected?</strong></p><p>At the start of connection (during handshake):</p><ul><li>Client chooses a random number</li><li>Server also chooses a random number</li></ul><p>Then sequence increases by number of bytes sent.</p><h3>Acknowledgment Number</h3><p>The sender assigns sequence numbers to track what it sent. The receiver, in turn, uses acknowledgment numbers to tell the sender exactly what it received — and what to send next.</p><blockquote>I have received everything up to this number, now I expect this next.</blockquote><h4>Example:</h4><p>Client sends:</p><ul><li>Seq = 1000 → “Hello” (5 bytes)</li></ul><p>Server replies:</p><ul><li>ACK = 1005</li></ul><p><strong>Meaning:</strong> I received bytes up to 1004, now send from 1005.</p><h3>Data Offset</h3><p>With sequencing and acknowledgment handled, TCP now needs to structure the segment itself cleanly — starting with knowing where the header ends and the actual data begins.</p><p>It indicates the <strong>size of the TCP header.</strong></p><p><strong>:: Why is it needed?</strong></p><p>TCP header is <strong>variable size </strong>(it may include options). So receiver must know where the Header ends.</p><blockquote>Header ends here → Data starts here</blockquote><ul><li>It is a <strong>4-bit field</strong> in TCP header.</li><li>Value is measured in <strong>32-bit words (4 bytes)</strong></li></ul><blockquote><strong><em>Header Size = Data Offset × 4 bytes</em></strong></blockquote><p>Using this formula receiver knows the header size. That results everything after that = data.</p><h4>Min and Max Data Offset:</h4><p>Header structure:</p><blockquote><strong><em>Fixed Header (20 bytes) + [ Options + Padding ]</em></strong></blockquote><p>TCP header cannot be smaller than 20 bytes. Because<strong> </strong>mandatory fields take 20 bytes.<br>So, Minimum Data Offset = 5 (20÷4).</p><p>Data Offset field size is 4 bits. So, Maximum Data Offset = 15 (2⁴ — 1)<br>So, Maximum TCP header size = 15×4 = 60 bytes.</p><h3>Reserved</h3><p>They are unused bits in the TCP header and reserved for future features.</p><p><strong>:: If they are unused why they are part of the TCP header?</strong></p><p>When TCP was designed, engineers anticipated that the protocol might need new flags or control functions later. By reserving these bits, future protocols could use them without creating an entirely new, incompatible header format.</p><h3>Flag</h3><p>TCP defines nine control flags, each flag corresponds to a specific bit position, and different combinations (like SYN+ACK) are formed by setting multiple bits to 1. <br>Some are familiar — like <strong>SYN</strong>, <strong>ACK</strong>, and <strong>FIN</strong> — while others handle congestion control and urgent data flow.</p><p>Here’s what each flag does:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-sF5vD2313NVDeU_Glpqw.png" /><figcaption>TCP header flags</figcaption></figure><p>Example:</p><pre>| Flag      | Bit Pattern |<br>| --------- | ----------- |<br>| SYN       | 00000010    |<br>| ACK       | 00010000    |<br>| FIN       | 00000001    |<br>| SYN + ACK | 00010010    |<br>| FIN + ACK | 00010001    |</pre><h3>Window Size</h3><p>It means how much data the sender is allowed to send without waiting<strong> </strong>for acknowledgment.</p><p>Let’s say — Window size = 5000 bytes.</p><p>It means sender can:</p><ul><li>Send 5000 bytes continuously</li><li>Then must wait for ACK</li></ul><h3>Checksum</h3><p>This is the <strong>error detection mechanism</strong> of TCP.</p><p>During transmission bits can change or data may get corrupted.<br>Checksum helps to detect whether data is correct or corrupted.</p><p>If error found, packet is discarded and retransmission happens.</p><h3>Urgent Pointer</h3><p>It indicates the position of high-priority data in the stream.</p><p>It is used only when the URG flag = 1. It was designed for priority data transmission, but is <strong>rarely used today as modern applications handle such logic at the application layer.</strong></p><p><strong>:: Why was it used?</strong></p><p>It was designed for immediate processing of critical data.</p><p>Example use cases:</p><ul><li>Interrupt signals (like Ctrl+C in terminal)</li><li>Real-time commands in remote login (Telnet)</li></ul><p>So receiver can process urgent data before normal data.</p><h3>Options</h3><p>Used to extend functionality, such as MSS, window scaling, timestamps.</p><h4>MSS (Maximum Segment Size):</h4><p>MSS defines the maximum amount of data (payload) a TCP segment can carry (MSS only limits data size, not full packet size).</p><blockquote>Don’t send data bigger than this.</blockquote><p>During the TCP three-way handshake, the Maximum Segment Size (MSS) is mutually established as a minimum of the values announced by the client and server<strong> </strong>(<strong>min[</strong>client MSS, server MSS<strong>]</strong>)<strong>.</strong></p><h4>Window Scaling:</h4><p>Think of Window Scaling as a way to fix a “speed limit” problem that was built into the internet 40 years ago.</p><blockquote>Increase how much data can be in transit at once (using multiplication).</blockquote><p>When TCP was first designed, it used a <strong>16-bit number</strong> to tell the sender how much data it could handle at once.</p><ul><li>Mathematically, a 16-bit number can only go up to 65,535 bytes (about 64 KB).</li><li>In the 1980s, 64 KB was huge. But today, on a high-speed fiber connection, 64 KB is tiny.</li></ul><p>Imagine you are sending letters to a friend.</p><ul><li><strong>The Rule:</strong> <br>- You can only send 10 letters at a time. <br>- After sending some letters, your friend replies (ACK). <br>- Then you can send more.</li><li><strong>The Problem:</strong> If your friend lives next door, the wait is short. But if your friend lives in another country, it takes days for that postcard to arrive.</li><li><strong>The Result:</strong> Even if you have 1,000 letters ready to go, you are stuck sitting by the mailbox doing nothing. Your “speed” isn’t limited by how fast you write; it’s limited by how much you are allowed to send before you have to stop and wait.</li></ul><p><strong>Window Scaling</strong> fixes this by <strong>changing the rule (introducing a multiplier)</strong>. Instead of changing “send 10 letters,” it keeps the 10 but says: <em>Send 10 letters × </em><strong><em>2ⁿ</em></strong><em>.<br></em>So if n = 4, the real window size becomes 10 × 16 = 160 letters at once — far more data in transit, far less waiting.</p><p>This is how modern TCP handles high-speed connections without redesigning the entire protocol.</p><h4>Timestamps:</h4><p>Timestamps let TCP track time, so it can <strong>adapt to network delays and avoid confusion from</strong> <strong>old data</strong>.</p><p><strong>:: Why it is used?</strong></p><ul><li>Measure RTT (Round Trip Time)</li><li>Detect old or duplicate packets</li></ul><h3>Padding</h3><p>Extra bytes added to the TCP header to make its size a multiple of 4 bytes.</p><p><strong>TCP header must be a multiple of 4 bytes (32 bits). </strong>But ‘Options’ can be any length (not always multiple of 4). <br>Padding fills the “gap” with zeros so the header ends precisely on a 32-bit boundary,<strong> allowing the Data Offset field to correctly point to the start of the data.</strong></p><p><strong>Reference:</strong> Concepts and structure inspired by <a href="https://youtu.be/JhuIYWGrKm8">TCP In Details</a> .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=05fccecdfbd4" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/inside-tcp-the-protocol-that-keeps-the-internet-reliable-05fccecdfbd4">Inside TCP: The Protocol That Keeps the Internet Reliable</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OSI Model: The Conceptual Architecture of Network Communication]]></title>
            <link>https://levelup.gitconnected.com/osi-model-the-conceptual-architecture-of-network-communication-d37115e0c49c?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/d37115e0c49c</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Tue, 17 Mar 2026 04:54:51 GMT</pubDate>
            <atom:updated>2026-03-18T14:57:25.273Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>OSI</strong> stands for <strong>Open Systems Interconnection</strong>. It is a <strong>conceptual </strong>networking model that describes how data travels from one computer to another over a network.</p><p>The OSI model was developed by the <strong>International Organization for Standardization (ISO) </strong>in 1984.</p><h4>Why Was the OSI Model Created?</h4><p>Before the OSI model, different companies built their own networking systems, and they were not compatible with each other.</p><p>Examples of companies building their own networking technologies:</p><ul><li>IBM — SNA (Systems Network Architecture)</li><li>Digital Equipment Corporation — DECnet</li><li>Xerox — XNS</li></ul><p>Each system used <strong>different communication rules </strong>(protocols).</p><p>Because of this Networks from different vendors could not communicate easily.</p><p>So the ISO created the OSI model to:</p><ul><li>Standardize network communication</li><li>Make devices from different vendors work together</li><li>Simplify network design and troubleshooting</li></ul><p>In the OSI context — <br>- <strong>Open</strong>: The communication standards are public and vendor-independent<br>- <strong>System</strong>: Any device that can communicate over a network<strong><br></strong>- <strong>Interconnection: </strong>Connecting multiple systems (devices) so they can communicate and exchange data</p><blockquote>OSI is a framework / philosophy that explains how different systems connect and communicate with each other using open standards.</blockquote><p>A <strong>protocol</strong> is a set of rules that defines:</p><ul><li>How data is formatted</li><li>How it is transmitted</li><li>How errors are handled</li><li>How devices identify each other</li></ul><p>The OSI model organizes these rules into <strong>7 layers</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*omUmB6mwfzKPSYXbq4-1xg.png" /></figure><p>Imagine you type <strong>“Hello”</strong> in a chat application and send it to your friend.</p><p>The message travels through <strong>7 OSI layers</strong> on the sender side, then through the network, and then <strong>up the 7 layers</strong> on the receiver side.</p><p>This process is called <strong>Encapsulation → Transmission → Decapsulation</strong>.</p><h3>7 : Application Layer</h3><ul><li>You type ‘<strong>Hello’</strong> in a messaging application</li><li>The application layer prepares the data to send</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nkWrk-w-m4ls_KK1V0pNNQ.png" /></figure><h3>6 : Presentation Layer</h3><ul><li>This layer <strong>formats the data</strong> so the receiver can understand it</li><li>It may convert data format, encrypt the message, compress the data<br>(Formats may include JSON, XML, YAML, PNG, JPG, MP4…)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NP95bC5HAlNS2pBdB7_4Lg.png" /></figure><h3>5 : Session Layer</h3><ul><li>This layer <strong>creates and manages the communication session</strong> between the sender and receiver</li><li>Example actions: Start connection, Maintain connection, End connection</li></ul><h3>4 : Transport Layer</h3><ul><li>This layer ensures <strong>reliable data delivery</strong></li><li>Break data into <strong>segments</strong></li><li>Add <strong>port numbers</strong></li><li>Handle error checking</li></ul><p>Imagine sender phone has many apps (Telegram, WhatsApp, UpScrolled, …etc.) and the receiver’s phone also has many apps.</p><p>When the message <strong>“Hello”</strong> is sent, the network must know <strong>which application should receive it</strong>. This is handled by the Transport Layer (L4).</p><p>The Transport Layer uses <strong>Port Numbers</strong> to identify applications. When the sender sends the message, the Transport Layer adds:<strong> </strong>Source Port, Destination Port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LhwTMW8Toy8jOZ9Mx0yHcQ.png" /></figure><h3>3 : Network Layer</h3><ul><li>This layer handles <strong>logical addressing</strong> and routing</li><li>It adds <strong>IP addresses</strong> using Internet Protocol (IP)</li><li>Routers use this information to decide where the<strong> packet </strong>should go</li></ul><blockquote>The network layer is responsible for creating a connection between the source computer and the destination computer.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ZG2K6r87scCGvxnwbuQ2A.png" /></figure><p><strong>Internet Protocol (IP)</strong> is a protocol used to identify devices on a network and deliver data to the correct device.</p><p>Each device connected to a network is assigned a unique <strong>IP address</strong>. This address helps routers know where the data should be sent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d4XptZ8G3UzI79Ma1i_k4A.png" /></figure><p>Think of a device like a building.</p><ul><li>The <strong>IP address</strong> is like the <strong>building address</strong></li><li>It identifies which building should receive the package (data)</li></ul><p>This tells the delivery service <strong>which building to deliver to</strong>, but it does not specify which apartment inside the building.</p><p>In networking port number identifies the application (apartment).</p><h3>2 : Data Link Layer</h3><ul><li>This layer handles physical device addressing using <strong>MAC addresses</strong></li><li>It creates a <strong>Frame</strong></li><li>Error detection also happens here</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7jIHsBYaHTg_nbikgLwEZg.png" /></figure><h3>1 : Physical Layer</h3><ul><li>The frame is converted into electrical signals, light signals, or radio waves</li><li>These signals travel through Ethernet cable, Fiber optic cable, WiFi, …</li><li>The signals pass through switches, routers, network cables until they reach the destination computer</li></ul><blockquote>The physical layer is responsible for carrying individual bits in a frame across the link.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JjJjmoCEb1FTIiAWoKn8Cw.png" /></figure><h4><strong>At a glance:</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yWh-zBl21FuMvkN_baKaNQ.png" /></figure><p>When the data reaches the destination computer, the process of <strong>decapsulation</strong> begins.<br>In this process, each layer <strong>removes the information (headers) that was added during encapsulation</strong> and passes the remaining data to the upper layer.</p><h3>1 : Physical Layer</h3><ul><li>At the receiver, the Physical Layer receives the signals from the network medium</li><li>The physical layer <strong>converts these signals back into binary data (bits)</strong></li><li>These bits are passed to the <strong>Data Link Layer</strong></li></ul><h3>2 : Data Link Layer</h3><ul><li>The Data Link Layer receives the bits and reconstructs them into a <strong>Frame</strong></li><li>The layer performs: <br>- <strong>MAC address verification</strong> (checks if the frame is for this device)<br>- <strong>Error detection</strong></li><li>After verification:<br>- The <strong>Data Link header and trailer are removed<br>- </strong>The remaining <strong>IP packet</strong> is passed to the <strong>Network Layer</strong></li></ul><h3>3 : Network Layer</h3><ul><li>Processes the <strong>IP packet</strong></li><li>The layer checks whether the <strong>destination IP address matches the current device</strong></li><li>After processing:<br>- The <strong>IP header is removed<br>- </strong>The remaining <strong>transport segment</strong> is passed to the <strong>Transport Layer</strong></li></ul><h3>4 : Transport Layer</h3><ul><li>The Transport Layer receives the <strong>segment</strong>.</li><li>The destination port number identifies <strong>which application should receive the data</strong>.</li><li>The transport layer performs segment reassembly (if the message was split), error checking, flow control</li><li>After processing:<br>- The <strong>Transport header (port information) is removed<br>- </strong>The remaining <strong>data</strong> is passed to the <strong>Session Layer</strong></li></ul><h3>5 : Session Layer</h3><ul><li>Manages the communication session between the two devices<br>- Maintaining the communication session<br>- Closing the session after communication is complete</li></ul><h3>6 : Presentation Layer</h3><ul><li>Transforms the data into a format the application can understand</li></ul><h3>7 : Application Layer</h3><ul><li>Delivers the final data to the user application</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*nkWrk-w-m4ls_KK1V0pNNQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d37115e0c49c" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/osi-model-the-conceptual-architecture-of-network-communication-d37115e0c49c">OSI Model: The Conceptual Architecture of Network Communication</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Containers: From Dependency Conflicts to Isolated Worlds]]></title>
            <link>https://levelup.gitconnected.com/containers-from-dependency-conflicts-to-isolated-worlds-bed7830a2d99?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/bed7830a2d99</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Tue, 20 Jan 2026 10:03:05 GMT</pubDate>
            <atom:updated>2026-01-21T02:56:05.356Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*im6lWHGTozs1790GHxt9TQ.png" /></figure><h3>Challenges Without Containers</h3><p>Before containers existed, deploying applications looked like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*03bFQ3GGxNgWI9XX0yXM9A.png" /></figure><p>Each application depends on:</p><ul><li>A runtime (e.g., Java, Node.js, Python)</li><li>Frameworks and libraries</li><li>OS-level configurations</li></ul><h4>Problem 1: Multiple applications on the same OS</h4><p>In real systems, we rarely run just one application. Instead:</p><ul><li>Multiple applications run on one operating system</li><li>All apps share the same OS and system libraries</li></ul><p>This creates a fragile environment.</p><h4>Problem 2: Dependency and version conflicts</h4><p>Different applications often require:</p><ul><li>Different versions of the <strong>same runtime or library</strong></li><li>Different system-level dependencies</li></ul><p>Example:</p><ul><li>App A needs <strong>Python 3.8</strong></li><li>App B needs <strong>Python 3.11</strong></li></ul><p>Because both apps run on the same OS:</p><ul><li>Installing or upgrading dependencies for one app can <strong>break another</strong></li></ul><h4>Problem 3: Tight coupling with the OS</h4><p>Applications become:</p><ul><li>Closely tied to a specific OS setup</li><li>Hard to move between environments (development → test → production)</li><li>Hard to reproduce reliably</li></ul><h3>Virtual Machines</h3><p>To solve these problems, <a href="https://medium.com/stackademic/understanding-virtual-machine-ef68ba494de0">Virtual Machines (VMs)</a> were introduced.</p><h4><strong>How virtual machines work:</strong></h4><ul><li>Multiple virtual machines share the same physical hardware (CPU, memory, disk, network)</li><li>A <strong>hypervisor</strong> sits on top of the hardware</li><li>The hypervisor creates and manages multiple virtual machines, each with its own OS, libraries, and applications</li></ul><p>Each VM has:</p><ul><li>Its <strong>own operating system</strong></li><li>Its own libraries and dependencies</li><li>Strong <strong>isolation</strong> from other VMs</li></ul><p>So instead of: “<em>Multiple apps sharing one OS”<br></em>We get: “<em>Multiple OSs running on the same hardware”</em></p><h4>Benefits of virtual machines:</h4><ul><li>No dependency conflicts</li><li>Strong isolation</li><li>Applications can run different OS versions safely</li></ul><h4>Limitations of virtual machines:</h4><p>Despite solving many problems, VMs introduced new ones:</p><ul><li><strong>Heavy</strong>: each VM includes a full OS</li><li><strong>Slow to provision</strong>: booting an OS takes time</li><li><strong>High resource usage</strong>: more CPU, memory, and storage</li><li><strong>Poor portability</strong>: large images, slower to move between systems</li></ul><p>VMs improved reliability but sacrificed speed and efficiency.</p><h3>Why Containers?</h3><p>Virtual machines virtualize <strong>hardware </strong>but containers virtualize the <strong>operating system</strong>.</p><p>This is the fundamental difference.</p><p>Because containers virtualize the OS instead of the hardware:</p><ul><li>They do not spin up a full operating system every time</li><li>They provide <strong>isolation without OS duplication</strong></li><li>They are <strong>very thin</strong></li><li>They are <strong>very fast to provision</strong></li></ul><p>At the same time, containers enforce a <strong>highly prescriptive way</strong> of building applications and are <strong>highly portable</strong>.</p><h4>What does a container need to function?</h4><p>To make containers work safely and predictably, the operating system must provide several capabilities.</p><p><strong>1: Isolation (Process &amp; Visibility Isolation)</strong></p><p>Each container:</p><ul><li>Must not see other containers</li><li>Must not see host processes</li><li>Must not see anything outside its own bubble</li></ul><p>Its view of the system should be <strong>strictly limited</strong> to what belongs to that container and nothing else.</p><p>This isolation is the <strong>first requirement</strong>.</p><p><strong>2: Resource Control (Noisy Neighbor Problem)</strong></p><p>All containers share the same machine, so resources are limited.</p><p>Without control:</p><ul><li>One container could consume too much CPU or memory</li><li>Other containers would suffer (the <em>noisy neighbor</em> problem)</li></ul><p>So the system must allow:</p><ul><li>Limiting how much CPU, memory, and other resources a container can use</li><li>Metering usage</li><li>Accounting — seeing how much a container is consuming</li></ul><p>This ensures fairness and stability.</p><p><strong>3: Different Views of the Operating System</strong></p><p>The operating system contains:</p><ul><li>Files</li><li>Runtimes</li><li>Frameworks</li><li>System configuration</li></ul><p>But earlier we saw the problem:</p><ul><li>Different applications require different runtimes and frameworks</li><li>Sharing the same OS view causes conflicts</li></ul><p>So each container needs:</p><ul><li>Its <strong>own filesystem view</strong></li><li>Its <strong>own perception</strong> of what exists on the system</li></ul><p>This allows containers to remain:</p><ul><li>Prescriptive (only what the app needs is included)</li><li>Conflict-free</li></ul><p><strong>4: Portability</strong></p><p>A container should be:</p><ul><li>Self-contained</li><li>Movable as-is</li></ul><p>We should be able to:</p><ul><li>Take a container</li><li>Preserve all of its views (filesystem, processes, limits)</li><li>Run it <strong>anywhere else</strong> without modification</li></ul><p>This portability is a <strong>key goal.</strong></p><h4>Why Linux made containers possible</h4><p>These requirements already existed in Linux. Linux provides native primitives (Namespaces, cgroups,…) for:</p><ul><li><strong>Isolation</strong></li><li><strong>Resource control</strong></li><li><strong>Modifying system views</strong></li></ul><p>These capabilities were already present in the OS.</p><p>Containers did not invent them — they <strong>combined and used them together</strong>. That is why containers <strong>started on Linux</strong>.</p><h3>Namespace</h3><p>A namespace is a Linux kernel feature that gives a process a <strong>restricted view of system resources</strong>.</p><blockquote>Namespaces <strong>isolate </strong>what a process can see, such as processes, filesystems, network interfaces, and host identifiers.</blockquote><p>Different namespaces can see different things, even though they run on the <strong>same kernel</strong>.</p><h4>The Initial Namespace:</h4><p>When we boot our computer:</p><ul><li>The Linux kernel starts</li><li>The <strong>initial namespace</strong> (also called the <em>root namespace</em>) is created</li><li>Every normal process starts here by default</li></ul><p>This initial namespace can see — all processes, filesystems, network interfaces, users, host resources.</p><p>Think of it as the <strong>main world.</strong></p><ul><li>Installed files belong to the filesystem visible in the initial (root) namespace.</li><li>When you run the application the process runs inside the initial namespace. It can see all other process, entire file system, host network. There is <strong>no isolation</strong>.</li></ul><h4>Parallel Worlds</h4><p>Think of namespaces like <strong>parallel universes</strong>:</p><ul><li>Same kernel</li><li>Different visible objects</li></ul><p>A process:</p><ul><li>Always lives <strong>inside exactly one namespace of each type</strong></li><li>Cannot see outside its namespace unless explicitly allowed</li></ul><h4>What happens if we create a new namespace?</h4><p>When we create a new namespace and run a process inside it:</p><ul><li>The process sees a <strong>different view</strong></li><li>The kernel is still the same</li><li>Other namespaces still exist</li></ul><h4>Can it see the initial namespace?</h4><p><strong>No</strong>, by default:</p><ul><li>A process inside a namespace <strong>cannot see outside</strong></li><li>Processes in the initial namespace can see resources from child namespaces because they have access to the full system view by default.</li></ul><h3>cgroups: Resource Control</h3><p>Linux already had a way to <strong>limit and measure resource usage</strong>. That mechanism is called <strong>cgroups</strong>.</p><blockquote>A cgroup is a Linux kernel feature that controls <strong>how much resources a process or group of processes can use.</strong></blockquote><p><strong>namespaces :</strong> <em>“What can this process see?”</em><strong><em> </em>(</strong>define visibility)<strong><em><br></em>cgroups:</strong> <em>“How much can this process use?”</em><strong><em> </em>(</strong>define limits)</p><p><strong>Purpose:<br>- </strong>namespaces: Isolation<br>- cgroups: Resource control</p><h4>Role of cgroups in isolation</h4><p>Using cgroups, we can limit:</p><ul><li><strong>CPU</strong> (how much processing time)</li><li><strong>Memory (RAM)</strong> (maximum usable memory)</li><li><strong>Disk I/O</strong> (read/write speed)</li><li><strong>Network usage</strong> (in some setups)</li></ul><h4>Creating “Different Worlds” with Namespaces &amp; cgroups</h4><p>Using:</p><ul><li><strong>Namespaces</strong> → separate views of the system</li><li><strong>cgroups</strong> → separate resource limits</li></ul><p>We can create <strong>multiple isolated worlds</strong> on the same machine.</p><p>Each world:</p><ul><li>Thinks it owns the system</li><li>Cannot see outside itself</li><li>Cannot exceed assigned resources</li></ul><p>If we need different version of same application (ex: Go 1.22 and Go 1.26) in one computer what role is played by namespaces and cgroups?</p><ul><li>Applications (Go 1.22, Go 1.26) <strong>exist on the same physical disk</strong></li><li><strong>namespaces</strong> give each container a <strong>different filesystem view</strong></li><li>Each container sees <strong>only its own version</strong></li><li>They are unaware of each other</li><li><strong>cgroups are not involved in filesystem isolation</strong></li><li>Filesystem isolation is done by <strong>namespaces</strong></li><li>cgroups only limit how much disk I/O they can use</li></ul><p>If Container A is limited to 512MB RAM and tries to allocate 1GB, the kernel will prevent it, protecting other containers.</p><h3>What is a Container?</h3><p>A container is a process running with <strong>restricted visibility</strong> (namespaces), <strong>restricted resources</strong> (cgroups). <br>Together, they create a <strong>self-contained world</strong>.</p><p><strong>VMs:</strong> One app → One OS → Shared hardware<br><strong>Containers:</strong> Multiple apps → One shared OS → Shared hardware</p><p>Containers share the host kernel. A kernel vulnerability could affect all containers.</p><blockquote>A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.</blockquote><h4><strong>Container as a “World”:</strong></h4><p>People inside believe:</p><ul><li>Nothing exists outside</li><li>All resources belong to them</li><li>They cannot access anything outside</li><li>Outsiders cannot see inside</li></ul><p>This <strong>illusion </strong>is created by:</p><ul><li><strong>Namespaces</strong> → hide the outside</li><li><strong>cgroups</strong> → limit the inside</li></ul><p>So from inside, the container <strong>feels like a full machine</strong>.</p><h3>What Is an Image?</h3><p>A container image is an <strong>immutable, read-only artifact</strong> that contains everything required to run an application.</p><blockquote>A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.</blockquote><p>It is a <strong>static specification</strong>, not a running process.</p><ul><li>An <strong>image</strong> is a blueprint</li><li>A <strong>container</strong> is a running instance created from that blueprint</li></ul><p>A container image includes:</p><ul><li>The exact filesystem layout</li><li>Exact versions of runtimes and libraries</li><li>Defined startup behavior</li></ul><p>Because of this:</p><ul><li>The same image can run on different machines</li><li>The behavior remains consistent across environments</li><li>Portability does not depend on the host’s user-space configuration</li></ul><p>Only the <strong>host kernel</strong> is shared.</p><p>Container images become containers at runtime and in the case of Docker containers — images become containers when they run on Docker Engine.</p><p>Images are built in layers:</p><ol><li>Base layer: OS files</li><li>Middle layers: Dependencies, libraries</li><li>Top layer: Application code</li></ol><p>Layers are reusable and cacheable, making images efficient.</p><h4>Summary:</h4><ul><li><strong>Problem:</strong> Dependency conflicts, tight OS coupling</li><li><strong>First solution:</strong> VMs (too heavy)</li><li><strong>Better solution:</strong> Containers (lightweight isolation)</li><li><strong>Made possible by: </strong>Linux namespaces + cgroups</li><li><strong>Packaged as:</strong> Immutable images</li><li><strong>Run as:</strong> Isolated processes</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bed7830a2d99" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/containers-from-dependency-conflicts-to-isolated-worlds-bed7830a2d99">Containers: From Dependency Conflicts to Isolated Worlds</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Virtual Machine : Isolating Operating Systems and Hardware]]></title>
            <link>https://blog.stackademic.com/understanding-virtual-machine-ef68ba494de0?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef68ba494de0</guid>
            <category><![CDATA[virtual-machine]]></category>
            <category><![CDATA[virtualization]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Wed, 17 Dec 2025 17:20:30 GMT</pubDate>
            <atom:updated>2025-12-29T05:15:29.005Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding Virtual Machine</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S4piEW1ujlYh6OHIF8hq_Q.png" /></figure><h3><strong>Virtual Machine (VM)</strong></h3><p>A Virtual Machine (VM) is a software-based computer that runs inside a real (physical) computer and behaves like a separate machine with its own OS, CPU, memory, and storage.</p><p><strong>VM is like a computer inside another computer</strong>.</p><p>Suppose:</p><ul><li>Your laptop runs <strong>Windows</strong></li><li>You install <strong>VirtualBox</strong></li><li>Inside VirtualBox, you run <strong>Ubuntu Linux</strong></li></ul><p>Now:</p><ul><li>Windows = <strong>real (host) machine</strong></li><li>Ubuntu = <strong>virtual machine</strong></li></ul><p>Ubuntu thinks it’s running on its own computer, but it’s actually using your laptop’s hardware.</p><h4><strong>Virtualization</strong></h4><p>Virtualization is the process of using <strong>software to create an abstraction layer</strong> between hardware and operating systems.</p><p>For VMs, that abstraction layer, or that abstraction software is called hypervisor.</p><h3><strong>Hypervisor</strong></h3><p>Hypervisor is software that allows a <strong>virtual machine to behave like a physical computer</strong>.</p><ul><li>Underneath the hypervisor, there is real hardware (CPU, RAM, disk).</li><li>The hypervisor <strong>manages and distributes these resources</strong> among multiple virtual machines.</li><li>A single physical computer can run <strong>many VMs at the same time</strong>.</li></ul><p>Each VM runs its own operating system and it has its own virtual hardware. Like — virtual CPU, virtual RAM, virtual storage, etc.</p><p>The hypervisor is responsible for <strong>creating, running, managing, and controlling</strong> all virtual machines.</p><h4><strong>Types of Hypervisor</strong></h4><p>1. Bare-Metal Hypervisor</p><ul><li>Runs <strong>directly on the physical hardware</strong></li><li>No host operating system in between</li><li>Commonly used in <strong>servers and data centers</strong></li></ul><p>2. Hosted Hypervisor</p><ul><li>Runs <strong>on top of an existing operating system</strong></li><li>Examples: VirtualBox, VMware Workstation</li><li>Commonly used on <strong>personal computers</strong></li></ul><h3>Virtual Hardware</h3><p>Virtual hardware means emulated hardware components provided to a virtual machine. These components are presented to the <strong>guest operating system</strong> as if they were real hardware.<br>Thy hypervisor is responsible for managing and allocating these virtual resources to each VM.</p><h3><strong>Guest </strong>Operating System</h3><p>A Guest OS is the operating system that runs <strong>inside a virtual machine</strong>.</p><ul><li>Each VM can have its <strong>own guest OS</strong></li><li>The guest OS can be <strong>different from the host OS</strong></li><li>Different VMs on the same machine can run <strong>different operating systems</strong></li></ul><p>This allows users to run multiple operating systems and applications on a single physical computer.</p><h3>Uses of Virtual Machines</h3><ol><li><strong>Multiple Operating Systems</strong><br>Run different operating systems on the same hardware.</li><li><strong>Isolation</strong><br>Each VM runs its own kernel and OS, keeping applications isolated from each other.</li><li><strong>Legacy Applications</strong><br>Run old or unsupported software that requires an older operating system.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef68ba494de0" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/understanding-virtual-machine-ef68ba494de0">Virtual Machine : Isolating Operating Systems and Hardware</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding HTTP : A Foundational Introduction]]></title>
            <link>https://blog.stackademic.com/understanding-http-59be17d92a55?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/59be17d92a55</guid>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[http-request]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Wed, 03 Dec 2025 17:40:26 GMT</pubDate>
            <atom:updated>2025-12-04T06:33:24.786Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding HTTP: A Foundational Introduction</h3><p>HTTP (Hypertext Transfer Protocol) is the medium through which browsers communicate with servers to send or receive data. <br>While there are many other <a href="https://www.cloudflare.com/learning/network-layer/what-is-a-protocol/">protocols</a> for client-server communication, HTTP is the most widely used for web interactions.</p><p>Two core principles define HTTP:<br>1. Statelessness<br>2. Client-Server model</p><h3>Statelessness</h3><p>Each request from a client to a server is treated as an independent transaction that is unrelated to any previous request.</p><h4>Key Concepts:</h4><ul><li><strong>Stateless Protocol:</strong> Each HTTP request is self-contained; the server does not retain information about previous requests.</li><li><strong>Session Management:</strong> Techniques such as cookies or tokens are used to maintain user sessions without server-side state.</li><li><strong>Request and Response:</strong> Each HTTP request includes all necessary information for the server to fulfill it, including headers and body.</li></ul><h4>Benefits:</h4><ul><li><strong>Scalability:</strong> Servers can handle requests more efficiently without needing to store session information.</li><li><strong>Simplicity:</strong> Developers can focus on request/response logic without worrying about session state.</li><li><strong>Fault Tolerance:</strong> Stateless servers are easier to recover since they do not rely on stored data.</li></ul><h4>How Statelessness Works:</h4><ul><li>Client sends HTTP request to server.</li><li>Server processes the request and returns a response.</li><li>No session data is stored on the server.</li><li>If the client needs to maintain state (e.g., login), it does so via tokens or cookies.</li></ul><h3>Client-Server model</h3><p>In a typical HTTP request flow, <strong>communication is always initiated by the client</strong>, usually a web browser or application. The server hosts resources (web pages, APIs, files) and waits for incoming requests.</p><p>The client is responsible for providing all information needed by the server such as URL, headers, HTTP method(GET, POST, PUT, DELETE, etc.), and everything.</p><p>When the server receives a request it processes it and sends back the appropriate response such as web page or data or error message or a JSON file or a text file or any other kind of content.</p><blockquote>HTTPS can be considered HTTP with encryption (via TLS/SSL), providing security features like encrypted communication. The underlying request-response principles remain the same as HTTP.</blockquote><p>For communication to happen, HTTP relies on <strong>TCP</strong>, a reliable transport protocol.</p><ul><li>HTTP is an <strong>application-layer protocol</strong>; it needs a transport mechanism.</li><li>It uses TCP (Transmission Control Protocol) because — TCP is <strong>connection-oriented</strong> and <strong>reliable</strong>. Ensuring messages arrive intact and in order.</li><li>While HTTP doesn’t require a connection-oriented protocol, it does require <strong>reliability</strong>, which TCP provides better than UDP(connectionless and unreliable).</li></ul><h3>Evolution of HTTP</h3><p><strong>HTTP/1.0</strong><br>In HTTP/1.0, each request opened a new connection. This caused inefficiencies because a TCP connection had to be established and closed for every single request–response cycle, which slowed down performance.</p><p><strong>HTTP/1.1</strong><br>HTTP/1.1 introduced persistent connections, allowing multiple requests and responses to be sent over the same TCP connection. This significantly improved performance. It also added features like chunked transfer encoding and better caching mechanisms.</p><p><strong>HTTP/2.0</strong><br>HTTP/2 introduced multiplexing, allowing multiple requests and responses to be sent simultaneously over a single TCP connection. It uses a binary framing layer instead of plain text and includes header compression with HPACK. It also supports server push, allowing servers to send resources before the client requests them.</p><p><strong>HTTP/3.0</strong><br>HTTP/3 is built on the QUIC protocol, a transport-layer protocol running over UDP. Using UDP allows faster connection establishment, reduced latency, and better handling of packet loss. HTTP/3 also continues to support multiplexing without the head-of-line blocking issue that still affects HTTP/2 over TCP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AaDGOOij3Puy4EAD3QDbbw.png" /></figure><blockquote>Client and Servers established some kind of network connection, and messages are sent and received.</blockquote><h3>HTTP messages</h3><p>Request message is the one that is sent by the client.<br>Response message is the one that is received by the client from the server.</p><p>This how HTTP Request and Response looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*0xRg5sgMFIP_Yi84VvgS3Q.png" /><figcaption>Just an Example</figcaption></figure><p>Both requests and responses share a similar structure:</p><ol><li>A <em>start-line</em> is a single line that describes the HTTP version along with the request method or the outcome of the request.</li><li>An optional set of <em>HTTP headers</em> containing metadata that describes the message. For example, a request for a resource might include the allowed formats of that resource, while the response might include headers to indicate the actual format returned.</li><li>An empty line indicating the metadata of the message is complete.</li><li>An optional <em>body</em> containing data associated with the message. This might be POST data to send to the server in a request, or some resource returned to the client in a response. Whether a message contains a body or not is determined by the start-line and HTTP headers.</li></ol><p>The start-line and headers of the HTTP message are collectively known as the <em>head</em> of the requests, and the part afterwards that contains its content is known as the <em>body</em>.</p><h3>HTTP Headers</h3><p>HTTP headers are key-value pairs sent between a browser and a server during every web request and response. They help both sides understand how to process the request or deliver the response.</p><p>Headers carry details such as the type of content being sent, how long it should be cached, what language is preferred, or whether the user is authorized to access the content. This additional context makes web communication more reliable, secure, and efficient.</p><p>HTTP headers control how browsers and servers interact. They handle essential tasks like identifying content type, managing authentication, setting cache policies, controlling redirects, and enforcing cross-origin rules. They also let developers customize how APIs respond in different scenarios.</p><h4>Why are HTTP Headers Important in Web Development</h4><p>Headers are critical in shaping how websites function, communicate, and protect user data. They help developers control behavior, enforce standards, and improve performance.</p><ul><li><strong>Content Identification:</strong> Headers like Content-Type define what kind of data is being transferred. This helps the browser understand whether it should render HTML, parse JSON, or download a file.</li><li><strong>User Authentication:</strong> The Authorization header is used to send credentials or tokens that allow users to access secure parts of an application without needing to log in repeatedly.</li><li><strong>Caching and Load Management:</strong> Headers such as Cache-Control or ETag help browsers store and reuse content when possible. This reduces server load and improves page load times for repeat visitors.</li><li><strong>Access Control Across Domains:</strong> Cross-Origin Resource Sharing (CORS) headers specify which external websites are allowed to access your resources. This is essential for APIs and public content.</li><li><strong>Language and Encoding Preferences:</strong> Headers like Accept-Language or Content-Encoding let developers tailor the response based on user preferences or connection types, such as serving compressed content to save bandwidth.</li><li><strong>Request Tracing and Logging:</strong> Headers such as User-Agent or Referer provide information about where the request is coming from and what browser or device is being used. This helps in debugging, analytics, and personalization.</li></ul><h4>References:</h4><ol><li><a href="https://www.swiftorial.com/swiftlessons/http-protocols/http-fundamentals/statelessness-in-http">Statelessness in HTTP</a></li><li><a href="https://youtu.be/a3C1DMswClQ">Understanding HTTP for backend engineers, where it all starts</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Messages">HTTP messages</a></li><li><a href="https://www.browserstack.com/guide/http-headers">HTTP Headers That Every Web Developer Should Know</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59be17d92a55" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/understanding-http-59be17d92a55">Understanding HTTP : A Foundational Introduction</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REST API : The Architecture Behind the Web]]></title>
            <link>https://blog.stackademic.com/rest-api-the-architecture-behind-the-web-8c609b1e1e37?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c609b1e1e37</guid>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Mon, 24 Nov 2025 09:19:26 GMT</pubDate>
            <atom:updated>2025-12-16T05:55:51.669Z</atom:updated>
            <content:encoded><![CDATA[<h3>REST API : The Architecture Behind the Web</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XVfJKMP2AdUXKMjkJHbINA.png" /></figure><h3>The History of REST</h3><p>In the mid-1990s, the World Wide Web was facing a <strong>scalability crisis</strong>. As more people started using the internet, the existing architecture struggled to handle the growing traffic efficiently.</p><p><strong>Roy Fielding</strong>, a computer scientist, was working on solving this problem. He collaborated with <strong>Tim Berners-Lee</strong> (the inventor of the World Wide Web) and others to improve the web’s scalability and standardize its design.</p><p>Together, they worked on creating a new version of the <a href="https://blog.stackademic.com/understanding-http-59be17d92a55">HTTP</a> protocol. Their efforts led to the development of <strong>HTTP/1.1</strong> — the first major standardized version of HTTP, which was officially published in <strong>1999</strong> (RFC 2616).</p><p>After successfully addressing the web’s scalability challenges, Roy Fielding wanted to formally document and describe the architectural principles that made the web scalable and successful.</p><p>In <strong>2000</strong>, Fielding completed his PhD dissertation titled <strong>“Architectural Styles and the Design of Network-based Software Architectures”</strong> at the University of California, Irvine.</p><p>In this dissertation, he <strong>named and described</strong> the web’s architectural style as <strong>REST (Representational State Transfer)</strong>. While these principles already existed in the web’s architecture, Fielding was the first to formally identify, name, and document them as a cohesive architectural style.</p><p>Today, REST is the foundation of most modern web APIs, and Fielding’s work remains the authoritative source on RESTful architecture.</p><h3>REST</h3><p>REST stands for Representational State Transfer — an architectural style or blueprint for designing networked applications.</p><p>It is a set of architectural constraints, not a protocol or a standard that define how systems should communicate over the web.</p><p>Like the other architectural styles, REST also has its guiding principles and constraints. These principles must be satisfied if a service interface is to be referred to as RESTful.</p><p><strong>Breaking Down REST:</strong></p><h4>:: Resource</h4><p>A resource is any entity or concept that can be named, identified and manipulated over the web.<br>For example — If the web was a library, each book is a resource.<br>It can be:<br>- A data object (user profile, blog post, product,…etc.)<br>- A concept (weather, location, currency rate,…etc.)</p><h4>:: State</h4><p>The state of a resource is its current condition at a specific moment.<br>Think of a blog post’s title, content, and author as its state.</p><h4>:: Representation</h4><p>A representation is the format in which a resource’s state is expressed.<br>Common formats: JSON, XML, HTML.</p><p>If the state is a User object, its representation could be:</p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;Mojammel&quot;,<br>  &quot;email&quot;: &quot;mojammel@example.com&quot;<br>}</pre><h4>How REST works:</h4><ul><li>Frontend requests a resource from the Backend via an API endpoint.</li><li>The backend sends the current state of that resource in a representation format (for example: JSON).</li><li>The frontend displays or processes the data.</li></ul><h4>Why “Representational State Transfer”?</h4><ul><li>Resource current condition = State</li><li>State in a specific format = Representational State</li><li>When the representational state is transferred from server to the client = Representational State Transfer (REST)</li></ul><h4>Terminology Breakdown:</h4><ul><li><strong>REST</strong> → The principles/rules (philosophy, blueprint, architectural style).</li><li><strong>RESTful</strong> → Any system/service that follows REST principles.</li><li><strong>RESTful Web Service</strong> → A web service that follows REST rules.</li><li><strong>REST API</strong> → An API that implements RESTful web services.</li></ul><h3>API</h3><p>An API is a set of definitions and protocols for building and integrating application software. It’s sometimes referred to as a contract between an information provider and an information user — establishing the content required from the consumer (the call) and the content required by the producer (the response).</p><p>You can think of an API as a mediator between the users or clients and the resources or web services they want to get. It’s also a way for an organization to share resources and information while maintaining security, control, and authentication — determining who gets access to what.</p><p><strong>Real-life analogy</strong>: Lamp + Socket + Electricity</p><ul><li>The socket is like an API — it’s the interface/connection point between the lamp (frontend) and electricity supply (backend).</li><li>Without the socket (API), the lamp can’t access electricity — just as a frontend can’t communicate with backend data without an API.</li></ul><h3>REST API</h3><p>A REST API is an application programming interface (API) that follows the design principles of the REST architectural style. REST is short for representational state transfer, and is a set of rules and guidelines about how you should build a web API.</p><h4>The Six Guiding Principles of REST:</h4><ol><li><strong>Client-Server </strong>— Separation of Concerns<br>The client and server are separate entities that interact through a well-defined interface which is API.<br>↪ The client manages the user interface and user experience.<br>↪ The server handles data storage and business logic.<br>This separation allows them to evolve independently and scale better.</li><li><strong>Stateless </strong>— No Client Context Stored on Server<strong><br></strong>↪ The server does <strong>not remember previous requests</strong>.<br>↪ Each client request must include everything necessary to process it.<br>These improving reliability, scalability (any server instance can handle any request), and visibility.</li><li><strong>Cacheable</strong> — Improve Performance and Reliability<strong> </strong><br>↪ Server responses must be explicitly labeled as cacheable or not.<br>↪ Caching improves performance by reducing server load, improving network efficiency, and providing faster response times to clients.</li><li><strong>Uniform Interface</strong> — Simplifies Architecture<br>A uniform interface means all resources follow the same predictable pattern for interaction. This standardization makes the API intuitive and easy to use.<br>Four key sub-constraints: <br><strong>i. Resource Identification<br></strong>↪ Every resource has a unique URI (Uniform Resource Identifier), usually a URL, which acts as a consistent address for the resource.<br>Example: /api/users/123 identifies user with ID 123.<br><br><strong>ii. Resource Manipulation through Representations<br></strong>↪ The client sends or receives resource data in standard formats like JSON or XML.<br>↪ The representation contains enough info to modify or delete the resource.<br><br><strong>iii. Self-Descriptive Messages<br></strong>↪ Each message (request or response) contains enough information to describe how to process it, using standard HTTP methods (GET, POST, PUT, DELETE) and metadata like HTTP headers.<br><br><strong>iv. HATEOAS (Hypermedia as the Engine of Application State)<br></strong>↪ The API tells you what actions you can do next by including links in its responses. <br>- Hypermedia = Links and actions (like hyperlinks on a webpage)<br>- Engine = Drives/controls what happens<br>- Application State = What the client can do next<br><br><strong>Real-Life Analogy: </strong>Website Navigation<strong><br></strong>When you visit a website, you don’t need to memorize all possible URLs. The website shows you clickable links based on what you can do:<br><em>“View Profile”, “Edit Settings”, “Logout”,…</em><br>for example(json):<br>{<br>&quot;id&quot;:1,<br>&quot;name&quot;:&quot;Mojammel&quot;,<br>&quot;links&quot;:{<br> &quot;self&quot;:&quot;/api/users/1&quot;,<br>&quot;posts&quot;:&quot;/api/users/1/posts&quot;<br> }<br>}<em><br></em>HATEOAS works the same way for APIs — the server guides the client by providing available actions.</li><li><strong>Layered System</strong> — Promotes Scalability and Security<br>↪ The architecture is composed of hierarchical layers.<br>↪ Each layer can only interact with the immediate layer below it.<br>This improves scalability and security, allowing the use of intermediaries like load balancers and proxies.</li><li><strong>Code on Demand</strong>(optional) — Extend Client Functionality<br>↪ Servers can extend client functionality by sending executable code (such as JavaScript) that runs in the client’s browser. <br>↪ This is optional and allows flexibility for client-side enhancements.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*1PUFHqgSb-mgXNgJm430vw.png" /></figure><h4>Resources:</h4><ol><li><a href="https://www.redhat.com/en/topics/api/what-is-a-rest-api">https://www.redhat.com/en/topics/api/what-is-a-rest-api</a></li><li><a href="https://restfulapi.net/">https://restfulapi.net/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c609b1e1e37" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/rest-api-the-architecture-behind-the-web-8c609b1e1e37">REST API : The Architecture Behind the Web</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go Concurrency: How Goroutines and Channels Work]]></title>
            <link>https://levelup.gitconnected.com/go-concurrency-how-goroutines-and-channels-work-4ee2c5a2e045?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ee2c5a2e045</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[threads]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Tue, 07 Oct 2025 18:18:20 GMT</pubDate>
            <atom:updated>2025-12-16T15:28:57.392Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wa7Evgke5Eft6STbjxZKJA.png" /></figure><p>Imagine you’re in a kitchen preparing a meal. You could chop vegetables, boil water, and grill meat one after another — waiting for each task to finish before starting the next. That’s sequential execution.</p><p>But what if you had helpers? One chops, another boils, another grills — all at the same time. That’s concurrency: multiple tasks progressing independently, often overlapping in time.</p><p>In Go, these helpers are called goroutines. They allow us to run functions independently and concurrently with just a single keyword: go.</p><h3>Concurrency</h3><p>Concurrency means structuring your program to handle multiple tasks at once. It doesn’t necessarily mean parallelism (running on multiple cores), but it does mean your program can do more without waiting.</p><p>Go’s concurrency model is inspired by <strong>CSP (Communicating Sequential Processes)</strong>, where independent routines communicate via channels. Goroutines are Go’s lightweight threads — cheap to create, fast to switch, and managed by Go’s runtime scheduler.</p><blockquote>Don’t communicate by sharing memory; share memory by communicating.</blockquote><h4><strong>Concurrency Across Execution Layers</strong></h4><p>Concurrency works at three distinct layers, each managing its own units of execution:</p><ul><li><strong>Hardware Layer (CPU Cores):</strong> CPU cores execute instructions in true parallel. A 4-core processor runs 4 instructions simultaneously, one per core.</li><li><strong>Operating System Layer (Threads):</strong> The OS scheduler manages threads — typically far more threads than available cores. It rapidly switches between threads, giving each a turn on the CPU to ensure fair execution time.</li><li><strong>Go Runtime Layer (Goroutines):</strong> Go’s scheduler runs goroutines on several OS threads. The number of threads that can run Go code at the same time is controlled by GOMAXPROCS, which usually matches the number of CPU cores. It can create extra threads for blocking operations, while efficiently multiplexing thousands of lightweight goroutines across these threads.</li></ul><p>These layers work hierarchically. The Go runtime schedules goroutines onto threads, but the OS ultimately controls when those threads run on physical CPU cores. This layered approach allows Go to handle massive concurrency with lightweight goroutines while the OS manages the heavier threading concerns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POGolfVI-iiwTVIacnVJ7Q.png" /></figure><h4>Key Points of Concurrency:</h4><ul><li>Group code (and data) by identifying independent task</li><li>Avoid race conditions</li><li>Avoid deadlocks</li><li>More workers = faster execution</li></ul><h4>Communication Sequential Processes (CSP)</h4><p>Introduced by Tony Hoare in 1978. The core principles are:</p><ul><li>Each process is built for sequential execution.</li><li>Data is communicated between processes via channels.<br>No shared state!</li><li>Scale by adding more of the same.</li></ul><h4>Go’s concurrency toolset:</h4><ul><li>Goroutines</li><li>Channels</li><li>Select statement</li><li>sync package</li></ul><h3>Goroutines</h3><p>A goroutine is a lightweight, user-space thread managed entirely by the Go runtime.</p><h4>Key Characteristics:</h4><ul><li><strong>Started and managed by Go runtime:</strong> Goroutines are like user-space threads because the Go runtime manages them — we don’t directly interface with the operating system. This also means goroutines are hardware independent.</li><li><strong>Small memory footprint:</strong> Goroutines start with just 2KB of memory, but have growable stacks that allow them to grow and shrink according to need. <br>When more memory is needed, the runtime grows the stack by allocating a larger segment, copying existing data, and continuing execution seamlessly.<br>This allocate-and-copy strategy makes stack shrinking very efficient — once our Goroutine releases the resources that is no longer needs those are just freed back onto the heap for the system to use as necessary.</li><li><strong>Efficient context-switching:</strong> The Go runtime scheduler maximizes efficiency through seamless context switching. The scheduler ensures we’re using all system resources optimally, keeping OS threads actively executing goroutines and scheduling blocked goroutines on and off as needed.</li><li><strong>Native inter-goroutine communication:</strong> Goroutines communicate through channels in a way that’s memory safe and works seamlessly with the scheduler, allowing goroutines to block and unblock on channel sends and receives.</li></ul><h3>Go scheduler</h3><p>We know our system has fixed resources, but we can spawn far more goroutines than available cores. A key part of maintaining efficiency and performance in Go programs is the Go scheduler.</p><h4>Three Key Components:</h4><ul><li><strong>G (Goroutine):</strong> The code to execute</li><li><strong>M (OS thread):</strong> Where to execute it</li><li><strong>P (Logical processor):</strong> Gives us the rights and resources to execute that code on the OS thread</li></ul><h4>Goroutine States:</h4><ul><li><strong>Blocked</strong> — Waiting for something (I/O, channel, system call)</li><li><strong>Runnable</strong> — Ready to execute, waiting for CPU time</li><li><strong>Running</strong> — Currently executing on a thread</li></ul><h4><strong>The Scheduler’s Job</strong></h4><p>Running goroutines as efficiently as possible.</p><p>The number of processors created by the runtime equals the number of logical cores on our machine which we can see in the Go environment variable GOMAXPROCS.</p><h4>How it works:</h4><ul><li>Goroutines wake idle processors</li><li>The processor creates an OS thread for running goroutines</li><li>The scheduler schedules the goroutine onto the thread and begins execution</li></ul><h4>What Do We Need Processor (P) ?</h4><p>It might seem like we’re just mapping goroutines onto threads with no real need for this middle layer. But processors serve a crucial purpose: they keep track of what goroutines need to run when there’s more work than available resources.</p><p>Processors maintain <strong>local run queues</strong> of goroutines that haven’t yet been executed. When one goroutine finishes and a processor becomes available, it looks in its local run queue for the next goroutine to run and schedules it onto the OS thread.</p><ul><li>In order to run goroutines (G), a thread (M) must hold a context (P)</li><li>Processors hold the run queues that tell threads what to execute next</li></ul><h4>Blocked Goroutines</h4><p>The scheduler should maximize execution time on OS threads. One of the scheduler’s most important jobs is moving blocked goroutines OFF operating system thread resources and bringing them back on once they’re ready.</p><p><strong>Goroutines can block on:</strong></p><ul><li>System calls (file I/O)</li><li>Network calls</li><li>Channel operations</li></ul><p>When a goroutine blocks, the scheduler detaches it from the thread so other goroutines can run, preventing waste of valuable OS thread time.</p><h4>Getting Started:</h4><p>Let’s begin with a basic example — no concurrency, just plain function calls:</p><pre>func JustPrint(n int) {<br>   fmt.Printf(&quot;%d &quot;,n)<br>}<br><br>func main() {<br>   for i:= 0; i&lt;10; i++ {<br>    JustPrint(i)<br>   }<br>}/* Output:<br> 0 1 2 3 4 5 6 7 8 9  */</pre><p>What happens here?<br>- Each task(i) runs one after another.<br>- The output is predictable and ordered.</p><p>This is simple and safe — but slow if each task takes time (e.g., network calls, file I/O).</p><p><strong>Adding Concurrency with Goroutines:</strong></p><pre>func JustPrint(n int) {<br>   fmt.Printf(&quot;%d &quot;,n)<br>}<br>func main() {<br>   for i:= 0; i&lt;10; i++ {<br>    //just added &#39;go&#39; keyword before the function call<br>      go JustPrint(i)<br>   }<br>}</pre><p><strong>We don’t see anything in output. Why?</strong></p><h4>What happens:</h4><ul><li>go JustPrint(i) starts goroutines.</li><li>But main() itself is also a goroutine.</li><li>As soon as the for loop ends, main() finishes.</li><li>When main() exits, the whole program terminates — even if other goroutines haven’t run yet.</li></ul><p>That’s why we see nothing printed: the scheduler doesn’t even get time to schedule our goroutines before the program exits.</p><p><strong>How can we fix it? <br></strong>To let goroutines complete, we need to wait for them.</p><h4>Using time.Sleep:</h4><pre>func JustPrint(n int) {<br>   fmt.Printf(&quot;%d &quot;,n)<br>}<br>func main() {<br>   for i:= 0; i&lt;10; i++ {<br>      go JustPrint(i)<br>   }<br>   time.Sleep(1*time.Second)<br>}</pre><p>The elements are printed, but running the program multiple times may produce a different sequence.</p><p><strong>Why this is unreliable?<br></strong>This approach is unreliable because it requires specifying an arbitrary sleep duration without knowing the exact time needed for all goroutines to finish. If the duration is too short, some goroutines may not complete, causing missing output.</p><h4>The Proper Way: sync.WaitGroup</h4><pre>import (<br>   &quot;fmt&quot;<br>   &quot;sync&quot;<br>)<br><br>func JustPrint(n int, wg *sync.WaitGroup) {<br>   defer wg.Done()<br>   fmt.Printf(&quot;%d &quot;,n)<br>}<br>func main() {<br>   var wg sync.WaitGroup<br>   <br>   for i:= 0; i&lt;10; i++ {<br>      wg.Add(1)<br>      go JustPrint(i, &amp;wg)<br>   }<br>   wg.Wait()<br>}</pre><h4>Understanding WaitGroup Methods</h4><ul><li>Add(n) – Declares how many goroutines we’re waiting for. Increases the counter by n. We usually call Add(1) to tell the WaitGroup: <em>“One more goroutine is about to start.”</em></li><li>Done() – Signals that a goroutine has finished. It decrements the counter by 1 that was increased by Add.</li><li>Wait() – Block until all goroutines are done. Execution continues only when the counter reaches zero.</li></ul><p>Normally, we call Add(1) for each goroutine we start. But technically, we can pass any integer to Add(n) . The counter starts at 0. What if we use Add(3) :</p><ul><li>Each loop increases the counter by 3, and you loop 10 times → total counter = 30.</li><li>But you only launch 10 goroutines, and each calls Done() once → total 10 decrements.</li><li>After all goroutines finish, counter = 20.</li><li>Since the counter never reaches 0,wg.Wait() blocks forever.</li><li>Result: program deadlocks.</li></ul><p>If we know how many goroutines are:</p><pre>func JustPrint(s string, wg *sync.WaitGroup) {<br>   defer wg.Done()<br>   fmt.Println(s)<br>}<br>func main() {<br>   var wg sync.WaitGroup<br>   <br>   wg.Add(2) // expecting 2 goroutines<br>   go JustPrint(&quot;Learning&quot;, &amp;wg)<br>   go JustPrint(&quot;Golang&quot;, &amp;wg)<br>   <br>   wg.Wait()<br>}  /* Output:<br>    Golang<br>    Learning  */</pre><p>Because goroutines run concurrently, the order is not guaranteed. Sometimes &quot;Learning&quot; may run first, sometimes &quot;Golang&quot; . The Go scheduler decides which goroutine gets CPU time first.</p><h3>Channels</h3><p>A channel in Go works like a pipe: we send data on one end and receive it on the other.<br>The main idea: when multiple goroutines are running concurrently, they often need to share information. Channels provide a safe way for one goroutine to send data to another goroutine.</p><p><strong>In short:</strong> Channels are the built-in mechanism in Go for communication between goroutines.</p><h4>Channel Properties:</h4><ul><li><strong>Goroutine-safe:</strong> Multiple goroutines can safely use the same channel</li><li><strong>Store and pass values:</strong> Between goroutines</li><li><strong>FIFO semantics:</strong> First In, First Out ordering</li><li><strong>Can block/unblock goroutines:</strong> Enables synchronization</li></ul><p>It has three components: sender, buffer (optional), receiver.</p><p>Let’s create a channel:</p><pre>func main() {<br>   ch := make(chan string)<br>   ch &lt;- &quot;how are you?&quot; // send <br>   <br>   msg := &lt;- ch // receive<br>   fmt.Println(msg)<br>} /* Output:<br>  ❌fatal error: all goroutines are asleep - deadlock!  */</pre><h4>Why the deadlock?</h4><ul><li>By default, channels are unbuffered.</li><li>An unbuffered channel send (ch &lt;-) will block until another goroutine is ready to receive.</li><li>But in our code, both send and receive are in the same goroutine (main).</li><li>So the main goroutine gets stuck at ch &lt;- &quot;how are you?&quot; forever, waiting for a receiver that isn’t running yet → deadlock.</li></ul><h4>How to fix it?</h4><ul><li><strong>Option 1: </strong>Use a goroutine</li></ul><pre>func main() {<br>    ch := make(chan string) // unbuffered(?) channel<br><br>    // Run send in a separate goroutine<br>    go func() {<br>        ch &lt;- &quot;how are you?&quot; // waits until main receives<br>    }() <br>    /* the anonymous goroutine does the send<br>    while main does the receive */<br><br>    msg := &lt;-ch // receive<br><br>    fmt.Println(msg)<br>} //Output: how are you?</pre><ul><li><strong>Option 2: </strong>Use a buffered channel</li></ul><pre>func main() {<br>    ch := make(chan string, 1) // buffer size 1<br><br>    ch &lt;- &quot;how are you?&quot;  /* won’t block, buffer has space<br>    it stores the value until receive. */<br><br>    // ch &lt;- &quot;fine&quot; // ❌ blocks — buffer is full<br>    msg := &lt;-ch<br><br>    fmt.Println(msg)<br>} //Output: how are you?</pre><p>But what does buffered / unbuffered channel means?</p><p><strong>Unbuffered Channel:</strong></p><ul><li>An unbuffered channel has <strong>no storage capacity</strong>.</li><li>This means every send (ch &lt;- value) waits until another goroutine is ready to receive (&lt;-ch), and every receive waits until there’s a sender.</li><li>Because of this property, unbuffered channels synchronize goroutines.</li></ul><p><strong>Buffered Channel:</strong></p><ul><li>A buffered channel has <strong>a fixed capacity</strong> that we set when creating it.</li><li>We can send values into it without an immediate receiver, as long as there’s space left in the buffer.</li><li>When the buffer is full, sends block until someone receives.</li></ul><p><strong>Why Blocking Matters?</strong></p><ul><li><strong>Blocking breaks concurrency:</strong> Goroutines waiting can’t do other work</li><li><strong>Blocking can lead to deadlocks:</strong> All goroutines waiting for each other</li><li><strong>Blocking can prevent scaling:</strong> Can’t add more workers if they all wait</li></ul><h4>Why No Need for Wait or Sleep with Channels?</h4><p><strong>Blocking Behavior Is Built-In:</strong><br>- Sending on a channel blocks until another goroutine is ready to receive.<br>- Receiving from a channel blocks until there’s data to receive.</p><p>In previous examples, we use ‘Bidirectional Channels’ that can both send and receive data.</p><pre>ch := make(chan int) // can send and receive</pre><p>But we can also have <strong>Send-Only Channel</strong> (can only send values) or <strong>Receive-Only Channel </strong>(can only receive values).</p><pre>// &#39;chan&lt;- string&#39; means send-only channel of string.<br>func sendOnly(ch chan&lt;- string) {<br>    ch &lt;- &quot;Hello&quot;<br>    // msg := &lt;-ch // ❌ Compile error<br>}<br><br>// &#39;&lt;-chan string&#39; means receive-only channel of string.<br>func receiveOnly(ch &lt;-chan string) {<br>    msg := &lt;-ch<br>    fmt.Println(msg)<br>    // ch &lt;- &quot;Hi&quot; // ❌ Compile error<br>}<br><br>func main() {<br>    ch := make(chan string, 1)<br>    <br>    sendOnly(ch)<br>    receiveOnly(ch)<br>}<br>// Output: Hello</pre><h4>Closing Channels</h4><p>Closing a channel signals to the receiver: <em>“Sender is done sending values — no more data will come through this channel”.</em></p><p>Closing is needed when using a loop that expects continuous input (like range), not because the sender’s loop ended.</p><ul><li>Receivers wait for values until the channel is closed. Without closing, they keep waiting → deadlock, expecting more data.</li><li>Only the sender closes, receivers should not close because they don’t know whether more sends are pending.</li><li>Sending on a closed channel causes a panic.</li><li>Closing a channel is a control signal, not a way to free resources.</li></ul><pre>func main() {<br>    ch := make(chan int)<br>    <br>    go func() {<br>        for i := 0; i &lt; 5; i++ {<br>            ch &lt;- i<br>        }<br>        close(ch) // signal: no more values coming<br>    }()<br>    <br>    // receive until channel is closed<br>    for val := range ch {<br>        fmt.Println(val)<br>    }<br>}</pre><p><strong>When Closing Is Not Needed</strong></p><pre>func main() {<br>    ch := make(chan int)<br><br>    go func() {<br>        for i := 1; i &lt;= 3; i++ {<br>            ch &lt;- i<br>        }<br>        //close(ch) // working without closing channel<br>    }()<br><br>    for i := 1; i &lt;= 3; i++ {<br>        fmt.Println(&lt;-ch)<br>    }<br>    fmt.Println(&quot;All data received&quot;)<br>}</pre><p>This program works without closing because we know exactly how many values are sent (3) and we receive exactly that many. We’re manually counting, so we don’t need the close signal.</p><p>So, Closing is optional, channels don’t need to be closed unless receivers need to know when to stop.</p><p>Manual counting works but is not a scalable pattern — it’s written here to clear up confusion.</p><h4>Handling Multiple Channels: Select</h4><p>Go provides the select statement, which works like a switch but for channels. It waits until at least one case can proceed and executes whichever is ready first. If no case is ready and there’s no default, it blocks.</p><pre>func main() {<br>    ch1 := make(chan string)<br>    ch2 := make(chan string)<br>    <br>    go func() {<br>        time.Sleep(100 * time.Millisecond)<br>        ch1 &lt;- &quot;from channel 1&quot;<br>    }()<br>    <br>    go func() {<br>        time.Sleep(50 * time.Millisecond)<br>        ch2 &lt;- &quot;from channel 2&quot;<br>    }()<br>    <br>    for i := 0; i &lt; 2; i++ {<br>        select {<br>        case msg1 := &lt;-ch1:<br>            fmt.Println(msg1)<br>        case msg2 := &lt;-ch2:<br>            fmt.Println(msg2)<br>        }<br>    }<br>}<br>/* Output:<br>from channel 2<br>from channel 1 */</pre><h4>Making Select Non-Blocking</h4><ul><li>We can add a default case to make select non-blocking.</li></ul><pre>default:<br>      fmt.Println(&quot;No data available right now&quot;)</pre><ul><li><strong>Timeout Pattern:</strong> We can wait for a specific time using time.After.</li></ul><pre>case &lt;-time.After(1 * time.Second):<br>// If no data arrives in 1 second, this case runs<br>fmt.Println(&quot;Timeout! Moving on...&quot;)</pre><p>These tricks are useful, but they’re not the true power of select.</p><p>Because select can do more and this becomes apparent if we see our channels as <strong>streams of data</strong>. With this perspective, select is about routing data . So we can define different routing patterns: <strong>Fan-out</strong>, <strong>Fan-in (Funnel),.. </strong>etc. This is where select shines — managing concurrent data flows.</p><h4>Where Channels Fail</h4><ul><li>We can create deadlocks with channels</li><li>Channels pass around copies, which can impact performance</li><li>Passing pointers through channels can create race conditions</li><li>What about ‘naturally shared’ structures like caches or registers?</li></ul><p>This is where the sync package (mutexes) becomes necessary.</p><h3>Race Conditions</h3><p>A race condition happens when two or more goroutines access the same shared resource at the same time, without proper synchronization.</p><p>Because goroutines run concurrently, the final result depends on timing, which makes the behavior unpredictable and unsafe.</p><pre>var count int<br><br>func add(wg *sync.WaitGroup) {<br>   defer wg.Done()<br><br> // Race condition here:<br> // Multiple goroutines can read and write to &#39;count&#39; at the same time.<br> /* For example:<br>    Goroutine A reads count = 5<br>    Goroutine B reads count = 5 (before A updates it)<br>    A increments and writes count = 6<br>    B increments and writes count = 6 (overwriting A’s update!) <br>    But count should be 7<br> */<br>   count += 1;<br>}<br><br>func main() {<br>   var wg sync.WaitGroup<br>   <br>   for i := 0; i &lt; 1000; i++ {<br>      wg.Add(1)<br>      go add(&amp;wg);<br>   }<br>   wg.Wait();<br><br>   fmt.Println(&quot;Total count:&quot;,count)<br>  } // Output: Total count: 943 (not 1000! and varies each run)</pre><p>Run your program with the race detector: go run -race main.go</p><h4>Mutex</h4><p>A mutex (mutual exclusion) ensures only one goroutine accesses shared data at a time.</p><p><strong>How it works:</strong></p><ul><li>mu.Lock() - Acquire the lock. If another goroutine holds it, wait.</li><li>mu.Unlock() - Release the lock so other goroutines can access.</li><li>Only one goroutine can hold the lock at a time.</li></ul><pre>var count int       <br>var mu sync.Mutex   // mutex to protect shared variable<br><br>func add(wg *sync.WaitGroup) {<br>   defer wg.Done()<br>  <br>   mu.Lock()       <br>   count += 1      // safe update<br>   mu.Unlock()     <br>}<br><br>func main() {<br>   var wg sync.WaitGroup<br>  <br>   for i := 0; i &lt; 1000; i++ {<br>      wg.Add(1)<br>      go add(&amp;wg)<br>   }<br>  <br>   wg.Wait()<br>   fmt.Println(&quot;Total count:&quot;, count) // Always prints 1000<br>}</pre><ul><li><strong>Without Mutex</strong>: Multiple goroutines read/write count at the same time → updates get lost → race condition.</li><li><strong>With Mutex</strong>: Only one goroutine at a time can hold the lock → no overwrites → result is correct.</li></ul><h4>Mutexes are not an optimal solution</h4><ul><li>Mutexes are like toilets. <br>The longer you occupy them, the longer the queue gets</li><li>Read/write mutexes can only reduce the problem (allow multiple readers)</li><li>Using multiple mutexes will cause deadlocks sooner or later</li><li>All-in-all not always the optimal solution</li></ul><h3>Three Shades of Code</h3><ul><li><strong>Blocking:</strong> Our program may get locked up for undefined time</li><li><strong>Lock free:</strong> At least one part of our program is always making progress</li><li><strong>Wait free:</strong> All parts of our program are always making progress (very hard to achieve, requires significant expertise)</li></ul><p>Going down this list does NOT automatically equal faster performance. The right choice depends on your specific use case.</p><h4>How to Write Lock-Free Code?</h4><p>In Go, we primarily write lock-free code using the sync/atomic package. The key to lock-free programming is using <strong>atomic CPU instructions</strong> to ensure that memory operations on simple data types are completed as a single, indivisible step.</p><h3>Channels vs Mutexes</h3><h4>Channels:</h4><p><strong>Purpose:</strong> Communication between goroutines — pass data, signals, or tasks.<br><strong>Philosophy:</strong> <em>“Share memory by communicating.”</em></p><p><strong>Use Channels when:</strong></p><ul><li>Passing ownership of data between goroutines</li><li>Distributing work to multiple workers (pipelines, worker pools)</li><li>Communicating results or coordination signals</li><li>You want natural synchronization without manual locks</li></ul><h4>Mutexes:</h4><p><strong>Purpose:</strong> Protect shared state — synchronize access to variables/data.<br><strong>Philosophy:</strong> <em>“Communicate by sharing memory.”</em></p><p><strong>Use Mutexes when:</strong></p><ul><li>Protecting shared state (counters, caches, maps)</li><li>Guarding short critical sections</li><li>Performance is critical (mutexes are faster than channels in low-level shared state cases)</li><li>You just need to prevent race conditions, not pass ownership of data</li></ul><h3>Concurrency in Practice</h3><h4>Best Practices:</h4><ul><li><strong>Avoid blocking</strong> whenever possible</li><li><strong>Avoid race conditions</strong> through proper synchronization</li><li><strong>Use channels</strong> to avoid shared state</li><li><strong>Use select</strong> to manage multiple channels</li></ul><p>When channels aren’t the right tool:</p><ul><li>Try to use tools from the <strong>sync package</strong> first (Mutex, RWMutex, Once, WaitGroup)</li><li>In simple cases or when really needed: try <strong>lock-free code</strong> (atomic operations)</li></ul><h4>References:</h4><ol><li><a href="https://youtu.be/rDRa23k70CU">Golang UK Conference 2017 | Arne Claus — Concurrency Patterns in Go</a></li><li><a href="https://youtu.be/S-MaTH8WpOM">Goroutines: Under the Hood | Vicki Niu | Go Systems Conf SF 2020</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ee2c5a2e045" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/go-concurrency-how-goroutines-and-channels-work-4ee2c5a2e045">Go Concurrency: How Goroutines and Channels Work</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Breaking Down JWT: How Base64, SHA and HMAC Work Together]]></title>
            <link>https://levelup.gitconnected.com/breaking-down-jwt-how-base64-sha-and-hmac-work-together-8e239b7aa4a7?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e239b7aa4a7</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Fri, 19 Sep 2025 06:21:00 GMT</pubDate>
            <atom:updated>2026-01-12T05:54:47.615Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GCk2kZJ61lkN80qUyfKiJA.png" /></figure><p>JWT (JSON Web Token) is a popular way to securely transmit user data between systems.</p><p>To understand how JWTs work, it’s important to first learn about a few key concepts: Base64 encoding, SHA (Secure Hash Algorithm), and HMAC (Hash-based Message Authentication Code). These building blocks help explain how JWTs are constructed, signed, and verified.</p><h4>Base64 encoding:</h4><p>Base64 is a way to encode binary data (like images, files, or arbitrary bytes) into a string of printable ASCII characters. It uses a set of 64 different symbols for this representation:</p><pre>A–Z → 26 chars<br>a–z → 26 chars<br>0–9 → 10 chars<br>+ and / → 2 chars</pre><p>The process generally involves converting bytes into a Base64 string, which consists of these printable ASCII characters.</p><p>First, let’s look at converting a string to a byte array:</p><pre>func main() {<br>   var s string<br>   s = &quot;a&quot;<br>   <br>   byteArray := []byte(s)<br>   fmt.Println(s) // a<br>   fmt.Println(byteArray) // [97]<br>}</pre><p>Now, we convert a byte array to a Base64 URL-encoded string:</p><pre>package main<br><br>import (<br>   &quot;fmt&quot;<br>   &quot;encoding/base64&quot;<br>)<br><br>func main() {<br>   var s string<br>   s = &quot;mhb&quot;<br>  <br>   // Convert string to byte array<br>   byteArray := []byte(s)<br>   fmt.Println(s) // mhb<br>   fmt.Println(byteArray) // [109 104 98]<br>   <br>   // Choose Base64 URL encoding<br>   enc := base64.URLEncoding<br><br>   // Specify no padding for the encoding<br>   enc = enc.WithPadding(base64.NoPadding)<br><br>   // Convert byte array to Base64 URL-encoded string<br>   b64Str := enc.EncodeToString(byteArray)<br>   <br>   fmt.Println(b64Str) // bWhi<br>}</pre><p>To decode the Base64 URL-encoded string back to the byte array:</p><pre>// Decode the Base64 URL-encoded string back to byte array<br>decodedStr, err := enc.DecodeString(b64Str)<br>if err != nil {<br>    fmt.Println(&quot;Decoding error:&quot;, err)<br>    return<br>}<br><br>// Print the decoded byte array and its string representation<br>fmt.Println(decodedStr) // [109 104 98]<br>fmt.Println(string(decodedStr)) // mhb</pre><h4>SHA (Secure Hash Algorithm)</h4><p>A one-way function that takes an input and produces a fixed-size hash output. Examples:</p><ul><li>SHA-1 → 160-bit output</li><li>SHA-256 → 256-bit output</li><li>SHA-512 → 512-bit output</li></ul><p>SHA converts input into a fixed-size hash output. Unlike Base64, where decoding can recover the original data, SHA hashes cannot be reversed to obtain the original input. This property is crucial for data integrity and security.</p><p>Example of SHA-256 hashing:</p><pre>package main<br><br>import (<br>   &quot;crypto/sha256&quot;<br>   &quot;fmt&quot;<br>)<br><br>func main() {<br>   data := []byte(&quot;Hello&quot;)<br>   hash := sha256.Sum256(data)<br>   fmt.Println(&quot;hash after sha-256&quot;, hash)<br>} /* Output:<br>hash after sha-256 [24 95 141 179 34 113 254 37 245 97 166 252 147 139 46 38 67 6 236 48 78 218 81 128 7 209 118 72 38 56 25 105]  */</pre><p>After changing the input from ‘Hello’ to ‘Hell’:</p><pre>func main() {<br>   data := []byte(&quot;Hell&quot;)<br>   hash := sha256.Sum256(data)<br>   fmt.Println(&quot;hash after sha-256&quot;, hash)<br>} /* Output:<br>hash after sha-256 [141 88 111 139 96 223 26 215 80 43 82 252 80 210 224 182 208 217 181 100 56 117 192 84 216 33 223 216 230 103 43 237] */</pre><p>Even a small change in input produces a completely different hash. This property ensures high security as the original input cannot be derived from the hash.</p><h4>HMAC (Hash-based Message Authentication Code)</h4><p>A way to make a hash more secure by mixing in a secret key. It generates a hash using both the text and a secret key.</p><ul><li>Without a secret key, SHA always produces the same hash for a given input.</li><li>With HMAC, the same text can produce different hashes using different secret keys.</li></ul><h4>HMAC-SHA-256</h4><p>This uses the HMAC technique combining the secret key with the input text, then hashes it using SHA-256.</p><p>Here’s an example of HMAC-SHA-256:</p><pre>package main<br><br>import (<br> &quot;crypto/hmac&quot;<br> &quot;crypto/sha256&quot;<br> &quot;fmt&quot;<br>)<br><br>func main() {<br> secret := []byte(&quot;my-secret&quot;) // Try changing this key<br> message := []byte(&quot;Hello world&quot;)<br><br> h := hmac.New(sha256.New, secret)<br> h.Write(message)<br><br> text := h.Sum(nil)<br> fmt.Println(text)<br>} /* Output:<br>[228 239 228 197 125 9 56 117 213 75 234 4 184 249 13 78 137 92 52 238 219 98 231 99 23 115 212 169 45 201 34 76] */</pre><h3>JSON Web Tokens (JWT)</h3><p>A JWT consists of three parts: header, payload (or claims), and signature, joined with dots.</p><p>The structure is Header.Payload.Signature . You can explore this structure interactively at <a href="https://www.jwt.io/">jwt.io</a>.</p><p>Each part is Base64 URL encoded and concatenated by dots.</p><ul><li><strong>Header</strong> <br>The header typically contains metadata about the token itself, specifically the type of token (JWT) and the cryptographic algorithm used for signing. For example:</li></ul><pre>{<br>  &quot;alg&quot;: &quot;HS256&quot;,<br>  &quot;typ&quot;: &quot;JWT&quot;<br>}</pre><ul><li><strong>Payload</strong> <br>Contains user data, expiry, etc. This is the information we want to transmit to the front-end. An example payload might look like this:</li></ul><pre>{<br>  &quot;sub&quot;: &quot;1234567890&quot;,<br>  &quot;name&quot;: &quot;Mojammel Haque Bhuiyan&quot;,<br>  &quot;admin&quot;: true,<br>}</pre><ul><li><strong>Signature</strong><br>Used to verify authenticity. Computed as:</li></ul><pre>signature = HMAC_SHA256(header + &quot;.&quot; + payload, secretKey)</pre><p>JWT is constructed as:</p><pre>base64(header) + &quot;.&quot; + base64(payload) + &quot;.&quot; + base64(signature)</pre><h4>Creating JWT using Go:</h4><p>You can access the code on the online IDE at this link: <a href="https://replit.com/@Mojammel-HaqueH/JWTusingGo">JWT_using_Go</a></p><pre>// util/create_jwt.go<br>package util<br><br>import (<br>   &quot;crypto/hmac&quot;<br>   &quot;crypto/sha256&quot;<br>   &quot;encoding/base64&quot;<br>   &quot;encoding/json&quot;<br>)<br>// JWT header structure<br>type Header struct {<br>   Alg string `json:&quot;alg&quot;`<br>   Typ string `json:&quot;typ&quot;`<br>}<br>// Payload contains the claims or data you want to encode in the JWT<br>type Payload struct {<br>   Sub         string `json:&quot;sub&quot;`<br>   FirstName   string `json:&quot;first_name&quot;`<br>   LastName    string `json:&quot;last_name&quot;`<br>   Email       string `json:&quot;email&quot;`<br>   IsShopOwner bool   `json:&quot;is_shop_owner&quot;`<br>}<br><br>func CreateJwt(secret string, data Payload) (string, error) {<br>   header := Header{<br>      Alg: &quot;HS256&quot;,<br>      Typ: &quot;JWT&quot;,<br>   }<br>  <br>   // Convert the header struct to a JSON byte array<br>   byteArrHeader, err := json.Marshal(header)<br>  <br>   if err != nil {<br>      return &quot;&quot;, err<br>   }<br>   // Encode the header to Base64Url format<br>   headerB64 := base64UrlEncode(byteArrHeader)<br>  <br>   // Convert the payload struct to a JSON byte array<br>   byteArrData, err := json.Marshal(data)<br>    if err != nil {<br>       return &quot;&quot;, err<br>   }<br>   // Encode the payload to Base64Url format<br>   payloadB64 := base64UrlEncode(byteArrData)<br>  <br>   message := headerB64 + &quot;.&quot; + payloadB64<br>  <br>   // Convert the secret and message to byte array<br>   byteArrSecret := []byte(secret)<br>   byteArrMessage := []byte(message)<br>  <br>   // Create a new HMAC-SHA256 hasher using the secret key<br>   h := hmac.New(sha256.New, byteArrSecret)<br>   h.Write(byteArrMessage)<br>  <br>   // Compute the HMAC signature<br>   signature := h.Sum(nil)<br>   // Encode the signature to Base64Url format<br>   signatureB64 := base64UrlEncode(signature)<br>  <br>   jwt := headerB64 + &quot;.&quot; + payloadB64 + &quot;.&quot; + signatureB64<br>  <br>   return jwt, nil<br>}<br>// Encode a byte array using Base64Url encoding without padding<br>func base64UrlEncode(data []byte) string {<br>   return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(data)<br>}</pre><p>main.go:</p><pre>// main.go<br>package main<br><br>import (<br>   &quot;JWT_using_Go/util&quot;<br>   &quot;fmt&quot;<br>)<br><br>func main(){<br>   jwt, err := util.CreateJwt(&quot;my-secret&quot;, util.Payload {<br>      Sub:         &quot;55&quot;,<br>      FirstName:   &quot;Mojammel Haque&quot;,<br>      LastName:    &quot;Bhuiyan&quot;,<br>      Email:       &quot;mhbhuiyan10023@gmail.com&quot;,<br>      IsShopOwner: true,<br>   })<br>   if err != nil {<br>      fmt.Println(err)<br>      return<br>   }<br>  <br>   fmt.Println(jwt)<br>} </pre><p>From this program, the JWT is composed of three parts: header, payload/claim, and signature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*k2Ca3dFvOG8nnpUEdSYm6w.png" /></figure><p>When placed into the <a href="https://www.jwt.io/">JWT Decoder</a>, the signature may initially show “Invalid Signature” because the secret key used for decoding does not match the secret key used for signing (“my-secret”). However, the header and payload are decoded correctly. Only someone with the correct secret key can verify the JWT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uF7Kw_v9G3MbEnAcYygFKg.png" /></figure><p>With the correct secret key, the signature shows as “Valid Secret”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zr9lCLfFtuI5Oiey-DMSEQ.png" /></figure><p>Any modification to the header or payload invalidates the JWT, as the signature will no longer match. This one-way hashing mechanism ensures security by verifying the token’s authenticity. You can try changing values and observe different outputs.</p><h4>Authenticate JWT:</h4><p>Once a JWT is created, the server must validate it on every request to confirm the user’s identity and access rights.</p><p>JWT verification follows these steps:</p><ol><li>Extract the token from the Authorization header</li><li>Split it into header, payload, and signature</li><li>Recompute the HMAC using the secret key</li><li>Compare the computed signature with the received signature</li><li>Allow or reject the request accordingly</li></ol><p>JWT Authentication Middleware using Go:</p><pre>package middleware<br><br>import (<br> &quot;crypto/hmac&quot;<br> &quot;crypto/sha256&quot;<br> &quot;encoding/base64&quot;<br> &quot;net/http&quot;<br> &quot;strings&quot;<br>)<br>/*<br>1. parse jwt<br>2. parse header and payload or claims<br>3. hmac-sha-256 algorithm -&gt; hash hmac(header, payload, secret key)<br>4. parse signature part from the jwt<br>5. if the signature and hash is same -&gt; forward to create post<br>6. otherwise 401 status code with Unauthorized<br>*/<br><br>func (m *Middlewares) AuthenticateJWT(next http.Handler) http.Handler {<br>   return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {<br>      header := r.Header.Get(&quot;Authorization&quot;)<br>      if header == &quot;&quot; {<br>       http.Error(w, &quot;Unauthorized&quot;, http.StatusUnauthorized)<br>       return<br>      }<br>    <br>      // get token by splitting bearer and token<br>      headerArr := strings.Split(header, &quot; &quot;)<br>      if len(headerArr) != 2 {<br>       http.Error(w, &quot;Unauthorized&quot;, http.StatusUnauthorized)<br>       return<br>      }<br>      accessToken := headerArr[1]<br>    <br>      // split the JWT token into three parts<br>      tokenParts := strings.Split(accessToken, &quot;.&quot;)<br>      if len(tokenParts) != 3 {<br>       http.Error(w, &quot;Unauthorized&quot;, http.StatusUnauthorized)<br>       return<br>      }<br>    <br>      jwtHeader := tokenParts[0]<br>      jwtPayload := tokenParts[1]<br>      signature := tokenParts[2]<br>    <br>      message := jwtHeader + &quot;.&quot; + jwtPayload<br>    <br>      byteArrSecret := []byte(m.cnf.JwtSecretKey)<br>      byteArrMessage := []byte(message)<br>    <br>      h := hmac.New(sha256.New, byteArrSecret)<br>      h.Write(byteArrMessage)<br>    <br>      hash := h.Sum(nil)<br>      newSignature := base64UrlEncode(hash)<br>    <br>      if signature != newSignature {<br>       http.Error(w, &quot;Unauthorized&quot;, http.StatusUnauthorized)<br>       return<br>      }<br>    <br>      next.ServeHTTP(w, r)<br>     })<br>}<br><br>func base64UrlEncode(data []byte) string {<br>   return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(data)<br>}</pre><h4>Verifying JWTs with a Go Library</h4><p>Instead of manually verifying JWTs, we can use a popular library like <a href="https://github.com/golang-jwt/jwt">golang-jwt/jwt</a> to simplify the process. This library handles parsing, signature verification, and claim extraction for us.</p><p>Using a library like this makes JWT verification easier and helps avoid common mistakes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e239b7aa4a7" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/breaking-down-jwt-how-base64-sha-and-hmac-work-together-8e239b7aa4a7">Breaking Down JWT: How Base64, SHA and HMAC Work Together</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interfaces in Go : Ask the Question —  What Can You Do?]]></title>
            <link>https://blog.stackademic.com/what-can-you-do-letting-interfaces-ask-the-question-in-go-dde15ded1350?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/dde15ded1350</guid>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Mon, 01 Sep 2025 13:53:17 GMT</pubDate>
            <atom:updated>2025-12-04T03:18:32.383Z</atom:updated>
            <content:encoded><![CDATA[<h3>What <em>Can</em> You Do? Letting Interfaces Ask the Question in Go</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*54r_ltvtF6rr1FF3BmqTSw.png" /></figure><p>Sometimes you don’t care about the particular type of a value. You don’t care about what it is. You just need to know that it will be able to do certain things. That you’ll be able to call certain methods on it. You don’t care whether you have a <em>Pen</em> or a <em>Pencil</em>, you just need something with a Draw method. You don’t care whether you have a <em>Car</em> or a <em>Boat</em>, you just need something with a Steer method.</p><p>That’s what Go interfaces accomplish. They let you define variables and function parameters that will hold any type, as long as that type defines certain methods. [1]</p><h4>What is interfaces in Go?</h4><ul><li>An interface is an <strong>abstract</strong> type.</li><li>It doesn’t expose the representation or internal structure of its values, or the set of basic operations they support.</li><li>It reveals only some of their methods.</li></ul><p>When you have a value of an interface type, you know nothing about what it is; you know only what it can do, or more precisely, what behaviors are provided by its methods. [2]</p><p>An interface type is defined as a set of method signatures.</p><p>Basic Syntax:</p><pre>type Gopher interface { // Gopher =&gt; Interface name<br>    Learning() string<br>    Earning() int<br>    // ... more method signatures<br>}</pre><h4>Why we need interfaces?</h4><p>Imagine we have two distinct types, Learning, and Earning, both of which have a What() method. So, we need two separate functions (Ask_What_Learning() and Ask_What_Earning()) to call their What() methods. That means if we had <em>n</em> types, we would need to declare <em>n</em> different functions—this quickly becomes a serious problem.</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>type Learning struct {<br>    Something string<br>}<br>func (l Learning) What() {<br>    fmt.Println(&quot;Learn &quot;, l.Something)<br>}<br><br>type Earning struct {<br>    Something string<br>}<br>func (e Earning) What() {<br>    fmt.Println(&quot;Earn &quot;, e.Something)<br>}<br><br>func Ask_What_Learning (l Learning) {<br>    l.What()<br>}<br><br>func Ask_What_Earning(e Earning) {<br>    e.What()<br>}<br><br>func main() {<br>   learn := Learning{&quot;Golang&quot;}<br>   Ask_What_Learning(learn)<br>  <br>   earn := Earning{&quot;Money&quot;}<br>   Ask_What_Earning(earn)<br>} /* Output: <br>   Learn  Golang<br>   Earn  Money */</pre><p>By introducing an interface, we can eliminate redundant functions and make our code more flexible.</p><p>Updated code using Interface:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>// interface describes the behaviour we need (a What method)<br>type Whatter interface {<br>    What()<br>}<br><br>type Learning struct{ <br>    Something string <br>}<br>func (l Learning) What() { <br>    fmt.Println(&quot;Learn&quot;, l.Something) <br>}<br><br>type Earning struct{ <br>    Something string <br>}<br>func (e Earning) What() {<br>    fmt.Println(&quot;Earn&quot;, e.Something) <br>}<br><br>// single function accepts anything that implements Whatter<br>func AskWhat(w Whatter) {<br>    w.What()<br>}<br><br>func main() {<br>    AskWhat(Learning{&quot;Golang&quot;}) // Learn Golang<br>    AskWhat(Earning{&quot;Money&quot;})   // Earn Money<br>}</pre><p>Declaring the Whatter interface and a single AskWhat() function solves the problem of needing <em>n</em> separate functions for <em>n</em> types. Here:</p><ul><li>Both Learning and Earning implement Whatter.</li><li>AskWhat() doesn’t know which one it gets — but it calls the correct method automatically.</li><li>That’s <strong>runtime polymorphism</strong> in action.</li></ul><blockquote>Any type that has <strong>all the methods</strong> listed in an interface definition is said to <em>satisfy</em> that interface. A type that satisfies an interface can be used anywhere that interface is called for. [1]</blockquote><h4>What this means:</h4><ul><li>An interface is just a list of methods (a contract).</li><li>If a type provides all those methods, then Go says that the type <strong>satisfies</strong> (or <strong>implements</strong>) the interface.</li><li>Because of this, we can pass values of that type to any function that expects the interface.</li></ul><p>If a type does not have all the methods required by an interface, then:</p><ul><li>It does not satisfy that interface.</li><li>We cannot use that type where the interface is expected.</li></ul><p>Example of missing method:</p><pre>type Whatter interface {<br>    What()<br>    Why()<br>}<br><br>type Learning struct{ <br>    Subject string <br>}<br><br>// Only implements What(), not Why()<br>func (l Learning) What() {<br>    fmt.Println(&quot;Learning&quot;, l.Subject)<br>}<br><br>func AskWhat(w Whatter) {<br>    w.What()<br>    w.Why() // without it - same error<br>}<br><br>func main() {<br>    l := Learning{&quot;Golang&quot;}<br>    AskWhat(l) // ❌ compile-time error<br>} /* Output:<br>cannot use l (type Learning) as type Whatter in argument to AskWhat:<br>Learning does not implement Whatter (missing method Why) */</pre><p>Therefore, we must implement the Why() method as well to solve this problem.</p><pre>func (l Learning) Why() {<br>    fmt.Println(&quot;Because ...!&quot;)<br>}</pre><p>An interface value in Go is actually two things inside:</p><ul><li><strong>type</strong> (what concrete type it holds)</li><li><strong>value</strong> (the actual data)</li></ul><h4>Nil interface values:</h4><p>If nothing is assigned, both parts are empty → this is a nil interface value.</p><p>Calling a method on a nil interface is a run-time error because there is no type inside the interface tuple to indicate which concrete method to call.</p><pre>type I interface {<br>   M()<br>}<br><br>func main() {<br>   var i I<br>   describe(i)<br>   // i.M() =&gt; runtime error<br>}<br><br>func describe(i I) {<br>   fmt.Printf(&quot;value: %v, Type: %T\n&quot;, i, i)<br>} /* Output:<br>  value: &lt;nil&gt;, Type: &lt;nil&gt; */</pre><h4>Interface values with nil underlying values:</h4><ul><li>The type inside the interface is set</li><li>But the value inside is nil</li></ul><p>If the concrete value inside the interface itself is nil, the method will be called with a nil receiver.</p><pre>type I interface {<br>   M()<br>}<br>type T struct {<br>   S string<br>}<br>func (t *T) M() {<br>   // handle being called with a nil receiver <br>   // otherwise runtime error<br>   if t == nil {<br>    fmt.Println(&quot;&lt;nil&gt;&quot;)<br>    return<br>   }<br>   fmt.Println(t.S)<br>}<br>func main() {<br>   var i *T<br>   describe(i)<br>   i.M()<br>}<br><br>func describe(i I) {<br>   fmt.Printf(&quot;(%v, %T)\n&quot;, i, i)<br>} /* Output:<br>  (&lt;nil&gt;, *main.T)<br>  &lt;nil&gt; */</pre><ul><li>nil interface → nothing inside</li><li>interface with nil value→ type is known, value is nil</li></ul><h4>The empty interface:</h4><p>The interface type that specifies zero methods is known as the <em>empty </em>interface<em>.</em></p><p>An empty interface may hold values of any type. (Every type implements at least zero methods.)</p><pre>func main() {<br>   var i interface{} // empty interface<br>   describe(i)<br>  <br>   i = 42<br>   describe(i)<br>  <br>   i = &quot;hello&quot;<br>   describe(i)<br>}<br><br>func describe(i interface{}) {<br>   fmt.Printf(&quot;(%v, %T)\n&quot;, i, i)<br>} /* Output:<br>  (&lt;nil&gt;, &lt;nil&gt;)<br>  (42, int)<br>  (hello, string) */</pre><h4>We can only call methods defined as part of the interface.</h4><p>When we assign a value to an interface, we can only call the methods that the interface type declares.</p><pre>type Speaker interface {<br>    Speak()<br>    // no Walk() method here<br>}<br><br>type Person struct {<br>    Name string<br>}<br><br>func (p Person) Speak() {<br>    fmt.Println(&quot;Hi, I am&quot;, p.Name)<br>}<br>func (p Person) Walk() {<br>    fmt.Println(p.Name, &quot;is walking...&quot;)<br>}<br><br>func main() {<br>    // assign Person to interface<br>    var p Speaker = Person{Name: &quot;Mojammel&quot;}<br>    p.Speak() <br><br> // p.Walk() // compile error: p.Walk undefined<br>            // because Walk() is not part of the Speaker interface.<br>}/* Output:  <br>  Hi, I am Mojammel */</pre><p>This presents a new question: what if we need to access the other methods of the underlying type that aren’t defined in the interface? To overcome this limitation, Go provides a mechanism called type assertion.</p><h4>Type Assertion</h4><p>A type assertion allows us to extract the interface value’s underlying concrete value using this syntax: interfaceVariable.(concreteType).</p><p>If we assign the result of this statement to a single value, we assert that the interface variable holds a value of the concrete type. As a result, it may panic at runtime if the interface value does not have the specified concrete type. For example:</p><pre>var input interface{} = 12<br>str := input.(string) // panic at runtime since input is not a string!</pre><p>We can test whether an interface value holds a specific concrete type by making use of both return values of the type assertion: the underlying value and a boolean value that reports whether the assertion succeeded. For example:</p><pre>str, ok := input.(string) // no panic if input is not a string</pre><p>If input holds a string, then str will be the underlying value and ok will be true. If input does not hold a string, then str will be the zero value of type string (ie. &quot;&quot; - the empty string) and ok will be false. No panic occurs in any case.[3]</p><p><strong>Syntax: </strong>value, ok := iface.(ConcreteType)</p><ul><li>iface → the interface variable (input in our case)</li><li>ConcreteType → the actual type we expect inside (string)</li><li>value → the extracted value as ConcreteType</li><li>ok → a boolean (true if it really is that type, false otherwise)</li></ul><p>another example:</p><pre>func main() {<br>   var i interface{} = &quot;hello&quot;<br>  <br>   s := i.(string)<br>   fmt.Println(s)<br>  <br>   s, ok := i.(string)<br>   fmt.Println(s, ok)<br>  <br>   f, ok := i.(float64) // no panic<br>   fmt.Println(f, ok)<br>  <br>   f = i.(float64) // panic<br>   fmt.Println(f)<br>}/* Output:<br>  hello<br>  hello true<br>  0 false<br>  panic: interface conversion: interface {} is string, not float64 */</pre><h4>Type switches:</h4><p>A type switch is a construct that permits several type assertions in series.</p><p>A type switch is like a regular switch statement, but the cases in a type switch specify types (not values), and those values are compared against the type of the value held by the given interface value.</p><p>The declaration in a type switch has the same syntax as a type assertion interfaceVariable.(concreteType), but the specific type concreteType is replaced with the keyword type.</p><pre>func do(i interface{}) {<br>   switch v := i.(type) {<br>   case int:<br>      fmt.Printf(&quot;Twice %v is %v\n&quot;, v, v*2)<br>   case string:<br>      fmt.Printf(&quot;%q is %v bytes long\n&quot;, v, len(v))<br>   default:<br>      fmt.Printf(&quot;I don&#39;t know about type %T!\n&quot;, v)<br>   }<br>}<br><br>func main() {<br>   do(21)<br>   do(&quot;hello&quot;)<br>   do(true)<br>}/* Output:<br>  Twice 21 is 42<br>  &quot;hello&quot; is 5 bytes long<br>  I don&#39;t know about type bool!  */</pre><h4>Embedding Interfaces:</h4><p>An interface can embed other interfaces. For example:</p><pre>type Reader interface {<br>   Read() string<br>}<br><br>type Writer interface {<br>   Write(s string)<br>}<br><br>// Embedding both Reader and Writer<br>type ReadWriter interface {<br>   Reader  <br>   Writer   <br>}</pre><h3>Stringer</h3><p>The Stringer interface in Go is defined in the fmt package and consists of just one method:</p><pre>type Stringer interface {<br>    String() string<br>}</pre><p>Any type that implements this String() method is considered to be a Stringer. The purpose of the Stringer interface is to provide a way to control how an object is printed as a string. By implementing the String() method, you can define a custom representation of your type when it is printed using the fmt package or other string formatting techniques.</p><h4>Using Stringer Interface:</h4><p>To use the Stringer interface, you need to define a custom type and implement the String() method for that type. The String() method should return a string representation of the object. Here’s an example to illustrate this:</p><pre>type Person struct {<br>    Name string<br>    Age  int<br>}<br><br>func (p Person) String() string {<br>    return fmt.Sprintf(&quot;Name: %s, Age: %d&quot;, p.Name, p.Age)<br>}</pre><p>In the above example, we define a Person struct with Name and Age fields. By implementing the String() method for the Person type, we can customize the string representation when a Person object is printed. The String() method uses the fmt.Sprintf() function to format and return a string containing the person’s name and age. [4]</p><p>another example:</p><pre>type Person struct {<br>   Name string<br>   Age  int<br>}<br><br>func (p Person) String() string {<br>   return fmt.Sprintf(&quot;%v (%v years)&quot;, p.Name, p.Age)<br>}<br><br>func main() {<br>   a := Person{&quot;Abdullah&quot;, 42}<br>   z := Person{&quot;Abdur Rahman&quot;, 53}<br>   y := Person{&quot;Mojammel&quot;, 24}<br>   fmt.Println(a)<br>   fmt.Println(z, y)<br>}/* Output:<br>  Abdullah (42 years)<br>  Abdur Rahman (53 years) Mojammel (24 years) */</pre><p>Exercise: <a href="https://go.dev/tour/methods/18">Stringers</a></p><h3>The http.Handler interface</h3><p>…handler is an object which satisfies the http.Handler interface:</p><pre>type Handler interface {<br>   ServeHTTP(ResponseWriter, *Request)<br>}</pre><p>In simple terms, this basically means that to be a handler an object must have a ServeHTTP() method with the exact signature:</p><pre> ServeHTTP(http.ResponseWriter, *http.Request)</pre><p>So in its simplest form a handler might look something like this:</p><pre> type home struct {}<br><br> func (h *home) ServeHTTP(w http.ResponseWriter, r *http.Request) {<br>     w.Write([]byte(&quot;This is my home page&quot;))<br> }</pre><p>Here we have an object (in this case it’s an empty home struct, but it could equally be a string or function or anything else), and we’ve implemented a method with the signature ServeHTTP(http.ResponseWriter, *http.Request) on it. That’s all we need to make a handler.</p><p>You could then register this with a servemux using the Handle method like so:</p><pre>mux := http.NewServeMux()<br>mux.Handle(&quot;/&quot;, &amp;home{})</pre><p>When this servemux receives a HTTP request for “/”, it will then call the ServeHTTP() method of the home struct — which in turn writes the HTTP response.</p><h4>Handler functions</h4><pre>func home(w http.ResponseWriter, r *http.Request) {<br>   w.Write([]byte(&quot;This is my home page&quot;))<br>}</pre><p>…this home function is just a normal function; it doesn’t have a ServeHTTP() method. So in itself it isn’t a handler.</p><p>Instead we can transform it into a handler using the http.HandlerFunc() adapter, like so:</p><pre>mux := http.NewServeMux()<br>mux.Handle(&quot;/&quot;, http.HandlerFunc(home))</pre><p>The http.HandlerFunc() adapter works by automatically adding a ServeHTTP() method to the home function. When executed, this ServeHTTP() method then simply calls the code inside of the original home function. It’s a roundabout but convenient way of coercing a normal function into satisfying the http.Handler interface. [5]</p><pre>mux := http.NewServeMux()<br>mux.HandleFunc(&quot;/&quot;, home)</pre><p>Want to learn along with me? I’m sharing my Go learning journey with code and notes on GitHub:</p><p>🔗 <a href="https://github.com/mhbhuiyan99/Go">https://github.com/mhbhuiyan99/Go</a></p><h4><strong>References:</strong></h4><ol><li><strong>Head First GO</strong> by Jay McGavren</li><li><strong>The Go Programming Language</strong> by Alan A. A. Donovan and Brian W. Kernighan</li><li><strong>Exercism Go Track:</strong> <a href="https://exercism.org/tracks/go/concepts/type-assertion">Type Assertion</a></li><li><a href="https://golang.howtos.io/understanding-and-using-go-s-stringer-interface/">Understanding and Using Go’s Stringer Interface</a></li><li><strong>Let’s Go </strong>by<strong> </strong>Alex Edwards</li><li><a href="https://go.dev/tour/methods/9">A Tour of Go</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dde15ded1350" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/what-can-you-do-letting-interfaces-ask-the-question-in-go-dde15ded1350">Interfaces in Go : Ask the Question —  What Can You Do?</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go Server Request-Response Lifecycle]]></title>
            <link>https://levelup.gitconnected.com/go-server-request-response-lifecycle-7e9872a80ef1?source=rss-518ac2752afd------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e9872a80ef1</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Mojammel Haque Bhuiyan]]></dc:creator>
            <pubDate>Mon, 11 Aug 2025 14:24:33 GMT</pubDate>
            <atom:updated>2025-10-07T08:00:36.964Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mqZxVSTtPrAlr3wvSST0Fg.jpeg" /></figure><p>Let’s eyeball the code for a moment:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;net/http&quot;<br>)<br><br>func helloHandler(w http.ResponseWriter, r *http.Request){<br> fmt.Fprintln(w, &quot;Hello, World!&quot;)<br>}<br><br>func aboutHandler(w http.ResponseWriter, r *http.Request){<br> fmt.Fprintln(w, &quot;I am Mojammel Haque Bhuiyan&quot;)<br>}<br><br>func main(){<br> mux := http.NewServeMux() // mux = router<br><br> // Handler is a receiver function of mux that handles HTTP requests<br> mux.HandleFunc(&quot;/hello&quot;, helloHandler) // route for /hello<br><br> mux.HandleFunc(&quot;/about&quot;, aboutHandler)<br><br> fmt.Println(&quot;Server is running on port 3000&quot;)<br><br> err := http.ListenAndServe(&quot;:3000&quot;, mux)<br> if err != nil {<br>  fmt.Println(&quot;Error starting server:&quot;, err)<br>  return<br> }<br> /*else - server started successfully<br> But, if we run the program in another terminal <br> without stopping the server there will be an error <br> because the port is already in use */<br>}</pre><h4>Background Concepts:</h4><ul><li>RAM = Kernel Space + User Space<br>— Kernel Space: OS codebase and kernel data structures reside here<br>— User Space: Where user applications (including Go runtime) execute</li><li>Go Runtime M:P:G Model:<br> — M = OS/Machine thread (actual kernel threads)<br> — P = Logical/Virtual processor (defaults to number of CPU cores)<br> — G = Goroutines (lightweight user-space threads)</li><li>Queue System<br> — Local Run Queue (LRQ) per P: 256 goroutine slots maximum<br> — Global Run Queue (GRQ): Holds goroutines not yet assigned to any P<br> — Work Stealing Algorithm: Idle P steals goroutines from busy P’s LRQ<br> — OS threads run logical processors, which execute goroutines from queues</li></ul><h3>Request-Response Lifecycle:</h3><h4>0. Server Start-up</h4><ul><li>Go Runtime Initialization (main goroutine):<br>1. Initialize Go scheduler (M:P:G components)<br>2. Go runtime → syscall → kernel → epoll_create<br> — epoll operations: create, control, wait<br> — Linux: epoll, Windows: IOCP, Mac: Kqueue<br>3. Set up Garbage Collector (GC)</li><li>Main goroutine starts http.ListenAndServe → creates a listening socket via syscall.</li></ul><h4>1. Client Sends Request</h4><ul><li>PC sends an HTTP request (over <a href="https://youtu.be/uwoD5YsGACg">TCP</a>) to the server’s IP port.</li><li>Router forwards it to the server’s NIC (Network Interface Card).</li></ul><pre>// src&gt;net&gt;http&gt;server.go&gt;(*Server).ListenAndServe<br>ln, err := net.Listen(&quot;tcp&quot;, addr)<br> if err != nil {<br>  return err<br> }<br> return s.Serve(ln)</pre><h4>2. NIC Receives Data</h4><ul><li>NIC stores incoming <a href="https://youtu.be/KjD3KANH-xc">packets</a> in a receive (RX) buffer (hardware-level memory).</li><li>Uses Direct Memory Access (DMA) to copy them to system RAM (kernel space ring buffers).</li><li>NIC raises a hardware interrupt to notify the <a href="https://youtu.be/8afhcpiMh24">kernel</a>.</li></ul><h4>3. Kernel Processes Packet</h4><ul><li>Kernel processes packets from NIC buffers.</li><li>Kernel identifies the destination socket via internal socket data structures, associated with a particular socket.</li><li>The <a href="https://stackoverflow.com/questions/5256599/what-are-file-descriptors-explained-in-simple-terms">file descriptor</a> is a process-specific handle used in user space to access that socket. (In Linux, sockets are treated as files.)</li></ul><h4>4. Data in Socket Receive Buffer</h4><ul><li>Kernel stores data in the socket’s receive buffer (per connection).</li><li>Kernel marks the file descriptor as ready-to-read.</li><li>Kernel signals Go runtime’s netpoll (epoll/IOCP/kqueue) that data is ready.</li></ul><h4>5. Go Runtime Accepts Connection</h4><ul><li>The main goroutine is typically blocked on Accept() waiting for new connections.</li><li>The kernel wakes the process (Go runtime) when a new TCP connection arrives and is in the listen queue.</li><li>Accept() returns a new file descriptor corresponding to the accepted client connection.</li><li>This file descriptor is used by the Go runtime to read/write data specific to that client.</li></ul><pre>// src&gt;net&gt;http&gt;server.go&gt;(*Server).Serve<br>for {<br>  rw, err := l.Accept() // Blocks until new connection<br>  if err != nil {<br>    // handle error<br>    continue<br>  }<br>  c := srv.newConn(rw) // Wrap raw connection<br>  go c.serve(connCtx)  // Spawn goroutine per connection<br>}<br>// Serve accepts incoming connections on the Listener l, creating a<br>// new service goroutine for each. The service goroutines read requests and<br>// then call s.Handler to reply to them.</pre><h4>6. Go Spawns Handler Goroutine</h4><ul><li><strong>Goroutine Creation &amp; Memory:</strong><br> — Go runtime creates new goroutine for this connection (Goroutine stack: 2KB initial size, grows dynamically).<br> — Goroutine placement: New G added to P’s local run queue (or global queue if local is full).</li><li><strong>I/O Operation Flow:</strong><br> — I/O Request: Goroutine tells Go runtime → runtime tells kernel via syscall.<br> — Goroutine Parking: On blocking I/O, the goroutine parks in user space (G transitions from Grunning to Gwaiting, state change tracked by the Go scheduler, not the OS).<br> — Thread Liberation: The OS thread (M) is freed to run other goroutines on the same P.</li><li><strong>M:P:G </strong><a href="https://www.ardanlabs.com/blog/2018/08/scheduling-in-go-part2.html"><strong>Scheduling</strong></a><strong> &amp; Work </strong><a href="https://rakyll.org/scheduler/"><strong>Stealing</strong></a><strong>:</strong><br> — M (OS/Machine thread) executes P (logical processor).<br> — P manages local run queue of G (goroutines).<br> — Work stealing: If P’s local queue empty, steals goroutines from other P’s LRQ or takes from GRQ.<br> — Connection handling: Reads from socket receive buffer, parses HTTP request, routes via ServeMux.</li><li><strong>Goroutine Unparking:<br></strong> — Network I/O Ready: When I/O completes, kernel notifies netpoll via epoll_wait.<br> — Goroutine Unparking: Go runtime unparks the waiting goroutine, changes status back to Grunnable.<br> — Scheduler Integration: Unparked goroutine placed in run queue for execution.</li></ul><h4>7. Response Sent Back</h4><ul><li>The HTTP handler writes the response data into the kernel’s send buffer, preparing it for transmission.</li><li>Kernel sends chunks to NIC TX (transmit) ring buffer when ready.</li><li>NIC transmits packets to router then router sends it to the client.</li></ul><h4>netpoll:</h4><ul><li>Net poller has its own OS thread, and handles events from goroutines doing network I/0.</li><li>Net poller Interfaces with the OS to poll the appropriate network sockets.</li><li>Then Re-schedules goroutines where their network resource is available.</li></ul><h4>netpoll interface:</h4><ul><li>netpoll function pulls the network and it return a list of goroutines are now ready for execution.</li><li>Then the other parts of the runtime is that we regularly call this netpoll function and inject the list of goroutines that back onto resources that are about to be scheduled on our processors.</li></ul><p>When the network call is complete the goroutine gets moved back onto the local run queue where it can then scheduled for execution on our OS thread.</p><p>We don’t need to spawn new operating system threads or new processors to handle these network calls and this also exposes a really nice interface for us as programmers where we can think of network io as blocking within our goroutines but the net poller handles the asynchronous part of that exposing this really nice interface.</p><p>Want to learn along with me? I’m sharing my Go learning journey with code and notes on GitHub:</p><p>🔗 <a href="https://github.com/mhbhuiyan99/Go">https://github.com/mhbhuiyan99/Go</a></p><h4>References:</h4><ol><li><a href="https://stackoverflow.com/questions/36112445/will-go-block-the-current-thread-when-doing-i-o-inside-a-goroutine">https://stackoverflow.com/questions/36112445/will-go-block-the-current-thread-when-doing-i-o-inside-a-goroutine</a></li><li><a href="https://rakyll.org/scheduler/">https://rakyll.org/scheduler/</a></li><li><a href="https://www.ardanlabs.com/blog/2018/08/scheduling-in-go-part2.html">https://www.ardanlabs.com/blog/2018/08/scheduling-in-go-part2.html</a></li><li><a href="https://www.linkedin.com/pulse/golang-how-does-goroutine-parks-chung-yi-kao/">https://www.linkedin.com/pulse/golang-how-does-goroutine-parks-chung-yi-kao/</a></li><li><a href="https://vitalcs.substack.com/p/goroutines-under-the-hood">https://vitalcs.substack.com/p/goroutines-under-the-hood</a></li><li><a href="https://youtu.be/S-MaTH8WpOM?t=999">https://youtu.be/S-MaTH8WpOM?t=999</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e9872a80ef1" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/go-server-request-response-lifecycle-7e9872a80ef1">Go Server Request-Response Lifecycle</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>