Messaging Patterns LabPython · ZeroMQ

Chapter 9

Detect a sequence gap

Find missing sequence numbers in a live feed.

What you’ll learn

Explain what a gap tells a subscriber and what remains unknown.

Prerequisite: Meet the late subscriber. Course map →

The situation

A consumer observes sequence 4 after sequence 2. It can detect missing sequence 3 but cannot yet explain where it disappeared.

A sequence number identifies the position of a message in a series. The subscriber compares each number it receives with the next number it expected. ZeroMQ carries the frames. The sample Python code assigns the numbers and records any gaps.

A jump in the sequence reveals a missing number
  1. Publisher1 → 2 → 4The fixture deliberately omits 3.
  2. Consumer stateExpect next = 3Compare the received sequence 4.
  3. DetectorSequence 3 missingFind the value in retained history if available.

Conceptual diagram. The example’s own code assigns and checks these numbers. A gap gives the missing number. Finding the cause requires other evidence.

Before you run

Does identifying gap 3 prove a network dropped that message?

Run the experiment

After the publisher sees the subscription, it sends sequences 1, 2 and 4. The example deliberately skips 3.

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 list missing_sequences contains 3. In this setup the publisher skipped the number. A subscriber in a different system would need more evidence to know the cause of a gap.

Full output from the original example
{
  "received_sequences": [
    1,
    2,
    4
  ],
  "missing_sequences": [
    3
  ],
  "policy": "Publisher intentionally omits a sequence; the detector does not diagnose network loss."
}

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

Try one change

Replace the last line with result = sequence_gap(skip=None) and click Run. Then use result = sequence_gap(skip=2) and click Run again. Compare received_sequences and missing_sequences. How does the missing sequence follow from the order the receiver sees?

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 sequence_gap(skip=3):
    with lab() as socket:
        pub,sub=publisher(socket)
        values=[i for i in range(1,5) if i!=skip]
        for value in values:pub.send_multipart([b'price',str(value).encode()])
        seen=[int(sub.recv_multipart()[1]) for _ in values];expected=1;gaps=[]
        for value in seen:gaps.extend(range(expected,value));expected=value+1
        return {'received_sequences':seen,'missing_sequences':gaps,'policy':'Publisher intentionally omits a sequence; the detector does not diagnose network loss.'}
Costs, waiting and recovery
AspectWhat to account for
WorkThe receiver compares the expected number with each received number.
Stored informationThe receiver stores its next expected number and the gaps it finds.
Message pathThe sequence number adds bytes to each publication.
WaitingOnce a receiver finds a gap, it can ask a separate source for the missing data.
RecoveryIf the publisher restarts, keep its counter or label the new sequence with a new session ID.

Check your understanding

A publisher restarts its counter at 1. How can a consumer distinguish restart from duplicate or out-of-order traffic?

Show hints
  1. Hint 1

    Can you tell which run of the publisher produced a number?

  2. Hint 2

    Where could a subscriber look for a missing value?

Show explanation

Include a publisher ID and a session ID that changes when the counter restarts. Compare sequence numbers within each session and define how to look up missing data.

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.

  • Read a publication and inspect its sequence

    The helper authors the sequence numbers and gap detector; neither is inferred by recv_multipart.

  • 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

The Python detector checks one publisher’s ordered sequence and reports missing numbers. A repair service would have to retrieve the missing values.

ZeroMQ reference · Runtime and model boundaries