I ran the same query 10,000 times: almost 2 seconds of database work for an answer that never changed. The database did not fail. I just kept asking it the same question.
Before fixing it, I wrote down a prediction: a cache should make this about 100 times faster. Keep that number. It is wrong in a way that teaches something.
The whole pattern is a map
Cache-aside is the oldest trick in the book: a map sitting in front of the database. Check the map first. Finding nothing is a miss: run the real query, store the answer on the way out. Next time the map already holds it. That is a hit, and the database never even hears about it.
Map<String, Result> cache = new ConcurrentHashMap<>();






