Skip to main content

Posts

Showing posts with the label Java

πŸ₯‘πŸ₯’JSON vs HashMap — Why They’re Cousins, Not Twins! πŸ₯‘πŸ₯’

πŸ— Structure 1️⃣ Setting the Scene — The Confusion Moment πŸ€” "I thought JSON was just a HashMap in a superhero costume!!" Nope! It’s like saying Spring Boot is just Spring MVC with coffee ☕ — there’s way more going on. 2️⃣ πŸ’‘ What is a HashMap? Java-only data structure Stores key-value pairs in memory (heap) Keys can be anything — Integer, String, custom objects (if hashCode() & equals() are implemented properly) Not meant for network transfer πŸ“¦ Example: Map<Integer, String> studentMap = new HashMap<>(); studentMap.put(101, "Anand"); studentMap.put(102, "Priya"); This stays inside your JVM. If you try to send it over HTTP as-is… good luck explaining it to a Python service. 🐍πŸ’₯ 3️⃣ πŸ“ What is JSON? Language-independent data format (Java, Python, JS, Go, you name it) Stores only String...

πŸš€ “Amazon-Style Microservice Performance Issues — My Interview Battle & How I Fixed It”

πŸš€ Faced This in an Interview — Microservice Performance Issues (Amazon Example) Interviewer: "In a large e-commerce platform like Amazon, what kind of performance issues could happen at the microservice level? And how would you solve them?" Me (thinking): "Performance issues? You mean like the time my cart took longer to load than my mom deciding what to order at Amazon Pantry? πŸ˜…" πŸ›’ The Amazon Microservice Setup User Service → Authentication Product Service → Show available products Cart Service → Manage your cart Order Service → Process payment & order Inventory Service → Update stock after purchase All of these talk to each other over REST APIs or gRPC. πŸ’₯ The Performance Problem Scenario: Big Diwali Sale πŸŽ‡ — millions of requests per minute. Suddenly the Order Service takes 3 seconds instead of 200ms . Customers refresh → double load → πŸ’£ disaster. Possible Causes: πŸ”— Service Chaining Latency — One slow API (like Inv...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

πŸ’‘ Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😡‍πŸ’« “Why is Redis cache not working?” 🀯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🀷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } πŸ•΅️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: πŸ›‚ Watchman = Spring Proxy 🏠 Your apartment = Your service class πŸšͺ Your room = A method inside that class πŸƒ Scenario 1: Outsider Visits Your friend from outside...

πŸ—“️ That Day I Learned Something Loggically Shocking! πŸ€―πŸ’‘ Clue: A tiny "+" in your logger can silently become your app's performance enemy! πŸ›⚠️

πŸ—“️  That Day I Learned Something Loggically Shocking! 🀯 Hey everyone! πŸ‘‹ So recently while working on my project, I stumbled upon something super basic — yet kinda blew my mind. πŸ’₯ I wrote this: log.info("hi {}", 0); Then I asked myself... πŸ€” "Why not just write this?" log.info("hi" + 0); I mean… both give me hi0, right? 🧡️ What could possibly go wrong? Turns out — A LOT. Let me take you into this funny-yet-real rabbit hole πŸ•³️πŸ‡ of how a tiny change in logging style can silently mess with your app’s performance — and how even your logs have trust issues! πŸ˜‚ πŸ” First of All — What is Log4j? Log4j (Logging for Java) is a popular logging library used to capture logs (debug, info, error, etc.) in your Java applications. It gives you control over: What gets logged (log level) Where it goes (console, file, DB) How it's formatted (pattern layout) And much more... It's basically your app's diary πŸ“œ — but wi...

🌟 Serialization & Deserialization Optimization: Why is My App Slow? The Surprising Answer!

 πŸ” Introduction: πŸ‘‹ Ever wondered why your application, which was blazing fast in development, suddenly slows down in production? Well, I was once in your shoes! When I was a fresher in Java, I was stuck on a weird performance issue that left me scratching my head. It wasn’t until I found out about serialization and deserialization that the whole mystery was solved. Let me walk you through a real-world problem that I faced and how serialization almost ruined my performance . πŸ€” What is Serialization? πŸ“¦ Serialization is the process of converting an object into a byte stream , so that it can be saved to a file or transmitted over the network . It's like packing your things into boxes before you move. 🏠 But here's the twist: What if you pack your stuff into too many boxes , and each one is overstuffed ? 🀯 That’s inefficient serialization ! And it hurts your performance . πŸ’‘ Here’s the Surprise: When I first started working with large datasets, I didn’t even th...

πŸ’¬ JVM Crash Deep Dive | Heap Dumps, RCA, and Developer Curiosity

  ☕ Have you ever had a Java application just... vanish ? No stack trace. No clear error. Just a core dump or a mysterious hs_err_pid file left behind like a clue from a crime scene. Over the past few weeks, I’ve been wrestling with this. 🧠 I kept asking myself: What really causes a JVM crash ? Is it always the app’s fault? How can I truly diagnose and prevent it? And how do I make sense of that gigantic heap dump file? So I went deep into the internals. Here’s what I found πŸ‘‡ πŸ” Why JVM Crashes (Beyond Just “OOM”) Yes, we all know about OutOfMemoryErrors. But JVM crashes often involve subtle, deeper issues: 🧬 Native Code Issues – JNI calls from Java to C/C++ can corrupt memory if not handled properly πŸ•Έ️ Threading Chaos – Deadlocks, race conditions, or blocked critical threads can destabilize the runtime πŸ—‚️ DirectByteBuffer Abuse – Memory allocated outside the heap can escape GC tracking 🧱 Corrupt JVM Installation – Misconfigured JDKs, mixed versions, or...

πŸ” "Billing Gone Wrong: How Lack of Synchronization Led to Swapped Bills (And What I Learned!)"

☕ A Walk Down Memory Lane: My First Bug Nightmare About 9 years ago, as a fresher, I worked on a billing system for a retail application. The system was accessed both from mobile and web UI , and the actual billing logic was written in stored procedures (SQL-based backend). Everything looked fine... until customers started reporting weird issues : πŸ› The Bug: Bills Were Swapping Items! 😱 🧾 Scenario: Two different users generated separate bills around the same time One from Mobile , the other from Web The output bills had mixed-up items — each invoice had products that belonged to the other one! 🧨 πŸ” Initial Questions: "Is this a DB issue?" "Are stored procedures broken?" "Race condition in app layer?" Turns out... 🚫 The code calling stored procedures wasn’t thread-safe. There was no synchronized mechanism to ensure each bill transaction was isolated. 🧠 The Root Cause Analysis (RCA): We were calling a shared BillingService.calculat...

πŸš€ Demystifying JUnit Testing with Mockito & Spring Extensions

πŸ‘‹ Before diving deep into testing, I always assumed: "JUnit is just something you use with @Test… it just works!"   But once I actually started exploring test frameworks in real-world projects, I realized there's so much more behind the scenes. So here’s a blog to clarify the confusion , make it visually digestible , and hopefully save you some debugging hours . Let’s roll! 🧠✨ πŸ” Step 1: What dependencies should I include for JUnit? You may have seen these terms in your build.gradle : testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' implementation 'com.example:some-library:1.2.3' But wait... what’s the difference? πŸ€” ✅ implementation This means: Include the library for both compile time AND runtime . It will be packaged inside the final JAR/WAR. Use it for things your production code needs. πŸ§ͺ testImplementation This means: Use it only during test execution . It will NOT be added to the production JAR/WAR. Perfect for JU...

πŸ” “MongoDB OIDC with Spring Boot: Driver Drama & Terraform Troubles (And How I Solved Them)”

  🧩 Introduction Setting up MongoDB with OIDC authentication in a production-grade Spring Boot app sounds simple — until reality strikes. πŸ˜… This post covers two specific issues I faced when integrating MongoDB v8 with Spring Boot 3.3.6 , and deploying the app via Terraform to Cloud Run . If you’re planning the same, ame, save yourself some debugging time and read on. πŸ‘‡ πŸ› Issue #1: MongoDB Driver Compatibility – “Unsupported authMechanism: MONGODB-OIDC” πŸ”₯ The Problem: After upgrading to MongoDB v8 , my app started throwing this error: Unsupported authMechanism: MONGODB-OIDC Turns out, the default MongoDB driver version (5.0.1) pulled in by Spring Boot 3.3.6 isn’t compatible with MongoDB v8, especially when using OIDC authentication . Turns out, the default MongoDB driver version (5.0.1) pulled in by Spring Boot 3.3.6 isn’t compatible with MongoDB v8, especially when using OIDC authentication . ✅ The Fix: Manually specify compatible driver versions ( 5.2.1 or abo...

🧭 My Deep Dive Into HashMap Internals πŸ”‘πŸͺ„

  πŸ‘‹ Everyone knows HashMap as “key & value”… but what’s inside? Honestly, I was also in that majority who just knew: πŸ‘‰ “HashMap = key & value storage.” …and never cared how it actually works under the hood . But recently I got curious and explored it myself, and here’s what I learned πŸ‘‡ (sharing in case it helps someone else too πŸ’›). πŸ”§ HashMap, HashSet, HashTable — what do they use internally? All of them rely on a common concept: Hashing Technique ⚡ …but there’s more happening than I expected! πŸ—️ How does Hashing work here? Think of it as splitting data into an array of buckets : ✅ Each bucket holds entries ( key → value ). ✅ When you insert, the hash function decides which bucket to drop your data into. ➡️ In Java: Your key’s hashCode() is used. That hash code is transformed into a bucket index. Inside that bucket, Java stores a Map.Entry (key and value pair). Why override hashCode() ? ✔️ To give a good distribution → fewer collisions → faste...

πŸ“Œ Database Indexing: In‑Depth Knowledge πŸ“–⚡

πŸ”Ž 1. What is an Index? Think of a database index like the index in the back of a book : πŸ“š Without an index: πŸ‘‰ To find every mention of “performance,” you’d have to read the entire book page by page. πŸ‘‰ In database terms, this is a full table scan (slow!). πŸ“š With an index: πŸ‘‰ You flip to the back, find “performance,” and see a list of page numbers. πŸ‘‰ You jump straight there. πŸ‘‰ In database terms, an index is a small, sorted data structure with pointers to rows — so you can quickly locate data. ✅ In short: An index is a data structure (often a B‑Tree) that speeds up lookups in a table or collection. πŸš€ 2. Why are Indexes Important? ✅ Mainly used for SELECT queries — especially with: WHERE clauses JOIN conditions ORDER BY GROUP BY They don’t usually help INSERT/UPDATE speed — they help you READ data faster. πŸ“¦ Scenario: An E‑commerce Database Customers Table: customer_id     first_name     last_name     email    ...