Event sourcing + CQRS + RESTFul

Hi,

Do you have any recommendations on how implement and RestFul API that exposes a CQRS+EventSourcing system?

It seems that all post, put, patch, delete… should return a 202 Accepted, even when the invariants could fail at some point.

is there any way to wait in the web controller layer for the actual creation or modification?

Thank you so much

Hi,

I’m also interested in this question, normally a POST (creation) in a REST api return the new resource in the body, how can be done this in Axon?
any recomendations when implementing a RESTfull API using CQRS+ES?

Best Regards.

Hi Jorge,

“pure” REST and CQRS, in my opinion, aren’t really compatible. REST is about State Transfer. CQRS is about transporting intent, as opposed to state.

Imagine a command to confirm an order. This would typically be implemented as a POST request. If you want to return the current state of the order, you’d have to know what the client is interested in knowing about the order. The idea of CQRS is that you have different models for different “audiences” and needs.

Typically, the result of a command is just an OK (or NOT OK). So returning a 200 and perhaps some server-generated identifier should be sufficient. Then, the client can request any information from any suitable query model to know more about it.

If you DO really want to return some state in the body of the POST’s response, you can simply execute a query after the command and return that query’s result.

Hope this helps.
Cheers,

Allard

Thanks Allard

Imagine the following situation:* There is a system that currently exposes a Restful API

  • creating one of the resources (e.g. add/post a new product in a catalog) already return a 201 with the contents of the new created resource and the location.
  • We are moving the backend to EventSourcing + CQRS
  • we cannot break the actual contract.

Restful and CQRS are not a perfect match, but in this example the audience of the command is the audience of the query. We cannot force the client to keep asking for the new created resource or listen to several domain events (success or fail).

Is there any way to map from CQRS to Rest (e.g. waining for productCreated or productRejected) to fulfil both requirements)?

Hi Victor,

There are few ways of realizing this, but a particularly nice one I think is to use the new Axon 3.3 subscription queries.

To illustrate, I created some sample code here: https://github.com/fransvanbuul/axon-sync-rest-frontend

There’s a CustomerRestController which can handle POSTs for a new Customer, and will synchronously return data from the read model, even though the read model itself a asynchronously updated through a tracking event processor - so it waits for that to happen.

This signalling is implemented by the REST controller subscribing to a find customer query prior to sending the command, and the query model emitting an update to the results of that query when it has registered a new customer.

Kind regards,

Thank you so much!

I will take a look at this on Monday morning!