Messaging Patterns LabPython · ZeroMQ

Chapter 20

Forward through a real proxy

Follow a request through a forwarding process.

What you’ll learn

Check which process forwards the message and which process writes the reply.

Prerequisite: Match out-of-order replies. Course map →

The situation

Clients and workers should connect through different endpoints so workers can change without exposing each worker address to every client.

A proxy is a process that passes messages between sockets. Here a ROUTER socket accepts messages from clients and a DEALER socket forwards them to workers. zmq.proxy does the forwarding. The worker reads the request and constructs the reply.

The proxy forwards; the worker replies
  1. Parent processREQ clientSend the business request.
  2. Separate childROUTER ↔ DEALERzmq.proxy forwards the message and its addressing frames.
  3. Parent processREP workerHandle the body and construct the reply.

Conceptual diagram. The client and the worker share one process. The proxy runs in another process and forwards messages between them.

Before you run

Which process writes the contents of the reply?

Run the experiment

The example starts the proxy in a second process. The client and worker connect to separate TCP addresses inside the Linux guest. The client sends one request.

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.

Troubleshooting

If Python stops responding, restart the environment and try again. Your code and last completed result are kept.

Downloads

Worked result

The reply contains done, job-1 and 12. proxy_separate_process records that the proxy ran separately from the requester and worker.

Full output from the original example
{
  "reply": [
    "done",
    "job-1",
    "12"
  ],
  "proxy_separate_process": true,
  "mechanism": "Actual zmq.proxy with ROUTER frontend and DEALER backend"
}

The build ran the original editor code to produce this output.

Try one change

Replace the last line in the editor with result = proxy_exchange(job_id='job-2'). Predict the three reply frames, then click Run again. Which frame came from the worker and which contains the changed job ID?

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 proxy_exchange(job_id='job-1'):
    with peer('proxy') as (ready,process),lab() as socket:
        client=socket(zmq.REQ);worker=socket(zmq.REP)
        client.connect(f"tcp://127.0.0.1:{ready['front']}");worker.connect(f"tcp://127.0.0.1:{ready['back']}")
        client.send_multipart([job_id.encode(),b'12']);job=worker.recv_multipart();worker.send_multipart([b'done',*job]);reply=client.recv_multipart()
        return {'reply':[v.decode() for v in reply],'proxy_separate_process':True,'mechanism':'Actual zmq.proxy with ROUTER frontend and DEALER backend'}
Costs, waiting and recovery
AspectWhat to account for
WorkForwarding adds a message step through the proxy.
Stored informationConnections to clients and workers may have their own queues.
Message pathThe messages travel between two Python processes using local TCP.
WaitingA new proxy can accept connections again before the application has checked its unfinished jobs.
RecoverySave jobs and their outcomes in a system that survives proxy failure if they must be recovered.

Check your understanding

If the proxy exits while a request is in flight, which state would a durable dispatcher need to recover?

Show hints
  1. Hint 1

    Which component remembers jobs after a restart?

  2. Hint 2

    Could the worker have finished before the reply disappeared?

Show explanation

Recover source work, stable IDs, assignment/acknowledgement status and effect outcomes under a declared persistence protocol. A restarted proxy can restore connectivity while losing knowledge of unfinished requests.

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. ZeroMQ manuals are pinned to libzmq 4.3.5.

Experiment scope

The example uses zmq.proxy and TCP inside one temporary Linux system. It stops the proxy once the one exchange finishes. Test unfinished requests separately if the service must recover them.

ZeroMQ reference · Runtime and model boundaries