Exercise 2: Two Crashes Before Successful Acknowledgment — Possible Solution ==================================================================== THE SEQUENCE ------------------------------ q.consume_one(award_points_idempotent, crash_before_ack=True) # 1st crash q.consume_one(award_points_idempotent, crash_before_ack=True) # 2nd crash q.consume_one(award_points_idempotent, crash_before_ack=False) # finally succeeds Three total delivery attempts for the same message, using this chapter's own idempotent handler (checks processed_ids before awarding). RESULTS ------------------------------ after 1st crash, points: 10 after 2nd crash, points: 10 after successful ack (3rd attempt), points: 10 (expected: 10, not 30) The handler ran on all three attempts (the crash only affects whether the message gets acknowledged and removed from the queue - it doesn't prevent handler code from running before the simulated crash). Despite running three separate times, points correctly stayed at 10 throughout - never climbing to 20 after the second attempt or 30 after the third. WHY THIS CONFIRMS IDEMPOTENCY SCALES TO ANY NUMBER OF REDELIVERIES ------------------------------ This chapter's own original example tested exactly one redelivery (two total attempts). This exercise confirms the identical protection holds for three attempts, not just two - because processed_ids.add(message_id) happens on the FIRST successful pass through the handler body, every subsequent attempt (regardless of how many there are) hits the early return and does nothing. The guarantee isn't "safe for one duplicate" - it's safe for arbitrarily many redeliveries of the same message_id, a genuinely important distinction since real message queues don't promise a maximum redelivery count. WHY THIS WORKS AS AN ANSWER ------------------------------ The scenario extends this chapter's own single-crash test to two crashes using the identical idempotent handler unmodified, and the points total is checked after every individual attempt rather than only at the end - confirming no duplicate award happened at any point in the sequence, not just in the final result.