Dharmendra Bhanushali’s Post

🚗 Tricky Java Bug — “When Ola’s Cache Broke Serialization: The serialVersionUID Mystery 🧩” 🎬 The Scene At Ola, a backend dev cached user data using Java serialization. Everything ran perfectly in staging. But the moment they deployed a new version… 💥 java.io.InvalidClassException: com.ola.user.UserInfo; local class incompatible: stream classdesc serialVersionUID = 124578, local class serialVersionUID = 987654 Suddenly, users vanished from cache faster than an Ola cab during rain 😅 💣 The Root Cause When Java serializes an object, it stores a unique identifier — serialVersionUID. If you don’t explicitly declare it, JVM generates one automatically based on class structure (fields, methods, etc.). So when someone adds or removes a field later, boom — the calculated ID changes, and deserialization fails because the stored bytes no longer match the “current version” of the class. ⚙️ The Problem Code public class UserInfo implements Serializable { private String name; private int age; private String city; } Then one fine day… someone adds a new field: private String gender; 💣 Old cache data can’t deserialize anymore. ✅ The Fix Always define a fixed serialVersionUID to maintain compatibility: public class UserInfo implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; private String city; private String gender; // newly added } 🧩 Quick Debugging Tips 🔍 Check the exception message — it always mentions both stream and local IDs. 🧠 Use serialver tool to generate UID for older class versions. 🚫 Don’t rely on JVM-generated IDs if your class might evolve over time. 💾 When backward compatibility isn’t needed — clear the cache before redeploy. 🔄 Consider using JSON-based cache (like Redis with Jackson) for human-readable, version-tolerant data. ✅ Quick Checklist ☑️ Always declare serialVersionUID manually. ☑️ Avoid unnecessary structural changes in serializable classes. ☑️ For distributed cache, prefer JSON serialization over Java native. ☑️ Be aware that adding/removing fields changes serialization compatibility. 💬 Real Talk At Ola, one dev said: > “My cache invalidated itself before I could even write the logic for it!” 😅 Lesson: In Java, serialVersionUID isn’t just a number — it’s your backward compatibility insurance policy. #Java #Serialization #Ola #TrickyBugs #Cache #BackendDevelopment #SpringBoot #Debugging #Developers #TechHumor #Concurrency #Microservices #JavaInterview #SoftwareEngineering

To view or add a comment, sign in

Explore content categories