Messaging Patterns LabPython · ZeroMQ

Chapter 10

Replay a cached value

Give a new subscriber the current value.

What you’ll learn

Check how sending a saved value changes what a late subscriber sees.

Prerequisite: Detect a sequence gap. Course map →

The situation

A late dashboard wants the current price immediately. Retaining every historical price would be a different requirement.

The publisher uses a Python dictionary to save the most recent value. This is a last-value cache. Once the publisher sees the subscription, the Python code can send the saved price before it sends a new update. The example keeps one topic and one subscriber.

A new subscriber can receive the last stored value
  1. Dictionary in the exampleprice = 12Store the latest price in memory.
  2. XPUBSubscription arrivesThe publisher sees the subscription request.
  3. Publisher policyCached 12, then live 15Send both values to the subscriber.
  4. Late SUBCurrent viewThis is not complete audit history.

Conceptual diagram. The example sends one stored price before the live update. With concurrent updates, use versions to decide which value comes next and how to apply deletes.

Before you run

Which values arrive with and without the replay policy?

Run the experiment

The publisher saves an initial price of 12. Once it sees a subscriber, it sends the stored price and then a new price of 15.

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

received contains 12, then 15. The first value came from the cache. The second came from the live update.

Full output from the original example
{
  "received": [
    "12",
    "15"
  ],
  "cache_replay": true,
  "mechanism": "Authored latest-value dictionary and XPUB subscription control; not built-in durable history."
}

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

Try one change

Replace the last line with result = last_value(replay=False) and click Run again. Compare the list in received. Without a later update, when would a late subscriber first learn the current price?

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 last_value(replay=True):
    with lab() as socket:
        pub=socket(zmq.XPUB);pub.bind('inproc://cache');cache={b'price':b'12'}
        sub=socket(zmq.SUB);sub.subscribe=b'price';sub.connect('inproc://cache');pub.recv()
        if replay:pub.send_multipart([b'price',cache[b'price']])
        pub.send_multipart([b'price',b'15'])
        rows=[sub.recv_multipart()[1].decode() for _ in range(2 if replay else 1)]
        return {'received':rows,'cache_replay':replay,'mechanism':'Authored latest-value dictionary and XPUB subscription control; not built-in durable history.'}
Costs, waiting and recovery
AspectWhat to account for
WorkThe publisher looks up the topic to get the saved price.
Stored informationThe dictionary keeps one value per topic. A design that also stores delete markers uses more memory.
Message pathEach saved value sent to a new subscriber adds a message.
WaitingA subscription triggers the Python code that sends the saved price.
RecoveryRecover the latest prices from a surviving source if the in-memory dictionary is lost.

Check your understanding

Can a latest-value cache support an audit consumer that must reconstruct every change?

Show hints
  1. Hint 1

    Which prices does the dictionary keep?

  2. Hint 2

    If a new price arrives during a snapshot, what could identify which value came later?

Show explanation

No. Provide a retained history or authoritative audit source for every change. A latest-value service may support a current view, but version ordering, deletions and a snapshot/live boundary must still be defined.

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 keeps one price in a Python dictionary and sends it through real sockets. A subscriber that needs older values would need a saved history.

ZeroMQ reference · Runtime and model boundaries