Java Bug Fix Interview Question - Caching User Profiles
Story
A junior engineer introduces an in-memory cache to speed up user lookups. Performance improves at first… but soon users start seeing stale data, memory usage keeps increasing, and under load the service behaves unpredictably.
Here is the implementation:
Discuss all the issues with the above implementation and provide potential solution:
Issues
Cache Never Expires
Once data is cached, it lives forever.
If a user updates their profile, they still see old data.
Memory Leak Risk
HashMap keeps growing with every new user access.
Long-running service = OOM risk.
Not Thread Safe:
Multiple threads may modify the map simultaneously:
race conditions
inconsistent reads
possible data corruption
No Eviction Strategy
Cold users and rarely accessed entries stay forever.
📢 Consider becoming a paid subscriber for as low as $2.5/mo (with an annual subscription) and support the work :)
Not convinced? Check out the details of the past work
Suggestions / Fixes
Thread-safe + TTL using ConcurrentHashMap
CachedUser class stores the expiry time and implements the logic for expiry.
Client code stores all the cached users in a concurrent HashMap, which is thread-safe. It also defines TTL, which we set in the CachedUser object.
If passed userId exists in the cache and it’s not expired, we return the user without hitting the database. But if it does exist, we query the database and get the user, add them to the cache along with TTL, and return the fresh user.





