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. نموذج تعليمي — الأرقام توضّح سلوكًا وليست قياسات أداء.

المفاهيم

مفيد قبل هذا التحدي

تحديات ذات صلة

المراجع