简体   繁体   English

如何设计一个pub-sub系统,同一实体可以有多个发布者?

[英]How to design a pub-sub system where there can be multiple publisher for same entity?

Consider the following situation: 考虑以下情况:

  1. There is a User table with fields like name, email, contact_no etc. 有一个用户表,其中包含姓名,电子邮件,contact_no等字段。
  2. We have multiple products/system(with their own db) which use User information for various purpose. 我们有多个产品/系统(有自己的数据库),它们将用户信息用于各种目的。
  3. These systems remain in sync by pub-sub pattern eg: System 1 changes name is consumed by system 2 and make changes in system 2. 这些系统通过pub-sub模式保持同步,例如:系统1更改名称由系统2消耗并在系统2中进行更改。
  4. For simplicity let us assume there are 3 systems: 为简单起见,我们假设有3个系统:
    • S1 having UI for the user. S1具有用户的UI。 Here user can himself change his information. 用户可以在这里自己更改信息。
    • S2 system given to the sales person. 给予销售人员的S2系统。 Here user can call sales person and update their information. 在这里,用户可以致电销售人员并更新他们的信息
    • S3 another product which uses information from S1 and S2 for various computations. S3另一种产品,它使用来自S1和S2的信息进行各种计算。

So information can be published from S1 and S2. 因此,可以从S1和S2发布信息。

Suppose a user initially have name N1. 假设用户最初具有名称N1。

  • At time t1, user updates name from N1 to N2. 在时间t1,用户将名称从N1更新为N2。
  • At time t1, sales person updates name from N1 to N3. 在时间t1,销售人员将名称从N1更新为N3。

  • Now S1 consumes event from S3 and updates name to N3. 现在S1从S3消耗事件并将名称更新为N3。 S2 consumes event from S1 and updates name to N2. S2从S1消耗事件并将名称更新为N2。

  • In S3 name can be anything N2 OR N3 depending which event is consumed first. 在S3中,名称可以是N2或N3,取决于首先消耗哪个事件。

This has caused a lot of data consistency among various system. 这导致各种系统之间的大量数据一致性。

In ideal system there is only one publisher but due to requirement we had to add publishing events from Sales panel. 在理想的系统中,只有一个发布者,但由于要求,我们必须从“销售”面板添加发布事件。 What can be done to minimize data inconsitency? 可以采取哪些措施来最大限度地减少数据不一致?

The problem seems to be in deciding who is the master of User data, because right now S3 is trying to serve two masters. 问题似乎在于决定谁是用户数据的主人,因为现在S3正在尝试为两个主人提供服务。

S1 --> S3
S2 --> S3

Let's make S1 the master of User data, so that any other system which accepts that data (eg S2) is responsible only for updating the master. 让S1成为用户数据的主人,这样接受该数据的任何其他系统(例如S2)只负责更新主数据。 This way S3 gets User data from a single source (the master of User data). 这样,S3从单个源(用户数据的主站)获取用户数据。

       S1 --> S3
S2 --> S1 --> S3

The master is responsible for both consuming and producing the data that it owns. 主人负责消费和生成它拥有的数据。 Another system may consume (and even store) the same data, but it can only produce to the master . 另一个系统可能消耗(甚至存储)相同的数据, 但它只能产生给主数据 No system can produce data that it doesn't own to any system other than the master of that data. 没有系统可以生成除了该数据的主数据之外的任何系统不拥有的数据。

It doesn't really matter which system is the master, as long as there is only one. 只要只有一个系统,哪个系统是主系统并不重要。 It doesn't really matter how many systems store copies of the data, as long as there is only one master. 只要只有一个主数据,有多少系统存储数据副本并不重要。 Having each system master one type of data is probably more scalable than a single master for all data. 让每个系统掌握一种类型的数据可能比所有数据的单个主数据更具可扩展性。

Let me rephrase it once again.. 让我再说一遍..

  1. you have multiple system which has independent DB attached to it. 你有多个系统,它附有独立的数据库。
  2. User able to change data from any system 用户可以从任何系统更改数据
  3. User should see data changed from any system. 用户应该看到从任何系统更改的数据。

In this scenario i would go with Master DB / System which will be single point of data source for every system. 在这种情况下,我将使用Master DB / System,它将成为每个系统的单点数据源。

If any system want to change data then data 1st need to update on Master Db / system then it should propagate to other system to reflect the changes. 如果任何系统想要更改数据,则数据首先需要在主Db /系统上更新,然后它应传播到其他系统以反映更改。

for Pub-Sub I will follow Fan-IN and Fan-out method. 对于Pub-Sub,我将遵循Fan-IN和扇出方法。

  1. Every system will act as publisher as well as consumer for different topic / channel. 每个系统都将充当不同主题/渠道的发布者和消费者。
  2. Publishers (which is on S1-SN) will push changes into one topic / channel for similar data change 发布者(在S1-SN上)会将更改推送到一个主题/频道,以进行类似的数据更改
  3. Master DB system will listen to topics / channel to get change request from other system. 主数据库系统将监听主题/频道以从其他系统获取更改请求。 It will also act as publisher for other system where it will push same message to different topic / channel after processing it. 它还将充当其他系统的发布者,在处理它之后它将相同的消息推送到不同的主题/频道。
  4. Consumers (which is on S1-SN) will listen to topics / channels for similar data change which is putted by master DB system. 消费者(在S1-SN上)将收听由主数据库系统推出的类似数据变化的主题/频道。 It will consume that data to update their own System DB for Data changes. 它将使用该数据来更新自己的System DB以进行数据更改。

In this way you can achieve Data consistency between systems. 这样,您就可以实现系统之间的数据一致性。 Only thing you need to take care is delay in this process, which could be arise if any system fails. 只有你需要注意的是这个过程的延迟,如果任何系统出现故障,可能会出现这种情况。

Hope I have answer your query. 希望我已经回答了你的问题。

Maintain one table to keep track of all latest updates (Latest One day updates). 维护一个表以跟踪所有最新更新(最新一天更新)。 Assuming that all updates can be propagated to all other systems via pub/sub system within a couple of minutes eventually. 假设所有更新都可以在几分钟内通过pub / sub系统传播到所有其他系统。

All systems should contact this primary source before updating the entry (for fields like name, email and contact number) 在更新条目之前,所有系统都应联系此主要来源(对于名称,电子邮件和联系电话等字段)

  • If there is already an entry in that primary source table then need to verify that which is the latest one. 如果该主要源表中已有条目,则需要验证哪个是最新的。 If their is newer entry then need to alert to the user that there were some recent updates happened to the same record, do you still want to update it. 如果它们是较新的条目,则需要提醒用户相同记录中发生了一些最新更新,您是否仍想更新它。

  • If there is no entry then you can easily update the data with the new data and insert one entry into these primary source table. 如果没有条目,则可以使用新数据轻松更新数据,并在这些主要源表中插入一个条目。

Whenever any system processes updates via pub-sub then it should update only if there are no new updates happend to the same record. 每当任何系统通过pub-sub处理更新时,只有在同一记录中没有新的更新发生时它才会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pub-Sub设计模式 - Pub-Sub Design Pattern 在实现中使用数据库时,观察者模式和pub-sub是否相同? - Is Observer pattern and pub-sub same when a database is used in the implementation? 用于了解pub-sub消息是否成功的模式 - Patterns for knowing if a pub-sub message was successful 这是基于消息总线的发布-订阅模式吗? - Is this a message bus based pub-sub pattern? 为什么要在这里使用Observer / Pub-Sub模式? - Why should we use Observer/Pub-Sub pattern here? 我的发布者未返回订阅的功能-DIY发布/订阅实现 - My publisher does not return the subscribed function - DIY pub/sub implementation 发布/订阅设计模式angularjs服务 - Pub/Sub design pattern angularjs service 如何使此发布/子代码更具可读性? - How can I make this pub/sub code more readable? Redis pub sub适用于多个生产者和多个消费者 - Redis pub sub for multiple producers and multiple consumers 如何在子实体可以包含不同数据,数据属于同一泛型的地方建立具体表关系的模型? - How to model concrete table relationship where a child entity can contain different data, where the data belongs to the same generic type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM