guava icon indicating copy to clipboard operation
guava copied to clipboard

Exception when one key has no value in Cache.getAll

Open julsal opened this issue 9 years ago • 9 comments

Cache interface was designed to throw an exception in the case of null or empty values. While this seems a reasonable behavior for Cache.get, I didn't figured out how to deal when calling Cache.getAll.

I/O is expensive, so I collect all data I need at once using Cache.getAll. In the case that at least one key has no value, it throws an exception, which does not allow me to access the other valid values.

I found to options: first, using Cache.get instead of Cache.getAll; second, including null values as Optional. Both options are terrible considering performance. Is there a recommended solution to deal with this scenario? Otherwise, if possible, how could I help to improve the Cache API in this issue?

julsal avatar Aug 03 '17 23:08 julsal

I'm not sure why you consider Optional "terrible considering performance." Relative to almost any I/O operation, I'd expect the boxing incurred by Optional is going to be relatively minor. I'd expect that to be the appropriate solution in most cases.

Do you have performance data -- perhaps from CacheStats -- suggesting otherwise?

lowasser avatar Aug 03 '17 23:08 lowasser

@lowasser thanks for your quick reply. Using Optional forces me to include in the cache data that should not be there. Even knowing that I can flag the key to be quickly removed, it doesn't seem an adequate solution specially if I have a high occurrence of null values. In an extreme example, if the frequency of null values is so high that it exceeds the maximum cache size, the cache becomes an useless overhead.

julsal avatar Aug 04 '17 00:08 julsal

If the frequency of not being found but requested is high then there will be a lot of unnecessary I/O and hash table locking. In that case negative caching of the misses is useful to reduce that, which is where Optional can be used as a sentinel value. If that leads to cache bloat then the best option is use a Bloom Filter to filter out most absent requests. This is a fairly standard idiom inside databases and a recent example is LinkedIn's feed rewrite, which uses enhancements to Guava that they contributed. So your worst-case is bad for many reasons and requires a richer solution for a workload with a high miss rate.

The cache interface was designed in a Java 4/5 world, where Guava was a leading force in reducing null as a common return or parameter value. In this case Josh Bloch felt strongly in favor for using a checked exception, where checked is for recoverable conditions. This and implementation details fit the trade offs of the time and would be hard to argue against even in retrospect if transported back then.

In the cache rewrite nulls are favored over exceptions. This is to match Java 8's compute methods and that getUnchecked or not handling the checked exception is the most common case. The update to a Java 8 API gave the opportunity to change this, but its more about keeping consistent with the community's coding style. I don't think that Guava's approach is incorrect, though, because when performance matters you'll have to add a filtering technique regardless to reduce bottlenecks.

ben-manes avatar Aug 04 '17 04:08 ben-manes

@ben-manes thanks for your explanation and references. Bloom filter sounds nicely, but would bring the overhead of managing a new structure ensuring consistency with the original data source. Given the options, I'll go for Optional and measure in a real scenario how it affects the performance. Thanks again @ben-manes and @lowasser for your help.

julsal avatar Aug 04 '17 12:08 julsal

Maybe I'm missing something, but it sounds like you're thinking solely in terms of operations defined by LoadingCache, which is a subinterface of Cache.

Do the Cache methods getIfPresent()/getAllPresent() do what you want?

jrtom avatar Aug 04 '17 14:08 jrtom

@jrtom I don't think so. I guess the OP wants getAllPresentLoadAbsentWhenPossible, which would be an aggregate of exception ignoring gets. When working with single elements, you may throw and handle it in get, so that you don't pollute the cache (though polluting it may be a good thing as Ben wrote). I guess, something like

try {return getAll();} catch (....) {return getAllPresent();}

could come close to what he requested. Despite the double lookup, it could be more efficient than the alternatives, assuming that what @ben-manes wrote doesn't apply in this case.

Maaartinus avatar Aug 04 '17 16:08 Maaartinus

@Maaartinus That could have a race due to an eviction, for whatever reason. It tiny improvement would be to throw a custom exception with the results and then use cache.putAll to insert them. Then the results would be cached and the found set returned.

ben-manes avatar Aug 04 '17 16:08 ben-manes

@ronshapiro Any plans of adding this in the future? I am also looking for something similar. Give it a list of IDs, load whatever is in there. Then, when I need to get them one by one, I throw and catch an exception when one is not there and act accordingly (which in my case is requesting it with a different service). Thanks!

FlavyP avatar Sep 04 '19 09:09 FlavyP

@FlavyP Try it in Caffeine, I think it matches the desired behavior.

ben-manes avatar Sep 04 '19 15:09 ben-manes