Notes

Two first deploys to one name have exactly one winner

When two assistants deploy to the same new site name at once, juudd gives it to exactly one and tells the other in a sentence, because a claim is one insert.

When two first deploys reach the same new site name at the same moment, exactly one of them gets the site. The other is told, in a sentence, that the name belongs to another account and that it should choose one nobody has taken. Which of the two wins is decided by the database, not by whichever write landed last.

It used to be otherwise, and the way it was otherwise is the reason this post exists.

Ownership used to be a file, and a file cannot say no

Who owned a site was recorded as a small file in the same store that holds the site's content. Claiming a name went in three steps: read the owner file, find nothing, write one.

Two callers can both do step one before either has done step three. Both read "unowned". Both write. The store accepted a plain write with no condition on it, so the second write simply replaced the first.

The deploy that lost reported success, and the site now belonged to someone else. Nothing detected it and nothing recorded it. The customer who lost found out later, by being refused on a site they had just deployed.

That is the quiet kind of failure this platform tries not to have. A loud failure is one the caller can read. This one was a success message that was wrong.

A claim is one insert, and the insert answers

Ownership is now a row, keyed by the site name. Claiming is a single statement: insert the row, do nothing if the name is already there, and return the name only if a row was actually written.

insert into site_owner (site, account_id, claimed_at, claimed_by)
values ($1, $2, $3, $4)
on conflict (site) do nothing
returning site

Because the name is the key, two concurrent inserts cannot both succeed. One gets a row back and one gets nothing. The caller learns whether it won from that answer and from nothing else. The code deliberately does not follow the insert with a separate read to find out who won, because that read would reopen the exact window the key closes: the loser could read the winner's row and believe it was its own.

This is not a mitigation of the race. The race is gone, because the thing that used to be a read followed by a write is now one operation.

An empty answer is not a failure

Getting nothing back means somebody already holds the name. The next move is to read the row and see who.

  • If the holder is the same account, the call goes on. Deploying twice to your own site is the ordinary case, and it works the same way every time.
  • If the holder is another account, the call is refused with the sentence above, and the refusal is written into the account's history like every other refusal.

So the loser of a race and a person who typed an already-taken name get the same answer, in the same words, at the first call rather than on a later one.

What is checked before the insert, and why

The insert is permanent, so the claim path asks its other questions first. Is the name a valid site name. Is it one the platform reserves. Is the account suspended. Does the account already hold all the sites it may hold. A refusal on any of those leaves nothing behind.

The claim also happens before anything is stored, so a name cannot be taken by someone else between the check and the first version landing under it. Of the tools that act on a site, the deploy is the one that may claim a name nobody holds yet.

The one race we left, on purpose

The count of sites an account may hold can still be raced. Two callers claiming their last two allowed names at the same instant can both read a count under the limit and both proceed. The code says so beside the check, and the reason is proportion: the key still decides who gets a name, and being one site over a guardrail for one account is not something the platform can measure as harm. Locking every claim to make that number exact would be more machinery than the number is worth.

The distinction is the point. A name has exactly one owner, always. A count is a guardrail, and it is allowed to be off by one.

The rule, once

A claim that two callers can both believe they won is not a claim, so ownership is one keyed insert whose answer is the only thing a caller is allowed to act on. The tools and their limits are listed in the docs.