<?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 Dsaurav on Medium]]></title>
        <description><![CDATA[Stories by Dsaurav on Medium]]></description>
        <link>https://medium.com/@dsaurav243?source=rss-74621d5bdbf4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EhstugHDKg6En47g</url>
            <title>Stories by Dsaurav on Medium</title>
            <link>https://medium.com/@dsaurav243?source=rss-74621d5bdbf4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:44:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dsaurav243/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[Kubernetes Services Explained: From Basics to Internal Networking]]></title>
            <link>https://medium.com/@dsaurav243/kubernetes-services-explained-from-basics-to-internal-networking-699dec90fb38?source=rss-74621d5bdbf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/699dec90fb38</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[container-networking]]></category>
            <dc:creator><![CDATA[Dsaurav]]></dc:creator>
            <pubDate>Mon, 16 Nov 2026 15:06:50 GMT</pubDate>
            <atom:updated>2026-11-16T15:06:50.884Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kjuLhnBOcSq7OVlecONEGA.png" /></figure><h3><strong>Introduction</strong></h3><p>In Kubernetes, applications run inside Pods, and Pods are designed to be ephemeral. They can be created, deleted, rescheduled, or replaced at any time. Whenever a Pod is recreated, it can receive a different IP address.</p><p>This creates an important networking challenge: <strong>How can one application reliably communicate with another when the destination Pod’s IP address keeps changing?</strong></p><p>Imagine deploying an Nginx application with three replicas. A frontend application needs to communicate with these Nginx Pods. If it directly uses their IP addresses, it must continuously track which Pods are available and update its connection details whenever Pods change.</p><p>Kubernetes solves this problem using <strong>Services</strong>.</p><p>A Service provides a stable network endpoint through which applications can communicate with a group of Pods, regardless of their lifecycle or individual IP addresses.</p><p>In this blog, we will explore Kubernetes Services from the fundamentals to their internal networking implementation, using a consistent Nginx deployment example.</p><h4><strong>1. What Is a Kubernetes Service?</strong></h4><p><strong>A Kubernetes Service is a Kubernetes object (resource) that provides a stable way to access a group of Pods, allowing applications to communicate with them without needing to know the individual IP addresses of each Pod.</strong></p><p>It provides:</p><ul><li>A stable virtual IP address (ClusterIP).</li><li>A consistent DNS name for service discovery.</li><li>Traffic distribution across matching backend Pods.</li><li>A decoupled communication mechanism between applications.</li></ul><p>Let’s consider an Nginx Deployment running three replicas.</p><p>Without a Service, clients need to know the IP address of each Pod. With a Service, clients communicate through a single stable endpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6hKyO6k5B1yx1_p1SB6UBQ.png" /></figure><p>In our Nginx example, in the above diagram we have three Pods, each with its own IP address:</p><ul><li><strong>Nginx Pod 1:</strong> 10.244.0.10</li><li><strong>Nginx Pod 2:</strong> 10.244.0.11</li><li><strong>Nginx Pod 3:</strong> 10.244.0.12</li><li><strong>Nginx Service:</strong> 10.96.0.10</li></ul><p>Instead of connecting to each Pod individually, the client only needs to communicate with the Service using its stable IP address or DNS name. The Kubernetes Service takes care of directing traffic to the appropriate backend Pod.</p><p><strong>Important:</strong> A Service does not create, own, or manage Pods. A Deployment is responsible for creating, scaling, and managing Pods, whereas a Service provides a stable network endpoint through which applications can access those Pods.</p><h4>2. Creating a Kubernetes Service</h4><p>Let’s assume we already have an Nginx application running in our Kubernetes cluster with three replicas.</p><p>All three Nginx Pods are running successfully and have the following common configuration:</p><ul><li><strong>Pod Label:</strong> app: nginx</li><li><strong>Application Port:</strong> 80</li></ul><p>Our goal is to create a Kubernetes Service that provides a single, stable endpoint to access these Pods.</p><p><strong>Step 1: Create a Service</strong></p><p>Create a file named nginx-service.yaml:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-service<br>spec:<br>  type: ClusterIP<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 80</pre><p>Apply the Service manifest:</p><pre>kubectl apply -f nginx-service.yaml</pre><p>Verify the Service:</p><pre>kubectl get services<br>kubectl describe service nginx-service</pre><p>You should see a ClusterIP assigned to the Service. This IP acts as a stable endpoint through which other applications can access the Nginx Pods.</p><p><strong>Understanding the Service Manifest</strong></p><p>Let’s understand the important fields in the Service manifest:</p><ul><li><strong>apiVersion:</strong> Specifies the Kubernetes API version used to create the resource. For a Service, it is v1.</li><li><strong>kind:</strong> Specifies the type of Kubernetes resource being created. Here, it is Service.</li><li><strong>type:</strong> Defines how the Service is exposed. ClusterIP makes it accessible within the Kubernetes cluster.</li><li><strong>selector:</strong> Identifies the backend Pods using their labels.</li><li><strong>port:</strong> Specifies the port exposed by the Service.</li><li><strong>targetPort:</strong> Specifies the port on which the application listens inside the backend Pod.</li><li><strong>protocol:</strong> Defines the network protocol used for communication, which is TCP by default.</li></ul><p><strong>How Does a Service Identify the Backend Pods?</strong></p><p>One of the most important fields in a Service manifest is selector.</p><p>Our existing Nginx Pods have the following label:</p><pre>labels:<br>  app: nginx</pre><p>The Service uses the same label in its selector:</p><pre>selector:<br>  app: nginx</pre><p>Kubernetes uses this label-matching mechanism to identify which Pods should receive traffic from the Service.</p><p>Since all three Nginx Pods have the label app: nginx, the Service automatically identifies them as its backend Pods.</p><p>This means we don’t need to manually specify the Pod IP addresses in the Service configuration. Kubernetes continuously monitors matching Pods and updates the Service’s backend endpoints accordingly.</p><p><strong>Understanding port, targetPort and nodePort</strong></p><p>These three fields are commonly used when working with Kubernetes Services, and it is important to understand the difference:</p><ul><li><strong>port:</strong> The port through which clients access the Service.</li><li><strong>targetPort:</strong> The destination port on the backend Pod where the application is listening.</li><li><strong>nodePort:</strong> The port exposed on each Kubernetes node to allow external access through a NodePort Service.</li></ul><p>For our Nginx example:</p><ul><li>port: 80 – Clients access the Nginx Service on port 80.</li><li>targetPort: 80 – Traffic is forwarded to port 80 on the selected Nginx Pod.</li></ul><p>Since we are using the ClusterIP Service type, we do not need to specify a nodePort.</p><h4><strong>3. Types of Kubernetes Services</strong></h4><p>Kubernetes provides four primary types of Services, each designed to handle a different networking requirement. Depending on whether an application needs to communicate internally, be accessed externally, or connect to an external service, we can choose the appropriate Service type.</p><p>The four types are:</p><ol><li><strong>ClusterIP</strong> — Provides internal access within the Kubernetes cluster.</li><li><strong>NodePort</strong> — Exposes an application through a port on each Kubernetes node.</li><li><strong>LoadBalancer</strong> — Exposes an application externally using a load balancer.</li><li><strong>ExternalName</strong> — Maps a Kubernetes Service name to an external DNS hostname.</li></ol><p>Let’s understand each type using our existing Nginx application.</p><p><strong>3.1 ClusterIP</strong></p><p>Default Service type. ClusterIP is the default Service type in Kubernetes. It exposes a Service through an internal virtual IP address, allowing applications running inside the cluster to communicate with the backend Pods.</p><p>This means that other applications within the cluster can access the Nginx application using the Service’s IP address or DNS name, without knowing the individual Pod IP addresses.</p><p><strong>How does it work?</strong></p><ul><li>Kubernetes assigns a virtual IP address to the Service.</li><li>The Service uses its selector to identify the matching Nginx Pods.</li><li>Applications inside the cluster communicate with the Service using its IP or DNS name.</li><li>Kubernetes networking directs the traffic to one of the available backend Pods.</li></ul><p><strong>Example configuration:</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-service<br>spec:<br>  type: ClusterIP<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 80</pre><p><strong>Use case:</strong> Internal communication between applications, such as a frontend application communicating with a backend API or database service.</p><p>Our existing nginx-service uses the ClusterIP type.</p><p><strong>3.2 NodePort</strong></p><p>Unlike ClusterIP, which provides access only within the cluster by default, NodePort allows applications to be accessed from outside the Kubernetes cluster.</p><p>NodePort exposes a Service on a specific port across all Kubernetes nodes. External clients can access the application using the IP address of any reachable node along with the assigned NodePort.</p><p><strong>How does it work?</strong></p><ul><li>Kubernetes assigns a port on each node.</li><li>External clients send requests to a node’s IP address and the assigned NodePort.</li><li>Kubernetes networking forwards the traffic to the Service.</li><li>The Service directs the traffic to one of the matching backend Pods.</li></ul><p>Access format: <strong>http://&lt;NodeIP&gt;:&lt;NodePort&gt;</strong></p><p><strong>Example configuration:</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-service<br>spec:<br>  type: NodePort<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 80<br>      nodePort: 30080</pre><p>Here:</p><ul><li>port: 80 – Port exposed by the Service.</li><li>targetPort: 80 – Port where Nginx listens inside the Pod.</li><li>nodePort: 30080 – Port exposed on the Kubernetes nodes.</li></ul><p>By default, Kubernetes uses the NodePort range 30000–32767.</p><p><strong>Use case:</strong> Development environments, testing, and basic external access when a dedicated external load balancer is not required.</p><p><strong>3.3 LoadBalancer</strong></p><p>LoadBalancer is commonly used when we want to expose an application to external users through a dedicated load balancer.</p><p>When we create a Service of type LoadBalancer, Kubernetes requests an external load balancer through the cloud provider or supported infrastructure integration.</p><p>Once provisioned, the load balancer provides an external IP address or hostname through which clients can access the application.</p><p><strong>How does it work?</strong></p><ul><li>An external client sends a request to the load balancer’s IP address or hostname.</li><li>The load balancer forwards the request towards the Kubernetes Service.</li><li>Kubernetes networking directs the traffic to one of the matching backend Pods.</li></ul><p><strong>Example configuration:</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx-service<br>spec:<br>  type: LoadBalancer<br>  selector:<br>    app: nginx<br>  ports:<br>    - protocol: TCP<br>      port: 80<br>      targetPort: 80</pre><p>Unlike NodePort, we don’t need to manually specify a NodePort. Kubernetes and the infrastructure integration handle the required provisioning and networking configuration.</p><p><strong>Use case:</strong> Exposing production applications, websites, and APIs to external clients.</p><p><strong>3.4 ExternalName</strong></p><p>The previous three Service types primarily deal with directing traffic to Pods within the Kubernetes environment. ExternalName works differently.</p><p>ExternalName allows us to map a Kubernetes Service name to an external DNS hostname using a DNS CNAME record.</p><p>In simple terms, it provides an internal Kubernetes DNS name that points to a service hosted outside the cluster.</p><p><strong>How does it work?</strong></p><ul><li>A client requests the Kubernetes Service DNS name.</li><li>Kubernetes DNS returns a CNAME record pointing to the configured external hostname.</li><li>The client resolves that hostname and connects to the external service.</li></ul><p><strong>Example configuration:</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: external-nginx<br>spec:<br>  type: ExternalName<br>  externalName: example.com</pre><p>In this example, applications can refer to the external service using:<br><strong>external-nginx.default.svc.cluster.local</strong></p><p>Kubernetes DNS maps this name to example.com.</p><p><strong>Important:</strong> Unlike ClusterIP, NodePort, and LoadBalancer, ExternalName does not select Pods, allocate a ClusterIP, or configure a proxy-based traffic path. It simply provides DNS-based name mapping.</p><p><strong>Use case:</strong> Accessing external databases, APIs, or other services using a consistent Kubernetes DNS name without hardcoding external hostnames in application configurations.</p><p><strong>Visualizing how the four Service types work</strong></p><p>The following diagram uses our Nginx example to illustrate how traffic flows through each Service type</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zBHSkTIBWwTVKKxmi2PEFw.png" /></figure><p>The key difference is where the client connects and how the request reaches its destination:</p><p><strong>ClusterIP: </strong>Internal Client → Service → Nginx Pods</p><p><strong>NodePort : </strong>External Client → Node IP:Port → Service → Nginx Pods</p><p><strong>LoadBalancer: </strong>External Client → External Load Balancer → Service → Pods</p><p><strong>ExternalName: </strong>Client → Kubernetes DNS → External DNS hostname</p><p><strong><em>Pods may come and go, but a Kubernetes Service keeps your connection alive.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=699dec90fb38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes Storage Explained: From Container Filesystems to Production-Grade Persistent Storage]]></title>
            <link>https://medium.com/@dsaurav243/kubernetes-storage-explained-from-container-filesystems-to-production-grade-persistent-storage-fb95b4e6c327?source=rss-74621d5bdbf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb95b4e6c327</guid>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[cloud-native]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[containerization]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Dsaurav]]></dc:creator>
            <pubDate>Thu, 17 Sep 2026 14:18:57 GMT</pubDate>
            <atom:updated>2026-09-17T14:18:57.947Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-7PanwKPK5AG7XzvakFUgg.png" /></figure><h3>Introduction</h3><p>Kubernetes makes it easy to deploy applications, scale workloads, and recover from container failures. But running an application is only one part of the problem.</p><p>What happens to your data when a container crashes, a Pod is recreated, or an application moves to another worker node?</p><p>For stateless applications, losing temporary files may not be a major concern. For databases, message queues, file-processing systems, and other stateful workloads, storage becomes a critical architectural decision.</p><p>Kubernetes provides a storage abstraction that separates how storage is provisioned from how applications consume it.</p><p>In this article, we will progress from the fundamentals of Kubernetes volumes to persistent storage management.</p><h3>1. Understanding the Storage Problem in Containers</h3><h4>1.1 A Container’s Filesystem: Why Your Data Needs a Home</h4><p>Imagine deploying a MySQL database inside a Kubernetes container. Your application is running, users are creating records, and everything looks perfect. But what happens if the container suddenly crashes?</p><p>Will your database still have all its data when Kubernetes brings it back?</p><p>This is where understanding container storage becomes essential.</p><h4>What happens inside a container?</h4><p>Every container has a filesystem that is initially based on its container image. When an application creates or modifies files in its writable filesystem, those changes belong to that container’s filesystem environment.</p><p>The challenge is that a container is designed to be replaceable, not a reliable home for important data.</p><p>According to the official Kubernetes documentation, files stored in a container’s filesystem can be lost when the container terminates and restarts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Q0oYXoeeT3pJZZY8AgAeg.png" /></figure><h3>2. The Shared Storage Problem</h3><p>Imagine a Kubernetes Pod running two containers:</p><ul><li>Application container: Generates report files.</li><li>Processing container: Reads and processes the generated reports.</li></ul><p>Both containers need access to the same files. Kubernetes volumes provide a shared storage location that multiple containers within a Pod can mount and use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yILfbGp6MSeJWcb19Yt5ag.png" /></figure><h3>3. What Is a Kubernetes Volume?</h3><p>A Kubernetes volume is a storage resource that makes files and directories available to containers running inside a Pod.</p><p>Volumes help address common storage requirements, such as:</p><ul><li>Providing temporary workspace for applications.</li><li>Sharing files between multiple containers in a Pod.</li><li>Making configuration data available to applications.</li><li>Retaining data beyond an individual container’s lifecycle.</li><li>Connecting workloads to storage systems that support sharing across Pods.</li></ul><p>Kubernetes supports a variety of volume types, and a single Pod can use multiple volumes simultaneously, depending on the application’s requirements.</p><h4>3.1 How Do You Configure a Volume in Kubernetes?</h4><p>f you’re new to Kubernetes, it’s helpful to think of a volume as a storage location that a container can access.</p><p>To use a volume, Kubernetes requires two things:</p><ol><li>Define the volume: Tell Kubernetes what storage should be available to the Pod.</li><li>Mount the volume: Tell Kubernetes where that storage should appear inside the container.</li></ol><p><strong><em>Step 1: Define the Volume at the Pod Level</em></strong></p><pre>volumeMounts:<br>  - name: app-data<br>    mountPath: /data</pre><p>Here, we define a volume named app-data.</p><ul><li>name: app-data gives the volume a name so it can be referenced later.</li><li>emptyDir: {} tells Kubernetes to create an initially empty directory for the Pod.</li></ul><p>The volume is defined at the Pod level, which means it can be made available to one or more containers within that Pod. Think of this step as preparing a storage location for your Pod.</p><p><strong><em>Step 2: Mount the Volume Inside the Container</em></strong></p><pre>volumeMounts:<br>  - name: app-data<br>    mountPath: /data</pre><p>This configuration tells Kubernetes:</p><ul><li>Use the volume named app-data.</li><li>Make it accessible inside the container at /data.</li></ul><p>The application can now create, read, and modify files inside /data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sAuefsV1yVHE5a6NXVK9Zw.png" /></figure><h3>4. Ephemeral vs. Persistent Storage</h3><p>One of the most important concepts in Kubernetes storage is understanding the difference between ephemeral and persistent storage.</p><p>The type of storage you choose depends on how long your application needs its data to remain available. Some storage is intended for temporary use during a Pod’s lifecycle, while other storage is designed to preserve data beyond the lifetime of a Pod.</p><h4>4.1 What Is Ephemeral Storage?</h4><p>Ephemeral storage is temporary storage associated with a workload or Pod. It is useful when data only needs to exist while the workload is running.</p><p>For example, an application might need temporary storage for:</p><ul><li>Processing intermediate files.</li><li>Caching frequently accessed data.</li><li>Sharing files between containers in the same Pod.</li><li>Storing configuration data injected into a container.</li></ul><h4>Example: emptyDir</h4><p>When a Pod is assigned to a node, Kubernetes creates an emptyDir volume. Containers in that Pod can use it to read and write files.</p><p>The volume remains available when a container crashes and restarts, as long as the Pod continues to exist. However, the contents of an emptyDir volume are deleted when the Pod is removed.</p><h4>4.2 What Is Persistent Storage?</h4><p>Persistent storage is designed to retain application data beyond the lifecycle of an individual Pod.</p><p>This is particularly important for stateful applications such as MySQL, PostgreSQL, and other databases, where losing data when a Pod is recreated is unacceptable.</p><p>A PersistentVolume (PV) represents storage that has a lifecycle independent of an individual Pod. A Pod can use the PV through a PersistentVolumeClaim (PVC), allowing the application to access storage managed separately from its own lifecycle.</p><p>However, persistent storage does not automatically guarantee:</p><ul><li>Data replication.</li><li>Backups.</li><li>High availability.</li><li>Disaster recovery.</li></ul><p>These capabilities depend on the underlying storage system, its configuration, and the operational processes supporting it.</p><h3>5. emptyDir: Temporary Shared Storage</h3><h4>What Is emptyDir?</h4><p>An emptyDir volume is a temporary directory created when a Pod is assigned to a worker node. It provides a shared storage location that all containers within the same Pod can access.</p><p>This makes emptyDir useful when containers need to exchange files, store temporary data, or process intermediate results. The volume remains available throughout the Pod&#39;s lifecycle, but its contents are deleted when the Pod is removed.</p><h4>Real-World Example: Application and Log Processor</h4><p>Consider a Pod with two containers. The application container generates log files, while the log processor container reads and processes those logs.</p><p>Both containers mount the same emptyDir volume at /shared. The application writes logs to /shared/app.log, and the log processor reads the same file from its own container.</p><p>This works because both containers access the same underlying volume, even though each container has its own filesystem.</p><h4>5.1 Configuring an emptyDir Volume</h4><p>The following manifest creates a Pod with two containers that share an emptyDir volume:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: emptydir-demo<br>spec:<br>  volumes:<br>    - name: shared-data<br>      emptyDir: {}<br><br>  containers:<br>    - name: writer<br>      image: busybox:1.36<br>      command: [&quot;/bin/sh&quot;, &quot;-c&quot;]<br>      args:<br>        - |<br>          while true; do<br>            date &gt;&gt; /shared/data.txt<br>            sleep 5<br>          done<br>      volumeMounts:<br>        - name: shared-data<br>          mountPath: /shared<br><br>    - name: reader<br>      image: busybox:1.36<br>      command: [&quot;/bin/sh&quot;, &quot;-c&quot;]<br>      args:<br>        - |<br>          while true; do<br>            cat /shared/data.txt<br>            sleep 10<br>          done<br>      volumeMounts:<br>        - name: shared-data<br>          mountPath: /shared</pre><p>Here, the shared-data volume is defined once at the Pod level and mounted into both containers at /shared. The writer appends timestamps to data.txt, while the reader periodically displays the file&#39;s contents.</p><h3>6. hostPath: Accessing the Node Filesystem</h3><h4>What Is hostPath?</h4><p>A hostPath volume makes a file or directory from the worker node&#39;s filesystem available inside a Pod.</p><p>Unlike emptyDir, which provides temporary storage associated with a Pod, hostPath directly references a specific location on the node. This allows a container to read from or write to files stored on the host machine.</p><h4>When Might You Use hostPath?</h4><p>A hostPath volume can be useful when a workload needs access to node-specific information, such as system logs, configuration files, or a directory used by a specialized node-level application.</p><p>For example, a monitoring agent running on a node might need to read logs generated by services on that node. However, using hostPath introduces a direct dependency on the node&#39;s filesystem and requires careful security and scheduling considerations.</p><h4>6.1 Configuring a hostPath Volume</h4><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: hostpath-demo<br>spec:<br>  containers:<br>    - name: app<br>      image: busybox:1.36<br>      command: [&quot;/bin/sh&quot;, &quot;-c&quot;]<br>      args:<br>        - |<br>          echo &quot;Hello from the Pod&quot; &gt; /host-data/message.txt<br>          sleep 3600<br>      volumeMounts:<br>        - name: node-storage<br>          mountPath: /host-data<br><br>  volumes:<br>    - name: node-storage<br>      hostPath:<br>        path: /tmp/k8s-demo<br>        type: DirectoryOrCreate</pre><p>In this example, the container accesses the node’s /tmp/k8s-demo directory through /host-data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OqcZugl9gMzESRz2kqus7Q.png" /></figure><h3>7. Why Do We Need PersistentVolumes and PersistentVolumeClaims?</h3><p>So far, we have explored emptyDir for temporary shared storage and hostPath for accessing a worker node&#39;s filesystem. While both are useful in specific scenarios, they are not always suitable for applications that require reliable, long-term storage.</p><p>Consider a development team deploying a database application. The developer needs 10 GiB of storage, but does not want to know whether that storage is backed by NFS, AWS EBS, or another storage system. The storage administrator, on the other hand, is responsible for provisioning and managing the underlying infrastructure.</p><p>Kubernetes addresses this separation of responsibilities through PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs). Developers can request storage based on application requirements, while Kubernetes and the storage infrastructure handle how that storage is provided.</p><p>This approach allows application teams to focus on deploying their workloads without managing the details of the physical or network storage infrastructure.</p><h3>8. PersistentVolume (PV)</h3><h4>What Is a PersistentVolume?</h4><p>A PersistentVolume (PV) is a storage resource available within a Kubernetes cluster. It represents storage that has been provisioned for workloads and has a lifecycle independent of any individual Pod.</p><p>A PV can be created manually by a storage administrator or provisioned dynamically through a StorageClass. The underlying storage may be backed by systems such as NFS, AWS EBS, or other storage technologies supported by the cluster.</p><p>The key purpose of a PV is to separate the application’s storage requirements from the underlying storage implementation. A developer does not need to manage the physical disk or understand every detail of the storage backend to use the storage in an application.</p><h4>Storage Administrator’s Role</h4><p>The storage administrator is typically responsible for preparing the storage infrastructure, configuring storage classes, and defining the capabilities available to workloads. These capabilities can include storage capacity, supported access modes, and backend-specific configuration.</p><p>The exact responsibilities depend on how storage is managed within the organization and the Kubernetes environment.</p><h3>9. PersistentVolumeClaim (PVC)</h3><h4>What Is a PersistentVolumeClaim?</h4><p>A PersistentVolumeClaim (PVC) is a request for storage made by a Kubernetes user, such as a developer or application team.</p><p>Instead of selecting a specific physical disk, the developer describes what the application needs. For example, a PVC might request 10 GiB of storage and specify an access mode or a particular StorageClass.</p><p>Kubernetes then attempts to bind the claim to a suitable existing PersistentVolume. If dynamic provisioning is configured, Kubernetes can also request that the storage system create a new volume that meets the claim’s requirements.</p><p>A PVC is therefore not the physical storage itself. It is a request and reference through which a workload can access storage.</p><h4>A Simple Real-World Analogy</h4><p>Imagine a storage provider that maintains an inventory of available storage. A customer submits a request for a particular amount of storage, and the provider assigns suitable storage from its inventory or provisions new storage.</p><p>The developer creates a PVC describing the required storage, and Kubernetes handles the process of finding or provisioning a suitable PV, depending on the cluster’s configuration.</p><p>The developer creates a PVC describing the required storage, and Kubernetes handles the process of finding or provisioning a suitable PV, depending on the cluster’s configuration.</p><h3>10. How PersistentVolumes, PVCs, and Pods Work Together</h3><p>Understanding the relationship between a storage backend, PersistentVolume, PersistentVolumeClaim, and Pod is essential for working with persistent storage in Kubernetes.</p><p>The storage backend represents the actual storage infrastructure, such as a cloud disk or an NFS server. A PersistentVolume represents that storage as a Kubernetes resource. A PersistentVolumeClaim expresses the application’s storage requirement, and the Pod references the claim to use the storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PEc0mSS0eDIHJ4T3" /></figure><h4>11.1 The Complete Workflow</h4><p>The process begins when storage is made available to the Kubernetes cluster. A storage administrator may provision a PersistentVolume manually, or a configured StorageClass may enable dynamic provisioning.</p><p>Next, the developer creates a PersistentVolumeClaim specifying the required capacity and other storage requirements. For example, the application may request 10 GiB of storage.</p><p>Kubernetes then evaluates the claim and attempts to bind it to a suitable PersistentVolume. If dynamic provisioning is enabled and no suitable PV is available, Kubernetes can request a new volume from the configured storage backend.</p><p>Once the PVC is bound, the Pod references the PVC in its volume configuration. Kubernetes makes the claimed storage available to the container through a volumeMounts entry.</p><h4>Important Concept to Remember</h4><p>A PVC does not directly represent a physical disk. It is a storage request that Kubernetes can bind to a suitable PV or use to trigger dynamic provisioning.</p><p>The main benefit of PVs and PVCs is separation of responsibilities: developers describe the storage they need, while storage administrators and Kubernetes manage how that storage is provided.</p><p><strong><em>The goal of Kubernetes is not just to run containers, but to make applications resilient, scalable, and easier to operate.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb95b4e6c327" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Channels — The Communication Highway]]></title>
            <link>https://medium.com/@dsaurav243/channels-the-communication-highway-2810b53ff5d9?source=rss-74621d5bdbf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2810b53ff5d9</guid>
            <category><![CDATA[channel]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Dsaurav]]></dc:creator>
            <pubDate>Sun, 24 May 2026 01:10:21 GMT</pubDate>
            <atom:updated>2026-05-24T01:10:21.642Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yo4uYSiuZ_q7_Cj3oEc8aA.png" /></figure><h3>Introduction</h3><p>Imagine a group project in college where different students handle different responsibilities such as research, presentation, documentation, and design. Even though everyone works independently, the project can only succeed if all members communicate properly. Without proper communication, tasks may conflict, work may get duplicated, and confusion can arise.</p><p>Concurrency in programming works in a very similar way. When multiple concurrent tasks run independently, they need a safe and efficient way to communicate with each other. In Go, that communication happens using <strong>Channels</strong>.</p><p>Channels are one of the most elegant and powerful features of Go. They help Goroutines exchange data safely while keeping concurrent programs:</p><ul><li>synchronised</li><li>simple</li><li>readable</li><li>easier to manage</li></ul><p>In this blog, we will explore channels from beginner to professional level using:</p><ul><li>real-world analogies</li><li>simple code examples</li><li>deep conceptual explanations</li></ul><p>By the end, you will understand not just how channels work, but also why they are such an important part of Go concurrency.</p><h3>Why Channels Exist</h3><p>Before understanding channels, it is important to first understand the problem they are designed to solve. In concurrent programming, multiple Goroutines often run independently at the same time, and many of them need to exchange information or coordinate their work. The challenge is ensuring that this communication happens safely and efficiently.</p><p>In many programming languages, concurrent tasks usually communicate through shared memory using mechanisms such as shared variables, locks, and mutexes. While this approach works, it can quickly become difficult to manage as applications grow larger and more complex. Improper handling of shared memory often leads to problems such as:</p><ul><li>race conditions</li><li>deadlocks</li><li>difficult debugging</li><li>unpredictable behavior</li></ul><p>To simplify concurrent programming, Go follows a very important philosophy:</p><blockquote><em>“Do not communicate by sharing memory; instead, share memory by communicating.”</em></blockquote><p>Channels are Go’s implementation of this idea. Instead of multiple Goroutines directly modifying shared data, channels allow them to safely exchange information through communication pipelines. This approach makes concurrent programs easier to understand, safer to write, and simpler to maintain.</p><h3>What is a Channel?</h3><p>A channel in Go is a communication mechanism that allows Goroutines to exchange data safely with each other. In simple terms, a channel acts like a communication pipe between Goroutines where one Goroutine can send data and another Goroutine can receive it.</p><p>A good real-life analogy is a courier service. One person sends a package, another person receives it, and the courier path connecting them acts as the communication medium.</p><p>Similarly, channels act as safe communication pipelines that transfer data between Goroutines without requiring direct shared memory access. This makes concurrent programs more synchronized, safer, and easier to understand.</p><h3>Creating Your First Channel and Understanding Synchronisation</h3><p>Creating a channel in Go is very simple. A channel is created using the make() function.</p><pre>ch := make(chan string)</pre><p>This creates a channel capable of carrying string values between the Goroutines.</p><p>Consider the following example:</p><pre>package main<br><br>import &quot;fmt&quot;<br>func main() {<br>    ch := make(chan string)<br>    go func() {<br>        ch &lt;- &quot;Hello from Goroutine&quot;<br>    }()<br>    message := &lt;-ch<br>    fmt.Println(message)<br>}</pre><p>At first glance, many beginners get confused about where the <em>“other Goroutine”</em> actually exists in this example. The important thing to understand is that every Go program already starts with one Goroutine known as the <strong>main Goroutine</strong>. This means the main() function itself is already running inside a Goroutine.</p><p>In this example, there are actually two Goroutines:</p><pre>| Goroutine           | Description                  |<br>| ------------------- | ---------------------------- |<br>| Main Goroutine      | The `main()` function itself |<br>| Anonymous Goroutine | Created using `go func()`    |</pre><p>When the program starts, the main Goroutine creates the channel:</p><pre>ch := make(chan string)</pre><p>Then another Goroutine is created:</p><pre>go func() {<br>    ch &lt;- &quot;Hello from Goroutine&quot;<br>}()</pre><p>At this point, two Goroutines are running concurrently:</p><ul><li>the main Goroutine</li><li>the anonymous Goroutine</li></ul><p>The main Goroutine then tries to receive data from the channel:</p><pre>message := &lt;-ch</pre><p>Here, the main Goroutine blocks and waits for data to arrive through the channel. Meanwhile, the Go scheduler runs the anonymous Goroutine, which sends data into the channel:</p><pre>ch &lt;- &quot;Hello from Goroutine&quot;</pre><p>Once the value is sent, the blocked receive operation gets unblocked, the data transfer completes, and both Goroutines continue execution.</p><p>This leads to one of the most important concepts in Go concurrency: <strong>channels are blocking by nature</strong>. When a Goroutine tries to send data into a channel, it waits until another Goroutine receives that value. Similarly, a Goroutine trying to receive data waits until another Goroutine sends something.</p><p>However, blocking does not stop the entire program. Blocking only pauses the specific Goroutine performing that operation. The Go scheduler can still run other Goroutines concurrently.</p><p>This is the key reason concurrency continues to work efficiently even with blocking channels.</p><p>Consider the following example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br>func worker(ch chan string) {<br>    fmt.Println(&quot;Worker started&quot;)<br>    time.Sleep(2 * time.Second)<br>    ch &lt;- &quot;Work completed&quot;<br>}<br>func printer() {<br>    for i := 1; i &lt;= 5; i++ {<br>        fmt.Println(&quot;Printing:&quot;, i)<br>        time.Sleep(500 * time.Millisecond)<br>    }<br>}<br>func main() {<br>    ch := make(chan string)<br>    go worker(ch)<br>    go printer()<br>    message := &lt;-ch<br>    fmt.Println(message)<br>}</pre><p>In this example:</p><ul><li>the main Goroutine blocks waiting for data from the worker</li><li>the worker Goroutine performs its task</li><li>the printer Goroutine continues executing independently</li></ul><p>Even though the main Goroutine is blocked, the printer Goroutine still continues printing values concurrently because the Go scheduler simply pauses the blocked Goroutine and schedules another runnable Goroutine.</p><p>You can think of:</p><ul><li>Goroutines as independent tasks</li><li>channels as communication pipelines</li><li>blocking as temporarily pausing one task</li><li>the scheduler as a traffic controller deciding which Goroutine runs next</li></ul><p>This blocking behavior is actually what makes channels powerful. Instead of destroying concurrency, channels use blocking as a synchronisation mechanism that safely coordinates Goroutines without requiring complicated locking logic or unsafe shared memory access. This design is one of the most elegant aspects of concurrency in Go.</p><h3>Buffered vs Unbuffered Channels</h3><p>Channels in Go are of two types:</p><ul><li>Unbuffered Channels</li><li>Buffered Channels</li></ul><p>Understanding the difference between them is very important because they behave differently in terms of synchronization and communication.</p><h4>Unbuffered Channels</h4><p>By default, channels in Go are <strong>unbuffered</strong>.</p><pre>ch := make(chan int)</pre><p>An unbuffered channel has no internal storage or queue. This means the sender and receiver must be ready at the same time for communication to happen.</p><p>If a Goroutine tries to send data:</p><pre>ch &lt;- 10</pre><p>it blocks until another Goroutine receives the value.</p><p>Similarly, if a Goroutine tries to receive data:</p><pre>value := &lt;-ch</pre><p>it waits until another Goroutine sends something.</p><p>You can think of an unbuffered channel like handing over an object directly from one person to another. Both people must be present at the same moment for the transfer to happen.</p><pre>Sender ──[waits]──► Channel ──► Receiver</pre><p>This behavior creates <strong>strict synchronization</strong> between Goroutines and is extremely useful when tasks need tight coordination.</p><h4>Buffered Channels</h4><p>Sometimes immediate receiving is not necessary. In such cases, Go provides <strong>buffered channels</strong>, which contain an internal queue that can temporarily store values.</p><p>A buffered channel is created by specifying a capacity:</p><pre>ch := make(chan int, 3)</pre><p>Here, the channel can store up to 3 integer values before blocking.</p><p>Example:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    ch := make(chan int, 3)<br>    ch &lt;- 1 // Does not block<br>    ch &lt;- 2 // Does not block<br>    ch &lt;- 3 // Does not block<br>    // ch &lt;- 4 // This would block because buffer is full<br>    fmt.Println(&lt;-ch)<br>    fmt.Println(&lt;-ch)<br>    fmt.Println(&lt;-ch)<br>}</pre><p>In buffered channels:</p><ul><li>sending does not block immediately if buffer has space</li><li>receiving does not require sender to be active at the exact same time</li></ul><p>You can think of a buffered channel like a small storage box where items can be temporarily kept until someone collects them later.</p><p>This creates <strong>temporary decoupling</strong> between sender and receiver. Buffered channels do not remove synchronization entirely. They simply delay it until the buffer becomes full. Once the buffer reaches capacity, send operations block again until some values are received.</p><p>Choosing between buffered and unbuffered channels depends on how tightly you want Goroutines to coordinate with each other.</p><h4>When to Use Which?</h4><pre>| Scenario                                  | Recommended Channel Type |<br>| ----------------------------------------- | ------------------------ |<br>| Strict synchronization between Goroutines | Unbuffered               |<br>| Producer-consumer systems                 | Buffered                 |<br>| Limiting concurrency (semaphore pattern)  | Buffered                 |<br>| Simple request-response flow              | Unbuffered               |<br>| Temporary burst handling                  | Buffered                 |</pre><h3>Channel Direction, Closing Channels, and Iterating Over Channels</h3><p>Channels in Go are not only used for communication between Goroutines, but they also provide mechanisms that improve code safety, readability, and synchronisation. Some very important concepts related to channels are:</p><ul><li>channel direction</li><li>closing channels</li><li>ranging over channels</li></ul><p>Understanding these concepts helps developers write cleaner, safer, and more maintainable concurrent programs.</p><h4>Channel Direction</h4><p>By default, a channel in Go can both:</p><ul><li>send data</li><li>receive data</li></ul><p>However, Go allows channels to be restricted to a single direction when passed to functions. This means a channel can be declared as:</p><ul><li>send-only</li><li>receive-only</li></ul><p>This restriction improves:</p><ul><li>code safety</li><li>readability</li><li>API clarity</li></ul><p>because the compiler prevents accidental misuse of the channel.</p><h4>Send-Only Channel</h4><p>A send-only channel allows a function to only send data into the channel.</p><pre>func send(ch chan&lt;- int)</pre><p>Here:</p><ul><li>chan&lt;- int<br> means the function can only send integer values.</li></ul><p>Example:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func send(ch chan&lt;- int) {<br>    ch &lt;- 100<br>    fmt.Println(&quot;Value sent&quot;)<br>}<br>func main() {<br>    ch := make(chan int)<br>    go send(ch)<br>    value := &lt;-ch<br>    fmt.Println(value)<br>}</pre><p>In this example:</p><ul><li>the send() function can only send values</li><li>attempting to receive from the channel inside send() would cause a compile-time error</li></ul><p>This makes the function’s responsibility very clear.</p><h4>Receive-Only Channel</h4><p>A receive-only channel allows a function to only receive data from the channel.</p><pre>func receive(ch &lt;-chan int)</pre><p>Here:</p><ul><li>&lt;-chan int<br> means the function can only receive integer values.</li></ul><p>Example:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func receive(ch &lt;-chan int) {<br>    value := &lt;-ch<br>    fmt.Println(&quot;Received:&quot;, value)<br>}<br>func main() {<br>    ch := make(chan int)<br>    go receive(ch)<br>    ch &lt;- 200<br>}</pre><p>In this example:</p><ul><li>the receive() function can only receive values</li><li>attempting to send into the channel inside receive() would produce a compile-time error</li></ul><p>This prevents accidental misuse and improves program safety.</p><h4>Closing Channels</h4><p>Channels can also be explicitly closed when no more values will be sent through them.</p><pre>close(ch)</pre><p>Closing a channel informs receivers that:</p><ul><li>communication is complete</li><li>no more values will arrive</li></ul><p>This is especially useful in producer-consumer systems where receivers need to know when processing should stop.</p><h4>Reading from a Closed Channel</h4><p>When receiving from a channel, Go provides an optional second return value:</p><pre>value, ok := &lt;-ch</pre><p>Here:</p><ul><li>value contains the received data</li><li>ok indicates whether the channel is still open</li></ul><p>If:</p><pre>ok == false</pre><p>it means:</p><ul><li>the channel has been closed</li><li>no more data is available</li></ul><h4>Range Over Channels</h4><p>One of the most elegant features in Go is iterating over channels using range.</p><pre>for value := range ch {<br>    fmt.Println(value)<br>}</pre><p>The loop:</p><ul><li>continuously receives values from the channel</li><li>automatically stops once the channel closes</li></ul><p>This makes channel-based communication much cleaner and simpler.</p><h4>Example with Close and Range</h4><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    ch := make(chan int)<br>    go func() {<br>        for i := 1; i &lt;= 5; i++ {<br>            ch &lt;- i<br>        }<br>        close(ch)<br>    }()<br>    for value := range ch {<br>        fmt.Println(value)<br>    }<br>}</pre><p>First, a channel is created using:</p><pre>ch := make(chan int)</pre><p>This creates a communication channel capable of transferring integer values between Goroutines.</p><p>Next, a separate Goroutine starts sending values into the channel using:</p><pre>ch &lt;- i</pre><p>Each value sent into the channel becomes available for another Goroutine to receive. After all values have been sent successfully, the sender closes the channel using:</p><pre>close(ch)</pre><p>Closing the channel signals to all receivers that no more values will be sent in the future. It is essentially a way of saying:</p><blockquote><em>“Communication is complete.”</em></blockquote><p>Meanwhile, the main Goroutine continuously reads values from the channel using:</p><pre>for value := range ch</pre><p>The range loop keeps receiving values one by one until the channel is closed and all remaining values are consumed. Once the channel is fully exhausted, the loop automatically terminates without requiring any explicit break condition. This makes channel-based iteration clean, elegant, and highly useful in concurrent Go programs.</p><h3>The select Statement — Traffic Control for Channels</h3><p>Imagine multiple people trying to send you messages at the same time, but you do not know who will respond first. Instead of waiting for only one person, you want to listen to all of them simultaneously and immediately process whichever message arrives first.</p><p>This is exactly what the select statement does in Go.</p><p>The select statement is similar to a switch statement, but instead of working with conditions or values, it works with channel operations. It allows a Goroutine to wait on multiple channel communications at the same time and automatically executes the case whose channel becomes ready first.</p><p>For example, if one channel receives data before the others, the corresponding case block executes immediately. This makes select extremely useful for:</p><ul><li>handling multiple concurrent operations</li><li>implementing timeouts</li><li>listening to multiple Goroutines</li><li>building responsive concurrent systems</li></ul><p>Here is a simple example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br>    ch1 := make(chan string)<br>    ch2 := make(chan string)<br>    go func() {<br>        time.Sleep(1 * time.Second)<br>        ch1 &lt;- &quot;one&quot;<br>    }()<br>    go func() {<br>        time.Sleep(2 * time.Second)<br>        ch2 &lt;- &quot;two&quot;<br>    }()<br>    // Wait for whichever channel sends first<br>    for i := 0; i &lt; 2; i++ {<br>        select {<br>        case msg1 := &lt;-ch1:<br>            fmt.Println(&quot;Received from ch1:&quot;, msg1)<br>        case msg2 := &lt;-ch2:<br>            fmt.Println(&quot;Received from ch2:&quot;, msg2)<br>        }<br>    }<br>}</pre><h4>select with a default Case (Non-blocking)</h4><pre>select {<br>case msg := &lt;-ch:<br>    fmt.Println(&quot;Received:&quot;, msg)<br>default:<br>    fmt.Println(&quot;No message ready, moving on...&quot;)<br>}</pre><p>The default case runs <strong>immediately</strong> if no channel is ready — making the select non-blocking.</p><h4>Timeout Pattern with select</h4><pre>select {<br>case result := &lt;-ch:<br>    fmt.Println(&quot;Got result:&quot;, result)<br>case &lt;-time.After(3 * time.Second):<br>    fmt.Println(&quot;Timed out! No response in 3 seconds.&quot;)<br>}</pre><p>This is one of Go’s most elegant patterns for handling timeouts.</p><blockquote>“Great concurrent systems are not built by running everything together, but by coordinating everything correctly.”</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2810b53ff5d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Goroutines — Go’s Superpower]]></title>
            <link>https://medium.com/@dsaurav243/goroutines-gos-superpower-7c8d053da21a?source=rss-74621d5bdbf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c8d053da21a</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Dsaurav]]></dc:creator>
            <pubDate>Sat, 23 May 2026 15:38:10 GMT</pubDate>
            <atom:updated>2026-05-24T01:11:37.643Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-8ls5g1xgkTk-el0pH_4Xg.png" /></figure><p>Modern software applications rarely perform just one task at a time. Think about applications you use daily:</p><ul><li>Messaging apps receive notifications while sending messages</li><li>Browsers download files while you continue browsing</li><li>Video streaming platforms buffer content while playing videos</li><li>Servers handle thousands of users simultaneously</li></ul><p>Behind all these systems lies one important concept:</p><blockquote><strong><em>Concurrency</em></strong></blockquote><p>And one of the most elegant implementations of concurrency exists in Go through something called <strong>Goroutines</strong>.</p><h3>Understanding the Problem Before Goroutines</h3><p>Before learning Goroutines, let us first understand why they are needed. Imagine you are cooking dinner. You have to</p><ul><li>boil rice</li><li>fry vegetables</li><li>answer phone calls</li><li>prepare salad</li></ul><p>If you perform one task completely before starting another, dinner becomes very slow.</p><p>Instead, you switch between tasks efficiently. This idea of managing multiple tasks together is called <em>Concurrency</em></p><p>Applications work similarly. A web server may need to do below thing all together</p><ul><li>accept user requests</li><li>query database</li><li>send responses</li><li>process logs</li></ul><p>Without concurrency:</p><ul><li>applications become slow</li><li>CPU remains underutilised</li><li>user experience suffers</li></ul><h3>What is a Goroutine?</h3><p>A <strong>goroutine</strong> is a function that runs concurrently with the rest of your program. Starting one is as simple as writing the keyword go before a function call.</p><h4>Normal Function</h4><pre>package main<br><br>import &quot;fmt&quot;<br><br>func hello() {<br>    fmt.Println(&quot;Hello&quot;)<br>}<br><br>func main() {<br>    hello()<br>}</pre><p>Execution flow of the above code snippet is</p><ol><li>main() function starts</li><li>hello() function is called</li><li>program execution ends</li></ol><p>Everything runs sequentially.</p><h4>Goroutine version of the above code snippet</h4><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func hello() {<br>    fmt.Println(&quot;Hello from Goroutine&quot;)<br>}<br><br>func main() {<br><br>    go hello()<br><br>    time.Sleep(time.Second)<br>}</pre><p>The <strong>go</strong> keyword creates a Goroutine. Now hello() runs independently and main() continues immediately.</p><pre>time.Sleep(time.Second)</pre><p>Without using time.Sleep() or another synchronization mechanism, the program may terminate before the Goroutine gets a chance to execute. This happens because the main() function continues executing independently after starting the Goroutine. Once the main() function finishes, the entire program exits immediately, and all running Goroutines are stopped automatically by the Go runtime. This is one of the most common beginner confusions in Go concurrency, as many developers initially expect the Goroutine to continue running even after main() completes.</p><h4>How Goroutines Actually Work</h4><p>To truly appreciate Goroutines, it is important to compare them with traditional threads used in many programming languages. In several languages, concurrency is implemented using operating system threads. Although threads are powerful, they are relatively expensive in terms of memory and system resources. Creating and managing thousands of threads can consume significant memory and put heavy pressure on the operating system scheduler.</p><p>Goroutines solve this problem elegantly. Instead of relying directly on operating system threads, Goroutines are lightweight units of execution managed by the Go runtime itself. This makes them extremely efficient and cheap to create. A traditional OS thread typically requires around 1 MB of memory initially, whereas a Goroutine starts with only about 2 KB of memory. This massive difference allows Go applications to create thousands or even millions of Goroutines efficiently without overwhelming the system. This lightweight nature is one of the biggest reasons why Go is highly popular for building scalable concurrent systems.</p><h4>Go Runtime Scheduler</h4><p>One of the greatest engineering achievements of Go is its runtime scheduler. The Go runtime intelligently manages Goroutines behind the scenes, allowing developers to write highly concurrent applications without manually handling complex thread management.</p><p>Internally, the Go scheduler is based on a model commonly referred to as the <strong>G-M-P model</strong>.</p><pre>| Component | Meaning                                                   |<br>| --------- | --------------------------------------------------------- |<br>| G         | Goroutine                                                 |<br>| M         | Machine (an OS Thread)                                    |<br>| P         | Processor (logical processor required to execute Go code) |</pre><p>The scheduler efficiently maps a large number of Goroutines onto a smaller number of operating system threads. Instead of creating a separate OS thread for every task, the Go runtime dynamically schedules Goroutines across available threads. This makes Goroutines extremely lightweight and efficient compared to traditional threading models.</p><p>This design is important because Goroutines are cheap to create, context switching between them is very fast, and concurrency becomes highly scalable. As a result, Go applications can handle massive numbers of concurrent tasks efficiently without exhausting system resources. This is one of the major reasons why Go is widely used for building high-performance systems such as web servers, cloud platforms, distributed systems, and container orchestration tools like Kubernetes.</p><h4>Multiple Goroutines Example</h4><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func task(name string) {<br>    for i := 1; i &lt;= 5; i++ {<br>        fmt.Println(name, i)<br>        time.Sleep(500 * time.Millisecond)<br>    }<br>}<br><br>func main() {<br><br>    go task(&quot;Worker-1&quot;)<br>    go task(&quot;Worker-2&quot;)<br><br>    time.Sleep(3 * time.Second)<br>}</pre><p>What happens here is two Goroutines:</p><ul><li>run independently</li><li>execute concurrently</li><li>scheduler switches between them</li></ul><p>Output order may vary every execution. This is an important concurrency characteristic.</p><h4>Anonymous Goroutines</h4><p>You can create Goroutines without separate functions.</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br><br>    go func() {<br>        fmt.Println(&quot;Anonymous Goroutine&quot;)<br>    }()<br><br>    time.Sleep(time.Second)<br>}</pre><p>Anonymous Goroutines are helpful for:</p><ul><li>quick concurrent tasks</li><li>background jobs</li><li>inline async execution</li></ul><p>Very commonly used in production systems.</p><h3>Goroutine Lifecycle and Scheduling</h3><p>A Goroutine goes through a simple lifecycle. It gets created, executes its assigned task, completes its execution, and is then automatically cleaned up by the Go runtime. Goroutines are not permanent or immortal entities. Once the function associated with a Goroutine finishes execution, the Goroutine terminates automatically. Developers do not need to manually destroy or clean up Goroutines, which makes concurrent programming in Go much simpler compared to traditional thread management.</p><p>Behind the scenes, the Go runtime scheduler is responsible for managing all Goroutines efficiently. The scheduler decides:</p><ul><li>when a Goroutine should run</li><li>where it should run</li><li>how long it should execute before switching to another Goroutine</li></ul><p>This scheduling happens automatically without requiring developers to manually manage threads or CPU allocation. The Go runtime intelligently distributes Goroutines across available system threads, making concurrency both efficient and developer-friendly.</p><h3>Common Beginner Mistakes and Race Conditions</h3><p>While Goroutines make concurrency simple and powerful in Go, beginners often encounter a few common mistakes when starting with concurrent programming.</p><p>One of the most common issues is that the main() function exits before a Goroutine gets a chance to execute. For example:</p><pre>go hello()</pre><p>If there is no waiting mechanism such as time.Sleep(), WaitGroup, or <a href="https://medium.com/@dsaurav243/channels-the-communication-highway-2810b53ff5d9">channels</a>, the program may terminate immediately after main() finishes, causing the Goroutine to stop before completing its task.</p><p>Another common mistake is creating too many Goroutines unnecessarily. Although Goroutines are lightweight compared to traditional threads, they are not completely free. Creating an excessive number of Goroutines can increase memory usage and scheduling overhead, eventually affecting application performance. Concurrency improves scalability, but it must still be used carefully and responsibly.</p><p>A more dangerous problem occurs when multiple Goroutines access and modify the same variable simultaneously. This leads to what is known as a <strong>race condition</strong>. A race condition happens when multiple Goroutines access shared data concurrently and at least one Goroutine modifies that data, resulting in unpredictable behavior.</p><p>Consider the following example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>var counter int<br><br>func increment() {<br>    for i := 0; i &lt; 1000; i++ {<br>        counter++<br>    }<br>}<br><br>func main() {<br><br>    go increment()<br>    go increment()<br><br>    time.Sleep(time.Second)<br><br>    fmt.Println(counter)<br>}</pre><p>At first glance, you might expect the output to always be:</p><pre>2000</pre><p>However, the actual result can vary unpredictably during different executions. This happens because the statement:</p><pre>counter++</pre><p>is not an atomic operation. Internally, it involves multiple steps:</p><ul><li>reading the current value</li><li>incrementing the value</li><li>writing the updated value back</li></ul><p>When multiple Goroutines perform these operations simultaneously, they can interfere with each other, causing inconsistent results.</p><h3>Goroutine Leaks</h3><p>One advanced but extremely important concept in Go concurrency is the idea of a <strong>Goroutine leak</strong>. A Goroutine leak happens when a Goroutine remains blocked forever and never gets a chance to terminate properly. Even though Goroutines are lightweight, leaked Goroutines still consume memory and runtime resources. Over time, especially in large-scale applications, these leaked Goroutines can accumulate and slowly degrade system performance.</p><p>A Goroutine leak commonly occurs when a Goroutine waits indefinitely on an operation that never completes. For example:</p><ul><li>waiting forever to receive data from a channel</li><li>trying to send data to a channel with no receiver</li><li>running an infinite blocked operation without cancellation handling</li></ul><p>Consider a scenario where a Goroutine is waiting on a channel, but no other Goroutine ever sends data to that channel. The Goroutine remains alive indefinitely, consuming resources while doing no useful work.</p><p>Goroutine leaks are particularly dangerous because applications may continue running normally for some time while memory usage gradually increases in the background. In production systems, this can eventually lead to:</p><ul><li>increased memory consumption</li><li>reduced application performance</li><li>scheduler overhead</li><li>system instability</li></ul><p>To avoid Goroutine leaks, developers should always ensure that Goroutines have a clear termination path. Some common best practices include:</p><ul><li>properly closing <a href="https://medium.com/@dsaurav243/channels-the-communication-highway-2810b53ff5d9">channels</a> when communication is complete</li><li>using context.Context for cancellation and timeout handling</li><li>avoiding infinite blocking operations</li><li>ensuring every channel send has a corresponding receiver</li><li>using select statements with timeout or cancellation cases</li><li>carefully managing long-running background Goroutines</li></ul><p>Proper Goroutine lifecycle management is an essential part of writing reliable and scalable concurrent applications in Go.</p><blockquote>“Goroutines are not just about making programs faster. They are about building systems that can perform multiple tasks efficiently, gracefully, and at massive scale.”</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c8d053da21a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unit Testing in Go for Beginners: From Simple Tests to Fakes, Stubs & Interfaces]]></title>
            <link>https://medium.com/@dsaurav243/unit-testing-in-go-for-beginners-from-simple-tests-to-fakes-stubs-interfaces-cd1f19d9670b?source=rss-74621d5bdbf4------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd1f19d9670b</guid>
            <category><![CDATA[go-programming-language]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Dsaurav]]></dc:creator>
            <pubDate>Mon, 09 Feb 2026 18:44:50 GMT</pubDate>
            <atom:updated>2026-02-09T18:45:32.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EcQcr5SwrnGLlCbNG1fLKw.png" /></figure><p>If you’re new to Go, unit testing may feel <em>too simple at first</em> — and then suddenly <strong>very hard</strong> when databases, APIs, or files appear.</p><p>This article will help you understand:</p><ol><li>How to write <strong>simple unit tests</strong></li><li>What Go <strong>does not support</strong></li><li>Why we need <strong>interfaces</strong></li><li>How <strong>stubs, fakes, and mocks</strong> help in testing</li><li>When to use <strong>which test double</strong></li></ol><p>No prior testing experience required.</p><h3>1. Writing Your First Simple Unit Test in Go</h3><p>Go comes with a built-in testing framework: testing. Tests live in *_test.go files.</p><h4>Example: Simple Function</h4><pre>func Add(a, b int) int {<br> return a + b<br>}</pre><pre>func TestAdd(t *testing.T) {<br> result := Add(2, 3)<br> if result != 5 {<br>  t.Fatalf(&quot;expected 5, got %d&quot;, result)<br> }<br>}</pre><ul><li>TestAdd → Go detects functions starting with Test</li><li>t *testing.T → test handler</li><li>t.Fatalf() → marks test as failed</li></ul><p>Run it using:</p><pre>go test</pre><p>But real applications are <strong>not this simple</strong>.</p><h3>2. The Real Problem: Code That Depends on External Systems</h3><p>Consider this function:</p><pre>func CreateUser(db *sql.DB, name string) error {<br> _, err := db.Exec(&quot;INSERT INTO users(name) VALUES(?)&quot;, name)<br> return err<br>}</pre><h4>Why Is This Hard to Test?</h4><p>To test this, you need:</p><ul><li>A running database</li><li>Network</li><li>Correct schema</li><li>Cleanup after tests</li></ul><p>❌ This is <strong>not unit testing</strong><br> ❌ Tests become slow and flaky</p><h3>3. What Go Does NOT Support (Important to Understand)</h3><p>Unlike Python or JavaScript, <strong>Go does not support</strong>:</p><p>❌ Monkey patching<br> ❌ Runtime function replacement<br> ❌ Method overriding<br> ❌ Decorator-based mocking</p><h4>Example (NOT POSSIBLE in Go)</h4><pre>sql.Exec = fakeExec   // cannot do this</pre><p>Because of these limitations, Go encourages <strong>better design</strong>.</p><h3>4. Go’s Solution: Interfaces + Dependency Injection</h3><p>Instead of depending on concrete implementations, Go depends on <strong>behavior</strong>.</p><h4>Step 1: Create an Interface</h4><pre>type DBExecutor interface {<br> Exec(query string, args ...any) error<br>}</pre><h4>Step 2: Use Interface Instead of Concrete Type</h4><pre>type UserService struct {<br> db DBExecutor<br>}<br><br>func (u *UserService) CreateUser(name string) error {<br> return u.db.Exec(&quot;INSERT INTO users(name) VALUES(?)&quot;, name)<br>}</pre><p>Now UserService:</p><ul><li>Doesn’t care if it’s MySQL</li><li>Doesn’t care if it’s fake</li><li>Only cares that Exec() exists</li></ul><p>✔ This makes testing possible</p><h3>5. Test Doubles: Replacing Real Dependencies in Tests</h3><p>A <strong>test double</strong> is a replacement for a real dependency.</p><p>There are different types of test doubles like Stub, Fakes, Mock and Spy.</p><h3>6. Stubs: The Simplest Test Double</h3><h4>What Is a Stub?</h4><p>A stub:</p><ul><li>Returns hardcoded values</li><li>Has no logic</li><li>Does not track calls</li></ul><h4>Example: Stub Database</h4><pre>type StubDB struct{}<br><br>func (s *StubDB) Exec(query string, args ...any) error {<br> return nil // always succeeds<br>}</pre><h4>Unit Test Using Stub</h4><pre>func TestCreateUser_WithStub(t *testing.T) {<br> stubDB := &amp;StubDB{}<br> service := &amp;UserService{db: stubDB}<br><br> err := service.CreateUser(&quot;Alice&quot;)<br> if err != nil {<br>  t.Fatal(&quot;expected no error&quot;)<br> }<br>}</pre><h4>When to Use Stubs?</h4><p>✔ You only care about <strong>success path</strong><br> ✔ You don’t care how many times it’s called</p><h3>7. Fakes: Real Behavior, Simplified Implementation</h3><h4>What Is a Fake?</h4><p>A fake:</p><ul><li>Actually stores data</li><li>Mimics real behavior</li><li>Works like a mini version of production code</li></ul><h4>Example: Fake User Repository</h4><pre>type FakeDB struct {<br> executedQueries []string<br>}<br><br>func (f *FakeDB) Exec(query string, args ...any) error {<br> f.executedQueries = append(f.executedQueries, query)<br> return nil<br>}</pre><h4>Unit Test Using Fake</h4><pre>func TestCreateUser_WithFake(t *testing.T) {<br> fakeDB := &amp;FakeDB{}<br> service := &amp;UserService{db: fakeDB}<br><br> err := service.CreateUser(&quot;Harry&quot;)<br><br>if err != nil {<br>  t.Fatalf(&quot;expected no error, got %v&quot;, err)<br> }<br><br>if len(fakeDB.executedQueries) != 1 {<br>  t.Fatal(&quot;expected one query execution&quot;)<br> }<br><br>}</pre><p>In this test case using a fake, we replace the real database with a <strong>FakeDB</strong> that behaves like a database but only stores data in memory. When CreateUser(&quot;Harry&quot;) is called, the fake database’s Exec method is invoked instead of a real SQL execution. The test first checks that <strong>no error is returned</strong>, which confirms that the function behaves correctly when the database operation succeeds. After validating the absence of errors, the test inspects the fake’s internal state—specifically the number of executed queries—to ensure that exactly <strong>one database operation was attempted</strong>. This approach verifies the behavior of the business logic without relying on an actual database, keeping the test fast, deterministic, and focused on what the function is responsible for doing.</p><h4>Fake Database That Simulates a Real Failure</h4><p>Instead of random errors, we simulate something <strong>realistic</strong>, like a <strong>duplicate entry</strong> or <strong>connection failure</strong>.</p><h4>Fake DB That Fails With Realistic Error</h4><pre>type FakeDBWithError struct {<br>  executedQuery string<br>  executedArgs  []any<br>}<br><br>func (f *FakeDBWithError) Exec(query string, args ...any) error {<br> f.executedQuery = query<br> f.executedArgs = args<br><br> // Simulating a real-world database error<br> return errors.New(&quot;duplicate entry &#39;john.doe&#39; for key &#39;users.name&#39;&quot;)<br>}</pre><h4>Test Case With Real Data</h4><pre>func TestCreateUser_WhenDBFails(t *testing.T) {<br> fakeDB := &amp;FakeDBWithError{}<br> service := &amp;UserService{db: fakeDB}<br><br> // Realistic user data<br> userName := &quot;john.doe&quot;<br><br> err := service.CreateUser(userName)<br><br> // Error must exist<br> if err == nil {<br>  t.Fatal(&quot;expected error, got nil&quot;)<br> }<br><br> // Validate error message (realistic)<br> expectedErr := &quot;duplicate entry &#39;john.doe&#39; for key &#39;users.name&#39;&quot;<br> if err.Error() != expectedErr {<br>  t.Fatalf(&quot;expected error %q, got %q&quot;, expectedErr, err.Error())<br> }<br><br> // Validate query was attempted<br> expectedQuery := &quot;INSERT INTO users(name) VALUES(?)&quot;<br> if fakeDB.executedQuery != expectedQuery {<br>  t.Fatalf(<br>   &quot;expected query %q, got %q&quot;,<br>   expectedQuery,<br>   fakeDB.executedQuery,<br>  )<br> }<br><br> // Validate real argument passed<br> if fakeDB.executedArgs[0] != userName {<br>  t.Fatalf(<br>   &quot;expected username %q, got %v&quot;,<br>   userName,<br>   fakeDB.executedArgs[0],<br>  )<br> }<br>}</pre><p>In this failure test case, a different fake (<strong>FakeDBWithError</strong>) is used to simulate a <strong>realistic database failure</strong>, such as a duplicate entry error. When CreateUser(&quot;john.doe&quot;) is executed, the fake deliberately returns an error that mimics what a real database would produce. The test asserts that an error is indeed returned, ensuring that the service <strong>does not silently ignore database failures</strong>. It may also validate the error message and confirm that the expected query and arguments were passed to the fake, proving that the failure occurred during a legitimate execution attempt.</p><h4>Official References (Must Read)</h4><ul><li>Go Testing Package<br> <a href="https://pkg.go.dev/testing">https://pkg.go.dev/testing</a></li><li>Effective Go — Testing<br> <a href="https://go.dev/doc/effective_go#testing">https://go.dev/doc/effective_go#testing</a></li><li>Go Blog: Testable Examples<br> <a href="https://go.dev/blog/examples">https://go.dev/blog/examples</a></li></ul><p><em>If your Go code is hard to test, your design is trying to tell you something — listen to it.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd1f19d9670b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>