Skip to main content

Posts

Showing posts with the label Performance

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

Redis Cache Issue – Why @Cacheable Didn’t Work (and Fix)

 Hi 👋, I’m Anandharaj. Today’s learning is about Redis Cache in Spring Boot.   We faced an interesting issue at work (Project) where @Cacheable didn’t behave as expected. 👉 The key takeaway: Avoid self-invocation when using @Cacheable methods. Let me explain with code examples below. ❌ Non‑Working Code (Self‑Invocation Issue) In the below snippet, the processData() method calls another @Cacheable method internalGetData() inside the same class.  Because it’s an internal call, Spring’s proxy is bypassed, so caching never happens. ✅ Working Code (Fixed with separate bean) To fix it, we separated the cached method into another Spring-managed bean (another class). Now the call goes through the proxy and caching works as expected. 💡 Key Takeaways: ✔️ Avoid calling @Cacheable methods from within the same class. ✔️ Move the cached method to another Spring bean. ✔️ This ensures the proxy logic and caching work correctly.