Shopify / Shopify/identity_cache

Support checking a percentage of cache fetches against the database

Open
#492 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Ruby
Stars
2k
Forks
174
Avg merge
13m
Merged PRs (30d)
3

Description

Problem

It can be hard to get a sense of how wrong the cache normally is (e.g. from cache invalidations being lost due to network issues) or notice when application bugs (e.g. writes that don't trigger after_commit) or unknown Identity Cache bugs make it worse.

Proposal

Add support for checking a percentage of cache hits for correctness against the database. This could then be exposed with ActiveSupport::Notifications.instrument, which could be used to get a correctness ratio over time to see regressions being introduced and that could also be split by cache index to notice bugs that affect a subset of cache indexes.

The data loaded from the database can be serialized and compared to the serialized data fetched from the cache. If they differ, then we can attempt to CAS set the data loaded from the database to the cache to both correct the cached value.

Cache invalidations aren't done atomically with the database write, so we should aim to reduce false positives. Detecting CAS set conflicts when correcting the cached value is one way to do this, but it is still possible for a recent database write to be loaded and for the cache invalidation to complete after the cache value is "corrected". As such, we should provide the maximum updated_at timestamp in any cached rows to try to get the age of the database data which can then be used to exclude recently written data when trying to find incorrectness from missing cache invalidations. Note that these timestamps are affected by clock skew and are from the time before the write rather than the commit time. An appropriate threshold for excluding recently written data should include the sum of the maximum expected clock skew, maximum transaction duration and the maximum expected duration to invalidate the cache after the transaction commits; we might just want to conservatively use the hard timeout duration for web requests & jobs for simplicity.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by tracing IdentityCache cache-hit handling and the database fetch path, then review the proposed ActiveSupport::Notifications, serialization, CAS correction, and updated_at age checks. Done means a documented design and implementation for sampled correctness checks that limits false positives.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.