Skip to content

fix(incremental): read tag states with one MGET; per-key only on Cluster - #36

Merged
leejpsd merged 2 commits into
mainfrom
fix/isr-tag-read-mget
Nov 24, 2026
Merged

leejpsd merged 2 commits into
mainfrom
fix/isr-tag-read-mget

Conversation

@leejpsd

@leejpsd leejpsd commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #34. Marko's benchmark put this handler at six to seven times fortedigital's Redis command count for the same steady traffic. The ISR handler read each tag's state with its own GET: auto-pipelined, so one round trip, but one command per tag, and a page carries an implicit _N_T_ tag for every layout segment on top of its own tags. With per-command pricing (Upstash) or command-rate limits, that count is the bill.

The per-key reads were there for Redis Cluster: ISR tag keys are deliberately un-namespaced so revalidateTag survives a deploy, which means they never share a hash slot and a multi-key MGET is refused with CROSSSLOT. That is a Cluster constraint, not a reason to pay per tag on a single node.

What changes

  • readTagStates reads all of an entry's tags with one MGET. A single tag stays a plain GET.
  • On Cluster it reads key by key, as before. It knows it is on Cluster from a new isCluster flag that adaptCluster sets on RedisClientLike, or from hashTag: true. A raw Cluster client passed through a factory without either is caught on its first refusal: the handler latches to per-key reads for good, retries that read in place so nothing is lost, and logs one warning.
  • Two spellings of the refusal are recognised: the server's CROSSSLOT ..., and ioredis's client-side All the keys in a pipeline command should belong to the same slot, which is what you actually get with enableAutoPipelining: true (found by running it against the 3-master cluster).
  • The same latch covers the other cases where MGET can never work: a Redis user whose ACL allows GET but not MGET (NOPERM), a server or proxy that does not know the command, and a custom client whose mGet is missing or does not return one value per key. Without it those reads would fall back to the local tag mirror every time and miss invalidations made by other instances. I hit the ACL case in QA against a real Redis: a hard revalidateTag on a second handler was not seen by the first until this was added.
  • Transient errors (timeouts, dropped connections) degrade to the local tag mirror, as before, and do not latch.
  • Concurrent first reads that all hit the refusal log one warning, not one each.
  • tag.expiration.read is now emitted by the ISR handler too, with meta.strategy: "mget" | "per-key".

Commands per get()

scripts/bench-isr-tag-read.mjs, local Redis 7, 200 reads per row, entry GET included:

tags on the page one MGET (this PR) per-key (Cluster, and every release so far)
1 2 2
3 2 4
5 2 6
7 2 8
10 2 11

Round trips are unchanged (one either way); only the command count moves.

Tests

  • Unit (tests/unit/isr-tag-read.test.ts, 17): one MGET per read, no tag keys touched for a tag-less entry, single tag is a GET, soft and hard invalidations from another instance still land through the MGET path, hashTag: true and isCluster start per-key, both cross-slot spellings latch without losing the read, NOPERM / unknown command / missing mGet / malformed reply latch, concurrent refusals warn once, a timeout and other errors fall back without latching, per-key reads after a latch still honour invalidations.
  • Integration (tests/integration/isr-tag-read.test.ts, redis@5 and ioredis): one MGET with three keys per read; hard and soft revalidateTag from a second instance seen by the first; a real ACL user with -mget still sees a second instance's hard invalidation.
  • Cluster e2e (tests/cluster/cluster.test.ts, +2): the cluster adapter never issues an MGET and still sees a hard invalidation; an adapter with the flag hidden hits the refusal once, latches, and later reads make no second probe.
  • Totals: 229 unit, 42 integration, 7 cluster.
  • One-off differential run (not committed): two handlers on one Redis, one on the MGET path and one forced per-key, 1,500 random set / soft / hard / get steps, comparing miss / fresh / backdated on every read. 0 mismatches in 5 of 5 runs with writes kept at least 2 ms apart. Without that gap the only mismatches were entries written in the same millisecond as a hard invalidation, including a single-tag entry where both handlers take the same GET path, so that is the harness writing the two entries 1 ms apart, not the read strategy.

Patch changeset. No option changes; hashTag keeps its meaning and gains a documented side effect in the README, API reference and agent skill.

…ter (#34)

The ISR handler read each tag's state with its own GET: one round trip
thanks to auto-pipelining, but one command per tag, and a page carries an
implicit _N_T_ tag per layout segment. That is six to seven times the
command count of other handlers under the same traffic, which is what
per-command pricing and command-rate limits see.

Per-key reads stay for Redis Cluster, where the un-namespaced tag keys
never share a slot: from the start when the adapter is flagged isCluster
(new on RedisClientLike) or hashTag is set, otherwise from the first
CROSSSLOT reply on, retried in place. ioredis with auto-pipelining refuses
client-side with a different message, which is recognised too.
@leejpsd
leejpsd force-pushed the fix/isr-tag-read-mget branch from a85a408 to cbac5a5 Compare November 24, 2026 01:46
QA against a real Redis: with an ACL user that may GET but not MGET, every
multi-tag read fell through to the local tag mirror, so a hard
revalidateTag from a second handler was not seen. Releases up to 1.1.0
read key by key and were not affected.

NOPERM, an unknown command, a client without mGet and a reply that is not
one value per key now latch to per-key reads the same way CROSSSLOT does,
retrying that read in place. Timeouts and dropped connections still fall
back without latching. Concurrent first reads warn once.
@leejpsd
leejpsd merged commit db92190 into main Nov 24, 2026
5 checks passed
@leejpsd
leejpsd deleted the fix/isr-tag-read-mget branch November 24, 2026 02:23
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant