المستهلكون الآمنون من التكرار وصندوق الوارد
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
apply each event once in effect, even if delivered many times.
الموضوعات المخطط لها
- Brokers redeliver after crashes, rebalances, and timeouts. Duplicates are normal.
- Natural idempotency:
SET status = 'PAID'is safe to repeat;balance = balance - 10is not. - Inbox table: store the processed message id with a unique constraint, in the same transaction as the effect.
- Insert the id first; a duplicate key means "already done", so skip.
- An in-memory set is not enough: it is lost on restart.
تفاصيل المخطط المبدئي
عرض عملي
@Transactional
void on(StockReserve msg) {
if (!inbox.tryInsert(msg.eventId())) return; // unique violation: duplicate
stock.reserve(msg.sku(), msg.qty());
}اختبر فهمك
Why must the inbox insert and the stock change share a transaction?
أظهر الإجابة
Otherwise a crash between them either skips the work or repeats it.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.