Skip to main content

Posts

🕰️ Cron Chaos in the Cloud? How I Learned to Outsmart Double Scheduler Execution! 🧠💥

 🔍 When I first learned Spring's @Scheduled , it felt too easy… @Scheduled(cron = "0 0 9 * * ?") // Every day at 9 AM public void sendDailyReports () { System.out.println( "Triggered!" ); } Done, right? Well… almost. 😅 But then came the million-dollar question 💸💣: 💬 “What happens if I deploy this in a cloud setup like GCP or OpenShift with minimum 2 instances running?” Will it execute twice ? Once on each instance? Or will the cloud gods be kind and run it just once? 🌩️ Let’s take a deep dive — not from a bug I faced, but something I researched deeply before implementing my first scheduler in the cloud. 😵 Wait, Will It Run Twice?!? Let’s say your Spring Boot app is deployed in GCP with 2 pods (or OpenShift with 2 replicas). Your app starts... and both instances boot up this code: @Scheduled(cron = "0 0 9 * * ?") public void emailBoss () { System.out.println( "📧 Sent morning update!" ); } ⚠️ Unless you ...

🧙 How Does Spring Boot Really Find Stuff?! (And How @SpringBootApplication + META-INF/spring.factories Do Their Secret Scan Magic)

  😵‍💫 When I started working in Spring Boot, I had a silly doubt… but it unlocked a serious concept . I knew that @SpringBootApplication does a lot — it’s the entry point of every Spring Boot app. So far, I only knew the basics: @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan But one day I asked myself: 💭 “How does Spring Boot load classes from external libraries like spring-security , spring-web , or even a custom JAR I wrote and added in dependencies? I never wrote any @ComponentScan for them… so how are their beans magically appearing inside my Spring Context?” Yeah yeah… people say “Auto-configuration” or “Classpath scanning.” But how ? Let’s deep dive 🔍 🚪 Let’s First Talk About @ComponentScan By default, this only scans packages below the class where @SpringBootApplication is placed. So if your MainApp.java is in com.anand.app , it will scan: com.anand.app.* But will it scan classes inside: org.springframework.sec...

🐞 Production Bug: 100% CPU Spike from Empty Catch Block — Fixed with Spring Retry! 😮🔥

  🌪️ The Chaos Begins It was a normal day in production... until suddenly, one server started heating up 🔥. And I don’t mean metaphorically — its CPU usage shot up to 100% and stayed there, like it had chugged 10 cups of espresso ☕💥. No OutOfMemoryError. No Exceptions. No logs screaming for help. Just... silence and a fan spinning like a helicopter 🌀. 🧩 The Initial Clues We SSH'd into the server and ran: top -H -p <java_process_id> We noticed one thread hogging the CPU — continuously. Then we used VisualVM to peek inside. Stack trace of that CPU-hogging thread looked like this: at com.example.MyProcessor.process(MyProcessor.java:42) at com.example.MyProcessor.run(MyProcessor.java:30) ... Hmm… That class wasn’t doing anything fancy. Just a retry mechanism inside a loop. 🔍 The Suspect Code Here’s what the code looked like: while ( true ) { try { doSomeCriticalOperation(); break ; // Exit on success } catch (Exception e) { ...

🌟 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...

🔐 Is final Really Final in Java? The Truth May Surprise You 😲

💬 “When I was exploring what to do and what not to do in Java, one small keyword caught my eye — final . I thought it meant: locked, sealed, frozen — like my fridge when I forget to defrost it.”   But guess what? Java has its own meaning of final… and it’s not always what you expect! 😅 Let’s break it down together — with code, questions, confusion, jokes, and everything in between. 🎯 The Confusing Case: You Said It's Final... Then It Changed?! 🫠 final List<String> names = new ArrayList <>(); names.add( "Anand" ); names.add( "Rahul" ); System.out.println(names); // [Anand, Rahul] 🤯 Hold on... that’s final , right?! So how on earth is it still changing ? Time to dive deeper... 🧠 Why Is It Designed Like This? Here’s the key secret: In Java, final applies to the reference , not the object it points to . Let’s decode this like a spy mission 🕵️‍♂️: Imagine This: final List<String> names = new ArrayList <>(); Be...

🧠 Java Production Trap I’ll Never Forget – Let’s Talk About ConcurrentModificationException

  💬 “When I was a fresher, I used to wonder: what things should I never do in Java?” That curiosity led me to explore tons of weird bugs and war stories people had faced in production. Some were scary, some silly — but all of them were super valuable to understand deeply. Today, I’m sharing one such issue I came across. ⚠️ P.S. I didn’t write this code — but I sure learned a lot digging into what went wrong and how it was fixed. 😅 This post is for the curious devs out there — especially freshers who want to learn the "why" behind Java's behaviors, not just memorize "what to do". 💥 The Problem: ConcurrentModificationException in a Simple Loop 📍Environment: This happened in a Spring Boot microservice deployed on OpenShift (Linux-based). Interestingly, it didn’t throw any error in the developer's local Windows machine during testing. But in prod? BOOM. 💣 Here’s the actual buggy code snippet: List<String> users = getActiveUsers(); // ...