Skip to content

Try to avoid 500s caused by database race conditions#5341

Merged
mitchellhenke merged 1 commit intomainfrom
mitchellhenke/fix-some-database-race-conditions
Aug 27, 2021
Merged

Try to avoid 500s caused by database race conditions#5341
mitchellhenke merged 1 commit intomainfrom
mitchellhenke/fix-some-database-race-conditions

Conversation

@mitchellhenke
Copy link
Contributor

find_or_create_by is subject to race conditions if a unique query runs more than one at a time

create_or_find_by flips it so it attempts to create first, and if a unique constraint database exception occurs, it then does a select to get that row.

Example New Relic error

Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!! I can't believe these are not the same method 🤦

Might be worth adding a lint/cop for to help us do the right thing?

@mitchellhenke mitchellhenke force-pushed the mitchellhenke/fix-some-database-race-conditions branch from ec59d13 to b36974c Compare August 26, 2021 23:30
@mitchellhenke
Copy link
Contributor Author

Might be worth adding a lint/cop for to help us do the right thing?

I was hoping the same, but the confounder right now is Throttle uses find_or_create_by, but does not have a unique index backing it. Switching it to create_or_find_by ends up creating a bunch of identical throttles, so we'd need to allow some usage for now.

@zachmargolis
Copy link
Contributor

Might be worth adding a lint/cop for to help us do the right thing?

I was hoping the same, but the confounder right now is Throttle uses find_or_create_by, but does not have a unique index backing it. Switching it to create_or_find_by ends up creating a bunch of identical throttles, so we'd need to allow some usage for now.

🤦 whoops, just realized neither of the indexes are unique my b

@mitchellhenke mitchellhenke marked this pull request as ready for review August 27, 2021 00:14
@mitchellhenke mitchellhenke merged commit 3c5fd83 into main Aug 27, 2021
@mitchellhenke mitchellhenke deleted the mitchellhenke/fix-some-database-race-conditions branch August 27, 2021 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants