<?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 ujjwal bansal on Medium]]></title>
        <description><![CDATA[Stories by ujjwal bansal on Medium]]></description>
        <link>https://medium.com/@ujjwal-bansal?source=rss-2329686cdb7a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aGpup1-yZmtcixAvfvFy7w.jpeg</url>
            <title>Stories by ujjwal bansal on Medium</title>
            <link>https://medium.com/@ujjwal-bansal?source=rss-2329686cdb7a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:36:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ujjwal-bansal/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[What the hack is KL Divergence?]]></title>
            <link>https://ujjwal-bansal.medium.com/what-the-hack-is-kl-divergence-54395db3a8e9?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/54395db3a8e9</guid>
            <category><![CDATA[gen-ai-for-business]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[machine-learning-ai]]></category>
            <category><![CDATA[probability]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Wed, 11 Nov 2026 04:17:33 GMT</pubDate>
            <atom:updated>2026-11-11T04:17:33.791Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*X4vi6D7tr6xpkbSSHPmd9w.jpeg" /></figure><h3>Introduction</h3><p>Before going into fancy formulas and mathematically jargons we stat with a simple question and I am assuming that you all are aware about what is Probability distributions?</p><p>Moving forward the whole point of this discussion start with a very simple question <strong>How do we measure how different two probability distributions are?</strong></p><p>Let’s start with an analogy — “Weather Forecasting”</p><p>Let us imagine we have two source of information:-</p><ol><li><strong>Reality(P)</strong> : The actual probability distribution of tomorrow’s weather, based on the historical observations and the underlying weather patterns.</li><li><strong>Our Predictions(Q) : </strong>A weather prediction model that we have created that estimates the probability of different weather conditions.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*-mloOmjbYSbRwTIGzR7yUg.png" /></figure><p>As per above chart we have some observations:</p><ul><li>Reality says sunny weather has a 60% probability and model predicts only 40%.</li><li>Reality says rain has a 10% probability and our model predicts 20%.</li></ul><p>So here we mismatch between two probability distributions.</p><p>We can quantify this mismatch with a mathmatical number which is called <strong>KL divergence</strong></p><p>Basically, it measures how much one probability distribution differs from another.</p><p>In above example:</p><ul><li>P represents the actual discribution.</li><li>Q represent our model’s approximation.</li></ul><p>Mathematically we write above as below and read this as — <strong>“KL divergence of P from Q”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/92/1*HJvpFjybUj-i9Mi4pnf9uw.png" /></figure><p>In simple words we can say — “KL divergence tells us how much inform- ation we lose when we use Q to represent P instead of using P itself.”</p><p>Three important properties:-</p><ol><li>KL divergence is always non-negative.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/125/1*E_nqcGqgZUh-XEAWHSNyAg.png" /></figure><p>2. KL divergence is zero when both distributions are identical.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/236/1*aC9lr2lKea3izhwEplMELw.png" /></figure><p>3. KL divergence is not symmetric.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/190/1*k5urXmT0CFc-4P2_Ui4a8w.png" /></figure><p>Swapping P and Q can give us a different result.</p><p>This is why we have two concepts:</p><ul><li>Forward KL</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/80/1*2Lmz9Y88kKKCchYuXj6JsQ.png" /></figure><ul><li>Reverse KL</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/81/1*R8eV0tklbxbCH0O_XgU8sA.png" /></figure><h3>Wrap-Up</h3><p>In ML, imagine we build a generative model and we have a dataset of thousand of images. These images were generated by some underlying real-world distribution — <strong>P(x). </strong>But we don’t have the exact mathematical formula for P. We build a generative model that learns an approximation — <strong>Q(x).</strong></p><p>Hence, our objective is to make the learned distribution resemble the actual data distribution.</p><blockquote><strong>Q(x) ≈ P(x)</strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54395db3a8e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Does os.ReadFile() really read the entire file into Memory in Golang? — An investigation]]></title>
            <link>https://ujjwal-bansal.medium.com/does-os-readfile-really-read-the-entire-file-into-memory-in-golang-an-investigation-463e0cf7d2ce?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/463e0cf7d2ce</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Tue, 30 Jun 2026 00:32:25 GMT</pubDate>
            <atom:updated>2026-06-30T00:32:39.192Z</atom:updated>
            <content:encoded><![CDATA[<p>Most Go tutorials say:</p><blockquote>“os.ReadFile() reads the entire file into memory.&quot;</blockquote><p>I believed it — but instead of taking it on faith, I wanted to prove it.</p><p>This led me down an interesting rabbit hole involving Linux process memory, Go’s runtime, and how go run actually works.</p><h3><strong>The Experiment</strong></h3><p>A simple Go program:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;os&quot;<br>)<br><br>func main() {<br> if len(os.Args) != 2 {<br>  fmt.Println(&quot;Please specify a path&quot;)<br>  return<br> }<br><br> b, err := os.ReadFile(os.Args[1]) // Reads the entire file into memory. Great for small files.<br> if err != nil {<br>  panic(err)<br> }<br> fmt.Println(&quot;File has been read into memory.&quot;)<br> fmt.Println(&quot;Size:&quot;, len(b), &quot;bytes&quot;)<br><br> fmt.Println(&quot;Press Enter to exit...&quot;) <br> fmt.Scanln()    // To pause the program<br><br> // fmt.Println(&quot;FirstByte: &quot;, b[0])<br>}</pre><p>The idea was simple:</p><ul><li>Read a large file</li><li>Pause the program (using fmt.Scanln())</li><li>Inspect the memory usage</li></ul><p><strong>STEP -1 — Create a large file use below command in linux,</strong></p><blockquote>$ fallocate -l 500M bigfile.txt</blockquote><p>To verify it was actually consuming disk space:</p><blockquote>OUTPUT</blockquote><blockquote>$ du -h bigfile.txt <br>501M bigfile.txt</blockquote><p><strong>STEP -2 — First Surprise</strong></p><p>Run above script using go tooling:</p><blockquote>OUTPUT</blockquote><blockquote>$ go run readingFile_bad.go bigfile.txt <br>File has been read into memory.<br>Size: 524288000 bytes<br>Press Enter to exit…</blockquote><p>While the program is paused, I inspected its memory.</p><p>Firstly, when we run <strong>go run &lt;&gt; </strong>command go actually performs below steps and for this investigation we have to investigate last part which is temporary executable which is atually reading 500MB file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*K8A5dXutLXqSeZtAJIRHnA.jpeg" /></figure><pre>$ ps -ef | grep readingFile_bad.go <br>bansiboy  227977   81701  1 23:39 pts/5    00:00:00 go run readingFile_bad.go bigfile.txt<br><br>$ ps -ef | grep go-buil<br>bansiboy  228159  227977  2 23:39 pts/5    00:00:06 /tmp/go-build2199976311/b001/exe/readingFile_bad bigfile.txt<br><br>$ ps -o pid,rss,vsz,cmd -p 228159<br>    PID   RSS    VSZ CMD<br> 228159  3116 1750908 /tmp/go-build2199976311/b001/exe/readingFile_bad bigfile.txt</pre><p>But here RSS ( Resident Set Size — actual physical RAM occupied by process) is only <strong>3.116 MB. </strong>But why????? It should be consuming <strong>500 MB… </strong>Let dig in..</p><p>Conceptually, lets try to understand what os.ReadAll() does:</p><p>Suppose there is a <strong>500 MB file</strong> stored on disk. When os.ReadFile() is called, Go allocates a <strong>500 MB backing array on the heap</strong> to store the file&#39;s contents. The operating system (through the kernel) reads the file from disk and copies its bytes into this backing array.The returned value, b []byte, is <strong>not the 500 MB data itself</strong>. It is only a <strong>slice header</strong> (or descriptor) that contains three pieces of information:</p><ul><li><strong>Pointer</strong> — points to the first byte of the backing array.</li><li><strong>Length</strong> — the number of bytes currently in the slice.</li><li><strong>Capacity</strong> — the total size of the backing array.</li></ul><p>The slice header is very small (typically 24 bytes on a 64-bit system), while the <strong>backing array occupies the actual 500 MB of memory</strong>. In other words, the slice acts as a lightweight view into the large block of memory where the file’s contents are stored.</p><pre>b │ <br>  ├── pointer ───────────────┐ <br>  ├── length = 524288000     |<br>  └── capacity = 524288000   │ <br>                             ▼ <br>                         +-----------------------+ <br>                         | 500 MB backing array  | <br>                         +-----------------------+</pre><p>Now, coming back to our discussion why RSS was ~13 MB this is because in our code after <strong>fmt.Scanln() </strong>we are not using variable <strong>b </strong>and it is dead…</p><blockquote>Once <strong>b </strong>is dead, the <strong>garbage collector </strong>is allowed to reclaim its backing array.</blockquote><p>If we uncomment the last line which the compiler cannot free b before Scanln(), because it knows you’ll access b[0] afterward and lets rerun the program.</p><pre>$ ps -ef | grep readingFile_bad.go <br>bansiboy  233498   81701  1 00:20 pts/5    00:00:00 go run readingFile_bad.go bigfile.txt<br>bansiboy  233996  216652  0 00:21 pts/7    00:00:00 grep --color=auto readingFile_bad.go<br><br>$ ps -ef | grep go-build<br>bansiboy  233685  233498 14 00:20 pts/5    00:00:03 /tmp/go-build1663734078/b001/exe/readingFile_bad bigfile.txt<br>bansiboy  234006  216652  0 00:21 pts/7    00:00:00 grep --color=auto go-build<br><br>$ ps -o pid,rss,vsz,cmd -p 233685<br>    PID   RSS    VSZ CMD<br> 233685 514680 1750844 /tmp/go-build1663734078/b001/exe/readingFile_bad bigfile.txt</pre><p>Now, if you check in above output <strong>RSS </strong>is <strong>514680 KB </strong>or <strong>514.68 MB.</strong></p><h3>Conclusion</h3><ul><li>os.ReadFile() allocates enough heap memory to hold the entire file.</li><li>A []byte is only a small slice header, the backing array stores the actual data.</li><li>go run launches a temporary executable which actually reading the file.</li></ul><p>What started as a simple question about reading a file became an exploration of the interaction between the Go runtime, Linux memory management, and operating system tools. That’s one of the things I enjoy most about systems programming: even small programs can teach deep concepts if you’re curious enough to investigate.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=463e0cf7d2ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LLM’s translator — Proxy Agent]]></title>
            <link>https://ujjwal-bansal.medium.com/llms-translator-proxy-agent-00254d7c6831?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/00254d7c6831</guid>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[llm-agent]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Tue, 26 May 2026 19:30:16 GMT</pubDate>
            <atom:updated>2026-05-26T19:30:16.808Z</atom:updated>
            <content:encoded><![CDATA[<p>Modern LLM applications are moving beyond simple text generation toward <strong>structured, automatable systems</strong>. One key concept in this transition is the <strong>Proxy Agent</strong> — a system that converts natural language into structured data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*81g-pMbaarmfR3PvsNUoQg.png" /></figure><h3>📌 Introduction</h3><p>In this article, we’ll explore how to build a <strong>basic proxy agent using LangChain’s LCEL (Lang Chain Expression Language)</strong>.</p><p>Do not be intimidated by the term <strong><em>proxy agent</em></strong>. In essence, it is a straightforward concept. A large language model (LLM) can be considered a general-purpose reasoning system that, by default, does not have any inherent understanding of the desired output format.</p><p>When a specific output format is required — such as a machine-readable structure like JSON — we must explicitly guide the model through carefully designed prompts.</p><p>In simple terms, a proxy agent is an LLM that has been guided through prompt instructions to produce structured, machine-readable output (such as JSON) instead of natural language responses.</p><pre>{<br>  &#39;intent&#39;: &#39;find_restaurants&#39;, <br>  &#39;location&#39;: &#39;near_me&#39;,  <br>  &#39;time_filter&#39;: &#39;open_now&#39;, <br>  &#39;format&#39;: &#39;list&#39;<br>}</pre><p>We can change behaviour of our LLM by changing its prompt, for instance</p><pre>Behavior      |  By changing<br>--------------|----------------<br>Chatbot       |  casual prompt<br>Proxy agent   |  structured prompt<br>Code generator|  coding prompt<br>Judge         |  evaluation prompt</pre><blockquote>The LLM <strong>doesn’t know it’s a proxy agent. </strong>We are <strong>forcing it to behave like one</strong> using prompt design.</blockquote><h3>⚙️ Code Implementation</h3><p>For instance, below prompt template is forcing our LLM to give structured data in JSON format rater than natural language</p><pre># prompts.py<br><br>from langchain_core.prompts import PromptTemplate<br><br><br>def template():<br>    template = &quot;&quot;&quot;<br>    you are a proxy agent responsible for translating natural language into structured queries.<br><br>    user input: &quot;{query}&quot;<br><br>    Return a JSON object with the following fields:<br>    - intent: The action to perform.<br>    - location: Inferred or stated location.<br>    - time_filter: Indicate if the query includes time-based constraints.<br>    - format: Response format (e.g., &#39;list&#39;).<br><br>    Reponse ONLY with JSON<br>    &quot;&quot;&quot;<br><br>    prompt = PromptTemplate(<br>        input_variables=[&quot;query&quot;],<br>        template=template,<br>    )<br><br>    return prompt</pre><p>Below piece of code to execute a LLM using Gemini-API</p><pre># llm_client.py<br><br># This returns a configured language model instance<br><br>from langchain_google_genai import ChatGoogleGenerativeAI<br><br>def get_llm():<br>    return ChatGoogleGenerativeAI(<br>        model=&quot;gemini-3.5-flash&quot;<br>    ) </pre><pre># config.py<br><br># This initializes authentication for your LLM provider (e.g., OpenAI, Azure, etc.).<br>import os<br>import getpass<br><br>def set_api_key():<br>    if &quot;GOOGLE_API_KEY&quot; not in os.environ:<br>        os.environ[&quot;GOOGLE_API_KEY&quot;] = getpass.getpass(&quot;Enter your Google AI API key: &quot;)</pre><pre># main.py<br><br>from config import set_api_key<br>from llm_client import get_llm<br><br>from prompts import template<br><br>import json<br><br># ✅ Step 1: Setup API key<br>set_api_key()<br><br># ✅ Step 2: Initialize LLM<br>llm = get_llm()<br><br># ✅ Step 3: Build LCEL chain<br>&#39;&#39;&#39;<br>  * This is the core idea of LCEL(LangChain Expression Language), and its a modern approch from Langchain<br>  * The | operator creates a pipeline:<br>    PromptTemplate → LLM<br>  * In Simple terms:<br>    template() → converts input into a prompt<br>    llm → processes the prompt and generates output<br><br>  NOTE: Here &quot;Chain&quot; basically means How data flows (pipeline)<br>&#39;&#39;&#39;<br>chain = template() | llm<br><br># ✅ Step 4: Invoke<br>response = chain.invoke({<br>    &quot;query&quot;: &quot;Find restaurants near me that are open now&quot;<br>})<br><br># print(&quot;\n----- RAW OUTPUT -----&quot;)<br># print(response.content)<br><br># ✅ Step 5: Parse JSON<br>parsed = json.loads(response.content[0][&#39;text&#39;])<br><br>print(&quot;\n----- PARSED JSON -----&quot;)<br>print(parsed)</pre><pre>OUTPUT<br><br>----- PARSED JSON -----<br>{<br>  &#39;intent&#39;: &#39;find_restaurants&#39;, <br>  &#39;location&#39;: &#39;near_me&#39;, <br>  &#39;time_filter&#39;: &#39;open_now&#39;, <br>  &#39;format&#39;: &#39;list&#39;<br>}</pre><h3>🎯 Final takeaway</h3><p>A <strong>proxy agent built using LCEL chains</strong> transforms natural language into structured data that machines can act on.</p><h3>📚 Bibliography</h3><ul><li>Book “30 Agent Every AI engineer Must Build” by Imran Ahmad.</li><li>Book “AI Agents and Applications with Lang Chain, LangGraph, and MCP” by <a href="https://www.linkedin.com/in/robertoinfante/">Roberto Infante</a>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=00254d7c6831" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the Power of Modular Arithmetic in Programming: A Deep Dive into the Multiplication…]]></title>
            <link>https://ujjwal-bansal.medium.com/understanding-the-power-of-modular-arithmetic-in-programming-a-deep-dive-into-the-multiplication-4169dc4ed277?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4169dc4ed277</guid>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Sat, 18 May 2024 18:01:54 GMT</pubDate>
            <atom:updated>2024-05-18T18:01:54.929Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding the Power of Modular Arithmetic in Programming: A Deep Dive into the Multiplication Property</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*yElECqFCR9VDjdqdU_yCEA.png" /></figure><h4><strong>Introduction</strong></h4><p>Modular arithmetic is a fundamental concept in mathematics and computer science, with applications spanning cryptography, number theory, and algorithm design. One of its most useful properties is the multiplication rule, which states that</p><p>(𝑎×𝑏)%𝑚= ( (𝑎%𝑚)×(𝑏%𝑚) )%𝑚</p><p>This article explores this property in detail, explaining its significance, mathematical foundation, and practical applications in programming.</p><h4><strong>What is Modular Arithmetic?</strong></h4><p>Modular arithmetic deals with integers and a special operation called the modulo operation. This operation finds the remainder when one number is divided by another. For example, 7%3=17%3=1 because 7 divided by 3 leaves a remainder of 1.</p><h4>The Multiplication Property in Modular Arithmetic</h4><p>One of the key properties of modular arithmetic is the multiplication rule. This rule can be stated as follows: When you multiply two numbers and then take the result modulo <em>m</em>, it is equivalent to first taking each number modulo <em>m</em>, then multiplying these results, and finally taking the result modulo 𝑚<em>m</em>. Mathematically:</p><p>(𝑎×𝑏)%𝑚= ( (𝑎%𝑚)×(𝑏%𝑚) )%𝑚</p><h4>Why This Property Holds</h4><p>To understand why this property holds, consider the mathematical definition:</p><ul><li>Let’s say a = q<em>1</em>⋅<em>m</em>+<em>r1 </em>​ and b= q2⋅<em>m</em>+<em>r2 </em>where 𝑟1​ and 𝑟2​ are the remainders when 𝑎 and 𝑏 are divided by 𝑚, respectively. Hence, 𝑟1=<em>a</em>%<em>m</em> and 𝑟2​=<em>b</em>%<em>m</em>.</li><li>When we multiply <em>a</em> and 𝑏</li></ul><p><em>a</em>×<em>b</em>=(<em>q</em>1​⋅<em>m</em>+<em>r</em>1​)×(<em>q</em>2​⋅<em>m</em>+<em>r</em>2​)</p><p>Expanding this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*B18tZdnbcCH_7ITV2dGtMw.png" /></figure><ul><li>Notice that every term except 𝑟1⋅𝑟2 contains 𝑚 as a factor, meaning they will contribute 0 to the remainder when divided by 𝑚.</li><li>Thus:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*cS7HqjYHMc8i5-m_N9HJhw.png" /></figure><ul><li>Therefore:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/316/1*OWXfconlU6WVnftO_uCtZw.png" /></figure><h4>Application</h4><ol><li>What exactly is “print it modulo 10⁹ + 7” in competitive programming web sites?</li></ol><p>This is one of the most common question scenario that uses the multiplication property of modulus.</p><p>Few thing need to be consider that are:</p><ol><li>10⁹ + 7 is a prime number.</li><li>Sometimes when answer in the form of (ans)%(10⁹+7), this means that the actual answer to a problem lies above the range of 64-bit integer which is not possible to calculate so they want only the remainder left behind our actual answer.</li></ol><h3>Example</h3><p>Suppose you are given a problem where you need to compute the sum of the first 𝑛 positive integers and print the result modulo 10⁹+7.</p><p>The sum of the first 𝑛<em>n</em> positive integers is given by:</p><p>𝑆=𝑛⋅(𝑛+1)/2</p><p>To print the result modulo ​ 10⁹+7, you would do the following:</p><ol><li>Compute the sum 𝑆.</li><li>Compute S mod (10⁹+7).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*s-v22ljQOYF7zIuW0sgJGQ.png" /></figure><p>When you see the formula:</p><p>(𝑎⋅𝑏)%𝑚=((𝑎%𝑚)⋅(𝑏%𝑚))%𝑚</p><p>This property helps prevent overflow during multiplication by ensuring that the intermediate results are kept within bounds. Here is an example:</p><p>Suppose 𝑎=1000000008 and 𝑏=1000000009, and 𝑚=1000000007.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*Vtl9SM-A_KOMZesjEG00zg.png" /></figure><h3>Conclusion</h3><p>In summary, “print it modulo 10⁹+7” is a common requirement in competitive programming to handle large numbers, prevent overflow, and take advantage of the properties of prime numbers in modular arithmetic. Always remember to apply the modulo operation at each step of your calculation to keep intermediate results manageable.</p><h3>Note</h3><p>Above content is compilation from various sources like chatgpt, qoura. I have comiple it for my personal reference.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4169dc4ed277" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Container Scheduling in Kubernetes: Resource Allocation vs. Priority Management]]></title>
            <link>https://ujjwal-bansal.medium.com/understanding-container-scheduling-in-kubernetes-resource-allocation-vs-priority-management-3f325af2f92c?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f325af2f92c</guid>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Sun, 05 May 2024 12:47:58 GMT</pubDate>
            <atom:updated>2024-05-05T12:47:58.483Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*GJuBhYCeKOcDEtwq4NT7WQ.png" /><figcaption>Figure 1: Stress program with default priority</figcaption></figure><p>In above image we are running a “stress” program is running. And two instances of “stress” are running one is consuming 93.8% CPU and other is consuming 0.0% CPU.</p><p>These two processes are started with default priority i.e, 0 as show in above figure NI column.</p><p>Even if we change the priority of above process, let say we changed priority to 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*oB_hPvxFe_HLaJMT0rgdzg.png" /><figcaption>Figure 2: Stress program with 16 priority</figcaption></figure><p>Post changing the priority to 16 which means we are lowering the priority than before. However, in Figure 2, our stress program is still using ~100% of one CPU! This situation seems contradictory at first glance because one might expect a lower-priority process to yield CPU resources to higher-priority processes.</p><p>The explanation lies in the nature of CPU scheduling and how priority is interpreted. Priority, in this context, is a relative measurement within the context of other processes competing for CPU time. If there are no other processes demanding CPU resources, even a process with a lower priority (higher nice value) can still fully utilize the CPU if it desires.</p><p>In simpler terms, priority doesn’t dictate an absolute limit on CPU usage; instead, it influences how CPU time is allocated among competing processes when there is contention for resources. If there are no competing processes or if they have lower priority, a lower-priority process can still consume as much CPU as it needs.</p><p>This behavior might seem counterintuitive, especially if one expects priority to directly limit CPU usage. However, it reflects the flexible and dynamic nature of CPU scheduling, where processes adjust their resource consumption based on system load and the priorities of other processes.</p><p>After all, if the CPU is available, shouldn’t we want our application components to be able to use it? This setup sounds resonable, it’s not suitable for our containerized applications for below two reason:-</p><ol><li><strong>Dynamic Resource Allocation</strong>: In a Kubernetes environment, containers should be able to be allocated to any host with enough resources to run them. It’s not practical for administrators to manually set the relative priority of every container, especially in a multi-tenant environment where multiple applications or teams are using the same cluster.</li><li><strong>Resource Utilization</strong>: Without some idea of how much CPU a particular container will use, Kubernetes cannot know which hosts are full and which ones have more room available. If multiple containers on the same host become busy simultaneously, they will compete for available CPU cores, leading to performance degradation for all containers on that host.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f325af2f92c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Linux’s “Everything is a File” Philosophy]]></title>
            <link>https://ujjwal-bansal.medium.com/understanding-linuxs-everything-is-a-file-philosophy-79469bef5864?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/79469bef5864</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linux-tutorial]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Tue, 19 Sep 2023 23:57:20 GMT</pubDate>
            <atom:updated>2023-09-19T23:57:20.750Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*G1z9MUP6DhGF4AcEZ3DULw.jpeg" /></figure><h3>Introduction</h3><p>The Linux operating system is renowned for its elegant and unifying concept known as the “Everything is a file” philosophy. This foundational principle lies at the heart of Linux’s design and operation, shaping the way it manages resources, interacts with hardware, and stores configuration information. Understanding this philosophy is essential for anyone seeking to grasp the inner workings of Linux and harness its power effectively. In this article, we delve into the significance of this concept, shedding light on how it simplifies resource management and contributes to the efficiency of Linux systems.</p><p>With the philosophy describe above we can say that hardware devices, system configuration settings, even processes running in linux and other resources are represented and accessed through the file system.</p><h3>Devices</h3><p>In Linux, hardware devices are represented as files in the /dev directory. For example, hard drives, USB devices, and other peripherals are exposed as device files, such as /dev/sda for the first hard drive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*-h3tNRaHntPX7kJkc0y5_Q.png" /><figcaption>/dev directory</figcaption></figure><h3>Processes</h3><p>Running processes are represented as directories and files under the /proc directory. Each process has its own directory named by its process ID (PID), and you can access various information about the process by reading files within this directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*7NJvUHhuuibYL_catB0Wog.png" /><figcaption>/proc directory</figcaption></figure><h3>Configuration</h3><p>System configuration settings are often stored in text files located in the /etc directory. For example, network configuration, user account settings, and other system configurations are typically stored as plain text files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*ztgGBWnjnU9Br-2g05sx5g.png" /><figcaption>/etc directory</figcaption></figure><p>Note: Above images of /proc, /etc and /dev directory are from my personal system, they might be different in your case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79469bef5864" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Architecting using AWS core services]]></title>
            <link>https://ujjwal-bansal.medium.com/architecting-using-aws-core-services-90720824e813?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/90720824e813</guid>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Wed, 06 Sep 2023 02:11:34 GMT</pubDate>
            <atom:updated>2023-09-06T08:21:21.368Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Amazon Web Services</em></strong> (AWS) is the world’s most comprehensive and broadly adopted cloud, offering over <strong><em>200 fully featured services</em></strong> from data centers globally.</p><p>Among these hundreds of services, we require only a select few known as “core services,” which assist us in hosting our entire tech stack. As depicted in the figure below, our tech stack finds its hosting through these essential core services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HEa0YE1NJ5vlPdYZmaDZYA.png" /><figcaption>AWS Core Services</figcaption></figure><p>Now that we have discussed core services, let’s delve into designing an architecture for deploying our application, which is composed of these core services.</p><h3>Overview</h3><ul><li>We start with a <strong><em>VPC network</em></strong> responsible for managing all aspects of our infrastructure. It’s important to note that this VPC network is region-specific.</li><li>Within this network, there are multiple subnets, each dedicated to hosting specific components of our application. These subnets are distributed across various availability zones (AZs).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_md4EUNlX3tth73n_Z90A.png" /></figure><p>We will start from hitting the URL and getting back the loading of full webpage in our browser.</p><p>First and foremost our browser sends a request to a DNS server to translate the human-readable URL (e.g., <a href="http://www.example.com/">www.rawdata.com</a>) into an IP address. In our case we are using <strong><em>Amazon Route 53</em></strong> as your DNS service, it will handle this translation and provide the IP address associated with the domain.</p><p>Once the DNS server provides the IP address, your browser now knows where to send the request. It doesn’t go directly to your AWS server at this point; it goes to the IP address obtained from the DNS resolution.</p><p>In the subsequent phase, our request reaches the AWS server, where load balancers play a pivotal role by ensures high availability and fault tolerance by distributing incoming requests across multiple application servers. Within the Virtual Private Cloud (VPC) network, our request is meticulously processed. This process may involve querying databases hosted on Amazon RDS (Relational Database Service), retrieving files from Amazon S3 (Simple Storage Service), or executing complex application logic on EC2 (Elastic Compute Cloud) instances.</p><p>Upon the completion of this journey, AWS promptly sends back the HTML response. This response is referred to as a SERVER-SIDE RESPONSE, and the request initiated by the user is termed a SERVER-SIDE REQUEST.</p><p>Lastly, once we receive the HTML response, the user generates another request, known as a CLIENT-SIDE REQUEST, often involving the retrieval of static assets such as JavaScript files, CSS, and images. To expedite the delivery of these static assets and enhance user experience, this request is directed to a content delivery network (CDN), in our case, <strong><em>AWS CloudFront</em></strong>. AWS CloudFront leverages a global network of edge locations to cache and serve static content from locations geographically closer to the user, reducing latency and improving load times. It also provides advanced security features, such as DDoS protection and web application firewall (WAF) capabilities, to safeguard against online threats.</p><h3>CONCLUSION</h3><p>This journey illustrates the power of AWS in simplifying the complexities of web development and hosting. Whether you’re a seasoned AWS practitioner or just embarking on your cloud journey, understanding these intricacies can empower you to build efficient, reliable, and performant web applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90720824e813" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Access Control in AWS: IAM Roles for Enhanced Security — Part-1]]></title>
            <link>https://ujjwal-bansal.medium.com/iam-role-and-other-related-stuff-in-aws-e8b12145e7d7?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8b12145e7d7</guid>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[aws-iam-role]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Wed, 30 Aug 2023 19:34:51 GMT</pubDate>
            <atom:updated>2024-08-29T07:02:38.217Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview</h3><p>In a given scenario, one AWS resource is attempting to access, create, or modify another AWS resource. For instance, as depicted in the image below, three EC2 instances (AWS resources) are attempting to access an S3 bucket (another AWS resource).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*zbUPzW83tJUEXCbCeUw1Rg.png" /></figure><h3><strong>IAM Role</strong></h3><p>An <strong><em>IAM Role</em></strong> is similar to the IAM user. It allows you to provide access from an AWS service resource to another service resource without the access keys. Be known that an IAM Role does not have the credentials like password, access key, and secret access key, but gains temporary credentials. It is much recommended to use IAM Role when providing access between AWS service and resources.</p><h3><strong>IAM Policy</strong></h3><p>There is another entity in AWS known as an IAM policy. An IAM policy is a JSON-formatted object in AWS. It can contain multiple statements that allow or deny access to a set of AWS resources and associated actions. These statements are referred to as Permissions Policies, which serve as access control rules. They are depicted in the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*97X7E-aXj1cntzHe.png" /><figcaption>IAM policy</figcaption></figure><p>Once policy is created we can attach it with any other IAM entity such as IAM user, IAM group or IAM role as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*QA2parMlFAj_d-I4wMU2xQ.png" /></figure><p>There are three types of IAM policies:</p><ol><li><strong>Customer Managed Policies</strong>: These are policies that are custom-written according to specific requirements.</li><li><strong>AWS Managed Policies</strong>: These are ready-made policies provided by AWS to simplify the process. These policies are designed to make our tasks more convenient.</li><li><strong>AWS Managed Job Function Policies</strong>: These policies are tailored for specific job roles. For instance, if we need to grant someone administrative privileges, or if a person in our organization is a data scientist, these policies are designed to provide the necessary permissions for their role and responsibilities.</li></ol><h3>Permission Boundary</h3><p>It imposes restrictions on the available permissions. However, the permissions specified in the policy are necessary. As shown in the illustration below, Joanne has full permissions for the IAM service, but due to the permission boundary’s “restriction on the granted permissions,” she is unable to create an IAM user, as the permission boundary does not permit this action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*5ymLzMyTWVSJOiWBHqS5CQ.png" /></figure><h3>AWS organizations and AWS SSO</h3><p>AWS IAM operates at the account level, but in an organization with numerous AWS accounts created for individuals across diverse teams, orchestrating control at the organizational level becomes crucial. The approach for overseeing all these accounts collectively is illustrated below: Account 1 and 2 fall under the Training Team, while Account 3 and 4 are under the Dev Team. All these accounts are interconnected with Account 5, which serves as the root account.</p><p>This structural setup offers several advantages. For instance, consolidated billing for Account 1, 2, 3, and 4 can be efficiently managed through Account 5, the root account. Furthermore, access management can be centralized, empowering the allocation of permissions to different organizational units via the root account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*kURnj9CDXsefBbrdbNmc4g.png" /></figure><p>Up until now, we have delved into IAM roles, IAM policies, permission boundaries, and AWS organizations. We’ve covered the concept of IAM roles, and now it’s time to delve deeper into this aspect of IAM roles.</p><h3>Usecase of IAM Role</h3><p>In the presented scenario, our application is deployed on an EC2 instance, and frequently, sensitive “secrets/keys” are incorporated within the application source code. Subsequently, the application employs these keys to authenticate various AWS services, such as S3 buckets in our case. Unfortunately, there are instances when developers store this code on open-source repositories such as GitHub, Bitbucket, and others. Regrettably, this leads to the inadvertent exposure of these keys/secrets along with the code. Consequently, unauthorized individuals might exploit these keys to gain access to our AWS services. This poses a substantial security risk that needs to be addressed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*WCM4x0b8wwCG3ixGuCkY6Q.png" /></figure><p>To address this security concern, AWS has introduced an ingenious solution using IAM roles. This involves associating an IAM role with our EC2 instance, <strong><em>which then grants temporary access keys to authenticate</em></strong> with an S3 bucket. One of the remarkable features of an <strong><em>IAM role is its ability to automatically rotate these credentials periodically</em></strong>.</p><p>This approach eliminates the need to store access keys within our application’s source code. Instead, the IAM role securely provides these keys at a designated location within the EC2 instance. When our application initiates an API call to the S3 bucket, it can seamlessly utilize the keys or secrets furnished by the IAM role.</p><p>This strategy not only enhances security but also streamlines the management of access credentials, offering a more robust and convenient way to interact with AWS resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*WCNCu0WlP8HGxvVWa8ufuw.png" /></figure><h3>Credits</h3><ul><li><a href="https://www.youtube.com/@DigitalCloudTraining">https://www.youtube.com/@DigitalCloudTraining</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8b12145e7d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How CPU registers comes into picture during process execution?]]></title>
            <link>https://ujjwal-bansal.medium.com/how-cpu-registers-comes-into-picture-during-process-execution-aece42388e0c?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/aece42388e0c</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[computer-architecture]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 21:53:54 GMT</pubDate>
            <atom:updated>2023-08-27T21:53:54.167Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*0IzO1v5ZA-i9VppTYnZ44w.png" /></figure><h3><strong>Overview</strong></h3><p>During the program execution, at what stage CPU registers came into picture while performing computation on our program?</p><p>As we all know our code is compile from plain English/human-readable language to machine code. This machine code consists primarily of CPU instructions, not data. CPU instructions are the low-level commands that the CPU can directly execute to perform operations like arithmetic, logic, and control flow.</p><p>Data, on the other hand, is typically separate from the machine code. Data includes variables, constants, arrays, and other values that the program uses during its execution. This data is not directly included in the machine code itself but is managed separately in memory.</p><p>Here’s a breakdown of the components involved:</p><ol><li><strong>Machine Code (CPU Instructions):</strong></li></ol><ul><li>These are the low-level instructions that the CPU can understand and execute.</li><li>Instructions include operations like arithmetic, logic, data movement, and control flow (branches and jumps).</li><li>They control the behavior of the program and define how data is manipulated.</li></ul><p>2. <strong>Data</strong>:</p><ul><li>Data includes variables, constants, arrays, and any other values the program needs to store or manipulate.</li><li>Data is stored in memory (RAM) and is accessed by the CPU instructions.</li><li>The machine code instructions reference memory locations where data is stored.</li></ul><p>When you compile a program, the compiler generates machine code instructions based on the source code’s logic and structure. The generated machine code includes the sequence of instructions required to perform the operations specified by the program. Data used by the program is stored separately in memory, and the machine code instructions interact with this data during execution.</p><p>In summary, machine code primarily consists of CPU instructions that define the program’s logic and operations. Data used by the program is stored in memory and is referenced by the machine code instructions during execution.</p><p><strong>Step-1:</strong> The discussion we had so far made us clear that before the program can be executed, the machine code, along with any data that the program uses (variables, arrays, etc.), is <strong>loaded into RAM</strong>. This process is managed by the operating system. <em>Loading the program into RAM allows for faster access and execution compared to running directly from secondary storage (e.g., hard drive).</em></p><p><strong>Step-2(Execution)</strong>: Once the program’s code and data are in RAM, the CPU fetches instructions from RAM, decodes them, and executes them. The program interacts with the data stored in RAM to perform calculations, process inputs, and produce outputs.</p><p><strong>Step-3 Cache Usage</strong>: The CPU also uses its cache memory to store frequently accessed instructions and data. Cache memory provides faster access than RAM, helping to reduce the time the CPU spends waiting for data.</p><p><strong>Step-4 Registers</strong>: During execution, the CPU uses its internal registers to hold data and intermediate results. <strong><em>Registers offer the fastest possible access times.</em></strong></p><p>In essence, loading the program and its data into RAM is an essential step because RAM provides a much faster access time compared to secondary storage like hard drives. This allows the CPU to execute the program more efficiently. Caching and using registers also help optimize execution speed by minimizing data access latencies.</p><h3>Bibliography</h3><ol><li>CHAT GPT</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aece42388e0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Essential Linux commands for Docker amd Kubernetes beginners — PART 1]]></title>
            <link>https://ujjwal-bansal.medium.com/essential-linux-commands-for-docker-amd-kubernetes-beginners-part-1-2126147bee30?source=rss-2329686cdb7a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2126147bee30</guid>
            <category><![CDATA[li̇nux]]></category>
            <category><![CDATA[linux-tutorial]]></category>
            <dc:creator><![CDATA[ujjwal bansal]]></dc:creator>
            <pubDate>Sun, 05 Mar 2023 14:27:08 GMT</pubDate>
            <atom:updated>2023-03-05T14:27:08.183Z</atom:updated>
            <content:encoded><![CDATA[<h3>Essential Linux commands for Docker amd Kubernetes beginners — PART 1</h3><p>Containerization has revolutionized the way we deploy and manage applications, and Linux is at the heart of this transformation. As developers, we often encounter a plethora of Linux commands that are essential to understanding and working with containers. In this series of articles, we’ll explore some of the most important Linux commands that are critical to mastering containerization and Kubernetes. In this article, we’ll start with the “<strong>ip addr</strong>” command, which is essential to understanding networking in containerized environments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*kikDaXcxg4wbNlwYkE1wkg.png" /></figure><p>Below images shows the output for the <strong>ip addr </strong>command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*U4mST50Vbd26gVcDDr79ig.png" /><figcaption>Figure 1: Network interface on linux</figcaption></figure><p>Before proceeding please go through my article on <strong>“</strong><a href="https://ujjwal-bansal.medium.com/what-are-network-interface-in-linux-e5948edbf655"><strong>network interface</strong></a><strong>”.</strong></p><p>Below I will try to elaborate the information which we can get from network interface given in Figure-1.</p><ol><li>We are having 3 network interfaces installed on our system with name <strong>lo, eth0, docker0</strong>.</li></ol><p>Let us consider only one interface among 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*wgGWoFXDxtGDVCr2ljssfw.png" /><figcaption>Figure 2: loopback interface</figcaption></figure><p>The <strong>first line</strong> shows the interface name lo, along with its current state and configuration information. Here, it shows that the interface is up and has no packet queueing discipline configured.</p><p>The <strong>second line</strong> shows the MAC address of the interface, which is a unique identifier for the network interface. Since this is the loopback interface, which doesn’t actually connect to any physical network, its MAC address is always all zeroes.</p><p>The <strong>third line</strong> inet 127.0.0.1/8 scope host lo indicates that the IP address associated with the interface is 127.0.0.1. This IP address is reserved for the loopback interface and is used by the local machine to communicate with itself.</p><p>Finally, the last two lines show the <strong>lifetime of the address</strong>, which is set to “forever” in this case. This means that the address will not expire and will always be available for use on this network interface.</p><h3><strong>Conclusion</strong></h3><p>Through above information we can say we are having a decent idea about the network interface present in Linux. I will be sharing more article on these kind of linux commands in near future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2126147bee30" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>