<?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 Aryash Jain on Medium]]></title>
        <description><![CDATA[Stories by Aryash Jain on Medium]]></description>
        <link>https://medium.com/@aryash.jain?source=rss-74638693b7e5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5mR-gHSod9yWdPLn</url>
            <title>Stories by Aryash Jain on Medium</title>
            <link>https://medium.com/@aryash.jain?source=rss-74638693b7e5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 11:34:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aryash.jain/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[Centralised Scalable Dynamic Config Service]]></title>
            <link>https://medium.com/@aryash.jain/centralised-scalable-dynamic-config-service-cfd201d9db6e?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfd201d9db6e</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[configuration-management]]></category>
            <category><![CDATA[hashicorp]]></category>
            <category><![CDATA[scalability]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Wed, 11 Nov 2026 03:55:29 GMT</pubDate>
            <atom:updated>2026-11-11T03:55:29.571Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently we had to move out of our legacy config manager service and we were searching for various Tools around the same.</p><h3>We had a few problems that were to be solved ?</h3><ul><li>Dynamic reflection of config changes</li><li>99.99999% available config service</li><li>A strict maker checker flow while updating configs</li><li>Gitops flow supported if possible</li><li>Easy to integrate accross services in various language</li></ul><h3><strong>Alternatives We Evaluated</strong></h3><p>When moving away from our legacy system, we explored several prominent configuration management and distributed coordination tools. Here is how they stacked up against our requirements:</p><ul><li><strong>Etcd:</strong> A highly reliable, strongly consistent distributed key-value store famous for backing Kubernetes. While excellent for distributed systems, its core focus isn’t application-level configuration management. Building a strict maker-checker flow and GitOps pipeline around it would require building and maintaining significant custom tooling.</li><li><strong>Apache ZooKeeper:</strong> The traditional heavyweight for distributed coordination. It offers high reliability but is notoriously complex to set up, operate, and maintain.</li><li><strong>Spring Cloud Config:</strong> An excellent tool that natively supports Git as a backend, making GitOps flows out-of-the-box. However, it is fundamentally designed for the Spring Boot ecosystem. Integrating it across services written in Go, Python, or Node.js introduces unnecessary friction and workarounds.</li><li><strong>Cloud-Managed Services (e.g., AWS AppConfig / Parameter Store):</strong> These provide massive scalability and built-in deployment strategies. However, relying heavily on a specific cloud provider’s proprietary configuration service introduces tight vendor lock-in.</li></ul><h3>Why We Selected HashiCorp Consul?</h3><p>Consul ultimately emerged as the clear winner because it elegantly checked all of our core boxes while providing additional architectural benefits.</p><ul><li><strong>True Dynamic Configuration via Watches:</strong> Consul’s Key/Value store provides built-in Watches and subscriptions. Instead of services repeatedly polling for changes, Consul agents monitor specific keys or prefixes and instantly push updates to the application. This allows configurations to refresh dynamically in real-time without requiring service restarts.</li><li><strong>Built for Extreme Availability:</strong> Consul achieves fault tolerance using the Raft consensus algorithm for state changes across server nodes, and the Serf gossip protocol for agent communication. Its native support for multi-datacenter federation ensures that even if an entire region goes down, the configuration service remains available.</li><li><strong>Native Fit for GitOps and Maker-Checker Flows:</strong> Consul integrates flawlessly with IaC tools. We established a strict GitOps workflow where all configuration values live in a Git repository. A developer opens a Pull Request, it is reviewed by a lead, and upon merging, our CI/CD pipeline securely applies the changes to the Consul KV store.</li><li><strong>Language Agnostic:</strong> Consul is built to be polyglot. It exposes a clean HTTP REST API, making it trivial to fetch or update configurations from any language.</li><li><strong>Stellar Documentation:</strong> The clear documentation at <a href="https://developer.hashicorp.com/consul">developer.hashicorp.com/consul</a> helped us navigate the setup smoothly.</li></ul><h3><strong>Bits on consul internal….</strong></h3><p>To truly trust a system with our most critical application configurations, we needed to understand how it maintains state and consensus under the hood. Consul’s architecture relies on a few brilliant internal mechanisms to balance strong consistency with high availability:</p><ul><li><strong>Raft Consensus for State:</strong> Consul server nodes use the Raft consensus algorithm to maintain a strongly consistent replicated state machine. This ensures that every configuration write is durably committed to a quorum of servers before it is acknowledged. If the leader node fails, Raft automatically elects a new leader in milliseconds, preventing split-brain scenarios and data corruption.</li><li><strong>Serf Gossip Protocol for Membership:</strong> While servers handle the heavy lifting of state, Consul utilizes the Serf gossip protocol (based on SWIM) to manage cluster membership and failure detection. Instead of a centralized heartbeat mechanism that bottlenecks at scale, nodes gossip health statuses peer-to-peer. This allows the cluster to scale to tens of thousands of nodes while keeping failure detection lightweight and lightning-fast.</li><li><strong>Blocking Queries (The Magic Behind Watches):</strong> Consul doesn’t rely on traditional, resource-heavy HTTP polling. Instead, it uses “blocking queries.” When a client agent watches a KV prefix, it issues an HTTP GET request with a special index. The Consul server holds the connection open until a change occurs or a timeout is reached. The moment a config is merged via our Argo workflow, the server responds, pushing the update immediately to the waiting client.</li></ul><h3><strong>The final Idea walk through!</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N0BWULW4x_qHzDG-YL9Jnw.png" /></figure><p><strong>1. The GitOps Source of Truth (GitHub + Argo Workflows)</strong> We set up a centralized GitHub repository dedicated entirely to application configurations, organized by service and environment (e.g., /java-service-configs/prod.json). When an engineer needs to update a timeout value or toggle a feature flag, they create a Pull Request. This enforces our strict &quot;Maker-Checker&quot; flow: the PR requires an approval from a lead before it can be merged. Once merged to the main branch, a webhook instantly triggers an Argo CI workflow.</p><p><strong>2. The Sync Engine (Bridging Git and Consul)</strong> The Argo workflow serves as the bridge between our static repository and our dynamic KV store. Its sole job is to parse the modified configuration files from the recent commit and publish those exact key-value pairs into the HashiCorp Consul cluster. By treating Git as the ultimate source of truth, we ensure that Consul’s state is always a perfectly synced mirror of the repository. If a bad config is merged, rolling back is as simple as reverting the Git commit; Argo automatically syncs the corrected state back to Consul.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83j4jO8fhj8sw4_1O290Ew.png" /></figure><p><strong>3. Real-Time Reflection (Zero-Downtime Updates)</strong> Once the Argo workflow pushes the new values to the Consul KV store, the real magic happens. Application instances do not sit around polling a database every five minutes to check if something changed. Instead, the Consul agent utilizes its blocking queries (Watches) to instantly push the update to the listening clients. The new configuration is injected into the application’s memory in less than a second, altering the service’s behavior in real-time without requiring a pod restart or a full redeployment.</p><p><strong>4. Frictionless Developer Onboarding (Custom Integration Libraries)</strong> To ensure this new system was adopted rapidly across our polyglot microservices, we abstracted the Consul integration entirely. We built lightweight, plug-and-play libraries for our primary languages (like a custom Spring Boot Starter for Java and a pip package for Python). Instead of every team writing their own HTTP logic to interface with Consul’s API, developers simply include our library, point it at their service name, and the library automatically handles the connection, the dynamic listening, and updating the application’s internal config context at lightning speed.</p><pre>// Before: 50 lines of custom HTTP polling<br>// After: Just one annotation<br>@EnableDynamicConsulConfig(serviceName = &quot;payment-gateway&quot;)</pre><h3>Wrapping Up</h3><p>Moving away from a legacy configuration manager is rarely just a tool swap; it is a fundamental shift in operational philosophy. By combining HashiCorp Consul’s robust, distributed KV store with a strict GitOps pipeline powered by GitHub and Argo Workflows, we successfully eliminated our configuration bottlenecks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7nFxgKgXwHCQsv8JDr7XZg.png" /></figure><p>Developers no longer have to jump through UI hoops or worry about manual syncing errors. Configuration changes are now treated with the exact same rigor, peer review, and automated testing as our application code. By coupling this CI/CD automation with our custom, lightweight integration libraries across our Java, Python, and Node.js microservices, we achieved the holy grail of config management: a system that is fiercely reliable, instantly dynamic, and practically invisible to the engineers using it. The infrastructure seamlessly gets out of the way, allowing us to focus on building features rather than fighting configuration drift.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfd201d9db6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Virtual Threads: Java 21 Vs Java 25]]></title>
            <link>https://medium.com/@aryash.jain/java-virtual-threads-java-21-vs-java-25-a7c714c5920f?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7c714c5920f</guid>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-virtual-threads]]></category>
            <category><![CDATA[project-loom]]></category>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Fri, 02 Oct 2026 09:18:20 GMT</pubDate>
            <atom:updated>2026-10-02T09:18:20.027Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Part 1: Understanding Virtual Threads</strong></p><p>Java Virtual Threads represent one of the most significant innovations in concurrent programming since the introduction of the Java Memory Model. If you’re building high-performance, scalable applications in Java, you need to understand this technology.</p><p><strong>Virtual Threads</strong> are lightweight threads managed by the JVM that make it easier to write, maintain, and observe high-throughput concurrent applications.</p><p><strong>1. What are Virtual Threads?</strong></p><p><strong>The Problem with Platform Threads<br></strong>Traditional Java threads (called platform threads) are OS-level threads with significant drawbacks:</p><ul><li>Heavy memory footprint (~1–2 MB per thread)</li><li>Expensive context switching</li><li>Limited scalability (typically thousands of threads before problems)</li><li>OS scheduler has limited visibility into your application</li></ul><p>Here’s an example of traditional thread limitations:</p><pre>// Traditional approach — Creating many platform threads<br>ExecutorService executor = Executors.newFixedThreadPool(1000);<br><br>for (int i = 0; i &lt; 10000; i++) {<br>    executor.submit(() -&gt; {<br>        try {<br>            Thread.sleep(1000);<br>            System.out.println(&quot;Task &quot; + i + &quot; completed&quot;);<br>        } catch (InterruptedException e) {<br>            Thread.currentThread().interrupt();<br>        }<br>    });<br>}<br><br>executor.shutdown();<br>executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);</pre><p>The problem with this approach: significant memory pressure and context switching overhead.</p><h3>Virtual Threads: The Solution</h3><p>Virtual threads are lightweight abstractions over OS threads that solve these problems.</p><p><strong>Architecture Overview:</strong></p><ul><li><strong>Virtual Threads:</strong> 100,000+ (User-level)</li><li><strong>JVM Scheduler:</strong> Manages Virtual Threads</li><li><strong>Platform Threads:</strong> A few hundred (OS-level)</li><li><strong>OS Scheduler:</strong> Manages Platform Threads</li></ul><p><strong>Key Characteristics Comparison:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ld7FmwvMdc0_joiVuP3dig.png" /></figure><p><strong>2. How to Initialize and Use Virtual Threads</strong></p><p><strong>Method 1: Using Thread.ofVirtual()</strong></p><pre>Thread vThread = Thread.ofVirtual()<br>    .name(&quot;my-virtual-thread&quot;)<br>    .start(() -&gt; {<br>        System.out.println(&quot;Running in a virtual thread!&quot;);<br>        System.out.println(&quot;Thread: &quot; + Thread.currentThread().getName());<br>    });<br>    <br>vThread.join();</pre><p><strong>### Method 2: ExecutorService with Virtual Threads</strong></p><pre>ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();<br><br>for (int i = 0; i &lt; 10000; i++) {<br>    int taskId = i;<br>    executor.submit(() -&gt; {<br>        System.out.println(&quot;Task &quot; + taskId + &quot; running on &quot; + Thread.currentThread().getName());<br>        try {<br>            Thread.sleep(100);<br>        } catch (InterruptedException e) {<br>            Thread.currentThread().interrupt();<br>        }<br>    });<br>}<br><br>executor.shutdown();<br>executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);<br>System.out.println(&quot;All tasks completed!&quot;);</pre><p><strong>Method 3: Thread Factory</strong></p><pre>ThreadFactory virtualThreadFactory = Thread.ofVirtual()<br>    .name(&quot;worker-&quot;, 0)<br>    .factory();<br>    <br>ExecutorService executor = Executors.newThreadPerTaskExecutor(virtualThreadFactory);<br>executor.submit(() -&gt; System.out.println(&quot;Executed by virtual thread!&quot;));<br>executor.shutdown();</pre><p><strong>### Best Places to Use Virtual Threads</strong></p><p><strong>IDEAL FOR:</strong></p><ul><li>Server applications (REST APIs, microservices)</li><li>High I/O workloads (database queries, API calls)</li><li>Applications with many concurrent connections</li><li>Legacy code with blocking I/O</li></ul><p><strong>NOT IDEAL FOR:</strong></p><ul><li>CPU-intensive tasks (computation-heavy work)</li><li>Low-latency, real-time systems</li><li>Tasks that hold locks for long periods</li></ul><p><strong>3. Scalability &amp; Performance Benefits</strong></p><p><strong>### Performance Comparison: Virtual vs Platform Threads</strong></p><p>Scenario: 10,000 HTTP Requests with 100ms Latency</p><pre>public class PerformanceComparison {<br>    <br>    public static void platformThreadApproach() throws Exception {<br>        long startTime = System.currentTimeMillis();<br>        ExecutorService executor = Executors.newFixedThreadPool(200);<br>        <br>        for (int i = 0; i &lt; 10000; i++) {<br>            executor.submit(() -&gt; {<br>                try {<br>                    Thread.sleep(100);<br>                } catch (InterruptedException e) {<br>                    Thread.currentThread().interrupt();<br>                }<br>            });<br>        }<br>        <br>        executor.shutdown();<br>        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);<br>        long duration = System.currentTimeMillis() - startTime;<br>        System.out.println(&quot;Platform Threads Time: &quot; + duration + &quot;ms&quot;);<br>    }<br><br>    public static void virtualThreadApproach() throws Exception {<br>        long startTime = System.currentTimeMillis();<br>        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();<br>        <br>        for (int i = 0; i &lt; 10000; i++) {<br>            executor.submit(() -&gt; {<br>                try {<br>                    Thread.sleep(100);<br>                } catch (InterruptedException e) {<br>                    Thread.currentThread().interrupt();<br>                }<br>            });<br>        }<br>        <br>        executor.shutdown();<br>        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);<br>        long duration = System.currentTimeMillis() - startTime;<br>        System.out.println(&quot;Virtual Threads Time: &quot; + duration + &quot;ms&quot;);<br>    }<br><br>    public static void main(String[] args) throws Exception {<br>        System.out.println(&quot;=== Platform Threads ===&quot;);<br>        platformThreadApproach();<br>        <br>        System.out.println(&quot;\n=== Virtual Threads ===&quot;);<br>        virtualThreadApproach();<br>    }<br>}</pre><p>Expected Results:</p><p>- Platform Threads Time: 5000ms</p><p>- Virtual Threads Time: 1200ms</p><p><strong>### Real-world REST API Example</strong></p><pre>@RestController<br>@RequestMapping(&quot;/api&quot;)<br>public class UserController {<br><br>    @Autowired<br>    private UserService userService;<br><br>    @GetMapping(&quot;/users/{id}&quot;)<br>    public ResponseEntity&lt;User&gt; getUser(@PathVariable Long id) {<br>        User user = userService.fetchUserFromDatabase(id);<br>        List&lt;Order&gt; orders = userService.fetchUserOrders(id);<br>        user.setOrders(orders);<br>        return ResponseEntity.ok(user);<br>    }<br><br>    @GetMapping(&quot;/users/batch/{count}&quot;)<br>    public ResponseEntity&lt;List&lt;User&gt;&gt; getBatchUsers(@PathVariable int count) {<br>        List&lt;User&gt; users = IntStream.range(1, count + 1)<br>            .parallel()<br>            .mapToObj(userService::fetchUserFromDatabase)<br>            .collect(Collectors.toList());<br>            <br>        return ResponseEntity.ok(users);<br>    }<br>}</pre><p><strong>— -</strong></p><h3><strong>4. Issues with Virtual Threads in Java 21</strong></h3><p><strong>Issue #1: Pinning to Platform Threads</strong></p><p>Virtual threads can get “pinned” to their underlying platform thread, preventing efficient scheduling.</p><pre>public class PinningExample {<br>    private static final Object LOCK = new Object();<br><br>    public static void main(String[] args) throws Exception {<br>        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();<br><br>        for (int i = 0; i &lt; 100; i++) {<br>            executor.submit(() -&gt; {<br>                synchronized (LOCK) { // Synchronization causes pinning!<br>                    try {<br>                        Thread.sleep(1000);<br>                        System.out.println(&quot;Task completed&quot;);<br>                    } catch (InterruptedException e) {<br>                        Thread.currentThread().interrupt();<br>                    }<br>                }<br>            });<br>        }<br>        <br>        executor.shutdown();<br>        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);<br>    }<br>}<br></pre><p>Why Its Bad ?</p><ul><li>Virtual thread remains bound to platform thread</li><li>Defeats the purpose of having thousands of virtual threads</li><li>Other virtual threads can’t use that platform thread</li><li>Performance degrades significantly</li></ul><p><strong>Issue #2: No Support for ThreadLocal Inheritance</strong></p><pre>ThreadLocal&lt;String&gt; context = new ThreadLocal&lt;&gt;();<br>context.set(&quot;user-123&quot;);<br><br>Thread vThread = Thread.ofVirtual().start(() -&gt; {<br>    String value = context.get(); // Returns null in Java 21!<br>    System.out.println(&quot;Context: &quot; + value);<br>});</pre><p><strong>Issue #3: Limited Observability</strong></p><p>Java 21 had limited built-in observability:</p><p>- No easy way to monitor how many virtual threads are running</p><p>- Limited thread utilization metrics</p><p>- Difficulty getting detailed stack traces for debugging</p><p><strong>Issue #4: WeakHashMap Performance</strong></p><p>Virtual threads in Java 21 had issues with WeakHashMap due to frequent garbage collection pauses caused by the large number of thread-related objects.</p><h3><strong>5. How These Issues Are Resolved in Java 25</strong></h3><p><strong>Solution #1: Improved Pinning Behavior</strong></p><p>Java 25 significantly reduces pinning issues. Use ReentrantLock instead of synchronized:</p><pre>public class ImprovedPinning {<br>    private static final ReentrantLock LOCK = new ReentrantLock();<br><br>    public static void main(String[] args) throws Exception {<br>        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();<br>        <br>        for (int i = 0; i &lt; 100; i++) {<br>            executor.submit(() -&gt; {<br>                LOCK.lock();<br>                try {<br>                    Thread.sleep(1000);<br>                    System.out.println(&quot;Task completed&quot;);<br>                } catch (InterruptedException e) {<br>                    Thread.currentThread().interrupt();<br>                } finally {<br>                    LOCK.unlock();<br>                }<br>            });<br>        }<br>        <br>        executor.shutdown();<br>        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);<br>    }<br>}</pre><p>Key improvements in Java 25:</p><p>- ReentrantLock no longer causes pinning</p><p>- ReentrantReadWriteLock fully supports virtual threads</p><p>- Reduced unnecessary pinning in core JDK classes</p><p><strong>### Solution #2: Enhanced ThreadLocal Support</strong></p><pre>ThreadLocal&lt;String&gt; context = new ThreadLocal&lt;&gt;();<br>context.set(&quot;user-123&quot;);<br><br>Thread vThread = Thread.ofVirtual().start(() -&gt; {<br>    String value = context.get(); // Now returns &quot;user-123&quot;!<br>    System.out.println(&quot;Context: &quot; + value);<br>});</pre><p>What changed:</p><p>- Virtual threads now properly inherit ThreadLocal values</p><p>- Better support for context propagation</p><p>- Improved integration with existing code patterns</p><p><strong>Solution #3: Better Observability and Monitoring</strong></p><pre>import java.lang.management.ManagementFactory;<br>import com.sun.management.ThreadMXBean;<br><br>public class VirtualThreadMonitoring {<br>    public static void main(String[] args) {<br>        ThreadMXBean threadBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();<br>        ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();<br>        <br>        for (int i = 0; i &lt; 1000; i++) {<br>            executor.submit(() -&gt; {<br>                try {<br>                    Thread.sleep(100);<br>                } catch (InterruptedException e) {<br>                    Thread.currentThread().interrupt();<br>                }<br>            });<br>        }<br>        <br>        System.out.println(&quot;Virtual threads: &quot; + threadBean.getVirtualThreadCount());<br>        System.out.println(&quot;Platform threads: &quot; + threadBean.getThreadCount());<br>        System.out.println(&quot;CPU time: &quot; + threadBean.getCurrentThreadCpuTime());<br>        <br>        executor.shutdown();<br>        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);<br>    }<br>}</pre><p><strong>Solution #4: Improved GC Performance</strong></p><p>- Better memory management for ThreadLocal and related structures</p><p>- Reduced GC pauses with large numbers of virtual threads</p><p>- Optimized object pooling and reuse</p><h3><strong>8. Conclusion</strong></h3><p><strong>### Key Takeaways</strong></p><p>Benchmarking has significantly improved with virtual threads , we are using it in our high QPS services , and they are really helping us in servicing highly concurrent requests without CPU overheads</p><p>If this article helped you in diving deep , please give a Clap and also comment what next should be served next , it can be related to Java , Virtual Threads or Any new programming concept.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7c714c5920f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Large Language Models (LLMs) Actually Work: From Pre-training to Reasoning]]></title>
            <link>https://medium.com/@aryash.jain/how-large-language-models-llms-actually-work-from-pre-training-to-reasoning-f3dce3751ee3?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3dce3751ee3</guid>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Wed, 10 Jun 2026 17:15:25 GMT</pubDate>
            <atom:updated>2026-06-10T17:15:25.646Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Large Language Models Actually Work</h3><p>Most people know LLMs are “trained on the internet.” That’s technically true and also a dramatic oversimplification. Here’s the full story, from raw data to reasoning.</p><p><strong>Deep Technical Breakdown</strong></p><p>Millions of people now ask AI to write code, explain concepts, summarize documents, and reason through complex decisions. Behind that smooth conversational interface lies an extraordinarily sophisticated system trained across multiple distinct stages ,each one serving a different purpose.</p><p>Think of it like educating a student:</p><p><strong>Stage 1 </strong>Pre-training</p><p>The student spends years reading — absorbing knowledge from books, research, and conversations.</p><p><strong>Stage 2 </strong>Fine-Tuning</p><p>The student learns how to answer questions properly — tone, helpfulness, safety.</p><p><strong>Stage 3</strong> Reinforcement Learning</p><p>The student sits practice exams, repeatedly failing and improving through feedback.</p><p>Understanding this pipeline changes how you think about AI entirely. You stop seeing it as magic. You start seeing it as engineering.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jr2eLR2S-E_gV_kxzHpNhA.png" /></figure><h3>Stage 1: Pre-training — How Models Acquire Knowledge</h3><p>Before an AI assistant can answer a single question, it must first learn the structure of language itself. This stage is called pre-training, and at this point the model isn’t an assistant at all — it can’t chat, it doesn’t follow instructions. It simply absorbs patterns from text at an enormous scale.</p><h3>Step 1: Collecting and Cleaning the Data</h3><p>Modern LLMs train on datasets assembled from websites, books, research papers, documentation, forums, and educational content. The raw internet, however, is messy — full of spam, duplicate articles, broken formatting, and contradictory information. So before any training begins, engineers run the data through a careful pipeline.</p><p>First comes filtering: low-quality content is removed. The principle is simple — better data produces better models, and garbage in means garbage out. Next is deduplication: the same Stack Overflow answer might appear across hundreds of sites, and if that content dominates training, the model becomes overly biased toward its specific patterns. Finally, cleaning standardizes text by removing broken HTML, encoding artifacts, and corrupted symbols.</p><p>What remains is tens of terabytes of high-quality text — the foundation on which everything else is built.</p><h3>Step 2: Tokenization</h3><p>Computers don’t understand words. They understand numbers. So before training begins, all text is converted into tokens — small chunks of language, each mapped to a numerical ID.</p><p>Human reads</p><p>“I love backend engineering”</p><p>Model sees</p><p>[145] [827] [6214] [9941]</p><p>Tokenization is subtler than it appears, and it affects model performance in ways most developers don’t expect. Words are often split — <em>engineering</em> becomes <em>engi + neer + ing</em>. The model never sees individual characters the way a human does; it sees compressed token chunks.</p><p>This explains a phenomenon engineers call <strong>jagged intelligence</strong>: an LLM can architect a distributed system with grace but stumble on a simple letter-counting task. It’s not a failure of reasoning — it’s a consequence of how the input was represented in the first place.</p><h3>Step 3: The Training Loop — Predict the Next Token</h3><p>Now comes the core of pre-training. The model is given one deceptively simple objective:</p><p><em>Predict what comes next.</em></p><p>Given <em>“Java is an object-oriented…”</em>, predict <em>programming</em>. Then <em>language</em>. Then <em>that</em>. This process repeats billions of times, across the entire corpus, adjusting the model’s internal weights with each prediction.</p><p>From this simple objective, something remarkable emerges: the model doesn’t memorize documents. It learns <em>statistical relationships</em>. Spring Boot gets associated with REST APIs. Docker gets associated with containers. Kubernetes with orchestration. Over time, the model builds a fuzzy internal simulation of human knowledge — a compressed representation of how concepts relate to one another.</p><p>Why hallucinations happen: Because the model generates statistically likely continuations rather than retrieving verified facts, it can confidently produce incorrect information. Ask it about a future World Cup winner, and it will invent a plausible-sounding answer. It’s not lying — it’s predicting. Treat LLM output as a highly intelligent assistant, never as an unquestionable source of truth.</p><h3>Stage 2: Post-training — Teaching a Model to Be an Assistant</h3><p>After pre-training, the model is knowledgeable but behaviorally erratic. Ask it a question and it may continue your sentence mid-thought, produce random internet text, or generate something entirely off-topic. It was never trained to behave like an assistant — only to predict continuations.</p><p>This is where Supervised Fine-Tuning (SFT) comes in.</p><h3>Learning to Converse</h3><p>Engineers curate thousands — sometimes millions — of examples structured as conversations: a user asks something, and a model responds correctly. Human annotators review and rank outputs across three dimensions that have become the industry standard: helpfulness, truthfulness, and harmlessness.</p><p>Without this stage, none of the familiar chat interfaces we use today would exist. Raw intelligence without behavioral alignment is unreliable in practice.</p><h3>Memory: Parameters vs. Context</h3><p>One of the most useful mental models for working with LLMs is the distinction between two kinds of memory.</p><p>Long-term memory</p><p>Model weights — everything learned during training. Static; doesn’t update between conversations.</p><p>Working memory</p><p>The context window — what the model sees right now. Your prompts, history, uploaded documents. Temporary; lost when the session ends.</p><p>This is why long conversations can degrade in quality: as context fills up, earlier information gets dropped. The model doesn’t “remember” — it reasons over whatever is currently in the window.</p><h3>Tools Extend Reliability</h3><p>Modern AI systems are increasingly augmented with external tools: web search, calculators, code interpreters, and database lookups. Rather than guessing a stock price from training data, the model performs a live lookup. Rather than simulating code execution, it actually runs the code.</p><p>The future of AI isn’t a single giant brain. It’s smart reasoning paired with the right tools at the right moment.</p><h3>Stage 3: Reinforcement Learning — Teaching Models to Reason</h3><p>Knowledge and conversational ability still aren’t enough. For genuinely hard problems -: math, multi-step logic, complex planning ,models need to develop better <em>reasoning strategies</em>. This is the role of reinforcement learning.</p><p>Instead of imitating human responses, the model attempts solutions, evaluates whether its reasoning path was effective, and adjusts accordingly. Correct approaches get rewarded; flawed ones get penalized. Over thousands of iterations, the model develops strategies that no human explicitly taught it.</p><h3>Thinking in Steps</h3><p>One key capability that emerges from this stage is the ability to generate intermediate reasoning traces ,working through a problem step by step rather than jumping directly to an answer. On difficult mathematics, logic, and coding tasks, this produces dramatically better results.</p><h3>Learning from Human Preferences</h3><p>Reinforcement Learning from Human Feedback (RLHF) extends this further. Two responses are shown to annotators, who select the better one. The model learns what users genuinely prefer ,not just what’s technically correct, but what’s useful, well-phrased, and appropriately calibrated to context. This is one of the primary reasons modern chat systems feel so different from their predecessors.</p><h3>What This Means in Practice</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ViCmM32OYZ1LiZQLdoZujQ.png" /></figure><p>LLMs are extraordinary tools ,and they have genuine sharp edges. Understanding the pipeline they come from helps you use them far more effectively.</p><ul><li>Jagged intelligence is structural, not random. When a model fails at something that seems easy, the cause is often tokenization or distributional gaps in training data, not a general lack of capability.</li><li>Hallucinations are a prediction artifact. The model generates likely text, not verified facts. High-stakes outputs always deserve verification.</li><li>Context is everything. The model only “knows” what’s in the window. More context, placed deliberately, produces better results.</li><li>Tools make models more reliable. When accuracy matters, augmenting a model with retrieval or code execution is far better than relying on memory alone.</li><li>Alignment is what makes it usable. The raw pre-trained model is powerful but ungoverned. Fine-tuning and RLHF are what turn it into something you’d actually want to interact with.</li></ul><p><em>The better you understand how LLMs work, the better you can use them, build with them, and reason clearly about their limits.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3dce3751ee3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot Request Lifecycle Deep Dive: Filters, Interceptors, Controller Advice & Execution Flow]]></title>
            <link>https://medium.com/@aryash.jain/spring-boot-request-lifecycle-deep-dive-filters-interceptors-controller-advice-execution-flow-91484f07fd50?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/91484f07fd50</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[interceptors]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[filters]]></category>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Sun, 24 May 2026 10:37:16 GMT</pubDate>
            <atom:updated>2026-05-24T10:40:52.359Z</atom:updated>
            <content:encoded><![CDATA[<p><em>What really happens before and after your controller in Spring Boot?</em></p><h3>Introduction</h3><p>While beginning with your Spring MVC application, have you given a thought about-</p><ul><li>Where should authentication logic go?</li><li>Where should the request logging happen?</li><li>How can request and response level filtering and modification happen.</li></ul><p>Many developers start by putting everything inside controllers. Authentication. Logging. Validation. Metrics. Tracing. Suddenly, controllers become 500-line monsters doing everything except business logic.</p><p>The result? Messy architecture. Poor reusability. Difficult debugging. Code duplication everywhere.</p><p>A clean backend is not just about writing good APIs. It is about understanding where logic belongs in the request lifecycle. This is exactly where Spring Boot’s request/response processing layers become powerful.</p><p>In this article, we will go deep into:</p><ul><li>Request lifecycle in Spring Boot</li><li>Interceptors &amp; Controller Advice</li><li>Execution sequence &amp; Latency impact</li><li>Body &amp; header tampering</li><li>Separation of concerns</li><li>What to use where (Decision Matrix)</li><li>Production-grade best practices</li></ul><p>By the end of this article, you will understand exactly what happens before and after a request hits your controller.</p><h3>Why Do We Even Need Filters?</h3><p>Let us first understand the real problem. Imagine building APIs without filters or interceptors. A typical controller may start looking like this:</p><pre>@RestController<br>@RequestMapping(&quot;/user&quot;)<br>public class UserController {<br><br>    @GetMapping(&quot;/{id}&quot;)<br>    public ResponseEntity&lt;User&gt; getUser(@PathVariable Long id, HttpServletRequest request) {<br>        log.info(&quot;Request received&quot;);<br>        String token = request.getHeader(&quot;Authorization&quot;);<br>        validateToken(token);<br>        metrics.increment();<br><br>        User user = userService.getUser(id);<br>        return ResponseEntity.ok(user);<br>    }<br>}</pre><p>At first glance, this looks fine. But now imagine 100 controllers. You suddenly have authentication logic repeated everywhere, logging in every API, metrics duplicated, and header parsing everywhere.</p><p>Your controller becomes responsible for Authentication, Logging, Metrics, Tracing, Validation, <strong>and</strong> Business Logic. That is bad design. A controller should focus only on business logic. Everything else is called a cross-cutting concern.</p><h3>What Are Cross-Cutting Concerns?</h3><p>Cross-cutting concerns are things required by multiple APIs but are not actual business logic. Examples include:</p><ul><li><strong>Authentication:</strong> Checking JWT tokens.</li><li><strong>Request Logging:</strong> Logging incoming payloads.</li><li><strong>Metrics:</strong> Tracking API latency.</li><li><strong>Correlation IDs:</strong> Tracing requests across microservices.</li><li><strong>Security:</strong> Validating headers and access.</li><li><strong>Compression:</strong> Compressing response payloads.</li></ul><p>Instead of repeating these concerns in every controller, Spring Boot provides request lifecycle hooks: Filters, Interceptors, and Controller Advice.</p><h3>The Big Picture: Spring Boot Request Lifecycle</h3><p>When a client sends a request (e.g., GET /users/1), it does not go directly to your controller. It passes through multiple architectural layers first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*hCNiIiGZYNaoTJYBStzOAg.png" /></figure><p>As you can see, filters execute even before Spring understands which controller will handle the request. That detail is powerful.</p><h3>What Are Filters in Spring Boot?</h3><p>Filters are components that execute before and after an HTTP request reaches Spring MVC. Think of filters as gatekeepers of incoming HTTP traffic. They operate at the <strong>Servlet Layer</strong> (like Tomcat or Jetty), which means they run before the framework routes the request to a specific @GetMapping.</p><p>Filters only know about the HttpServletRequest, HttpServletResponse, and HTTP metadata. They do not know your controller logic.</p><h3>Common Use Cases of Filters</h3><ul><li><strong>Request Logging:</strong> Tracking incoming endpoints.</li><li><strong>Authentication:</strong> JWT token validation.</li><li><strong>Correlation IDs:</strong> Injecting request IDs.</li><li><strong>Header Manipulation:</strong> Adding or modifying headers.</li></ul><h3>Understanding Filters Through Code</h3><p>Imagine a request enters /api/users. Before the controller executes, a filter intercepts it:</p><pre>@Component<br>public class LoggingFilter extends OncePerRequestFilter {<br><br>    @Override<br>    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {<br>        long startTime = System.currentTimeMillis();<br>        log.info(&quot;Incoming Request: {} {}&quot;, request.getMethod(), request.getRequestURI());<br><br>        // This line passes the request forward!<br>        filterChain.doFilter(request, response);<br><br>        long endTime = System.currentTimeMillis();<br>        log.info(&quot;Request completed in {} ms&quot;, (endTime - startTime));<br>    }<br>}<br></pre><p>Notice filterChain.doFilter(request, response). Without this line, the request stops. Your API will never reach the controller.</p><blockquote><strong><em>Why </em></strong><strong><em>OncePerRequestFilter?</em></strong><em> Spring guarantees this filter executes exactly once per request. Plain </em><em>Filter interfaces can accidentally run multiple times during internal dispatches. In production systems, always prefer </em><em>OncePerRequestFilter.</em></blockquote><h3>Can Filters Modify the Request Body?</h3><p>Short answer: Yes. But there is a big catch.</p><p>The HTTP request body behaves like a stream. When you call request.getInputStream(), the stream gets consumed. Meaning: <strong>You cannot read it again.</strong> If your filter reads the JSON body, the controller will later receive an empty body, resulting in mysterious bugs.</p><h3>The Correct Way: Wrappers</h3><p>Spring provides ContentCachingRequestWrapper and ContentCachingResponseWrapper. These wrappers cache the body payload so it can be read multiple times.</p><h3>Should Filters Modify the Request Body?</h3><p>Technically, yes. Architecturally, <strong>usually no</strong>.</p><p>Body mutation introduces hidden side effects, breaks API contracts, and makes debugging a nightmare. Filters should ideally observe, validate, and enrich — not mutate business payloads. Only modify bodies for exceptional infrastructure reasons like decryption, PII masking, sanitization, or compression.</p><h3>Should Filters Modify Headers?</h3><p><strong>YES — This is one of their best use cases.</strong></p><p>Unlike the body, headers are lightweight metadata. They are expected to be enriched.</p><ul><li><strong>Headers = Metadata</strong></li><li><strong>Body = Business Data</strong></li></ul><p>Filters are perfect for injecting X-Correlation-ID, parsing Authorization headers, checking Tenant-ID, or appending security headers like Cache-Control on the way out.</p><h3>Separation of Concern: The Real Superpower</h3><p>This is the biggest benefit of the request lifecycle.</p><p><strong>Without Filters:</strong></p><pre>Controller<br> ├── Authentication<br> ├── Logging<br> ├── Metrics<br> ├── Validation<br> └── Business Logic</pre><p><strong>With Filters:</strong></p><pre>Filter → Authentication<br>Filter → Logging<br>Interceptor → ValidationController<br> └── Business Logic</pre><p>Suddenly, controllers become clean, focused, maintainable, and reusable. This is how scalable backend systems are designed. A clean architecture is not achieved by writing better controllers; it is achieved by putting logic in the correct layer.</p><h3>Interceptors: How Are They Different?</h3><p>Filters intercept the raw HTTP request. <strong>Interceptors</strong> operate at the Spring MVC layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*jHh10zugPsO6vD5w89pFow.png" /></figure><p>Because Interceptors live inside the Spring Context, they know exactly which HandlerMethod (Controller class and method) is going to execute.</p><p>Interceptors provide three hooks:</p><ul><li>preHandle(): Before the controller logic.</li><li>postHandle(): After the controller executes, but before the view/response is fully rendered.</li><li>afterCompletion(): After the entire request has finished (great for resource cleanup).</li></ul><p><strong>Best used for:</strong> Role-Based Access Control (RBAC), API permission checks, audit logging based on the specific controller method being called.</p><h3>Controller Advice: The Global Response Shaper</h3><p>While Filters and Interceptors process traffic before it reaches the core business logic, <strong>Controller Advice</strong> is responsible for what happens afterward, specifically concerning the response shape and exceptional outcomes.</p><p>Developers often litter controllers with try/catch blocks to handle common exceptions (like database constraints, validation errors, or resource-not-found issues), turning controllers into error-handling monsters.</p><p>This approach fails the “Separation of Concerns” principle. Business logic becomes coupled with error reporting, and defining a standard error format across the API becomes impossible.</p><h3>The Solution: @RestControllerAdvice</h3><p>A class annotated with @RestControllerAdvice allows you to define centralized logic for all or some controllers, most commonly used for global exception handling using @ExceptionHandler.</p><p>By moving exception handling here, your controller methods focus only on the happy path.</p><p>By using @RestControllerAdvice, You can pull exception handling completely out of your controllers. It executes <em>after</em> the controller throws an exception, intercepting the error and transforming it into a standardized JSON response.</p><p>Java</p><pre>@RestControllerAdvice<br>public class GlobalExceptionHandler { <br>@ExceptionHandler(UserNotFoundException.class)<br>    public ResponseEntity&lt;ErrorResponse&gt; handleNotFound(UserNotFoundException ex) {<br>        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(ex.getMessage()));<br>    }<br>}</pre><h3>Sequence of Execution</h3><p>This is the exact sequence of events when a request enters a Spring Boot application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dYrolThOO1oEHRxynuZNhg.png" /></figure><ul><li><strong>Request enters application</strong></li><li><strong>Filter (Before):</strong> Servlet filter chain begins executing.</li><li><strong>Dispatcher Servlet:</strong> Spring takes over routing.</li><li><strong>Interceptor </strong><strong>preHandle():</strong> Spring MVC interceptor runs.</li><li><strong>Controller:</strong> Your business logic executes.</li><li><strong>Service Layer &amp; DB:</strong> Core application logic.</li><li><strong>Controller Response:</strong> Controller returns data or throws an exception.</li><li><strong>Controller Advice:</strong> Catches any thrown exceptions and formats errors.</li><li><strong>Interceptor </strong><strong>postHandle():</strong> Runs after successful controller execution.</li><li><strong>Interceptor </strong><strong>afterCompletion():</strong> Final cleanup after the response is ready.</li><li><strong>Filter (After):</strong> Servlet filter chain unwinds.</li><li><strong>Response returned to Client.</strong></li></ul><h3>Do Filters Add Latency?</h3><p>Yes — but it is usually negligible.</p><p>Both Filters and Interceptors generally execute in <strong>microseconds</strong>. Controller Advice adds very little overhead.<strong>The biggest mistake is putting expensive business logic inside filters.</strong> Keep them focused on fast, in-memory metadata validation.</p><h3>Production Best Practices</h3><ul><li><strong>Keep filters lightweight.</strong> They run on every single HTTP request. Milliseconds add up.</li><li><strong>Avoid business logic in filters.</strong> If it requires a database transaction, it likely belongs in the service layer.</li><li><strong>Do not mutate the request body unless absolutely required.</strong> It breaks transparency.</li><li><strong>Prefer header manipulation over body manipulation.</strong></li><li><strong>Always use </strong><strong>OncePerRequestFilter</strong> instead of the standard Filter interface.</li><li><strong>Use Correlation IDs.</strong> Inject them at the very first filter and log them everywhere.</li></ul><h3>IN The End, build not just to build it, but build to scale</h3><p>A clean backend is not built by writing better controllers. It is built by knowing where logic belongs. Separation of concerns should be the priority.</p><p>Filters, Interceptors, and Controller Advice exist to separate infrastructure concerns from business logic. Once you understand the request lifecycle and respect the boundaries of HTTP metadata vs. business data, Spring Boot suddenly becomes much easier to design at scale.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91484f07fd50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why HashMap Fails in Multithreading and How ConcurrentHashMap Fixes It]]></title>
            <link>https://medium.com/@aryash.jain/everything-about-concurrenthashmap-in-java-how-it-solves-multithreading-and-concurrency-problems-df1a81a19b00?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/df1a81a19b00</guid>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Fri, 08 May 2026 05:22:55 GMT</pubDate>
            <atom:updated>2026-05-08T05:50:28.884Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A deep dive into Java concurrency, race conditions, lock-free reads, CAS operations, and why HashMap fails under multithreaded workloads.</blockquote><p>Modern backend systems are highly concurrent. Imagine working on a fintech orchestration service handling:</p><p>- thousands of API requests,<br>- parallel Kafka consumers,<br>- async workflows,<br>- thread pools,<br>- caching layers,<br>- and database operations simultaneously.</p><p>In such systems, multiple threads constantly read and update shared data structures concurrently.</p><p>This is where things become dangerous.</p><p>One of the most common mistakes developers make is using a normal HashMap inside a multithreaded environment. At first glance it may seem harmless. But internally, it can lead to:</p><blockquote>This can lead to severe production issues including data corruption, request failures, infinite loops, CPU spikes, and unpredictable outages.</blockquote><p>I realized this only after a production incident was escalated during a high-traffic window. A critical orchestrator service had started returning 5XX responses unpredictably. Some requests timed out, some hung forever, and CPU consumption suddenly shot upward.</p><p>The root cause was surprisingly small:</p><blockquote>a shared HashMap being accessed concurrently by multiple threads.</blockquote><p>That incident pushed me to deeply understand how concurrent data structures actually behave under multithreaded workloads.</p><h3>The Core Problem: Shared Mutable State</h3><p>Concurrency problems usually begin with one thing:</p><blockquote><em>Multiple threads modifying shared mutable state simultaneously.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*ZgM2zBGhg6RBUt_rZB1L-g.png" /></figure><p>Example:</p><pre>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</pre><p>Now imagine:</p><ul><li>Thread A inserts a value</li><li>Thread B updates another value</li><li>Thread C resizes the map</li><li>Thread D reads from the same bucket</li></ul><p>All at the same time. Without proper synchronization, these operations can overlap unpredictably. This creates classic race conditions in multithreaded Java applications.</p><h3>Why HashMap Is NOT Thread Safe</h3><p>HashMap was designed for single-threaded performance. It performs no synchronization internally. That means multiple threads can simultaneously:</p><ul><li>modify buckets,</li><li>resize arrays,</li><li>rehash entries,</li><li>and change linked lists.</li></ul><p>This can corrupt internal state. Often Ending up in throwing a 5xx or Increasing the CPU consumption.</p><p>For example:</p><pre>Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;();<br>Runnable task = () -&gt; {<br>    for (int i = 0; i &lt; 1000; i++) {<br>        map.put(i, Thread.currentThread().getName());<br>    }<br>};<br>new Thread(task).start();<br>new Thread(task).start();</pre><blockquote>Both threads are modifying the same `HashMap` simultaneously without synchronization. Since `HashMap` is not thread safe, concurrent writes can corrupt its internal bucket structure during insertion or resizing, leading to lost updates, inconsistent reads, high CPU usage, or even infinite loops in older JDK implementations.</blockquote><h4>Another Example: The Resizing Issue</h4><pre>Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;(2);</pre><pre>Runnable task = () -&gt; {<br>    for (int i = 0; i &lt; 10000; i++) {<br>        map.put(i, &quot;value&quot;);<br>    }<br>};</pre><pre>new Thread(task).start();<br>new Thread(task).start();</pre><p>As both threads keep inserting elements, the HashMap eventually triggers a resize operation.</p><p>In older JDK implementations, concurrent rehashing could corrupt the internal linked list structure of buckets, accidentally forming cyclic references.</p><p>As a result, internal traversal logic behaving like:</p><pre>while (node != null)</pre><p>could loop forever because node.next would never become null.</p><p>This often caused:</p><ul><li>100% CPU spikes,</li><li>stuck application threads,</li><li>request timeouts,</li><li>and severe production instability.</li></ul><h3>The Naive Solution: synchronizedMap()</h3><p>Java provided:</p><pre>Collections.synchronizedMap(map)</pre><p>This wraps the map with synchronization.</p><p>Example:</p><pre>Map&lt;String, Integer&gt; map =<br>    Collections.synchronizedMap(new HashMap&lt;&gt;());</pre><p>Now operations become thread safe.</p><p>But another problem appears.</p><h3>The Performance Bottleneck</h3><p>synchronizedMap() uses a single global lock.</p><p>This means that only ONE thread can access the map at a time. Even reads become serialized.</p><p>Imagine: 100 threads mostly reading cache data but waiting on one giant lock.</p><p>As a result concurrency collapses and performance suffers badly.This is where ConcurrentHashMap changed everything.</p><h3>Enter ConcurrentHashMap</h3><p>ConcurrentHashMap was designed specifically for high-concurrency environments.</p><p>Goals:</p><ul><li>thread safety,</li><li>high throughput,</li><li>minimal locking,</li><li>concurrent reads,</li><li>scalable performance.</li></ul><p>Unlike synchronized collections, it does NOT lock the entire map for every operation.</p><h3>Java 7: Segment-Based Locking</h3><p>In Java 7, ConcurrentHashMap divided the map into segments.</p><p>Think of it like:</p><pre>Map<br> ├── Segment 1<br> ├── Segment 2<br> ├── Segment 3<br> └── Segment 4</pre><p>Each segment had its own lock.</p><p>This allowed multiple threads to write simultaneously to different segments.</p><p>Example:</p><ul><li>Thread A locks Segment 1</li><li>Thread B locks Segment 3</li></ul><p>No blocking between them.</p><p>This massively improved concurrency.</p><h3>Java 8: Bucket-Level Concurrency</h3><p>Java 8 redesigned ConcurrentHashMap.</p><p>Segments were removed.</p><p>Instead:</p><ul><li>synchronization became bucket-level,</li><li>and CAS operations were introduced heavily.</li></ul><p>This improved scalability even further.</p><h3>Internal Working of ConcurrentHashMap</h3><p>Internally, it uses:</p><ul><li>an array of buckets,</li><li>hash spreading,</li><li>CAS operations,</li><li>synchronized bucket locking,</li><li>and lock-free reads.</li></ul><h3>Lock-Free Reads</h3><p>One of the biggest optimizations:</p><blockquote><em>Reads usually do NOT require locking.</em></blockquote><p>Example:</p><pre>map.get(key)</pre><p>can happen concurrently across many threads.</p><p>This makes ConcurrentHashMap extremely efficient for read-heavy workloads.</p><h3>CAS (Compare-And-Swap)</h3><p>Modern ConcurrentHashMap heavily relies on CAS.</p><p>CAS is a CPU-level atomic operation.</p><p>Conceptually:</p><pre>&quot;If current value == expected value,<br> update it atomically.&quot;</pre><p>This avoids expensive locking in many cases. CAS is one of the foundations of modern lock-free programming.</p><h3>Bucket-Level Locking</h3><p>When collisions occur, and updates are needed:</p><ul><li>Only the specific bucket is locked.</li></ul><p>Not the whole map.</p><p>This means:</p><ul><li>Unrelated buckets continue operating normally.</li></ul><p>Concurrency remains high.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*8elySdU7PmVNGAetrCO3Hg.png" /></figure><h3>What Happens During Resizing?</h3><p>Resizing is one of the hardest concurrent problems.</p><p>ConcurrentHashMap handles this carefully.</p><p>Multiple threads can actually help in resizing cooperatively.</p><p>This distributes resizing cost across threads instead of blocking everything behind one giant resize lock.</p><p>This is a huge engineering improvement over traditional HashMap.</p><h3>Atomic Compound Operations</h3><p>A very important feature:</p><p>Operations like these are atomic:</p><pre>map.putIfAbsent(key, value)</pre><pre>map.compute(key, ...)</pre><pre>map.merge(key, value, ...)</pre><p>Without these APIs, developers often write unsafe check-then-act code.</p><p>Unsafe example:</p><pre>if (!map.containsKey(key)) {<br>    map.put(key, value);<br>}</pre><p>Between these two operations:</p><ul><li>another thread may modify the map.</li></ul><p>Race condition.</p><p>ConcurrentHashMap solves this using atomic methods.</p><h3>Example: Safe Concurrent Counter</h3><pre>ConcurrentHashMap&lt;String, Integer&gt; map =<br>    new ConcurrentHashMap&lt;&gt;();<br><br>map.merge(&quot;count&quot;, 1, Integer::sum);</pre><p>This safely updates values even under heavy concurrency.</p><h3>When ConcurrentHashMap Still Does NOT Save You</h3><p>Important point:</p><p>ConcurrentHashMap only guarantees thread safety for individual operations.</p><p>NOT for entire workflows.</p><p>Example:</p><pre>if (map.get(key) == null) {<br>    map.put(key, value);<br>}</pre><p>Still unsafe.</p><p>Because the sequence itself is not atomic.</p><p>You must use:</p><ul><li>computeIfAbsent(),</li><li>putIfAbsent(),</li><li>or external synchronization.</li></ul><h3>Performance Characteristics</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*KCjZMvrNtz9T366HAfJjyg.png" /></figure><h3>Real-World Use Cases</h3><p>ConcurrentHashMap is used heavily in:</p><blockquote>API gateways, distributed systems, caching layers, thread pools, metrics systems,rate limiters, microservices, in-memory indexing, session stores, and reactive systems.</blockquote><p>Even many frameworks internally rely on it.</p><h3>One Important Limitation</h3><p>ConcurrentHashMap <strong>does NOT allow:</strong></p><blockquote><strong>null keys</strong></blockquote><blockquote><strong>null values</strong></blockquote><p>Reason: ambiguity in concurrent reads.</p><p>Example:</p><pre>map.get(key)</pre><p>If it returns null:</p><ul><li>Does the key not exist, or is the value actually null?</li></ul><p>Disallowing null removes this ambiguity.</p><h3>Final Thoughts</h3><p>ConcurrentHashMap is one of the most important concurrent data structures in Java. It represents years of engineering improvements around:</p><ul><li>lock granularity,</li><li>non-blocking algorithms,</li><li>memory visibility,</li><li>atomicity,</li><li>and scalable multithreaded performance.</li></ul><p>Understanding it deeply teaches much more than just a collection class.</p><p>It teaches how concurrency breaks systems, why naive synchronization fails, and how modern concurrent programming balances correctness with performance.</p><p>For backend engineers, mastering ConcurrentHashMap is almost a rite of passage into understanding real-world multithreaded systems.</p><p>If this article helped you, DO Give it a Like and a Share.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df1a81a19b00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JWT Based User login -: Refresh Token And Access Token]]></title>
            <link>https://medium.com/@aryash.jain/jwt-based-user-login-refresh-token-and-access-token-d8b5ced036bf?source=rss-74638693b7e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8b5ced036bf</guid>
            <category><![CDATA[backed]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[jwt-token]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[jwt-authentication]]></category>
            <dc:creator><![CDATA[Aryash Jain]]></dc:creator>
            <pubDate>Tue, 20 Feb 2024 18:52:13 GMT</pubDate>
            <atom:updated>2024-02-20T18:52:13.583Z</atom:updated>
            <content:encoded><![CDATA[<p>Whenever we create a JWT-based authentication there are a few basic problems that might arise</p><p><strong>Major problems in JWT-based authentication and login?</strong></p><ul><li><strong>Poor User Experience </strong>-: If we authenticate using a single JWT and store data using it, the user can probably get logged out when the JWT expires. So they will need to sign repetitively</li><li><strong>Security Threat-: </strong>If we try to save the token for a very long time, it will be prone to cyber threats and can be hacked.</li><li><strong>Extra Details passed on to the Client -:</strong> we generally inject a few information while generating the token and someone with a key to it has access to all of the information</li></ul><p>How can we solve this?<br>To solve these issues major tech organizations proposed using two tokens for login, one with major data that will expire soon and another one with less information but longer expiry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dBqTTppHaCqN4BaATuyhmg.png" /><figcaption>HLD of the flow</figcaption></figure><p><strong>To solve these problems </strong>we follow the practice of storing two JWT tokens.<br>The Access Token and The Refresh Token. As the name suggests the Access Token grants access to the logged-in User and the refresh token is used to refresh the access token on expiry.</p><ul><li>The<strong> Access token </strong>is <strong>short-lived</strong> and expires in a very short duration. But the <strong>refresh Token is long-lived</strong> and is used to regenerate the access token.</li><li>The refresh token is stored in the database and while renewing the access token the refresh token is verified and then the renewal is done</li><li>Once new access and refresh tokens are generated the existing refresh token is updated.</li><li>Both access and refresh tokens are then injected in cookies so that user information can be accessed by other services post-login.</li></ul><p>For a video tutorial on the same pls refer to -:</p><p>CHAI AND CODE BACKEND -: <a href="https://youtu.be/EH3vGeqeIAo?si=B7yrnm0mnM8I_WE4">https://youtu.be/EH3vGeqeIAo?si=B7yrnm0mnM8I_WE4</a></p><p>For code and implementation -: <a href="https://github.com/aryashjain/Backend_PlayGround">https://github.com/aryashjain/Backend_PlayGround</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8b5ced036bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>