Databases · Senior · Question 14 of 75

Why should the uniqueness of an idempotency key be enforced with a unique constraint rather than a 'check, then insert'?

Explanation

Insert the key first (ON CONFLICT DO NOTHING), and only the request that inserted it does the work. Others return the stored result.

Further reading

Problem with a question

Databases · Senior · question 14

Test yourself

Take the Databases · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz