pythondesign-patternsgoogle-cloud-pubsubenvelope

What is an envelope pattern in Pubsub messaging?


I am looking for some resources in understanding the envelope pattern and google isn't being too helpful. Can someone illustrate this with an example? For more info, I am working with Pubsub in Python and I'm looking to tag messages with some additional info (apart from plainly adding that info in the message body) for complex applications.


Solution

  • The envelope pattern is a way to wrap a message with metadata to help determine what to do with that message without having to understand the contents of the message itself. The metadata can be used to determine how to route a message to the appropriate receiver to deal with the message contents or it could indicate what to do with the message itself.

    Typically, the way this pattern works is that different layers add a wrapper on top of a message that is passed to it. There can be multiple, nested envelopes. When a message is received, there is a corresponding layer that removes one of the envelopes for every layer that added one.

    The side that creates messages would look like this:

    Adding envelopes

    The side that consume messages would look like this:

    Remvoing evenlopes

    A Google Cloud Pub/Sub message has an attributes map that can be used for precisely this purpose. You can put data in the attributes that can indicate what to do with the message without having to decode the bytes stored in the data portion of the message.

    For example, imagine you have a topic that contains messages that correspond to different user actions on an ecommerce website. Some of the messages are for profile updates while others are transactions. You want to perform different actions on these types of messages. Instead of looking into the data field in the message to determine which type it is and what to do with it, you could add key/value pairs in the attributes, e.g. action: UPDATE and action: TRANSACTION. Then, your subscriber would look at the value for the action attribute and determine what to do with the message. It could execute a different piece of code or maybe even send it to a different service, acting as a router for messages. The key point is that the determination of what to do could be performed without having to decode the message data itself.

    Messages sent to push subscription endpoints have an additional envelope added that indicates the subscription the message came from. This is necessary because it is possible for multiple push subscriptions to point to he same endpoints.