Open1

Posgresqlのrepeatable readでのlost update対策について

kitabatakekitabatake

Designing Data Intensive Application を読んでいて、MySQL以外の主要なDBはrepeatable read設定だとlost updateを防ぐという記述があって、初見だった。

気になるので普段の開発環境で再現できるか見てみたい。

再現

Rails

  • account.balanceに対して2つのトランザクションからアクセス

    • tranA:
      • read target record
      • sleep
      • update balance +100
    • tranB:
      • read target record
      • update balance +100

    tranA側で、ERROR:couldn't serialize access due to concurrent update が発生する?

Railsで再現

  • ActiveRecord::Base.transaction(isolation: :repeatable_read)
    • isolation modeを設定したトランザクション
  • ActiveRecord::SerializationFailure

retryは https://github.com/nfedyashev/retryable を使うと行けそう

Prisma