identity_cache icon indicating copy to clipboard operation
identity_cache copied to clipboard

how to handle inconsistency?

Open saiqulhaq-hh opened this issue 3 years ago • 4 comments

I am having many issues when using .fetch method

IdentityCache::RecordNotFound: An error occurred on xxx

I could solve it by overriding the .fetch method with

module IdentityCache
  module WithPrimaryIndex
    module ClassMethods
      def fetch(id, includes: nil)
        fetch_by_id(id, includes: includes) || find(id)
      end
    end
  end
end

which is not a correct way

I am just wondering do you have this issue too in Shopify?

saiqulhaq-hh avatar Oct 25 '22 06:10 saiqulhaq-hh

@saiqulhaq-hh what kind of issues are you facing? I'm having issues with IDC as well, as described at https://github.com/Shopify/identity_cache/issues/525

raivil avatar Dec 19 '22 22:12 raivil

@saiqulhaq-hh IDC will try to load the record from cache (if enabled) and if it's missing it will pull it from DB, so when IDC raises RecordNotFound error , it couldn't find it in DB either.

driv3r avatar Jan 17 '23 11:01 driv3r

IDC will cache the absence of a record, so this could just be a cache inconsistency, which is an expected occurrence when cache invalidations fails. I think we failed to document this, so I opened #529 to update the caveats to mention this symptom of lost cache invalidations:

IdentityCache also caches the absence of database values (e.g. to avoid performance problems when it is destroyed), so lost cache invalidations can also result in that value continuing to remain absent. As such, avoid sending the id of an uncommitted database record to another process (e.g. queuing it to a background job), since that could result in an attempt to read the record by its id before it has been created. A cache invalidation will still be attempted when the record is created, but that could be lost.

dylanahsmith avatar Jan 17 '23 17:01 dylanahsmith

Also, we would ideally have a way to opt-out of nil caching (as discussed in #501)

dylanahsmith avatar Jan 17 '23 17:01 dylanahsmith

thanks @dylanahsmith the readme doc looks good

saiqulhaq-hh avatar Jun 21 '24 07:06 saiqulhaq-hh