问题
Basic pubsub architecture question. At a high level, when designing pubsub, I sometimes face a choice between two architectures:
- Publish mutations or "new-state".
Some DB state is mutated, and publishers notify of that change via pubsub. But they include enough information in the message so that the subscriber doesn't need to do a look-up on the DB. Imagine the subscriber has a cache of the DB. It could receive the mutations or new-state, and update its cache without doing a look-up.
- Notification-and-check The publishers only notify that "there is a new change", which prompts the subscriber to pull the latest from the DB. This is more robust IMO. The pubsub may not guarantee order of delivery, and this architecture is robust to that, while #1 is not, if the mutations are order-sensitive.
My question is, is there a common name for these 2-types of architectures? Thanks!
回答1:
Publish/Subscribe is known as the Observer Pattern in the Gang of Four Design Patterns book. On page 298 they describe these two variations as push and pull models.
Implementations of the Observer pattern often have the subject broadcast additional information about the change. The subject passes this information as an argument to Update. The amount of information may vary widely.
At one extreme, which we call the push model, the subject sends observers detailed information about the change , whether they want it or not. At the other extreme is the pull model; the subject sends nothing but the most minimal notification, and observers ask for details explicitly thereafter.
The pull model emphasizes the subject's ignorance of its observers, whereas the push model assumes subjects know something about their observers' needs. The push model might make observers less reusable, because Subject classes make assumptions about Observer classes that might not always be true. On the other hand, the pull model may be inefficient, because Observer classes must ascertain what changed without help from the Subject.
来源:https://stackoverflow.com/questions/63321138/name-of-notification-and-check-pubsub-architecture