The Real Fullstack

The Lost Transaction

Each order checked the stock inside a transaction. How did we sell six of five?

エンジニア8 分タイムラインシミュレーターコード課題
sqlpostgresqlspring-data-jpajava

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Make the reservation correct for every interleaving of two concurrent orders.背景 · 学習目標 ▾

Fernway's reservation code loads the product with JPA, checks the stock and saves the new value, all inside @Transactional. During a flash sale, more items were sold than existed. Each transaction committed without error.

ミッション: Make the reservation correct for every interleaving of two concurrent orders.

学習目標

  • Recognise a lost update hidden behind an ORM entity.
  • Compare pessimistic locking, optimistic versioning and atomic conditional updates.
  • Understand why a transaction alone doesn't serialise read-modify-write.
シミュレーションシミュレーション値

シミュレーションを実行すると出力が表示されます。

シミュレーションを実行
@Transactionalpublic void reserve(long productId, int qty) {  Product p = products.findById(productId).orElseThrow();   // SELECT stock → 5  if (p.getStock() < qty) throw new OutOfStock();  p.setStock(p.getStock() - qty);                          // UPDATE stock = 2 (blind write)}

簡略化

Two orders, two steps each. Real systems also need to handle the retry or 'out of stock' path in the UI. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

先に取り組むと役立つ課題

関連する課題

参考資料