Which events to store with event-sourcing?

I’m planning out the events that my application will be creating, but am running into problems with my design. I believe I’m not applying the event-sourcing concepts correctly.

Take the bank account example. My application receives a command to deposit cash, accepts it, and then creates the event CashDeposited. Based on the business logic, the higher account balance triggers the interest rate to be increased, so I create the event InterestRateChanged.

Now if I record both of these events to my event store, upon restart, both events will be replayed. But because the CashDeposited event triggers the InterestRateChanged event, won’t the InterestRateChanged be replayed twice: once from the event store and once as a result of CashDeposited being replayed?

1

My application receives a command to deposit cash, accepts it, and then creates the event CashDeposited. Based on the business logic, the higher account balance triggers the interest rate to be increased, so I create the event InterestRateChanged.

Now if I record both of these events to my event store, upon restart, both events will be replayed.

Good. That’s exactly what you want.

But because the CashDeposited event triggers the InterestRateChanged event, won’t the InterestRateChanged be replayed twice: once from the event store and once as a result of CashDeposited being replayed?

Yeah, if you are having this problem, your business logic may be in the wrong place.

Very broadly: the event handler part of your entity is supposed to be simple; apply the state changes implied by the event, validate the business invariant, and nothing else.

The events themselves are typically created by running a command on your entity — it’s the command that has the business logic to say “increase the balance, and then if the trigger fires then change the interest rate”. In your specific example, that one command will generate two events (perfectly reasonable).

When you reload the entity, you are going to replay the events, but not the command(s) that produced them. So the replay includes all the changes to state, but you don’t replay the business logic.

I recommend a proper implementation of Gateway pattern. As Martin Fowler states:

Many of the advantages of Event Sourcing stem from the ability to
replay events at will, but if these events cause update messages to be
sent to external systems, then things will go wrong because those
external systems don’t know the difference between real processing and
replays.

To handle this you’ll need to wrap any external systems with a
Gateway
. This in itself isn’t too onerous since it’s a thoroughly good
idea in any case. The gateway has to be a bit more sophisticated so it
can deal with any replay processing that the Event Sourcing system is
doing.

E.g.:

public class VerySimpleGateway()
{
    public bool IsReplayMode;

    public void FilterProcessingEvent(IEvent event)
    {
        if (IsReplayMode) {
            // for example delete event from event processing queue or adjust event state itself
            // next processing component in the pipeline is informed not to touch this event
        }
    }
}

In event processing pipeline you can treat such gateway as a step in Chain-responsibility pattern.

2

You have two choices

  • Only store trigger/first/external events in the event store
    In this case your “CashDeposited” event. So when you replay the
    this all the subsequent events will be triggered as per normal processing.
  • Store all events but check an “isReplay” flag before triggering a subsequent event.

I like the first option best. If you are replaying because an error occurred (say you set the threshold for an increase in interest rate too low) then the system will replay correctly with the fixed parameter.
Plus you really don’t want to complicate your code and testing with all those extra paths for “isReplay”.

I believe you are looking for the Process Manager pattern which listens for events and issues commands. In a replay scenario, the commands are ignored.

(This came from an informative answer to another question.)

In short, the PM listens for the right kind of CashDeposited event and issues a ChangeInterestRate command if necessary.

I think the problem here is that InterestRateChanged is not strictly an event from an event sourcing perspective – it’s a consequence of the CashDeposited event that might not happen if, for example, the business logic changes.

Remember that the key benefit of event sourcing is resilience in the face of changes in the rules. Your cash will always have been deposited, but how that results in interest being calculated is dependent on the business rules. Supposing there was a bug in interest rate calculation, then, simply changing that logic will fix the account automagically the next time the write model is loaded.

That’s the main reason you’re storing events and not a state snapshot. Not so you can “replay” them manually later, but so they’re always replayed and accounting for changes (or more likely – corrections) in the rules.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật