DEV Community

Cover image for Same "Seat Taken", Before And After The Race
Anton Brilliantov
Anton Brilliantov

Posted on

Same "Seat Taken", Before And After The Race

Whether the keeper saw the seat was taken, or two visitors sat down at the same moment, "seat taken" should sound the same. Built, barely used yet.


👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.

This part is about built code that is barely used yet: the package is in the shared library, and no service imports it so far. The printed write guard in our services follows the same rule by its own route; this part is about the rule and the library's version of it.

This is part 9 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.


In real life

There is one good bench in the zoo, in the shade, next to the giraffes. Miso is on it. Pickle walks up: "Can I sit?" Nora, passing by, answers for the cat: "Seat taken." Clear, polite, done.

The next day the bench is empty, and Pickle and Biscuit see it at the same moment. They both run, they both sit, and they land on each other. "Ouch!" "Mine!"

A four-panel comic: Pickle asks to sit on a bench where Miso sits and Nora says seat taken; Pickle and Biscuit sit on the bench at once, ouch, mine; Biscuit complains he got error 23505 while the other got seat taken; Nora says seat taken, both times, and Biscuit says fair is fair

The joke is in the third panel. One of them gets told "seat taken". The other one, who lost the race by a hair, gets told "error 23505". Same bench, same outcome - you do not get to sit - and two completely different answers depending on when exactly you lost. Biscuit is outraged, and he is right. In the last panel Nora fixes it the only fair way: "Seat taken. Both times."

Two ways to lose a key

A service that keeps something unique - an email, a name, a slug - usually checks before it writes: is the key taken? If it is, the caller gets a clean refusal with a reason and the field. That is the keeper who saw the cat on the bench.

But a check before a write cannot see a write that happens between the check and the write. Two requests can both pass the check, and one of them loses at the unique index. What that loser hears depends on how the write path was written. Often it is the database's own error - a unique violation code - mapped to something generic, or leaking through as is.

Three tiles with faces: a pre-check, happy; the write, worried; one refusal, proud; labels read free, write, and race recognised; pills read built, and barely used yet

For the caller that is two answers to one question. A client that handles "seat taken" well will fail on the race, and nobody will see it in tests, because tests rarely race.

One reason, both times

The library's conflict package makes the rule explicit: a pre-check that finds the key taken and a race recognised at the write give the same refusal. A probe answers "is it taken?" before the write; a recogniser looks at the write's error and answers "was that a race on this key?"; both produce one reason with one field. In the shape of the code - names shortened:

// one refusal for a taken key - a form
if probe.Taken(ctx, key) {
    return fault.Reason(KeyTaken, "email")   // seen before the write
}
err := write(ctx, row)
if race.Race(err) {
    return fault.Reason(KeyTaken, "email")   // two writers at once
}
Enter fullscreen mode Exit fullscreen mode

A dark card with the conflict form; the line with the race returning the same reason is highlighted; pills read built, and barely used yet

Path What the caller heard before What it hears now
pre-check finds it taken a reason and the field the same reason and field
race lost at the write a database code, or a generic error the same reason and field
no conflict success success

Columns: reasons a caller sees for one taken key - before 2, after 1; Pickle says one answer, easy to test; a pill reads by design

The benefit is not only fairness. One reason means one branch in the client and one test case for it. And the race path - the one nobody tests because it is hard to make happen - now goes through the same code as the easy path, which is the one everybody tests.

What a lost race looks like from inside

In PostgreSQL, the loser of the race gets an error like this - the shape is standard, the names are placeholders:

ERROR:  duplicate key value violates unique constraint "<table>_<column>_key"
SQLSTATE 23505
Enter fullscreen mode Exit fullscreen mode

That error is true and useless to a caller. It names a constraint, not a field the caller sent; it uses the database's vocabulary, not the API's; and it says nothing a client can branch on except "something about uniqueness". The recogniser's job is to read it and answer one question: was this a race on the key I care about? If yes, the caller gets the same reason and field as the pre-check. If it is some other unique constraint, it stays some other error.

This is also why the pre-check is still worth keeping. It is not the guarantee - the unique index is - but it is the common path, it is cheap, and it gives the answer without a failed write. The race path exists for the rare moment when two people want the same bench, and now that moment sounds exactly like the common one.

An animation: writer A and writer B both see the seat free; A writes first; B loses the race and turns angry; B hears seat taken, field email, and turns happy

Built, barely used yet

Bars: files 5, commits since October 0, services importing it 0; Miso, bored, says a fine bench, nobody sits; pills read built, and barely used yet

Here is the honest state. The package is small - five files - and it has not changed since it landed. No service imports it. Our services guard their writes with a printed write guard that follows the same rule - one refusal for a taken key, with the race mapped - and the library package is the shared version of that rule waiting for its first tenant. When I wrote a list of things in the library that are declared but not taken, this one was on it.

Before and after: two answers - a reason when the pre-check saw it; a db code when the race lost it - and one answer - 1 reason with the field, both times; 1 test for the caller to write

What went wrong

The failure this answers is the classic one: the pre-check gives a nice answer, the race gives an ugly one, and the ugly one only appears under load. A pre-check alone feels safe, because every test passes - tests rarely send two requests at the same instant.

The second thing that went wrong is the one I am reporting on myself: a rule this useful, built in the shared library, with no service using it. A package nobody imports is a bench nobody sits on - fine to look at, and it proves nothing until someone does. The next step for it is a first real caller, not more code.

Service tiles with faces: the pre-check, the race check and the caller are happy; the bench and services bored; the library proud; pills read built, and barely used yet

What I do not claim

  • Not that any service uses the package. None imports it yet.
  • Not how often the race happened in production. I did not measure it.
  • Not that the printed write guard and the package are the same code. They follow the same rule.
  • Not the exact API. The Go above is a form.

What of this is method

  • One question, one answer. "Taken" sounds the same before and after the race.
  • Route the hard path through the easy one. The race ends in the code everybody tests.
  • A rule nobody uses is a proposal. Count the callers before counting it done.

Zoo keeping, built in - Part 9. Next: The Feeder That Learns From Bites.

If you pre-check unique keys, what does the loser of a race hear? If you map unique violations, do they become the same reason as the pre-check? If you have built code nobody uses yet, how do you find its first caller? What does "seat taken" sound like in your API?

Top comments (0)