How to properly implement the internal consistency of microservices through localization events, and the final consistency between event bus across MicroServices

Source: Internet
Author: User

Directory
    1. Design Focus
    2. Flow chart
    3. Pseudo code
      2.1. Publishevent
      2.2. Subscribeevent
      2.3. Publisher
      2.4. Subscriber
    4. Micro-Service Strong consistency
      3.1 Publisher
      3.2 Subscriber
    5. Event Bus-cross-service final consistency
      4.1 Publisher & Subscriber All open local transactions, ensuring strong consistency
      4.2 Problem Scenario One: What happens when ③ fails to publish?
      4.3 Problem Scenario Two: What if the ③ release succeeds but the ④ update event state fails?
      4.4 Problem Scenario Three: Publisher side Ok,subscriber consumption error
0. Design Focus
    1. publisher localization publishevent Guarantee Event Publishing reliability
    2. subscriber localization subscribeevent guaranteed Event Subscription Reliability
    3. Subscribeevent guaranteed non-recurring consumption events through EventId & handlertype combination constraints
    4. Event Central Console handles Publisher & subscriber Event retry
1. Execution flowchart

2. Pseudo-code 2.1 publishevent
    public abstract class Event    {        public Event()        {            Id = Guid.NewGuid();            CreationTime = DateTime.UtcNow;        }        public Guid Id { get; set; }        public DateTime CreationTime { get; set; }    }    public class PublishEvent : Event    {        public PublishEvent(Event @event)        {            Id = @event.Id;            CreationTime = @event.CreationTime;            Type = @event.GetType().FullName;            Data = JsonConvert.SerializeObject(@event);            Status = PublishEventStatus.NotPublished;        }        public String Type { get; set; }        public String Data { get; set; }        public PublishEventStatus Status { get; set; }    }    public enum PublishEventStatus    {        NotPublished = 0,        Published = 1,        PublishedFailed = 2    }
2.2 Subscribeevent
    public class SubscribeEvent    {        public SubscribeEvent(Event @event, IEventHandler handler)        {            EventId = @event.Id;            EventCreationTime = @event.CreationTime;            EventType = @event.GetType().FullName;            EventData = JsonConvert.SerializeObject(@event);            HandlerType = handler.GetType().FullName;            HandlingStatus = HandlingStatus.HandleSucceeded;            HandlingTime = DateTime.Now;        }        public Guid EventId { get; set; }        public String EventType { get; set; }        public String EventData { get; set; }        public DateTime EventCreationTime { get; set; }        public String HandlerType { get; set; }        public DateTime HandlingTime { get; set; }        public HandlingStatus HandlingStatus { get; set; }    }    public enum HandlingStatus    {        HandleSucceeded = 0,        HandleFailed = 1    }
2.3 Publisher
    try    {        BeginTransaction(); // ①        //Biz Flow        EventRepository.PubilshEvent(@event);// ②        CommitTransaction();    }    catch(Exception ex){        RollbackTransaction();        throw ex;    }    EventBus.Publish(@event); // ③    EventResitory.EventPublished(@event.ToString()); // ④
2.4 Subscriber
    try    {        BeginTransaction();        //Biz Flow        EventRepository.SubscribeEvent(@event , eventHandler); // ⑤        CommitTransaction();    }    catch(Exception ex){        RollbackTransaction();        throw ex;    }
3. Micro-Service Strong consistency 3.1 Publisher
    1. Enable local transactions to achieve strong consistency
    2. Execute local business code
    3. Save Event Pre-release status inside local transaction
    4. Publish event to Event bus
    5. Modify event publication status to published
3.2 Subscriber
    1. Enable local transactions to achieve strong consistency
    2. Execute local business code
    3. Save subscription events to the local warehouse
4 Event Bus-cross-service final consistency 4.1 Publisher & Subscriber all on-premises transactions, guaranteed strong Consistency 4.2 problem scenario One: What happens when ③ fails to publish?
    1. publish failure, meaning throw exception, does not execute, then event status remains pre-release state
    2. Subsequent events retry republishing the event and update the event status to published
4.3 Problem Scenario Two: What if the ③ release succeeds but the ④ update event state fails? 4.3.1 Scene Figure Subscriber subscription succeeded
    1. Publish succeeded, but update event status failed, event status is still pre-release status
    2. subscriber Successful execution of business code after subscribing to this event
    3. Subscriber Save subscription events to the local subscription event warehouse
      Problem with this scenario: publisher will retry publishing the pre-release status event via an event , then subscriber will repeat the event
      Scenario: The problem we can avoid repeated consumption by combining subscribeevent EventId & handlertype with UNIQUE constraints
4.3.2 Scene Shi Shi Subscriber subscription failed
    1. Publish succeeded, but update event status failed, event status is still pre-release status
    2. Subscriber failed to execute consumption
    3. subscriber rolling back local transactions
      There is no problem with this scenario because publisher retries the pre-release status event again with an event retry .
4.4 Problem Scenario Three: Publisher side Ok,subscriber consumption error
    1. Publisher -side processing goes smoothly
    2. Subscriber consumption failed, rollback of local transaction, subscribeevent not stored to local warehouse at this time
      Problems with this scenario:
      Publisher is sent successfully, and the local publishevent event is published , meaning that it is not known from the publisher side Subscriber consumption failure needs re-consumption
      Solution:
    3. Get publishevent for event retry by detecting publishevent & subscribeevent
    4. Republish publishevent to subscriber
5. Support the above programming model with NuGet installation components
Install-Package SmartEventBus.RabbitMQImplInstall-Package SmartEventBus.Repository
6. Orm:smartsql Advertise

Smartsql = Dapper + MyBatis + Cache (Memory | Redis) + ZooKeeper + r/w splitting + ...

How to properly implement the internal consistency of microservices through localization events, and the final consistency between event bus across MicroServices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.