Invental/Review lessons/Background jobs when something fails

Background jobs when something fails

Redis hash tags don’t make KEYS cheap

A Redis hash tag decides which node holds a key; it does not make KEYS cheap, so keep an index or counter for any key family you need to enumerate.

01 · Before and afterThe pattern

TypeScript
// before: blocks Redis while it walks every key in the database
const cycles = await redis.keys(`{${runId}}:wp:*`);
// after: read the counter the writer already keeps, then check existence in one round trip
const high = Number((await redis.hget(`{${runId}}:seq`, "c")) ?? 0);
const keys = Array.from({ length: high }, (_, i) => `{${runId}}:wp:${i + 1}`);
const p = redis.pipeline(); keys.forEach((k) => p.exists(k));
const found = (await p.exec())!.map(([, n], i) => (n ? keys[i] : null)).filter(Boolean);

02 · Run it on your appThe check

Does any background job in your app call Redis KEYS (or an unbounded scan) on the same instance your requests use?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.