One small rule in the schema does more for you under load than almost anything in your code. It is the first part of the sentence this chapter is building towards.
Unique constraint
A rule that no two rows may have the same value in a column, or in a set of columns. The database checks it as each row goes in, so it holds even when two people try in the same millisecond. The loser gets an error.
Asked to stop duplicate signups, an agent writes the obvious thing: look for the email, and insert if it isn't there. That passes every test, because tests run one at a time. In production, two requests can both look before either inserts. The only check that can't be raced is the one inside the database.
“A SELECT before the INSERT won't stop duplicates under concurrency. Add a unique constraint on users.email, catch the duplicate key error and return a 409. Add a test that fires 20 signups with the same email at once with Promise.all, and asserts one row.”