Skip to main content
added 111 characters in body
Source Link
pllee
  • 1.2k
  • 1
  • 9
  • 13

TLDR; Absolutely fix this at the ui layer and add absolutely add a unique index if it fits the data model.

I am sorry I think some of the answers are missing a big point here. A unique index is an amazing tool that should be used as much as possible when it fits the data model. Data integrity and not relying on the "client" in the data model is key for building resilient apis and unique indexes can absolutely help with that. But using a unique index to fix a race condition should only be done when it fits the data model.

We don't have enough context here to say a unique index is the best solution since it may end up breaking valid user input. "whatever it is that defines the uniqueness of the table" might not work because there might not be a valid business case for unique enforcement. My stock order shouldn't fail the second time if I buy 20 shares at 102.12 dollars and then make the same order 5 minutes later because a unique index for (user, ticker, amount, price) was added to fix a race condition.

I am sorry I think some of the answers are missing a big point here. A unique index is an amazing tool that should be used as much as possible when it fits the data model. Data integrity and not relying on the "client" in the data model is key for building resilient apis and unique indexes can absolutely help with that. But using a unique index to fix a race condition should only be done when it fits the data model.

We don't have enough context here to say a unique index is the best solution since it may end up breaking valid user input. "whatever it is that defines the uniqueness of the table" might not work because there might not be a valid business case for unique enforcement. My stock order shouldn't fail the second time if I buy 20 shares at 102.12 dollars and then make the same order 5 minutes later because a unique index for (user, ticker, amount, price) was added to fix a race condition.

TLDR; Absolutely fix this at the ui layer and add absolutely add a unique index if it fits the data model.

I am sorry I think some of the answers are missing a big point here. A unique index is an amazing tool that should be used as much as possible when it fits the data model. Data integrity and not relying on the "client" in the data model is key for building resilient apis and unique indexes can absolutely help with that. But using a unique index to fix a race condition should only be done when it fits the data model.

We don't have enough context here to say a unique index is the best solution since it may end up breaking valid user input. "whatever it is that defines the uniqueness of the table" might not work because there might not be a valid business case for unique enforcement. My stock order shouldn't fail the second time if I buy 20 shares at 102.12 dollars and then make the same order 5 minutes later because a unique index for (user, ticker, amount, price) was added to fix a race condition.

Source Link
pllee
  • 1.2k
  • 1
  • 9
  • 13

I am sorry I think some of the answers are missing a big point here. A unique index is an amazing tool that should be used as much as possible when it fits the data model. Data integrity and not relying on the "client" in the data model is key for building resilient apis and unique indexes can absolutely help with that. But using a unique index to fix a race condition should only be done when it fits the data model.

We don't have enough context here to say a unique index is the best solution since it may end up breaking valid user input. "whatever it is that defines the uniqueness of the table" might not work because there might not be a valid business case for unique enforcement. My stock order shouldn't fail the second time if I buy 20 shares at 102.12 dollars and then make the same order 5 minutes later because a unique index for (user, ticker, amount, price) was added to fix a race condition.