[PR #29] Narrowing the Response-Time Gap Between Unknown and Registered Numbers
- What changed: The response-time gap for a code check between an unknown number and a registered number was reduced. An unknown number now takes the same path.
- Decision: The response status, code, and body stay the same, and only the checking process was made the same.
- Still unknown: The measurement was done once in this server setup, so whether it holds under real operating load is not yet known.
In PR #28, the content of the code-check response was made the same whether or not a phone number was registered. But the time each response took still differed. PR #29 measured that difference and narrowed it. Only one function in the code-check service was changed.
What was done and why
- For an unknown number, the response ended as soon as the user lookup found nothing. For a registered number, the code ran a transaction (a unit that groups several database operations), locked and read the code rows, compared a fingerprint value (an HMAC, a hash made with a secret key), and saved the failure count.
- Repeated timing therefore revealed whether a number was registered. Claude confirmed the gap, then made unknown numbers take the same path.
- An unknown number now also locks and reads the code table, using an empty ID that matches no row.
- The fingerprint is computed once per purpose. Before, it was computed once for each of that person’s code rows.
- The failure count is always saved with one SQL statement. If there are no rows to increase, an empty list is passed. The number of database round trips becomes the same.
- The response status, code and body are unchanged.
Options chosen and rejected
- Chosen: take the same path for real. This follows the same idea as login, where an unknown number is compared against a placeholder value so the timing matches.
- Rejected 1: wait on purpose. Sleeping for a set time on unknown numbers breaks again whenever the database gets faster or slower. Matching the real work is sturdier.
- Rejected 2: delay every response to a minimum time. This slows the whole server, and the difference shows again once real processing exceeds the limit. It also needs a separate decision about the limit.
- Rejected 3: always update a dummy row for the failure count. This could match the cost of actually changing a row. But dummy rows would have to be kept in the table and managed. The remaining gap was small, so this was not done now.
How it was checked
- Because time is hard to fix in a test, a test was written first to compare how many steps each case goes through. Before the change, the test failed on the registered-number case because the failure-count step ran zero times. After the change, it passed.
- The full check command (
npm run verify) passes, with 98 tests. - Response times were measured with a temporary test file that is not in the repository. Three cases were run alternately, 60 times each, with the first 10 discarded. This was repeated three times.
- Before the change, the three distributions did not overlap at all. A few measurements were enough to tell whether a number was registered.
- After the change, an unknown number and a registered number without a live code overlap closely. The case with a live code still differs by about 1 ms.
- Unknown numbers got about 4 ms slower. Code checks are not a frequently called address, so this was accepted.
- These measurements are of the service function alone, without HTTP. Real network and rate limits are not included, so the numbers must not be read as the real-world difference.
Review
- ChatGPT (Codex, model gpt-6-luna) reviewed the change statically, without running tests. The work record lists the same Codex reviewer as PR #28, so the same model name is given. It raised two points. First, the remaining gap of about 1 ms depends on how many live code rows exist. Second, a spy (a device that records calls) could be left behind when a test fails. The second point was already handled by the test teardown.
- The first point was confirmed as correct. However, issuing a new code for the same person and purpose revokes the earlier one, so there is at most one live row per purpose.
Lessons
- A timing difference appears where only the unknown case finishes quickly. The “exists but has no code” case showed up separately, and it would have been missed without measuring first.
- Matching the content of failure responses is not enough. The time taken must also follow the same path.
- A timing result is only meaningful with its measurement conditions attached, so others can interpret it.