TASK
Implementation
Guarantee at-least-once delivery:
- Consumer receives message (not removed from queue)
- Message marked as "in-flight" with timestamp
- Consumer processes and sends acknowledgment
- Queue removes message on ack
- If no ack within timeout, redeliver message
Consumer must be idempotent to handle potential duplicates.
Sample Test Cases
Redeliver on timeoutTimeout: 5000ms
Input
{"src":"c0","dest":"queue","body":{"type":"init","msg_id":1,"node_id":"queue","node_ids":["queue"]}}
{"src":"producer","dest":"queue","body":{"type":"enqueue","msg_id":2,"message_id":"m1","value":"hello"}}
{"src":"consumer","dest":"queue","body":{"type":"dequeue","msg_id":3,"ack_timeout":1000}}
Expected Output
(no output)
Hints
Hint 1▾
Do not remove until acknowledged
Hint 2▾
Redeliver after timeout
Hint 3▾
Track in-flight messages
OVERVIEW
Theoretical Hub
At-Least-Once Delivery
At-least-once means every message is delivered at least once, possibly more. If an ack is lost, the message is redelivered. This requires idempotent consumers that handle duplicates gracefully.
Visibility Timeout
When a consumer receives a message, it becomes invisible to others for a timeout period. If not acknowledged, it reappears. This prevents multiple consumers processing the same message simultaneously.
Key Concepts
at-least-onceacknowledgmentredelivery
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
import sys
import json
import time
import threading
import uuid
class AtLeastOnceQueue:
def __init__(self, visibility_timeout=30):
self.messages = {} # id -> message
self.visible = [] # [id, ...]
self.in_flight = {} # id -> (consumer, invisible_until)
self.visibility_timeout = visibility_timeout
self.lock = threading.Lock()
def send(self, message):
# TODO: Add message with unique ID
pass
def receive(self, consumer_id):
# TODO: Return message, mark as in-flight
pass
def acknowledge(self, message_id):
# TODO: Remove message permanently
pass
def check_timeouts(self):
# TODO: Make timed-out messages visible again
pass
if __name__ == "__main__":
pass