Invental/Review lessons/Check, then act on stale state

Check, then act on stale state

Limits that hold by construction

If a limit has to be checked in two places before a lock, two writers can each pass it. Prefer a limit that holds by construction, such as “at most N references per field”, over a computed total that has to be re-checked.

01 · Before and afterThe pattern

TypeScript
// before: total checked on save A and save B separately, both outside the lock -> racy
if (expandedLength(field, currentVars) > MAX_LEN) throw new BadRequest(...)

// after: a structural bound that no concurrent edit can break
const MAX_REFS_PER_FIELD = 3;               // 3 refs x 8,192-char values => bounded output
if (countRefs(field) > MAX_REFS_PER_FIELD) throw new BadRequest(...)

02 · Run it on your appThe check

If your app expands user-supplied templates, is the largest possible output bounded by the input rules alone, without a check that a concurrent edit can get around?

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.