Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Prevent Concurrency Problems! A Practical Intro...

Prevent Concurrency Problems! A Practical Introduction to Transactions

PHP Conference Ehime 2026

Avatar for Shoichi Ochi

Shoichi Ochi

October 04, 2026

More Decks by Shoichi Ochi

Other Decks in Programming

Transcript

  1. About Me Shoichi Ochi SmartBank, Inc. Software Engineer From: Saijo,

    Ehime Hobby: Weight training @ochi11181101 @sho-work 2
  2. 3

  3. Introduction "Transaction" A mechanism that treats multiple reads and writes

    as one logical unit of work. All reads and writes in a transaction are treated as a single unit, giving you an all-or-nothing guarantee. In other words, either everything succeeds (commit), or it is as if nothing happened (rollback). 5
  4. Introduction "Transaction" A mechanism that treats multiple reads and writes

    as one logical unit of work. All reads and writes in a transaction are treated as a single unit, giving you an all-or-nothing guarantee. Wrap multiple queries in BEGIN ~ COMMIT In other words, either everything succeeds (commit), or it is as if nothing happened (rollback). 6
  5. Introduction A simple concurrency problem Consider two transactions that update

    the same counter row * https://www.oreilly.co.jp/books/9784873118703/ 10
  6. Introduction A simple concurrency problem Consider two transactions that update

    the same counter row BEGIN * https://www.oreilly.co.jp/books/9784873118703/ COMMIT 11
  7. Introduction A simple concurrency problem Consider two transactions that update

    the same counter row BEGIN * https://www.oreilly.co.jp/books/9784873118703/ COMMIT 12
  8. Introduction A simple concurrency problem Consider two transactions that update

    the same counter row Tx A Tx B * https://www.oreilly.co.jp/books/9784873118703/ 13
  9. Introduction A simple concurrency problem Both users want to add

    1 to the current counter So we want this: • 43 right after User 1 commits • 44 right after User 2 commits * https://www.oreilly.co.jp/books/9784873118703/ 14
  10. Introduction A simple concurrency problem 1. Tx A reads the

    counter (= 42) * https://www.oreilly.co.jp/books/9784873118703/ 15
  11. Introduction A simple concurrency problem 1. Tx A reads the

    counter (= 42) 2. Tx B reads the counter (= 42) * https://www.oreilly.co.jp/books/9784873118703/ 16
  12. Introduction A simple concurrency problem 1. Tx A reads the

    counter (= 42) 3. 42 + 1 → UPDATE to 43 2. Tx B reads the counter (= 42) * https://www.oreilly.co.jp/books/9784873118703/ 17
  13. Introduction A simple concurrency problem 1. Tx A reads the

    counter (= 42) 3. 42 + 1 → UPDATE to 43 2. Tx B reads the counter (= 42) * https://www.oreilly.co.jp/books/9784873118703/ 4. 42 + 1 → UPDATE to 43 18
  14. Introduction A simple concurrency problem The final result is 43,

    not 44! 😭 3. 42 + 1 → UPDATE to 43 4. 42 + 1 → UPDATE to 43 * https://www.oreilly.co.jp/books/9784873118703/ 19
  15. Lost Update can happen at Snapshot Isolation and below (depends

    on the DB) https://www.postgresql.org/docs/current/transaction-iso.html 71
  16. Read Skew happens at Read Committed and below and is

    prevented at Snapshot Isolation and above 79 79
  17. Aside: other ways to avoid deadlocks Lowering the isolation level

    is also an option, but it's a last resort https://kaigionrails.org/2026/talks/sho-work/ 153
  18. Aside: other ways to avoid deadlocks Coming up in a

    later talk! Lowering the isolation level is also an option, but it's a last resort https://kaigionrails.org/2026/talks/sho-work/ 154
  19. Appendix - References • • • • • • •

    • • Designing Data-Intensive Applications (Martin Kleppmann, O'Reilly, 2017) ◦ https://www.oreilly.com/library/view/designing-data-intensive-applications/9781491903063/ Japanese edition: データ指向アプリケーションデザイン (O'Reilly Japan, 2019) ◦ https://www.oreilly.co.jp/books/9784873118703/ PostgreSQL: Transaction Isolation ◦ https://www.postgresql.org/docs/current/transaction-iso.html PostgreSQL: Explicit Locking ◦ https://www.postgresql.org/docs/current/explicit-locking.html PostgreSQL: Serialization Failure Handling ◦ https://www.postgresql.org/docs/current/mvcc-serialization-failure-handling.html MySQL 8.4: Transaction Isolation Levels ◦ https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html MySQL 8.4: InnoDB Locking ◦ https://dev.mysql.com/doc/refman/8.4/en/innodb-locking.html MySQL 8.4: Locking Reads ◦ https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html MySQL 8.4: Deadlocks in InnoDB ◦ https://dev.mysql.com/doc/refman/8.4/en/innodb-deadlocks.html 164