Chapter 19
Match out-of-order replies
Use request IDs to match replies.
Show that reply order can change while the replies still go with the same requests.
Prerequisite: Reject an unknown route. Course map →
The situation
Three independent lookups are in flight. The fastest response belongs to the last submitted request.
The client can have several requests outstanding with a DEALER socket. It includes a request ID in each message. The reply carries the same ID. The client uses the ID to look up the correct request even when the replies arrive in a different order. This matching rule is called correlation.
- Client sends a, b and cHandler reads all three
- Client matches c to CHandler starts with the last request
- Client matches b to BHandler replies to the second request
- Client matches a to AHandler replies to the first request
Conceptual diagram. The example sends replies in reverse order. The client still matches each response to its ID, giving A for a, B for b and C for c.
Before you run
Which mapping would naive positional matching produce?
Run the experiment
The client sends a/A, b/B and c/C in that order. The server sends the replies in reverse order.
For the original example, click Restore code if it is shown, then click Run. Compare your output with the worked result.
Python prepares in the background while you read. First use can download about 71 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
Your edits are saved in this browser when storage is available.
Full error details
Run output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
Downloads
Worked result
Reply arrival order is c,b,a; matched remains a:A, b:B, c:C.
Full output from the original example
{
"reply_arrival_order": [
"c",
"b",
"a"
],
"matched": {
"a": "A",
"b": "B",
"c": "C"
}
}The build ran the original editor code to produce this output.
Try one change
Replace the last line in the editor with result = correlation(reverse_replies=False). Click Run again. Compare reply_arrival_order and matched with the original. What stays the same when the reply order changes?
Inspect the socket implementation
The editor calls this read-only helper. Read all socket helpers or read the separate-process code. The code download includes both files.
def correlation(reverse_replies=True):
with lab() as socket:
router=socket(zmq.ROUTER);client=socket(zmq.DEALER);router.bind('inproc://ids');client.connect('inproc://ids')
for identity in ['a','b','c']:client.send_multipart([identity.encode(),identity.upper().encode()])
requests=[router.recv_multipart() for _ in range(3)]
reply_order=reversed(requests) if reverse_replies else requests
for identity,request,value in reply_order:router.send_multipart([identity,request,value])
replies=[client.recv_multipart() for _ in range(3)]
return {'reply_arrival_order':[row[0].decode() for row in replies],'matched':dict(sorted((key.decode(),value.decode()) for key,value in replies))}
Costs, waiting and recovery
| Aspect | What to account for |
|---|---|
| Work | The client looks up the request ID in a dictionary. Sorting the result is extra work for displaying it. |
| Stored information | Keep one record per request while waiting for its reply. |
| Message path | The request ID adds bytes to the message independently of the connection address. |
| Waiting | A deadline sets how long a client keeps a request open if it sees no reply. |
| Recovery | Use the same request ID across retries. Give each attempt a separate number when you need to diagnose repeats. |
Check your understanding
What should a client do with a reply whose ID is unknown or already completed?
Show hints
Hint 1
Could the response belong to a request that is already done?
Hint 2
What rule tells the client when to stop waiting for a response?
Show explanation
Validate IDs and protocol versions, classify unknown/late/duplicate responses, and avoid applying a result twice. Bound pending state by deadlines and capacity. Do not silently attach an unexpected response to the next available request.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs. PyZMQ API links use version 27.3.0.
Separate routing from correlation
SocketType.DEALER(new tab)SocketType.ROUTER(new tab)Socket.send_multipart(new tab)Socket.recv_multipart(new tab)The helper supplies application request IDs and matches replies by those IDs.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
These sockets exchange real messages. The Python server chooses a fixed reply order so both runs can be compared.
ZeroMQ reference · Runtime and model boundaries