so rails has added another race-condition-having insert API to replace its existing race-condition-having insert API further material for the article I'm working on about how the type { read() -> T, write(T) } cannot possibly provide consistency
-
Show this thread
-
essentially, because find_or_create has race conditions, they've added create_or_find, which has... different race conditions you do not actually want some combo of read-or-write, you want insert-or-update, in a single query if possible, or transactionally otherwise
2 replies 0 retweets 5 likesShow this thread -
and it does the initial INSERT in its own transaction which the on-failure SELECT is not part of, which admits races if the record is deleted, regardless of isolation level
1 reply 0 retweets 1 likeShow this thread -
and on mysql it means the caller can't fold this into their own transaction
2 replies 0 retweets 0 likesShow this thread -
Replying to @mountain_ghosts
To be fair, there's not really any way to write `find_or_create` that isn't racy. Closest you can get (that works on both pg and mysql) is a select for update, followed by an insert ignore, followed by another select -- but even that can fail
1 reply 0 retweets 2 likes -
Replying to @sgrif
I know that, but create_or_find is also racy, and its own docs say as much, so I'm confused about why it's been added if I wanted consistency I wouldn't use either of these APIs
1 reply 0 retweets 1 like -
Replying to @mountain_ghosts
I am definitely not advocating that `create_or_find` is a good API. I argued against adding it at the time. For real improvement to happen though, I think Rails needs to:
1 reply 0 retweets 0 likes -
Replying to @sgrif @mountain_ghosts
- Become more comfortable with APIs that are coupled to a single DB. Upsert in particular is really tricky, and the number of DB agnostic APIs you can build for it are *extremely* limited - Build more APIs focusing on the DB as the source of truth.
1 reply 0 retweets 1 like -
Replying to @sgrif @mountain_ghosts
Users are too used to the app holding state, and then telling the DB, expecting consistency. It's hard to undo that and train folks that it woks by giving the DB a change and then asking it for the state of things
1 reply 0 retweets 0 likes -
Replying to @sgrif @mountain_ghosts
I'm curious what do you think about the Ecto API with change sets? Seems more in line with what you're saying here
2 replies 0 retweets 0 likes
I don't think that validating user input belongs in an ORM, but in general separating the structure used for updating data makes sense -- I went with a similar design for Diesel.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.