SetFailSafe(true) and exception handling #234
-
Hi, I'm trying to understand the behaviour when fail safe is enabled. Am I right in thinking any exception at all from the factory method will be treated as downtime/failure, consumed and return from cache, not exposing the exception to the caller? What if the factory method may throw a genuine exception, such as a "not found" exception in a database query? This will effectively be ignored? e.g.
I want to use the factory timeout feature to allow re-caching to occur in the background where that operation is slow, but it looks like that only works with fail safe enabled and it looks like that will hide exceptions we want. Out database is file based (LiteBD) and less likely to ever be "down" so not that bothered about fail safe, but lags in caching have serious impacts on performance with the frequency of requests we have to handle, which is why I'm interested in FusionCache. Ideally not found would return null not throw an exception, but have complex calls with multiple items in params and one or more may not exist so an exception is thrown detailing which doesn't exist, for example. Also, kind of related? #203 Though don't want to trigger fail safe without throwing an exception, but bypass fail safe in some situations. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @tjmoore and thanks for using FusionCache!
Yes and no: IF you enable fail-safe AND there's a stale (expired) value to use as a fallback (AND/OR you haven't specified a fail-safe default value in the call) then yes, no exception will be exposed to the caller since that's the whole point of fail-safe. BUT if there's no stale value (like, the first time a factory is called and you haven't specified a fail-safe default value) then the factory exception will be exposed to the caller.
Yes, since throwing an exception when something is not found is typically considered not a good practice.
The behaviour you described is in fact how it works, but again I would suggest not to throw exceptions when "something is not in the database" or similar. The reason is that even "xyz doesn't exist" should be cached, maybe for less time if you want, but still cached. Otherwise think about your service and what would happen if I start repeatedly asking for "/product/99999999" (just an example): you just opened a venue for a good old DoS attack, and your database will be bombarded constantly by external calls.
If you really want your data layer to throw when not found, I would suggest to try/catch that exception inside the factory and return null and/or cache that for less time, for example by using Adaptive Caching. Does this make sense?
Not really, I think: that issues is for activating fail-safe even without throwing, whereas you here are asking for kinda the opposite: being able to throw without activating fail-safe.
Hope this helps, let me know. |
Beta Was this translation helpful? Give feedback.
Hi @tjmoore and thanks for using FusionCache!
Yes and no: IF you enable fail-safe AND there's a stale (expired) value to use as a fallback (AND/OR you haven't specified a fail-safe default value in the call) then yes, no exception will be exposed to the caller since that's the whole point of fail-safe.
BUT if there's no stale value (like, the first time a factory is called and you haven't specified a fail-safe default value) then the factory exception will be exposed to the caller.