Learning center

The reviewer says your cache can go stale: find the write that skips it

A stale-cache claim is right when some write changes an input the cached value depends on and that write never reaches the invalidation. List the inputs, find every path that writes each one, and settle each path with a test that writes, then reads the cache. Decline the claim for a path only when you can show the path cannot occur.

Turn the claim into a list of write paths#

A cache entry depends on a set of inputs. The invalidation is correct when every write to any of those inputs clears or refreshes the entry. A reviewer that says "this can go stale" is naming one write it thinks the invalidation misses, or one it could not find a test for. Both are checkable in the code.

In ORM code, the writes that most often miss an invalidation are the ones that bypass the hook it listens on. Django's QuerySet.update() "does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals". Bulk inserts, raw SQL, migrations and foreign keys set to NULL on delete can do the same. An invalidation wired to post_save misses all of them unless something else covers them.

A missing test, added on request#

In May 2026, NetBox opened a pull request that stores each device's and virtual machine's rendered config context, with signal handlers that "invalidate cached data upon any applicable change". A NULL value means "invalidated; render on demand". Pull request

Its automated Claude review listed, among other items, "No test for Cluster.type/group change → VM cache invalidation". It explained that the code "registers a handler for Cluster changes", "But there's no test in test_configcontext_cache.py that exercises a Cluster.type or Cluster.group change and verifies the associated VM's cache is invalidated." It raised the same point for a tenant's group. Claude review

The maintainer answered item by item. Against both items he wrote "Added tests". Maintainer's reply The merged test is the write-then-read shape this claim calls for:

def test_direct_upstream_cluster_type_change_invalidates_vm(self):
    ct1 = ClusterType.objects.create(name='CT1', slug='ct1')
    ct2 = ClusterType.objects.create(name='CT2', slug='ct2')
    cluster = Cluster.objects.create(name='Cluster', type=ct1)
    site = Site.objects.create(name='Site', slug='site')
    vm = VirtualMachine.objects.create(name='VM', site=site, role=self.role, cluster=cluster)
    _set_cache(vm, {'cached': True})

    cluster.snapshot()
    cluster.type = ct2
    cluster.save()

    self.assertIsNone(_get_cache(vm))

Merged test file

It sets a cached value, writes an upstream input the way the application does, and asserts the cache is empty afterward. The handler already existed, so here the reviewer found a gap in coverage, not a defect. The test is what keeps a later refactor of the handler registry from opening the gap.

Over-invalidation is a cost, not a stale read#

The same review said that reparenting a tenant group triggered invalidation although config contexts match tenant groups by direct equality: "This over-invalidates but doesn't produce stale caches." The maintainer marked it "Fixed". At the merge commit, TenantGroup no longer appears in the reparent handlers in extras/signals.py, and still appears in the delete handlers. Merged signals

The reviewer classified this one correctly itself. Extra invalidation costs renders. Missing invalidation serves wrong data. Weigh the two kinds of claim differently.

The merged file also shows a write path found by reading. A comment there says that deleting an upstream object referenced through a SET_NULL foreign key "silently nulls that FK with a bulk UPDATE that emits no post_save signal", so the code invalidates on pre_delete instead.

A claim declined because the path cannot occur#

In August 2026 a follow-up pull request fixed invalidation queries that ran on whichever database Django's routers chose, instead of the one the save used. That left "a stale pre-rendered config context" on deployments with routers. Pull request Its Claude review said: "The alias is resolved too late — reads and writes can still split when using is None". Claude review The maintainer replied: "Only a concern when using=None, which does not occur in practice." Maintainer's reply

That reply holds for the signal handlers. Django passes every model signal a using argument, "The database alias being used", and at the merge commit every invalidation call in extras/signals.py forwards it. I did not check callers outside that file, such as plugins.

Decide by write path#

What you findDecision
A write changes a cached input and nothing on its path invalidatesChange. Add the invalidation and a write-then-read test for that path
The invalidation exists but no test exercises that writeAdd the test. The claim is about coverage
The write bypasses the hook the invalidation listens on (update(), bulk insert, raw SQL, cascade)Change, or invalidate where that write happens
The claim is that something invalidates too muchDismiss or simplify. It costs work and does not serve stale data
The path the reviewer describes cannot be reached from any callerDismiss, and list the callers you checked
A time-to-live bounds the staleness and the product accepts that windowDismiss, and cite where the window is agreed

Write the test against the application's real write, as the NetBox test does with cluster.save(), not against the invalidation function. A test that calls the invalidation directly proves the function works and says nothing about whether the write reaches it. When the bot could not have seen the handler registry because it sits outside the diff, the context guide covers how to tell a reviewer's blind spot from a defect.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading