Last active
November 11, 2019 16:00
-
-
Save gonzaloserrano/a29187d9baac9c629ec332269476b878 to your computer and use it in GitHub Desktop.
command vs event, sync vs async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hey, | |
So I've been thinking about the source of data for the delivery system which is | |
the creation of the delivery. I've read a bit and talk to some people and | |
wanted to share some info. | |
First, the difference between a command and an event is their intent. Both are | |
DTOs (just transfer data) but: | |
- an event is something that happened in the past in the producer of the event, | |
e.g rails-backend produces Delivery Created | |
- an event can be handled by different event handlers (e.g increment the | |
counter of creations and send an email) | |
- a command is like an action and is in the present tense, e.g Create Delivery | |
- and a command is handled by one and just one command handler (but it can do | |
multiple things). | |
Notice that I've not talked about message brokers yet. A message broker enables | |
asynchronicity. You send a message (fire&forget) and optionally you consume | |
other messages that will arrive in the future. | |
The typical messages to send are events (message broker support multiple | |
consumers), but you could also send commands (even we just need one consumer). | |
Also, that would make the commands async, another key difference. The nature of | |
an event is fire&forget, which means you don't care about how it's handled. On | |
the contrary, for a command, you want a response back for consistency reasons | |
because you need it to know it has been executed properly before executing | |
other commands. | |
By using a message broker to send commands in an async way, you also need a | |
message with its response somehow (that's how WebSocket RPC systems work for | |
e.g). Using an HTTP API you also have a response, but it's sync: you get the | |
HTTP response right away. | |
So in the end what I'm saying is that we don't need async for creating | |
deliveries and it should be a plain HTTP endpoint instead of a message via | |
message broker like we proposed to Suraj some days ago. That makes everything | |
simpler. | |
Sorry for the long text, and happy to discuss any doubts. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment