Spot the bug
A consumer gets each message at least once. Which line makes a redelivery double-credit the account?
processed: set[str] = set()balances = {"acc-1": 0}def handle(msg_id: str, account: str, amount: int) -> None:balances[account] += amountprocessed.add(msg_id)for msg in [("m1", "acc-1", 50), ("m1", "acc-1", 50)]:handle(*msg)print(balances)
Hint 1 of 1
Where is processed read?
Choose an answer, then open Show answer below to check it.
Show answer
- Answer: line 6
balances[account] += amount
It adds before checking whether m1 was already processed. Check if msg_id in processed: return first, and in a real system record the id in the same transaction as the credit.