Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use REST + Event Sourcing + CQRS together

I understand the basics of REST + Event Sourcing. I never worked on a strict RESTful API and not either in any Event Sourcing project.

Can someone explain if both can be used together?

As in event sourcing, the client send events, does this mean that on the server there is a single collection of event and all POSTs of the API will be on that collection, to add events to it?

How can the client discover the commands it can send to the server?

like image 797
Sebastien Lorber Avatar asked Jun 21 '13 08:06

Sebastien Lorber


People also ask

What is the difference between CQRS and Event Sourcing?

CQRS is implemented by a separation of responsibilities between commands and queries, and event sourcing is implemented by using the sequence of events to track changes in data.

Which database is best for Event Sourcing?

The core features such as guaranteed writes, concurrency model, granular stream and stream APIs make EventStoreDB the best choice for event-sourced systems - especially when compared with other database solutions originally built for other purposes, And on top of that, it's open source.

Is CQRS asynchronous?

Asynchronous commands are not required for CQRS.

Is CQRS pattern good?

CQRS is a popular architecture pattern because it addresses a common problem to most enterprise applications. Separating write behavior from read behavior, which the essence of the CQRS architectural pattern, provides stability and scalability to enterprise applications while also improving overall performance.


2 Answers

Can someone explain if both can be used together?

Yes. The client (browser) simply does what it wants to do and the (http) server can record those actions as events.

As in event sourcing, the client send events, does this mean that on the server there is a single collection of event and all POSTs of the API will be on that collection, to add events to it?

No. The client can be the originator of events, but should not known what constitutes an event in order to prevent tight coupling between the server and the client based on that collection of events. Event Sourcing should be encapsulated and is hidden from the actor.

How can the client discover the commands it can send to the server?

This is not necessary if you don't need to send events on the same collection as you've suggested in your previous question. You can simply publish a REST API in any way you want and hide the event sourcing from the client/actor. Have a look at http://restdesc.org/.

like image 148
Lodewijk Bogaards Avatar answered Sep 20 '22 20:09

Lodewijk Bogaards


REST is a delivery method, it determines the interface of your application. You use REST mostly with immediate consistency model, but it can support an eventual consistency model by responding 202 accepted by commands.

Event sourcing is a general data storage mechanism. You use event sourcing usually with an eventual consistency model along with domain driven design, and command and query segregation, but it can support an immediate consistency model probably by a multi-phase commit.

They solve completely different things in your application and they are compatible with each other, so you can use them together.

As in event sourcing, the client send events, does this mean that on the server there is a single collection of event and all POSTs of the API will be on that collection, to add events to it?

You completely misunderstood the concept. You can store an event sequence in an event storage. Event are state changes, so if you store every state change of your application and replay it in the proper order, then you can recreate the current state of your application. This is very good, because you can migrate data to another database simply by replaying the events and transforming them into database queries. You can do just the same to create a query cache using a regular database. So your clients can read that query cache instead of always recreating the current state from the grounds.

The events are usually not created by the client. By domain driven design, your domain logic creates domain events by processing commands.

How can the client discover the commands it can send to the server?

By REST the clients use links to send requests to the REST service. The REST service can process those requests and transform them into commands and queries. The commands are processed by the domain logic, and will result in raising domain events. The queries are transformed into database queries which address the query caches.

like image 43
inf3rno Avatar answered Sep 16 '22 20:09

inf3rno