Stop reading the same device row twice on every poll (relay#51) #102

Merged
paulhitt merged 1 commits from fix/device-row-read-twice into main 2026-09-04 20:40:58 +00:00
paulhitt commented 2026-09-04 15:34:34 +00:00 (Migrated from gitlab.com)

Found by relay#38's cost measurement, and the first thing it ranked.

authenticate loads the device row to check that the token's cnf key
matches the identity key on file. poll then loaded the same row, by the same
primary key, microseconds later, for the revocation check that answers 410.
Two identical reads on the busiest route in the system, paid once per poll by
every phone with the app open.

authenticated now hands the row back alongside the caller, and poll uses
it. Measured: send-and-collect drops from ten database round trips to nine, a
poll from five to four. docs/COST.md carries the post-fix numbers.

The two checks stay two checks. They are not the same question — the
binding check asks whether this token's key matches what is registered, the
revocation check asks whether the device is registered at all, and it answers
410 rather than 401 because "sign in again" is the wrong instruction for a
device somebody has removed. What is shared is the row, not the decision.

The id comparison kept in the guard cannot fail today: the row was loaded
for the caller's device and the guard above already refuses anything else. It
is there so that reordering those two guards can never quietly turn this into
one device's registration standing in for another's.

The cost budgets were also tightened to sit near the measurements rather than
at double them — a ceiling of twenty against a measured ten would not have
caught the doubling it exists to catch.

631 relay tests in 56 suites green, core paths 7/7.

Co-Authored-By: Claude Opus 5 noreply@anthropic.com

Found by relay#38's cost measurement, and the first thing it ranked. `authenticate` loads the device row to check that the token's `cnf` key matches the identity key on file. `poll` then loaded the same row, by the same primary key, microseconds later, for the revocation check that answers 410. Two identical reads on the busiest route in the system, paid once per poll by every phone with the app open. `authenticated` now hands the row back alongside the caller, and `poll` uses it. Measured: send-and-collect drops from ten database round trips to nine, a poll from five to four. docs/COST.md carries the post-fix numbers. The two checks stay two checks. They are not the same question — the binding check asks whether this token's key matches what is registered, the revocation check asks whether the device is registered at all, and it answers 410 rather than 401 because "sign in again" is the wrong instruction for a device somebody has removed. What is shared is the row, not the decision. The id comparison kept in the guard cannot fail today: the row was loaded for the caller's device and the guard above already refuses anything else. It is there so that reordering those two guards can never quietly turn this into one device's registration standing in for another's. The cost budgets were also tightened to sit near the measurements rather than at double them — a ceiling of twenty against a measured ten would not have caught the doubling it exists to catch. 631 relay tests in 56 suites green, core paths 7/7. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
paulhitt commented 2026-09-04 15:34:35 +00:00 (Migrated from gitlab.com)

assigned to @paulhitt

assigned to @paulhitt
paulhitt commented 2026-09-04 20:40:56 +00:00 (Migrated from gitlab.com)

deleted the feat/measure-conversation-cost branch. This merge request now targets the main branch

deleted the `feat/measure-conversation-cost` branch. This merge request now targets the `main` branch
paulhitt commented 2026-09-04 20:40:58 +00:00 (Migrated from gitlab.com)

mentioned in commit 199a474b5d

mentioned in commit 199a474b5d838e9c12e89b742086a85844aa1dfc
paulhitt (Migrated from gitlab.com) merged commit 199a474b5d into main 2026-09-04 20:40:58 +00:00
paulhitt commented 2026-09-09 18:59:08 +00:00 (Migrated from gitlab.com)

changed the description

changed the description
Sign in to join this conversation.