Messaging Patterns LabPython · ZeroMQ

Chapter 24

What is the wire contract?

Specify which message bytes each side sends, what they mean and how those rules can change.

Prerequisite: Retry with stable identity. Course map →

The situation

Two services both call their body JSON, but one interprets amount as pounds and the other as pence. Structural decoding succeeds while the business outcome is wrong.

Specify structure and meaning

Define frame positions, topic/envelope rules, encoding, schema version, timestamp basis, units, stable logical IDs, correlation IDs and error responses. Separate route frames from business payload and distinguish an empty frame from a missing one.

Validate size, frame count, allowed operations, required fields and value ranges before expensive work. A compatible integer type does not establish compatible units or business semantics. Reject unknown versions through a visible, bounded policy.

Choose safe decoding

The course uses explicit bytes, strings and JSON. Untrusted deserialization needs a real security review. In particular, Python pickle-based object transport is not a safe default for untrusted senders. Avoid treating convenience serialization as schema validation.

Preserve enough error provenance to diagnose malformed traffic without unnecessarily copying secret or personal payloads into logs. If you keep rejected messages in quarantine, a holding area for later review, decide who can see them, when to delete them and which IDs a repaired retry uses.

Which changes can break compatibility

Illustrative case.

Which field can change meaning even when multipart and JSON decoding succeed?

BoundaryUseful evidenceRemaining question
Multipart structureFrame boundaries preservedMeaning and limits still required
JSON decodingSyntax acceptedSchema/units/permissions separate
Version rolloutSelected reader/writer pairsState and rollback compatibility
QuarantineVisible rejected inputOwnership, privacy and replay policy

Roll out both sides

Plan producer and consumer changes together. Test a message from the new writer with the old reader, and a message from the old writer with the new reader. State which combinations your deployment supports. Reserve message types/fields deliberately and define unknown-field and unknown-operation rules.

Deploying new code can also change pending-request interpretation, key/identity format, cached state and duplicate handling. A wire schema alone does not describe those application state transitions or rollback requirements.

Test invariants and boundaries

Use fixtures for empty/binary frames, missing fields, unknown versions, oversized bodies, duplicate IDs, late replies and disconnects. For mutually exclusive routing outcomes, account for every accepted/rejected/quarantined input under the policy.

Record transform/protocol versions with run evidence. Test the real handler and failure cut for claims about effects. A parser unit test, socket exchange and transaction/restart experiment establish different layers of evidence.

Check your understanding

The field amount keeps its name and type. In a new version it means pence instead of pounds. What would you assert in a compatibility test?

Show hints
  1. Hint 1

    Both readers can still decode the number.

  2. Hint 2

    Try one price, such as £12, with an old and a new reader.

Show explanation

Test what each reader interprets as £12 when it reads a message from each version. Record the unit and version together. Include saved older messages in the test if the service can replay them.

ReferencesPrimary sources for this chapter’s design case

Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.