zeromqdistributed-systempyzmqmulti-agent

How to make a 'shared volatile variable' with ZeroMQ?


I'm using ZeroMQ.

I want a node B to subscribe to a node A. Node A will send ( PUB? ) values of some 'volatile variable' (say, the position of some object). 'Volatile' in this case means that node B only cares about the most recent value.

The upshot should be that A can send values to subscribers, but if two values of the variable ever get queued up in the outgoing (or incoming) queues, then the most recent value would replace the earlier values. And another upshot would be: there's no high-water mark.

I can achieve this with PUB/SUB, obviously, but that wouldn't get things like most-recent-value-always-wins. It seems like there'd be some established pattern to achieve this, but I haven't found it.

( I suppose this means I want a ZeroMQ socket pattern to work as if it's a pure udp )


Solution

  • Q : How to make a 'shared volatile variable' ( s.t. most-recent-value-always-wins ) with ZeroMQ?

    In case your application-level logic is happy with the PUB/SUB - one PUB-lishes, by .send()-ing messages, others SUB-scribe to theirs respective topic-of-choice, so as to start .recv()-ing Scalable Formal Communications Pattern archetype, we may fine-tune the configuration so as it meets all your requirements, expressed above ( s.t. most-recent-value-always-wins )

    In case one has never worked with ZeroMQ,
    one may here enjoy to first look at "ZeroMQ Principles in less than Five Seconds"
    before diving into further details


    The proper configuration step :

    The trick is to use .setsockopt( ZMQ_CONFLATE, 1 ) method to just "switch-ON" this very kind of behaviour, managed by the Context()-engine instance(s) silently to the user, right "inside" the Queue-managers' policy.

    If set, a socket shall keep only one message in its inbound/outbound queue, this message being the last message received/the last message to be sent. Ignores ZMQ_RCVHWM and ZMQ_SNDHWM options. Does not support multi-part messages, in particular, only one part of it is kept in the socket internal queue.

    Applicable socket types ZMQ_PULL, ZMQ_PUSH, ZMQ_SUB, ZMQ_PUB, ZMQ_DEALER


    It was that easy !

    All the best with mastering the art of Zen-of-Zero.