<?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 ANUPAM GOGOI on Medium]]></title>
        <description><![CDATA[Stories by ANUPAM GOGOI on Medium]]></description>
        <link>https://medium.com/@anupamgogoi?source=rss-c738a6af2285------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*R0GBc_T-CAADZ-8RgCFitA.jpeg</url>
            <title>Stories by ANUPAM GOGOI on Medium</title>
            <link>https://medium.com/@anupamgogoi?source=rss-c738a6af2285------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 17:17:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anupamgogoi/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[Multi-Head Attention, Step by Step: The Mechanism Behind Modern AI]]></title>
            <link>https://anupamgogoi.medium.com/multi-head-attention-step-by-step-the-mechanism-behind-modern-ai-7b3f64c564f0?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b3f64c564f0</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Tue, 01 Sep 2026 00:01:12 GMT</pubDate>
            <atom:updated>2026-09-01T00:01:12.353Z</atom:updated>
            <content:encoded><![CDATA[<h3>Multi-Head Attention, Step by Step: The Mechanism Behind Modern AI</h3><p><em>A plain-English walkthrough of the idea that powers every transformer, from GPT to BERT to the model you’re probably talking to right now.</em></p><h3>TL;DR</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9dvB5asyPQSTtuwo4aPqrw.png" /></figure><p>If you’ve ever wondered what’s actually happening inside a transformer when it “pays attention” to different words in a sentence, this is the mechanism. Multi-head attention sounds intimidating, but it’s really just a clever bit of matrix multiplication repeated a handful of times in parallel. Let’s break it down.</p><h3>The Big Picture</h3><p>At a high level, multi-head attention takes a sequence of token representations and transforms them into a new sequence — one where each token’s representation now incorporates relevant context from the other tokens around it. The “multi-head” part just means the model does this several times simultaneously, each time looking for different kinds of relationships.</p><p>Here’s the shortest possible summary:</p><p><strong>Input → Q, K, V (linear) → Split into heads → Attention per head → Concatenate heads → Output projection → Final output Z</strong></p><p>Now let’s walk through each stage.</p><h3>Step 1: Input Embeddings</h3><p>Everything starts with X, a tensor of shape [batch, tokens, d_model]. In plain terms: for every sequence in your batch, you have a row per token, and each row is a vector of length d_model (a common size might be 512 or 768) representing that token&#39;s meaning so far.</p><h3>Step 2: Create Q, K, and V</h3><p>This is where the “attention” part begins. X is passed through three separate learned linear layers, producing three new matrices:</p><ul><li><strong>Q (Query)</strong> — what this token is “looking for”</li><li><strong>K (Key)</strong> — what this token “contains” or advertises about itself</li><li><strong>V (Value)</strong> — the actual information this token carries</li></ul><p>A useful analogy: imagine a library. Q is the question you bring to the librarian. K is the label on the spine of every book. V is the actual content inside the book. Attention is the process of matching your question against every label, then pulling out the content from the books that match best.</p><h3>Step 3: Split Into Multiple Heads</h3><p>Instead of doing this matching once, the model splits Q, K, and V into h smaller chunks — one per &quot;head.&quot; Each head operates in a reduced dimension d_k = d_model / h.</p><p>Why bother? Because a single attention computation can only learn one kind of relationship at a time. With multiple heads working in parallel, one head might learn to track grammatical structure (subject-verb agreement, say), another might track long-range topic relevance, and another might specialize in something no human would even think to name. Splitting the work lets the model build a much richer, multi-faceted understanding of the sequence.</p><h3>Step 4: Compute Attention Scores</h3><p>For each head, the model computes:</p><p><strong>Scores = (Q · Kᵀ) / √d_k</strong></p><p>This produces a [tokens, tokens] matrix where each entry represents how relevant one token is to another. The division by √d_k is a scaling trick — without it, the dot products can grow large as dimensionality increases, which would push the softmax step into regions with vanishing gradients. It&#39;s a small detail, but it&#39;s what makes training stable.</p><h3>Step 5: Mask + Softmax</h3><p>Two things happen here:</p><ol><li><strong>Masking.</strong> In decoder-style models (like GPT), a token shouldn’t be allowed to “see” future tokens — otherwise it could cheat by peeking ahead. A causal mask blocks those positions before the next step.</li><li><strong>Softmax.</strong> The remaining scores are normalized row by row so that each row sums to 1. These become the <em>attention weights</em> — essentially a probability distribution over “how much attention should this token pay to every other token.”</li></ol><h3>Step 6: Mix Information From V</h3><p>Now the attention weights are multiplied against V:</p><p><strong>Z_i = (Attention weights) × V_i</strong></p><p>This is the payoff step: each token’s new representation becomes a weighted blend of the <em>values</em> of every token it attends to, according to the weights just computed. This is literally what “paying attention” means mathematically — a weighted average, where the weights are learned rather than fixed.</p><h3>Step 7: Concatenate All Heads</h3><p>Each head produces its own output Z_i, shaped [batch, tokens, d_k]. These are concatenated along the last dimension, reconstructing a tensor of shape [batch, tokens, h × d_k] — which conveniently equals [batch, tokens, d_model] again, since d_k = d_model / h.</p><h3>Step 8: Output Projection</h3><p>Finally, the concatenated heads pass through one more learned linear layer, W_O. This blends the information from all the heads into a single unified representation, producing the final output Z — same shape as the original input, but now infused with contextual information from across the whole sequence.</p><h3>Why Bother With Multiple Heads At All?</h3><p>It comes down to three things:</p><ul><li><strong>Capturing different relationships.</strong> Language (and data generally) has many kinds of structure — syntax, semantics, position, coreference — and a single attention mechanism struggles to model all of them simultaneously.</li><li><strong>Looking at different positions.</strong> Different heads can specialize in short-range versus long-range dependencies.</li><li><strong>Building a richer representation.</strong> By combining several specialized “views” of the sequence, the model ends up with a more expressive, more useful representation than any single attention head could produce alone.</li></ul><h3>The Takeaway</h3><p>Multi-head attention isn’t magic — it’s Q, K, and V matrices, a scaled dot product, a softmax, and a concatenation, repeated in parallel a handful of times. But that simple recipe, stacked across many layers, is what allows transformers to model relationships across an entire sequence at once — which is a big part of why they replaced recurrent networks as the backbone of modern AI.</p><p>Once you can trace the shapes through each of these eight steps — [batch, tokens, d_model] in, [batch, tokens, d_model] out — the rest of the transformer architecture (layer norms, feed-forward blocks, residual connections) is comparatively easy to layer on top.</p><h3>References</h3><p><a href="https://sebastianraschka.com/llms-from-scratch/?source=post_page-----fcfdbf36300e---------------------------------------">Build a Large Language Model (From Scratch)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b3f64c564f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Self-Attention with Query, Key, and Value — Explained Simply]]></title>
            <link>https://anupamgogoi.medium.com/self-attention-with-query-key-and-value-explained-simply-fcfdbf36300e?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/fcfdbf36300e</guid>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Thu, 27 Aug 2026 00:46:52 GMT</pubDate>
            <atom:updated>2026-08-27T00:46:52.567Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>TL;DR</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*avARJM56JTz6-89wgdQPYw.png" /></figure><p>Self-attention is one of the core ideas behind Transformer-based language models such as GPT.</p><p>At a high level, self-attention helps a model answer a simple question:</p><blockquote><strong><em>For the word I am currently looking at, which other words in the sentence are important?</em></strong></blockquote><p>To understand the mechanism, let’s use the sentence:</p><blockquote><strong><em>“The universe started with a big bang.”</em></strong></blockquote><p>We will keep the explanation simple and focus on the main flow without introducing scaled dot-product attention yet.</p><h3>Step 1: Convert the Sentence into Embeddings</h3><p>A Transformer cannot work directly with words. Each token is first converted into a numerical vector called an <strong>embedding</strong>.</p><p>For our sentence:</p><pre>The<br>universe<br>started<br>with<br>a<br>big<br>bang</pre><p>we have seven tokens.</p><p>Conceptually:</p><pre>The       → x¹<br>universe  → x²<br>started   → x³<br>with      → x⁴<br>a         → x⁵<br>big       → x⁶<br>bang      → x⁷</pre><p>In the diagram, each token is represented using three numbers, so the input matrix has shape:</p><pre>[7, 3]</pre><p>This simply means:</p><pre>7 tokens<br>×<br>3 values per token</pre><p>These embeddings are the starting point of the self-attention mechanism.</p><h3>Step 2: Create Query, Key, and Value Vectors</h3><p>The original embedding of each token is transformed into three different vectors:</p><pre>Query<br>Key<br>Value</pre><p>This is done using three trainable weight matrices:</p><pre>Wq<br>Wk<br>Wv</pre><p>Mathematically:</p><pre>Q = XWq<br>K = XWk<br>V = XWv</pre><p>A useful way to remember the three roles is:</p><pre>Query = What am I looking for?</pre><pre>Key   = What kind of information do I match?</pre><pre>Value = What information can I provide?</pre><p>The same token therefore has <strong>three different representations</strong> depending on the role it is playing.</p><p>For example:</p><pre>&quot;started&quot;<br>     ↓<br>original embedding<br>     ↓<br> ┌───────┬───────┬───────┐<br> ↓       ↓       ↓<br>Query    Key     Value</pre><p>This separation allows the model to learn much more flexible relationships between tokens.</p><h3>Step 3: Compute Attention Scores</h3><p>Now suppose we want to build a better representation of:</p><blockquote><strong><em>“started”</em></strong></blockquote><p>The model uses the Query vector for &quot;started&quot; and compares it with the Key vector of every token.</p><p>Conceptually:</p><pre>Query(&quot;started&quot;) · Key(&quot;The&quot;)<br>Query(&quot;started&quot;) · Key(&quot;universe&quot;)<br>Query(&quot;started&quot;) · Key(&quot;started&quot;)<br>Query(&quot;started&quot;) · Key(&quot;with&quot;)<br>Query(&quot;started&quot;) · Key(&quot;a&quot;)<br>Query(&quot;started&quot;) · Key(&quot;big&quot;)<br>Query(&quot;started&quot;) · Key(&quot;bang&quot;)</pre><p>Each comparison uses a <strong>dot product</strong> and produces one number.</p><p>These numbers are called <strong>attention scores</strong>.</p><p>The purpose of the score is simple:</p><blockquote><strong><em>How strongly does this key match what the query is looking for?</em></strong></blockquote><p>Higher scores indicate stronger matches.</p><p>At this point, the model has a raw relevance score for every token relative to &quot;started&quot;.</p><h3>Step 4: Convert the Scores into Attention Weights</h3><p>The raw attention scores are not yet easy to use directly.</p><p>So the model passes them through <strong>softmax</strong>.</p><p>Softmax converts the raw values into normalized <strong>attention weights</strong>.</p><p>These weights have two useful properties:</p><blockquote>they are positive</blockquote><blockquote>they sum to 1</blockquote><p>This makes them easier to interpret as relative importance values.</p><p>Conceptually:</p><pre>The       → some attention<br>universe  → more attention<br>started   → more attention<br>with      → less attention<br>a         → less attention<br>big       → some attention<br>bang      → more attention</pre><p>The key point is:</p><blockquote><strong><em>The model does not treat all words equally.</em></strong></blockquote><p>Some tokens contribute more information than others.</p><h3>Step 5: Create the Context Vector</h3><p>Once the model knows the attention weights, it uses the <strong>Value vectors</strong>.</p><p>Each Value vector is multiplied by its corresponding attention weight.</p><p>Conceptually:</p><pre>weight₁ × Value(&quot;The&quot;)<br>+<br>weight₂ × Value(&quot;universe&quot;)<br>+<br>weight₃ × Value(&quot;started&quot;)<br>+<br>weight₄ × Value(&quot;with&quot;)<br>+<br>weight₅ × Value(&quot;a&quot;)<br>+<br>weight₆ × Value(&quot;big&quot;)<br>+<br>weight₇ × Value(&quot;bang&quot;)</pre><p>All of these weighted vectors are added together.</p><p>The result is a new vector called the <strong>context vector</strong>.</p><p>For &quot;started&quot;, this context vector is written as:</p><pre>z³</pre><p>The important idea is that z³ is no longer just a representation of the isolated token &quot;started&quot;.</p><p>Instead, it represents:</p><blockquote><strong><em>“started” in the context of the entire sentence.</em></strong></blockquote><p>That is the main purpose of self-attention.</p><h3>Step 6: Repeat the Process for Every Token</h3><p>We do not calculate a context vector only for &quot;started&quot;.Every token gets a turn as the Query.</p><p>So we eventually produce:</p><pre>The       → z¹<br>universe  → z²<br>started   → z³<br>with      → z⁴<br>a         → z⁵<br>big       → z⁶<br>bang      → z⁷</pre><p>Each output vector contains information from the other relevant tokens in the sentence.</p><p>This means that after self-attention, every token has become <strong>context-aware</strong>.</p><h3>Why Query, Key, and Value Matter</h3><p>The easiest way to understand the roles is through a simple analogy.</p><p>Imagine each token is a person in a meeting.</p><p>The <strong>Query</strong> says:</p><blockquote><em>“What information am I looking for?”</em></blockquote><p>The <strong>Key</strong> says:</p><blockquote><em>“Here is what kind of information I can match.”</em></blockquote><p>The <strong>Value</strong> says:</p><blockquote><em>“If I am considered relevant, here is the information I can contribute.”</em></blockquote><p>The Query is compared with the Keys to determine importance. The resulting attention weights are then used to mix the Values.So:</p><pre>Query + Key<br>    ↓<br>decide importance</pre><pre>Attention weights + Value<br>    ↓<br>collect information</pre><p>That distinction is one of the most important ideas in Transformer self-attention.</p><h3>The Whole Process</h3><p>The complete flow is:</p><pre>Input sentence<br>      ↓<br>Token embeddings<br>      ↓<br>Create Query, Key, and Value vectors<br>      ↓<br>Compare Queries with Keys<br>      ↓<br>Attention scores<br>      ↓<br>Softmax<br>      ↓<br>Attention weights<br>      ↓<br>Weighted combination of Values<br>      ↓<br>Context vectors</pre><p>Or even more simply:</p><pre>Words<br> ↓<br>Embeddings<br> ↓<br>Q / K / V<br> ↓<br>Who is important?<br> ↓<br>How important?<br> ↓<br>Mix their information<br> ↓<br>Context-aware representations</pre><h3>Final Takeaway</h3><p>The main idea behind self-attention is context.</p><p>A token starts with a basic embedding that represents the token itself.</p><p>Self-attention then allows that token to look at the other tokens, determine which ones are relevant, and combine useful information from them.</p><p>The three roles make this possible:</p><blockquote><strong><em>Query asks what is needed.<br>Key helps determine what matches.<br>Value provides the information.</em></strong></blockquote><p>The resulting context vector gives the model a richer representation of each token based on the sentence around it.</p><p>That is the foundation of the attention mechanism used inside modern Transformer-based Large Language Models.</p><h3>References</h3><p><a href="https://sebastianraschka.com/llms-from-scratch/?source=post_page-----2b669d6e9aba---------------------------------------">Build a Large Language Model (From Scratch)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcfdbf36300e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Self-Attention Explained Step by Step]]></title>
            <link>https://anupamgogoi.medium.com/self-attention-explained-step-by-step-2b669d6e9aba?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b669d6e9aba</guid>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Mon, 24 Aug 2026 23:49:06 GMT</pubDate>
            <atom:updated>2026-08-24T23:49:06.076Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2HYCZea4L4iWI71ct1jJcQ.png" /></figure><p>Self-attention helps a language model understand a word by looking at the other words in the same sentence and deciding which ones are important.</p><p>We will use the sentence:</p><blockquote><strong><em>“The universe started with a big-bang.”</em></strong></blockquote><p>and focus on the word:</p><blockquote><strong><em>“started”</em></strong></blockquote><h3>Step 0: Convert the Words into Embeddings</h3><p>The model cannot directly work with words, so each word is first converted into a vector of numbers called an <strong>embedding</strong>.</p><p>For our example:</p><pre>The       → [0.20, 0.10, 0.70]<br>universe  → [0.60, 0.80, 0.10]<br>started   → [0.30, 0.70, 0.20]<br>with      → [0.10, 0.40, 0.60]<br>a         → [0.50, 0.20, 0.30]<br>big-bang  → [0.90, 0.10, 0.40]</pre><p>These numbers are only a small example. Real language models use vectors with hundreds or thousands of dimensions.</p><p>At this point, each word has its own numerical representation.</p><p>The important thing to remember is:</p><blockquote><strong><em>An embedding represents a token in a form that the neural network can work with.</em></strong></blockquote><h3>Step 1: Choose a Query</h3><p>Now we choose the word we want to understand better.</p><p>In this example, that word is:</p><blockquote><strong><em>“started”</em></strong></blockquote><p>In self-attention terminology, this word becomes the <strong>query</strong>.</p><p>So:</p><pre>Query = &quot;started&quot;</pre><p>You can think of the query as the word asking:</p><blockquote><strong><em>Which other words in this sentence are useful for understanding me?</em></strong></blockquote><p>The model will now compare &quot;started&quot; with every word in the sentence.</p><h3>Step 2: Compute the Attention Scores</h3><p>The model compares &quot;started&quot; with:</p><pre>The<br>universe<br>started<br>with<br>a<br>big-bang</pre><p>Each comparison produces a number called an <strong>attention score</strong>.</p><p>In the diagram, the scores are:</p><pre>The       → 0.39<br>universe  → 0.73<br>started   → 0.73<br>with      → 0.37<br>a         → 0.29<br>big-bang  → 0.49</pre><p>In this simplified example, a higher score means that the word is more strongly related to the query.</p><p>So &quot;universe&quot; has a stronger relationship with &quot;started&quot; than &quot;a&quot; does.</p><p>The model calculates these scores using a mathematical operation called a <strong>dot product</strong>.</p><p>But the main idea is more important than the math:</p><blockquote><strong><em>The model is measuring how relevant every word is to the word “started.”</em></strong></blockquote><p>These values are still raw scores. They are not yet the final attention values.</p><h3>Step 3: Convert the Scores into Attention Weights</h3><p>Next, the model passes the raw scores through a function called <strong>softmax</strong>.</p><p>Softmax converts the attention scores into normalized values called <strong>attention weights</strong>.</p><p>The diagram shows:</p><pre>The       → 0.106<br>universe  → 0.198<br>started   → 0.198<br>with      → 0.100<br>a         → 0.090<br>big-bang  → 0.128</pre><p>The useful thing about these weights is that they add up to approximately:</p><pre>1.0</pre><p>We can loosely think of them like percentages:</p><pre>The       → 10.6%<br>universe  → 19.8%<br>started   → 19.8%<br>with      → 10.0%<br>a         → 9.0%<br>big-bang  → 12.8%</pre><p>This tells us how much attention &quot;started&quot; should give to each word.</p><p>For example, &quot;universe&quot; gets more attention than &quot;a&quot;.</p><p>The simple idea is:</p><blockquote><strong><em>Attention weights tell the model how important each word is for the current query.</em></strong></blockquote><h3>Step 4: Create the Context Vector</h3><p>Now we have two pieces of information for every word:</p><pre>word embedding<br>+<br>attention weight</pre><p>The model uses the attention weight to control how much of each word’s information should be included.</p><blockquote>A word with a larger weight contributes more.</blockquote><blockquote>A word with a smaller weight contributes less.</blockquote><p>You can think of it like mixing ingredients.</p><p>For &quot;started&quot;, the model takes:</p><pre>some information from &quot;The&quot;<br>more information from &quot;universe&quot;<br>more information from &quot;started&quot;<br>some information from &quot;with&quot;<br>less information from &quot;a&quot;<br>some information from &quot;big-bang&quot;</pre><p>It combines all of this information into one new vector:</p><pre>z³ = [0.369, 0.379, 0.272]</pre><p>This is called the <strong>context vector</strong> for &quot;started&quot;.</p><p>The original embedding represented:</p><blockquote><strong><em>“started”</em></strong></blockquote><p>The context vector represents something closer to:</p><blockquote><strong><em>“started in the context of the universe and the big-bang.”</em></strong></blockquote><p>That is the main goal of self-attention.</p><h3>Step 5: Use the Context-Aware Representation</h3><p>The context vector is then passed to the later layers of the Transformer.</p><p>Instead of working only with the original representation of&quot;started&quot;, the model now has a representation that contains information from the surrounding sentence.</p><p>This matters because the same word can have different meanings in different contexts.</p><p>Consider:</p><blockquote><strong><em>The car started.</em></strong></blockquote><blockquote><strong><em>The meeting started.</em></strong></blockquote><blockquote><strong><em>The universe started with a big-bang.</em></strong></blockquote><p>The word &quot;started&quot; is the same, but its surroundings are different.</p><p>Self-attention allows the model to produce a different context-aware representation in each case.</p><p>So the complete process is:</p><pre>Step 0<br>Words → Embeddings        <br>      ↓<br>Step 1<br>Choose &quot;started&quot; as the Query       <br>      ↓<br>Step 2<br>Compare &quot;started&quot; with all words        <br>      ↓<br>Step 3<br>Convert scores into Attention Weight<br>      ↓<br>Step 4<br>Mix information according to those weights        <br>      ↓<br>Step 5<br>Get a Context-Aware Representation</pre><p>The simplest way to remember self-attention is:</p><blockquote><strong><em>A word looks at the other words in the sentence, decides which ones matter, and uses their information to understand itself better.</em></strong></blockquote><h3>Reference</h3><p><a href="https://sebastianraschka.com/llms-from-scratch/">Build a Large Language Model (From Scratch)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b669d6e9aba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding the Embedding Diagram]]></title>
            <link>https://anupamgogoi.medium.com/understanding-the-embedding-diagram-c889988a8122?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/c889988a8122</guid>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Fri, 21 Aug 2026 01:24:56 GMT</pubDate>
            <atom:updated>2026-08-21T01:24:56.203Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dPq7TvBK4ivYyj18xMFEQg.png" /></figure><p>The diagram shows the complete process of converting token IDs into the input embeddings that are passed to a GPT-style Transformer.</p><p>The example uses deliberately small dimensions:</p><pre>vocabulary_size = 6<br>embedding_dimension = 3<br>batch_size = 2<br>max_length = 4</pre><p>There are six possible token IDs in the vocabulary. Each token is represented by a three-dimensional vector. The batch contains two samples, and each sample contains four tokens.</p><p>The first part of the diagram shows the token IDs produced by the <strong>tokenizer</strong>:</p><pre>Sample 1: [2, 3, 1, 4]<br>Sample 2: [0, 5, 2, 3]</pre><p>Because there are <strong>two samples</strong> and <strong>four tokens in each</strong> sample, the shape of the input is:</p><pre>[2, 4]</pre><p>The next step is the token <strong>embedding layer.</strong> Since the <strong>vocabulary</strong> contains <strong>six tokens</strong> and each token is represented using <strong>three numbers</strong>, the embedding matrix has the shape:</p><pre>[6, 3]</pre><p>Each row corresponds to one token ID. For example:</p><pre>Token 0 → [ 0.1,  0.2,  0.3]<br>Token 1 → [ 0.4, -0.1,  0.2]<br>Token 2 → [ 0.0,  0.5, -0.3]<br>Token 3 → [-0.2,  0.3,  0.8]<br>Token 4 → [ 0.6, -0.4,  0.1]<br>Token 5 → [-0.3,  0.2, -0.5]</pre><p>When the token IDs are passed through the embedding layer, PyTorch performs a lookup. For Sample 1:</p><pre>[2, 3, 1, 4]</pre><p>the embedding layer retrieves:</p><pre>Token 2 → [ 0.0,  0.5, -0.3]<br>Token 3 → [-0.2,  0.3,  0.8]<br>Token 1 → [ 0.4, -0.1,  0.2]<br>Token 4 → [ 0.6, -0.4,  0.1]</pre><p>The same lookup happens for Sample 2.</p><p>After this step, the tensor has shape:</p><pre>[2, 4, 3]</pre><p>This means there are two samples, four tokens in each sample, and three embedding values for every token.</p><p>The diagram then introduces positional embeddings. Token embeddings tell the model which token it is, but they do not tell the model where that token appears in the sequence.</p><p>Because max_length = 4, there are four positions:</p><pre>Position 0<br>Position 1<br>Position 2<br>Position 3</pre><p>The positional embedding layer also uses an embedding dimension of three, so its matrix has shape:</p><pre>[4, 3]</pre><p>For example:</p><pre>Position 0 → [0.01, 0.02, 0.03]<br>Position 1 → [0.04, 0.05, 0.06]<br>Position 2 → [0.07, 0.08, 0.09]<br>Position 3 → [0.10, 0.11, 0.12]</pre><p>These positional vectors are shared across all samples in the batch. Every token at position 0 receives the position-0 embedding, every token at position 1 receives the position-1 embedding, and so on.</p><p>The final step in the diagram adds the token embedding and positional embedding together.</p><p>For example, Sample 1 starts with token ID 2 at position 0.</p><p>Its token embedding is:</p><pre>[0.00, 0.50, -0.30]</pre><p>The positional embedding for position 0 is:</p><pre>[0.01, 0.02, 0.03]</pre><p>They are added element by element:</p><pre>[0.00, 0.50, -0.30]<br>+<br>[0.01, 0.02,  0.03]<br>=<br>[0.01, 0.52, -0.27]</pre><p>For token ID 3 at position 1:</p><pre>Token embedding:<br>[-0.20, 0.30, 0.80]</pre><pre>Position embedding:<br>[ 0.04, 0.05, 0.06]</pre><pre>Final input embedding:<br>[-0.16, 0.35, 0.86]</pre><p>The same process is performed for every token in every sample.</p><p>The resulting tensor still has shape:</p><pre>[2, 4, 3]</pre><p>The dimensions have not changed because two three-dimensional vectors are being added together. The result is still a three-dimensional vector.</p><p>What has changed is the information contained in that vector. Before the addition, the vector represented only the token. After the addition, it represents both the token and its location.</p><p>So the diagram can be summarized as:</p><pre>Token ID<br>   ↓<br>Token embedding<br>   +<br>Positional embedding<br>   ↓<br>Input embedding<br>   ↓<br>Transformer</pre><p>The key idea is that the token embedding tells GPT <strong>what the token is</strong>, while the positional embedding tells GPT <strong>where the token appears in the sequence</strong>. Their sum becomes the actual input passed to the Transformer.</p><p>A useful way to remember the entire concept is:</p><blockquote><strong><em>Token embeddings represent what a token is. Positional embeddings represent where it is. GPT combines both before processing the sequence.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c889988a8122" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Reusable Datadog Monitoring Infrastructure with Terraform]]></title>
            <link>https://anupamgogoi.medium.com/building-reusable-datadog-monitoring-infrastructure-with-terraform-6f3f3bd951ee?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f3f3bd951ee</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[datadog]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Sun, 01 Feb 2026 20:42:17 GMT</pubDate>
            <atom:updated>2026-02-01T20:42:17.023Z</atom:updated>
            <content:encoded><![CDATA[<p>Managing application health checks and SLOs (Service Level Objectives) across multiple APIs can quickly become a maintenance nightmare. In this article, I’ll walk you through how to create a modular, reusable Terraform configuration for Datadog synthetic tests and SLOs.</p><h3>The Problem</h3><p>Imagine you have multiple APIs that need health monitoring. For each API, you need:</p><p>- A synthetic health check that runs periodically</p><p>- An SLO to track availability over time</p><p>- Consistent tagging and alerting</p><p>Manually creating these in the Datadog UI is error-prone and doesn’t scale. Copy-pasting Terraform code for each API leads to duplication and drift.</p><h3>TL;DR</h3><p>You can go directly to the <a href="https://github.com/cloudintegrator/datadog-terraform/tree/main">repo</a></p><h3><strong>The Solution: Modular Terraform</strong></h3><p>We’ll create a modular Terraform structure where each API has its own module, but shares a common pattern. This gives us:</p><ul><li><strong>Reusability</strong>: Add a new API by copying a module and updating a few values</li><li>-<strong>Consistency</strong>: All APIs follow the same monitoring pattern</li><li><strong>Maintainability</strong>: Update the pattern once, apply everywhere</li></ul><h3><strong>Project Structure</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JFw2R2c8WP7Ih7YBg5s0Vg.png" /></figure><h4><strong>Step 1: Configure the Provider</strong></h4><p>First, set up the Datadog provider in `provider.tf`:</p><pre>terraform {<br>  required_providers {<br>    datadog = {<br>      source  = &quot;DataDog/datadog&quot;<br>      version = &quot;~&gt; 3.39&quot;<br>    }<br>  }<br>}<br><br>provider &quot;datadog&quot; {<br>  api_key  = var.datadog_api_key<br>  app_key  = var.datadog_app_key<br>  api_url  = &quot;https://api.us5.datadoghq.com/&quot;<br>  validate = false<br>}</pre><h4><strong>Step 2: Define Root Variables</strong></h4><p>In `variables.tf`, define the structure for API credentials:</p><pre>variable &quot;datadog_api_key&quot; {<br>  description = &quot;Datadog API key&quot;<br>  type        = string<br>  sensitive   = true<br>}<br><br>variable &quot;datadog_app_key&quot; {<br>  description = &quot;Datadog Application key&quot;<br>  type        = string<br>  sensitive   = true<br>}<br><br>variable &quot;api_credentials&quot; {<br>  description = &quot;API credentials map keyed by environment and API name&quot;<br>  type = map(map(object({<br>    client_id              = string<br>    client_secret          = string<br>    url                    = string<br>    health_check_test_name = string<br>    slo_name               = string<br>  })))<br>  sensitive = true<br>}</pre><p>This structure allows you to organize credentials by environment (qa, staging, prod) and API name.</p><h4><strong>Step 3: Create the API Module</strong></h4><p>Each API module contains four key files:</p><p><strong>health-check.tf</strong></p><pre>resource &quot;datadog_synthetics_test&quot; &quot;breweries_api_health_check&quot; {<br>  name      = var.health_check_test_name<br>  type      = &quot;api&quot;<br>  subtype   = &quot;http&quot;<br>  status    = &quot;live&quot;<br>  message   = &quot;Health check failed for breweries-api-q1&quot;<br>  locations = [&quot;aws:us-east-1&quot;, &quot;aws:us-west-2&quot;, &quot;aws:eu-west-1&quot;]<br><br>  tags = [<br>    &quot;env:qa&quot;,<br>    &quot;service:breweries-api&quot;,<br>    &quot;team:Mule&quot;<br>  ]<br><br>  request_definition {<br>    method = &quot;GET&quot;<br>    url    = var.url<br>  }<br><br>  request_basicauth {<br>    username = var.client_id<br>    password = var.client_secret<br>  }<br><br>  assertion {<br>    type     = &quot;statusCode&quot;<br>    operator = &quot;is&quot;<br>    target   = &quot;200&quot;<br>  }<br><br>  assertion {<br>    type     = &quot;responseTime&quot;<br>    operator = &quot;lessThan&quot;<br>    target   = &quot;5000&quot;<br>  }<br><br>  options_list {<br>    tick_every = 300  # Check every 5 minutes<br><br>    retry {<br>      count    = 2<br>      interval = 300<br>    }<br><br>    monitor_options {<br>      renotify_interval = 120<br>    }<br>  }<br>}</pre><p><strong>slo.tf</strong></p><pre>resource &quot;datadog_service_level_objective&quot; &quot;breweries_api_health_check_slo&quot; {<br>  name        = var.slo_name<br>  type        = &quot;monitor&quot;<br>  description = &quot;SLO tracking the availability of breweries-api health check endpoint&quot;<br><br>  monitor_ids = [datadog_synthetics_test.breweries_api_health_check.monitor_id]<br><br>  thresholds {<br>    timeframe = &quot;7d&quot;<br>    target    = 99.9<br>    warning   = 99.95<br>  }<br><br>  thresholds {<br>    timeframe = &quot;30d&quot;<br>    target    = 99.9<br>    warning   = 99.95<br>  }<br><br>  thresholds {<br>    timeframe = &quot;90d&quot;<br>    target    = 99.9<br>    warning   = 99.95<br>  }<br><br>  tags = [<br>    &quot;env:qa&quot;,<br>    &quot;service:breweries-api&quot;,<br>    &quot;team:Mule&quot;<br>  ]<br>}</pre><p><strong>variables.tf (module)</strong></p><pre>variable &quot;client_id&quot; {<br>  description = &quot;Client ID for basic auth&quot;<br>  type        = string<br>  sensitive   = true<br>}<br><br>variable &quot;client_secret&quot; {<br>  description = &quot;Client secret for basic auth&quot;<br>  type        = string<br>  sensitive   = true<br>}<br><br>variable &quot;url&quot; {<br>  description = &quot;Health check URL&quot;<br>  type        = string<br>  sensitive   = true<br>}<br><br>variable &quot;health_check_test_name&quot; {<br>  description = &quot;Name of the health check test&quot;<br>  type        = string<br>}<br><br>variable &quot;slo_name&quot; {<br>  description = &quot;Name of the SLO&quot;<br>  type        = string<br>}</pre><p><strong>outputs.tf</strong></p><pre>output &quot;synthetic_test_id&quot; {<br>  description = &quot;The ID of the synthetic test&quot;<br>  value       = datadog_synthetics_test.breweries_api_health_check.id<br>}<br><br>output &quot;slo_id&quot; {<br>  description = &quot;The ID of the SLO&quot;<br>  value       = datadog_service_level_objective.breweries_api_health_check_slo.id<br>}</pre><h4><strong>Step 4: Wire Up the Modules</strong></h4><p>In `main.tf`, instantiate each module:</p><pre>module &quot;breweries_api&quot; {<br>  source = &quot;./breweries-api&quot;<br><br>  client_id              = var.api_credentials[&quot;qa&quot;][&quot;breweries-api&quot;].client_id<br>  client_secret          = var.api_credentials[&quot;qa&quot;][&quot;breweries-api&quot;].client_secret<br>  url                    = var.api_credentials[&quot;qa&quot;][&quot;breweries-api&quot;].url<br>  health_check_test_name = var.api_credentials[&quot;qa&quot;][&quot;breweries-api&quot;].health_check_test_name<br>  slo_name               = var.api_credentials[&quot;qa&quot;][&quot;breweries-api&quot;].slo_name<br>}<br><br>module &quot;countries_api&quot; {<br>  source = &quot;./countries-api&quot;<br><br>  client_id              = var.api_credentials[&quot;qa&quot;][&quot;countries-api&quot;].client_id<br>  client_secret          = var.api_credentials[&quot;qa&quot;][&quot;countries-api&quot;].client_secret<br>  url                    = var.api_credentials[&quot;qa&quot;][&quot;countries-api&quot;].url<br>  health_check_test_name = var.api_credentials[&quot;qa&quot;][&quot;countries-api&quot;].health_check_test_name<br>  slo_name               = var.api_credentials[&quot;qa&quot;][&quot;countries-api&quot;].slo_name<br>}<br><br>output &quot;breweries_api_synthetic_test_id&quot; {<br>  description = &quot;The ID of the breweries-api synthetic test&quot;<br>  value       = module.breweries_api.synthetic_test_id<br>}<br><br>output &quot;breweries_api_slo_id&quot; {<br>  description = &quot;The ID of the breweries-api SLO&quot;<br>  value       = module.breweries_api.slo_id<br>}</pre><h4><strong>Step 5: Configure Your APIs</strong></h4><p>In <em>terraform.tfvars</em>, define your API configurations:</p><pre>datadog_api_key = &quot;your-api-key&quot;<br>datadog_app_key = &quot;your-app-key&quot;<br><br>api_credentials = {<br>  &quot;qa&quot; = {<br>    &quot;breweries-api&quot; = {<br>      client_id              = &quot;your-client-id&quot;<br>      client_secret          = &quot;your-client-secret&quot;<br>      url                    = &quot;https://api.openbrewerydb.org/v1/breweries&quot;<br>      health_check_test_name = &quot;[TF][breweries-api] Health Check Test&quot;<br>      slo_name               = &quot;[TF][breweries-api] Health Check Availability&quot;<br>    }<br>    &quot;countries-api&quot; = {<br>      client_id              = &quot;your-client-id&quot;<br>      client_secret          = &quot;your-client-secret&quot;<br>      url                    = &quot;https://restcountries.com/v3.1/name/brazil&quot;<br>      health_check_test_name = &quot;[TF][countries-api] Health Check Test&quot;<br>      slo_name               = &quot;[TF][countries-api] Health Check Availability&quot;<br>    }<br>  }<br>}</pre><p>Note that all keys and secrets must be provided via system variables. For the sake of simplicity, only I have exposed those variables.</p><h3><strong>Usage</strong></h3><h4><strong>Initialize and Apply</strong></h4><blockquote>terraform init</blockquote><blockquote>terraform plan</blockquote><blockquote>terraform apply</blockquote><h4><strong>Target Specific Modules</strong></h4><p>Apply only breweries-api</p><blockquote>terraform apply -target=module.breweries_api</blockquote><p>Destroy only countries-api</p><blockquote>terraform destroy -target=module.countries_api</blockquote><h3><strong>Adding a New API</strong></h3><p>1. Create a new folder (e.g., `users-api/`)</p><p>2. Copy files from an existing module</p><p>3. Update resource names in the new module</p><p>4. Add the module declaration in `main.tf`</p><p>5. Add credentials in `terraform.tfvars`</p><p>6. Run `terraform init` and `terraform apply`</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f3f3bd951ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Upgrade Mule 4.3 to Mule 4.6 and Java 11]]></title>
            <link>https://anupamgogoi.medium.com/upgrade-mule-4-3-to-mule-4-6-and-java-11-786a4403164f?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/786a4403164f</guid>
            <category><![CDATA[mulesoft]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Wed, 15 Jan 2025 11:32:48 GMT</pubDate>
            <atom:updated>2025-01-15T11:32:48.190Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*h3y14nbDNWsHeb06JrYS0Q.png" /></figure><h3>Introduction</h3><p>Nowadays, it’s likely that most people no longer use MuleSoft’s on-prem infrastructure. However, there are still some legacy systems that continue to rely on it.</p><p>This article will show how to upgrade on-prem <strong>Mule 4.3</strong> runtime to <strong>Mule 4.6</strong>. Note that this will be an in-place upgrade.</p><h3>Things you will need</h3><ul><li>Download <strong>mule-ee-distribution-standalone-4.6.0.zip</strong> from the Support portal (<a href="https://help.mulesoft.com/">https://help.mulesoft.com/</a>)</li><li>Install <strong>OpenJDK-11</strong> on the server</li><li>Download<strong> mule-runtime-upgrade-tool-1.1.4.zip</strong> from the Support portal (<a href="https://help.mulesoft.com/">https://help.mulesoft.com/</a>)</li><li>Download <a href="https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.6.2/applicationinsights-agent-3.6.2.jar">applicationinsights-agent-3.6.2.jar</a> (in case you use it)</li></ul><h3>Before you proceed</h3><blockquote><em>Notify the stakeholders that you will do the upgrade because it needs to stop the servers. Normally, if God is with you this can be done within one hour but in most of the cases God is busy with others.</em></blockquote><blockquote><em>So, be preventive and ask for a 2 hours window so that all the rollback can be done within the period.</em></blockquote><blockquote><em>The very first thing before you proceed is to take a backup of the complete on-prem server. If not one, take at least two or three copies of the backup just to be sure and in case of a wreck quickly rollback to prevent any damage to the business.</em></blockquote><h3>Steps to follow</h3><p>Now let’s start the initial steps.</p><h4>Step 1 — Install Java</h4><p>Install OpenJDK-11 on the server. You can use the <strong>yum</strong> commands for this. (In my case I am using Red Hat Linux). After this, you need to notify the server to use Java 11. You can do this by executing the below command:</p><blockquote>sudo update-alternatives --config java</blockquote><p>It will list the available jdks installed and you can choose the jdk11.</p><p>Now, we need to set the <strong>JAVA_HOME</strong> variable in the <strong>.bashrc</strong> file found under the <strong>/mule</strong> folder:</p><blockquote>export JAVA_HOME=&quot;/usr/lib/jvm/openlogic-openjdk-11-hotspot&quot;</blockquote><blockquote>export PATH=$PATH:$JAVA_HOME/bin</blockquote><h4>Step 2 — Use the upgrade tool</h4><p>Make any temporary folder and unzip the following zips:</p><ul><li><strong>mule-ee-distribution-standalone-4.6.0.zip</strong></li><li><strong>mule-runtime-upgrade-tool-1.1.4.zip</strong></li></ul><p><strong>mule-runtime-upgrade-tool </strong>comes with a straightforward tool that we will use for the process.</p><p>It accepts the following arguments:</p><blockquote><em>-o: The directory in which the old Mule 4.3 is found</em></blockquote><blockquote><em>-n: The directory in which the new Mule 4.6 is found</em></blockquote><blockquote>./upgrade-tool upgrade -o /mule/mule-enterprise-standalone-4.3.0 -n /mule/bkp/9-jan-2025/mule-enterprise-standalone-4.6.0</blockquote><p>Note: Provide a full path to the arguments otherwise it will not work.</p><p><em>Well, it will fail because no one is that lucky to win a lottery!</em></p><p>So, don’t panic! It’s simple to resolve.</p><blockquote><em>Our idea is to not change anything in our existing Mule 4.3 runtime. Keep all configs same except the internal lib updates (that will be done by the tool).</em></blockquote><p>The above command execution creates the following structure inside <em>mule-enterprise-standalone-4.3.0</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*aonnFyWZW-uF8L88k8VClw.png" /></figure><blockquote><strong><em>new-mule-enterprise-standalone-4.6.0</em></strong><em>: The files inside this folders are the new configs that are for Mule 4.6.</em></blockquote><blockquote><strong><em>to_review</em></strong><em>: This folder contains the old configs from our </em><em>mule-enterprise-standalone-4.3.0</em>that conflicted with the Mule 4.6.</blockquote><blockquote><strong><em>ready</em></strong><em>: We need to decide what changes we need to merge between the old and new configs.</em></blockquote><blockquote><em>And we technically don’t need anything from Mule 4.6 configs. So, copy everything from from </em><strong><em>to_review</em></strong><em> folder to </em><strong><em>ready</em></strong><em>.</em></blockquote><p>We need a very small change in the <strong>wrapper.conf</strong> file that we have moved from <strong><em>to_review</em></strong> to <strong><em>ready.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*hWAlw83Vs4U0yJsUklQ6eg.png" /></figure><p>Open it and add the below line at the end:</p><blockquote>wrapper.java.additional.&lt;n&gt;=-Dmule.bootstrap.container.wrapper.class=org.mule.runtime.module.boot.tanuki.internal.MuleContainerTanukiWrapper</blockquote><blockquote>Search for the word applicationinsightsand modify the version to <strong>3.6.2</strong></blockquote><blockquote>wrapper.java.additional.&lt;n&gt;=-javaagent:/mule/<em>mule-enterprise-standalone-4.3.0</em>/lib/boot/applicationinsights-agent-3.6.2.jar</blockquote><p>Cool. Now one last thing. Remember the <a href="https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.6.2/applicationinsights-agent-3.6.2.jar">applicationinsights-agent-3.6.2.jar</a> we downloaded?</p><p>Put it in the below location:</p><p>/mule/<em>mule-enterprise-standalone-4.3.0</em>/lib/boot</p><p>Now all set. Execute the migration tool again:</p><p>./upgrade-tool upgrade -o /mule/<em>mule-enterprise-standalone-4.3.0</em> -n /mule/bkp/9-jan-2025/mule-enterprise-standalone-4.6.0</p><p>Now, I believe God is with you and the tool will be able to do the upgrade.</p><h4>Step 3 — Start the server</h4><p>Use the below command to start the server:</p><p>/mule/<em>mule-enterprise-standalone-4.3.0/bin&gt; ./mule</em></p><p>Check for the logs in this file:</p><p>/mule/<em>mule-enterprise-standalone-4.3.0</em>/logs/mule_ee.log</p><p>If there is no error you will see this nice screen with Mule 4.6 and Java 11</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q8_VwWYDfDJeO00t1E44zw.png" /></figure><p>Cheers!!!!</p><p><strong>References</strong>:</p><p><a href="https://docs.mulesoft.com/mule-runtime/latest/mule-upgrade-tool">Mule Upgrade Tool | MuleSoft Documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=786a4403164f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Standalone Apache Spark cluster in Mac M1]]></title>
            <link>https://anupamgogoi.medium.com/standalone-apache-spark-cluster-in-mac-m1-0d1bf65371b1?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d1bf65371b1</guid>
            <category><![CDATA[apache-spark]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Thu, 11 Jan 2024 18:21:18 GMT</pubDate>
            <atom:updated>2024-01-11T18:21:18.746Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>I had a pain in the arse while creating a Standalone Apache cluster in a Mac machine armed with an M1 processor (I hate this *h!&amp;). The issue was not with the Spark but it was while creating some virtual machines in the Macbook.</p><h3>Problems</h3><p>The Oracle VirtualBox does not work on an Apple M1 processor. So, the alternative solution was to use the <a href="https://mac.getutm.app/">UTM</a>. You can take a look into it and seemingly it worked quite fine in my Mac M1.</p><h3>Deployment Architecture</h3><p>I used a quite simple deployment architecture for this demonstration and also for the sake of not burning my machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*uB2pMIY0gtsCyotdY10HxA.png" /><figcaption>Deployment Architecture</figcaption></figure><p>One Master and one Worker (Slave/Executor or whatever you want to call it) node configuration resolved my demo cluster.</p><p>Initially, I configured the Master node (<strong>Ubuntu 22.04.3 LTS</strong>) with Apache Spark Standalone and then I simply cloned the VM to save my time and brain.</p><h3>Spark Standalone configuration</h3><p>We will configure 2 nodes. 1) Master, 2) Worker</p><h4>Master node</h4><p>You can either download the <a href="https://spark.apache.org/downloads.html">prepackaged</a> version or build the <a href="https://github.com/apache/spark/tree/branch-3.5">source code</a> from zero. I opted for the second option because it&#39;s the fun of OSS.</p><blockquote>For building the source code I used openjdk version “17.0.2” 2022–01–18</blockquote><p>Once the source code is built it will generate all the executables and libs. I configured only the PATH to point to the executables.</p><p>Below are my PATH settings:</p><blockquote>export PATH=$PATH:/home/acme/github/spark/bin<br>export PATH=$PATH:/home/acme/github/spark/sbin</blockquote><p>Cool! We have our one node ready. Just make one more change in the following file:</p><blockquote>/etc/hostname</blockquote><p>Write a nice name here let&#39;s say <strong>spark-master </strong>so that when we run the master node it gets a cool name (sometimes does not pick the hostname and picks the IP instead).</p><h4>Worker node</h4><p>Now, just clone the master node, and boom! We have one worker node ready. We will only need to do some basic configuration.</p><p>Edit the below file and put a nice name to identify the worker.</p><blockquote>/etc/hostname</blockquote><p>In my case, I put <strong>spark-worker-1. </strong>That&#39;s all.</p><h3>Configure the Host machine</h3><p>At this point, we have two VMs already up &amp; running and we need to configure quick stuff in our host i.e Macbook</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*zKt58GaC-A5mQ3j54_1nfQ.png" /></figure><p>That&#39;s all!</p><h3>Run the Master node</h3><p>Make sure that your <strong>spark-master</strong> and <strong>spark-worker-1</strong> nodes can communicate with each other. This is essential (of course it makes sense).</p><p>You can run the cluster in several different <a href="https://spark.apache.org/docs/latest/spark-standalone.html">ways</a>. But, being naive I will start with the simplest way.</p><h4>Master node</h4><p>Well, you can enter directly into the <strong>spark-master </strong>Ubuntu VM or via SSH and it&#39;s up to you. (If you feel like a hacker go for the latter option)</p><p>Once you are in, just run the below command:</p><blockquote>acme@spark-master:~$ start-master.sh</blockquote><p>At this moment, we have our master node i.e. <strong>spark-master</strong> running. In our <strong>host</strong> machine i.e. Mac we can even open the web UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*3KtDg9OxR-zPJe1h74_t-g.png" /><figcaption>Master UI</figcaption></figure><p>Oh crap!!!! It&#39;s working. Note this information:</p><blockquote><strong>Master URL:</strong> spark://192.168.5.220:7077</blockquote><blockquote>Sometimes it might show as spark://spark-master:7077</blockquote><p>Now let&#39;s jump to the next step.</p><h4>Worker node</h4><p>Now, let&#39;s enter the <strong>spark-worker-1 </strong>node and start the worker.</p><blockquote>$ start-worker.sh spark://192.168.5.220:7077</blockquote><p>That&#39;s all you need to do. The master URL is the one you got while running the master node.</p><p>Now, refresh the master console i.e <a href="http://spark-master:8080/">http://spark-master:8080/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*7u8zs182U8rPEplr62eK_Q.png" /></figure><p>Alleluia! We have a worker registered too. So, it means that our cluster is up &amp; running.</p><h3>Conclusion</h3><p>In this brief demo, I have explained how to configure a Spark Cluster in a Master+Worker setup. In the next article, I will try to write a driver program and publish it into the cluster, and let&#39;s explore the behaviors.</p><p>Until then, happy learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d1bf65371b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The most pathetic way to implement HashMap in Mule 4]]></title>
            <link>https://anupamgogoi.medium.com/the-most-pathetic-way-to-implement-hashmap-in-mule-4-a34f7c114cd3?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/a34f7c114cd3</guid>
            <category><![CDATA[mulesoft]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Fri, 08 Dec 2023 20:20:43 GMT</pubDate>
            <atom:updated>2023-12-12T16:08:57.736Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this article, I am going to discuss how to implement a HashMap in Mule 4. Who has already worked with Mule3, it&#39;s well-known how to do it. But in Mule 4, I found it quite painful.</p><h3>Mule 3</h3><p>It&#39;s quite simple to declare a HashMap and add values to it in Mule 3.</p><pre>#[new java.util.HashMap()]</pre><p>That&#39;s all. You can use all the <strong>Get</strong> and <strong>Put</strong> methods of the Java HashMap.</p><h3>Mule 4</h3><p>I hated it but had to do it. For the TL;DR just copy and paste the below code.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br><br>&lt;mule<br> xmlns:outlook365=&quot;http://www.mulesoft.org/schema/mule/outlook365&quot;<br> xmlns:salesforce=&quot;http://www.mulesoft.org/schema/mule/salesforce&quot;<br> xmlns:ee=&quot;http://www.mulesoft.org/schema/mule/ee/core&quot;<br> xmlns:http=&quot;http://www.mulesoft.org/schema/mule/http&quot;<br> xmlns:db=&quot;http://www.mulesoft.org/schema/mule/db&quot;<br> xmlns=&quot;http://www.mulesoft.org/schema/mule/core&quot;<br> xmlns:doc=&quot;http://www.mulesoft.org/schema/mule/documentation&quot;<br> xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;<br> xsi:schemaLocation=&quot;http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd<br>http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd<br>http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd<br>http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd<br>http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd<br>http://www.mulesoft.org/schema/mule/outlook365 http://www.mulesoft.org/schema/mule/outlook365/current/mule-outlook365.xsd&quot;&gt;<br> &lt;http:listener-config name=&quot;HTTP_Listener_config&quot;<br>  doc:name=&quot;HTTP Listener config&quot;<br>  doc:id=&quot;2b4cf156-0b38-4d7f-a016-318fd127ee59&quot;&gt;<br>  &lt;http:listener-connection host=&quot;0.0.0.0&quot;<br>   port=&quot;8081&quot; /&gt;<br> &lt;/http:listener-config&gt;<br><br><br> &lt;flow name=&quot;test-appFlow&quot;<br>  doc:id=&quot;5072b12e-f55e-48b6-87d4-0e0280e7f6e5&quot;&gt;<br>  &lt;http:listener doc:name=&quot;Listener&quot;<br>   doc:id=&quot;9dde9763-a782-425d-81db-0276cf80dd5e&quot;<br>   config-ref=&quot;HTTP_Listener_config&quot; path=&quot;/test&quot; /&gt;<br><br><br>  &lt;set-variable value=&quot;#[{}]&quot; doc:name=&quot;map&quot;<br>   doc:id=&quot;2e954952-221c-4a7c-8021-9ddebd612f44&quot; variableName=&quot;map&quot; /&gt;<br>  &lt;ee:transform doc:name=&quot;k1:value1&quot;<br>   doc:id=&quot;436d500d-a83f-443f-b179-f7d230aa9b7e&quot;&gt;<br>   &lt;ee:message&gt;<br>   &lt;/ee:message&gt;<br>   &lt;ee:variables&gt;<br>    &lt;ee:set-variable variableName=&quot;map&quot;&gt;&lt;![CDATA[%dw 2.0<br>output application/java<br>---<br>vars.map ++ {&quot;k1&quot;:[&quot;a&quot;,&quot;b&quot;]}]]&gt;&lt;/ee:set-variable&gt;<br>   &lt;/ee:variables&gt;<br>  &lt;/ee:transform&gt;<br>  &lt;ee:transform doc:name=&quot;k2:value2&quot;<br>   doc:id=&quot;487e17ca-64de-43a1-9b66-6603e6206658&quot;&gt;<br>   &lt;ee:message&gt;<br>   &lt;/ee:message&gt;<br>   &lt;ee:variables&gt;<br>    &lt;ee:set-variable variableName=&quot;map&quot;&gt;&lt;![CDATA[%dw 2.0<br>output application/java<br>---<br>vars.map ++ {&quot;k2&quot;:[&quot;c&quot;,&quot;d&quot;]}]]&gt;&lt;/ee:set-variable&gt;<br>   &lt;/ee:variables&gt;<br>  &lt;/ee:transform&gt;<br>  &lt;logger level=&quot;INFO&quot; doc:name=&quot;Logger&quot;<br>   doc:id=&quot;13ab49c9-6b06-44a8-b291-b159c2fbd6c6&quot; /&gt;<br>  &lt;set-variable value=&quot;k1&quot; doc:name=&quot;Find the key&quot;<br>   doc:id=&quot;90204a71-46b1-4fab-9a5e-6332b6891f1a&quot; variableName=&quot;key&quot; /&gt;<br>  &lt;ee:transform doc:name=&quot;Transform Message&quot; doc:id=&quot;b2a4cf67-0fa0-4e23-8735-dd0c00f9cfc6&quot; &gt;<br>   &lt;ee:message &gt;<br>    &lt;ee:set-payload &gt;&lt;![CDATA[%dw 2.0<br>output application/java<br>---<br>vars.map[vars.key]]]&gt;&lt;/ee:set-payload&gt;<br>   &lt;/ee:message&gt;<br>  &lt;/ee:transform&gt;<br>  &lt;logger level=&quot;INFO&quot; doc:name=&quot;Logger&quot; doc:id=&quot;6a7738cd-fd73-4166-85cf-1754ae11be26&quot; /&gt;<br> &lt;/flow&gt;<br>&lt;/mule&gt;</pre><p>Below is the explanation of the code:</p><h4>Step 1</h4><p>Declare a variable (<strong>map</strong> in my case) with default value <strong>{}.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KkkhO89yG0-S7DgzNdWzhg.png" /></figure><h4>Step 2</h4><p>Add a key value to the same variable declared in the previous step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AcD56itxgqw6oIQpAMJD3g.png" /></figure><h4>Step 3</h4><p>Add another value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2uX-bSo5tzUYfPheWSgykg.png" /></figure><h4>Step 4</h4><p>Set the name of the key to be searched in the <strong>map</strong> variable in a separate variable named <strong>key</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GCrKBvVqfhj2GWv_zbG9eg.png" /></figure><h4>Step 5</h4><p>Finally, get the value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LfHekICqNCP-eXD036mkLw.png" /></figure><p>That&#39;s all.</p><h3>Conclusion</h3><p>I found it quite stupid way but it works. Let me know if you have a better solution.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a34f7c114cd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Troubleshoot a Mule Application]]></title>
            <link>https://anupamgogoi.medium.com/troubleshoot-a-mule-application-31b54341beba?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/31b54341beba</guid>
            <category><![CDATA[cloudhub]]></category>
            <category><![CDATA[mulesoft]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Mon, 31 Jul 2023 12:40:27 GMT</pubDate>
            <atom:updated>2023-07-31T14:59:55.037Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>The easiest part of software development is writing code. Anyone can write a piece of code (even a kid). But, the biggest challenge is writing code that will perform smoothly in hostile conditions. And to achieve this goal the code must pass through severe conditions.</p><p>I want to present some troubleshooting cases of a Mule Application in this article. So, let&#39;s get started.</p><h3>Problem Scenario</h3><p>I have a simple Mule application deployed in Cloudhub 1.0.</p><blockquote>The application is a REST API that invokes Salesforce to upsert some data. Recently, we have seen that the application becomse stale after some hours of running and there was no threads available to process new requests.</blockquote><h3>Troubleshooting</h3><p>When the application is running but it&#39;s not accepting new requests the next best way after logs is the thread dump analysis. The focus of this article is analyzing thread dumps and how to take action based on them.</p><h4>Taking a thread dump</h4><p>You can take thread dumps of only running applications (obvious). Open the logs of the mule application and hover around the Worker and you will be given an option:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*-mnyoRkWA8sB_ylmS6gBbA.png" /><figcaption>Diagnostics</figcaption></figure><p>Click the <strong><em>Diagnostics</em></strong> option and it will download the thread dumps of your application.</p><h4>Analyze the issue</h4><p>There are many tools for thread dump analysis however I prefer the <a href="https://fastthread.io/">fastthread.io</a>. You can upload your download dump and it provides a lot of helpful information.</p><p>Well, you can see that it already has detected some issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zuKy-lgspM7x8QzmhCicjg.png" /><figcaption>Analysis</figcaption></figure><p>Let&#39;s dig deeper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jsu_yBTq-J4khv1wiPHhlQ.png" /><figcaption>Summary</figcaption></figure><p>You can see that 141 threads out of 220 are in a <strong><em>WAITING</em></strong> state. Something must have gone wrong.</p><p>Let&#39;s check the identical stack traces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n2Df4v8ErHvvFV9mnOSK9A.png" /><figcaption>Identical traces</figcaption></figure><blockquote>Well, if you see the first trace (stackTrace1), it&#39;s quite obvious that something bad is going on where lots of threads are doing something and they are generating identical traces.</blockquote><p>Now, let&#39;s open this stackTrace1, and let&#39;s see what&#39;s going on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYX7YtbdhCPIk6PzA2abFw.png" /><figcaption>SF_Request</figcaption></figure><p>Well, you can see that 114 threads are printing identical traces and if you go one by one (left-hand side), you can see the same behavior.</p><blockquote>All these threads are in a WAITING state while making a call to Salesforce using the salesforce connector.</blockquote><p>So, the problem is obvious that we have done some misconfiguration in the Salesforce connector.</p><h4>Final outcome of the analysis</h4><p>Well, this is a known issue. Check the official Mulesoft <a href="https://help.mulesoft.com/s/article/Applications-using-Salesforce-connector-are-hung-without-errors">article</a> on it.</p><blockquote>In brief, if we don&#39;t set the <strong>Read Timeout</strong> in the SF connector (default is 0), the Salesforce connector will wait <strong>infinitely to receive a response</strong> from Salesforce. And if Salesforce is taking too much time (or down) to respond, for every incoming request to your Mule application, Mule will open a new thread to process the request. Thereby Mule will run out of threads and your application will halt.</blockquote><h4>Solution</h4><p>Set a proper Read Timeout in the Salesforce Connector configuration to avoid such issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*xTEjr9hdWSlYJxQlVvmByQ.png" /></figure><h3>Conclusion</h3><p>In this brief article, I have shown how to troubleshoot a Mule application using thread dumps. If you have any other situations where thread dumps helped you to analyze the root cause please let us know.</p><p>Thanks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31b54341beba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Internal stuff of Mulesoft + JWT]]></title>
            <link>https://anupamgogoi.medium.com/internal-stuff-of-mulesoft-jwt-95c6b358bd48?source=rss-c738a6af2285------2</link>
            <guid isPermaLink="false">https://medium.com/p/95c6b358bd48</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[mulesoft]]></category>
            <category><![CDATA[anypoint-platform]]></category>
            <dc:creator><![CDATA[ANUPAM GOGOI]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 14:45:59 GMT</pubDate>
            <atom:updated>2023-02-20T11:55:19.564Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this brief article, I would like to explore some internal stuff of the Mulesoft JWT Validation policy. The target audience of the article is the advanced ones who have in-depth knowledge of the platform i.e how the Mulesoft platform works internally.</p><h3>Article Division</h3><p>As it will be an in-depth article, I would like to start everything from scratch. You may skip some parts if you have good knowledge. I will divide the article into the below sections.</p><blockquote>JWT Configuration in Keycloak</blockquote><blockquote>Validate JWT with Java</blockquote><blockquote>Configure JWT in Mulesoft</blockquote><blockquote>Check the Source code of the Mulesoft JWT policy</blockquote><h3>JWT Configuration in Keycloak</h3><p>There are tons of articles regarding JWT on the internet, so I will not delve into it. For this demo, I am going to use the <a href="https://www.keycloak.org/">Keycloak</a> Identity Server for the JWT generation (I found it free of use and quite simple).</p><h4>Keycloak installation</h4><p>If you have Docker in your machine then it&#39;s the best way to try it out.</p><pre>docker run -p 8080:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:20.0.3 start-dev</pre><p>That&#39;s all. You will have an excellent Keycloak server up &amp; running.</p><p>Browse to <a href="http://localhost:8080/">http://localhost:8080</a> and you will be presented with a friendly welcome screen as seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2H96Fhi8uxZ8eEGFHFj-XQ.png" /></figure><h4>Configure a Client</h4><p>First, we need to configure a client. Click the <strong><em>Clients</em></strong> menu option on the left-hand side and then click <strong><em>Create client</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8IyoGhTteHMhk5XmzlOt5w.png" /></figure><p>Add the client name as shown in the below screenshot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sNqwdcmzK_FE4AKxdWZB6A.png" /><figcaption>General Settings</figcaption></figure><p>Add the <strong><em>capabilities</em></strong> to the client:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0nUWxDuSKCF-qNxODtI5wA.png" /><figcaption>Capabilities</figcaption></figure><p>And Save.</p><p>In the <strong><em>Credentials</em></strong> section of the client (<strong><em>mule</em></strong>), you can find the <strong><em>Client secret</em></strong> as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZOvZab4OcOJIOTZKs-TGCQ.png" /><figcaption>client_secret</figcaption></figure><h4>Find the Well-Known URL of Keyclock</h4><p>Now let&#39;s find out the well-known URL of Keyclock. Click</p><blockquote>Configure → Realm settings → General</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xU_R5U4D5xUX1AJJLF2QSg.png" /></figure><p>You can see that there is a link <a href="http://localhost:8080/realms/master/.well-known/openid-configuration">OpenID Endpoint Configuration</a>. That&#39;s the well-known endpoints exposed by the Keycloak server. Cool.</p><p>In my case, it&#39;s</p><blockquote><a href="http://localhost:8080/realms/master/.well-known/openid-configuration">http://localhost:8080/realms/master/.well-known/openid-configuration</a></blockquote><p>Invoke the endpoint in Postman or anywhere of your choice and you are in the game:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O5pJ-2qLkakWV2UIXjyElQ.png" /><figcaption>Well Known</figcaption></figure><h4>Generate JWT</h4><p>Now, let&#39;s generate a JWT by invoking the token endpoint.</p><p>Below is my CURL command to generate JWT:</p><blockquote>curl — location ‘<a href="http://localhost:8080/realms/master/protocol/openid-connect/token&#39;">http://localhost:8080/realms/master/protocol/openid-connect/token&#39;</a> \<br> — header ‘Content-Type: application/x-www-form-urlencoded’ \<br> — data-urlencode ‘client_id=mule’ \<br> — data-urlencode ‘grant_type=client_credentials’ \<br> — data-urlencode ‘client_secret=SIGlvYOaD8cJ3DggiW2UBw9E3zpkpWrS’ \<br> — data-urlencode ‘scope=openid’</blockquote><p>Check my Postman screen-shot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kj02qkIm5EsaWKlei9Vpzg.png" /><figcaption>Postman JWT</figcaption></figure><p>The <strong><em>id_token</em></strong> is our most wanted JWT. You can decode it to check its contents:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XOURYFg8Li7wIHy-kilMpw.png" /><figcaption>JWT.io</figcaption></figure><p>In the next section let&#39;s validate the token using Java code.</p><h3>Validate JWT with Java</h3><p>I am going to write the simplest ever possible code to validate a JWT token against the <strong><em>JWKS URL</em></strong>.</p><p>Note that from the well-known URL, we can easily find the JWKS URL. In my case it is:</p><blockquote><a href="http://localhost:8080/realms/master/protocol/openid-connect/certs">http://localhost:8080/realms/master/protocol/openid-connect/certs</a></blockquote><p>What it does is no genius. In short, it exposes a set of keys (kid) against whom we can validate our JWT. Well, I am not going to enter in details of that because the internet is already full of such information.</p><p>For the Java code, I am going to use the <a href="https://connect2id.com/products/nimbus-jose-jwt/examples/validating-jwt-access-tokens">Nimbus JOSE+JWT libraries</a>. Below is the code snippet:</p><pre>package com.utility;<br><br>import com.nimbusds.jose.JWSAlgorithm;<br>import com.nimbusds.jose.jwk.source.JWKSource;<br>import com.nimbusds.jose.jwk.source.RemoteJWKSet;<br>import com.nimbusds.jose.proc.JWSKeySelector;<br>import com.nimbusds.jose.proc.JWSVerificationKeySelector;<br>import com.nimbusds.jose.proc.SecurityContext;<br>import com.nimbusds.jwt.JWTClaimsSet;<br>import com.nimbusds.jwt.proc.ConfigurableJWTProcessor;<br>import com.nimbusds.jwt.proc.DefaultJWTProcessor;<br><br>import java.net.URL;<br><br>public class App {<br>    public static void main(String[] args) {<br>        validate(&quot;eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJIYkVIUE5COWRLOWlpNG16YUhzejJTYUZzTFo5ZkloYTdwcmpPdW5YUE5NIn0.eyJleHAiOjE2NzY4MTQ4NTYsImlhdCI6MTY3NjgxNDc5NiwiYXV0aF90aW1lIjowLCJqdGkiOiIyOGYxODUzYy00MGRiLTQ2ZGItYjc2My00ZTlhMTkyZDYzODAiLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvcmVhbG1zL21hc3RlciIsImF1ZCI6Im11bGUiLCJzdWIiOiIyYmRiMjUzMy04Y2ZlLTQ3ZWEtOWY0Zi1lOGJlMDEyYzdhM2EiLCJ0eXAiOiJJRCIsImF6cCI6Im11bGUiLCJhdF9oYXNoIjoiM3FGU0otNXI0R2NOT0ZoeWxpZnlIZyIsImFjciI6IjEiLCJjbGllbnRIb3N0IjoiMTcyLjE3LjAuMSIsImNsaWVudElkIjoibXVsZSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwicHJlZmVycmVkX3VzZXJuYW1lIjoic2VydmljZS1hY2NvdW50LW11bGUiLCJjbGllbnRBZGRyZXNzIjoiMTcyLjE3LjAuMSJ9.E4nINN9ebHnYxfJovL_Qwd9uB6AyGE6-jwSWiENfxzAOy0iZJriT0bsENjgf3XuoA0amfEwN6DKBr8eQeVxdkpOhq_rZpo4GgDYz11cOvYX-3-qVbr2-LCssSxmqcvvNq1lWj6VqkbcVGHZzrvjHdST05lNAJG1Jfnd59u9Tjoxmy7RJktVfasaSPLZm_qdmffAAPXKwLA-XD9HOZsUV-MHgOq2yg7q-sdzIokWtfCnKdjGjZ3XVMmTNAa_H1Vqh8Nab3Nsi1kxuAHi10Xac70NUgATcSEICfclwMmS02SkRo5PZbDRinKQMWWNmoWRCaS97qpT_NKb6Urez0ea1uw&quot;);<br>    }<br><br><br>    public static void validate(String accessToken) {<br>        try {<br>            ConfigurableJWTProcessor&lt;SecurityContext&gt; jwtProcessor = new DefaultJWTProcessor&lt;&gt;();<br><br>            JWKSource&lt;SecurityContext&gt; keySource = new RemoteJWKSet&lt;&gt;(new URL(&quot;http://localhost:8080/realms/master/protocol/openid-connect/certs&quot;));<br>            JWSKeySelector&lt;SecurityContext&gt; keySelector = new JWSVerificationKeySelector&lt;&gt;(JWSAlgorithm.RS256, keySource);<br>            jwtProcessor.setJWSKeySelector(keySelector);<br><br>            JWTClaimsSet claimsSet = jwtProcessor.process(accessToken, null);<br>            System.out.println(claimsSet.toJSONObject());<br>        }<br>        catch (Exception e){<br>            e.printStackTrace();<br>        }<br><br>    }<br><br>}</pre><p>When the token is valid you get the below output:</p><pre>{at_hash=3qFSJ-5r4GcNOFhylifyHg, sub=2bdb2533-8cfe-47ea-9f4f-e8be012c7a3a, clientHost=172.17.0.1, clientId=mule, email_verified=false, iss=http://localhost:8080/realms/master, typ=ID, preferred_username=service-account-mule, clientAddress=172.17.0.1, aud=mule, acr=1, azp=mule, auth_time=0, exp=1676814856, iat=1676814796, jti=28f1853c-40db-46db-b763-4e9a192d6380}</pre><p>When the JWT expires you get the below error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1GHMUu95zcOYcTxptX12UA.png" /></figure><p>How cool!!!!!</p><p>And just to let you know that this is the magic that Mulesoft uses in the JWT Policy in a sophisticated way. No more secrets!</p><h3>Configure JWT in Mulesoft</h3><p>Create a RAML in the Design Center → Publish to Exchange → Import it in API Manager → Create the API.</p><p>After creating the API, just apply the JWT Validation policy to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*oiLUAY25RzG5E36e7mSALw.png" /><figcaption>JWT Validation policy</figcaption></figure><p>That&#39;s it.</p><h3>Check the Source code of the Mulesoft JWT policy</h3><p>This part is cool where we will actually debug the JWT Policy of Mulesoft. In my previous articles, I wrote about how to debug the whole Mulesoft Runtime (I am not talking about debugging a Mulesoft application, it&#39;s for dummies :))</p><p><a href="https://anupamgogoi.medium.com/debugging-the-mule-esb-4-x-c566e128c21c">Debugging the Mule ESB 4.x</a></p><p>This is what we are going to do. The first step is how to get hold of the JWT Validation policy code. Check the below steps:</p><h4>Configure Anypoint Platform credentials</h4><p>The best way is to download a standalone Mulesoft Runtime 4. x on your machine. Then edit the <strong>wrapper.conf </strong>with the platform <strong><em>client_id</em></strong> &amp; <strong><em>client_secret</em></strong>. In my case, I just retrieved the environment&#39;s credentials to which I am going to deploy my API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*soNgYlSnVHnh3EZieaWlzw.png" /></figure><p>Also, set the configuration for the debugging:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8K5rGybz8haZ8Jsv4q7RtQ.png" /></figure><p>I will not enter that much detail as you can already check those stuff in the article I cited.</p><h4>API Development</h4><p>I believe when you created the API in API Manager you got the API Instance ID. So, with that just create a mock implementation on your local machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*B_joVokZOBDJoV4aUwaXFA.png" /></figure><blockquote>Now, export the zip and publish it to your standalone Mule Runtime (not Anypoint IDE) i.e put it in the /apps folder.</blockquote><blockquote>Run the standalone Mule Runtime and wait for some moments. Note that you will need to attach a debugger to the runtime. I normally use Intellij</blockquote><p><em>Although at this moment we are not going to debug anything, I just kept everything configured for later use.</em></p><p>After some time you can see that the JWT policy is downloaded automatically and installed in your standalone Mulesoft Runtime in the following location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L_JQzONd1qYPRKjqU8KWiA.png" /></figure><p>That&#39;s awesome.</p><h4>Explore the JWT policy</h4><p>Now, we have all we wanted in hand. We can explore the jwt-validation-1.3.1-mule-policy.jar. Just unzip it with any tool you prefer.</p><p>Check the exploded jar. Inside it, there are many other jars. However, I am interested only in that marked one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OejHnKhY1q2TjR7QOsw-aw.png" /></figure><p>This is the jar file that I will import into my IntelliJ IDE for debugging and that will be the cool thing.</p><p>Now let&#39;s make a request to my API that I deployed to my standalone Mulesoft Runtime. See what&#39;s happening:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ypPe_FUJRZcbogsCgj_jnw.png" /><figcaption>Debug</figcaption></figure><p>This is the method executed for JWT Validation. And if you deep dive into the code you will find that Mulesoft uses the Nimbus JOSE + JWT libs with a sophisticated way to validate the JWT. No secrets.</p><h3>Conclusion</h3><p>Well, that&#39;s all for today. Hope you find the article insightful. 100% of the articles on the internet don&#39;t dive into what&#39;s under the hood. That&#39;s why I decided to expose the truth underneath the platform.</p><p>So, what do you think? JWT is complex? Hell no!</p><p>See you soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95c6b358bd48" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>