Main

Events vs Commands: The same but opposites

Message bus communication often uses Commands and Events. But, what's the difference between them? Aren't they both just messages? The difference is about grammar, intention, and also the direction of some different kinds of arrows... 0:00 Intro 0:18 What do the words mean? 0:34 When do they happen? 1:36 Who are they for? 2:53 Direction of control and dependency 4:19 Example third party provider 5:07 Related concepts

Drawing Boxes

7 days ago

what's the difference between a command and an event both are messages they're serialized payloads containing a bunch of information that another service needs to know about so why do we have two different names there are some different ways we can look at this but first let's start with what the words command and event really mean in plain English to get an idea of the intent behind them a command is like an instruction or a demand it says go and do this thing whereas an event is a thing that h
as happened is just a fact the thing was done one key difference is when these messages happen in relation to the thing happening a command happens before and an event happens after this shows in the conventional names of the messages do thing make payment close account the verbs use the present tense and imperative mood this is different to done thing made payment closed account the these are in past tense because the event has already happened by this point commands come before the thing happe
ns events come after this might seem like a trivial distinction but it has an important impact on validation commands can be rejected like if they're invalid or if there's an error trying to perform the action that would just mean the thing never happens but events on the other hand well the things already happened by the time the event is published they can't be rejected it's a fact that the thing has happened in the past and anything reading the message must handle that accordingly another imp
ortant difference in the choice of wording is who the message is intended for commands are written in second person they're addressed to the recipient directly who is the implied subject of the verb you do the thing you make the payment and not that it makes a difference in English but it's the singular form of you there's only one intended recipient of a command which is the system that is expected to do the thing the sender knows exactly who is supposed to receive the command but events aren't
really addressed to anyone specifically there's just enough language missing from the event name that you could consider them to be in first person using a perfect tense like I have done the thing as if the speaker is the system that published the event but most often the sentence is thought of as being in the passive voice where the subject is the thing itself the thing was done unlike in a command there's no intended recipient just anyone that wants to listen that could be no one someone or i
t could be many different systems that all care about the same event one of which may be the system that sent the original command this is why message Brokers have the ability to Fan out messages and put copies of them into different cues for each recipient if we look at which systems cause which other systems to handle something we see this clear flow of control from left to write the original command sender initiates the flow which causes the middle system to do the thing and then publish the
event which in turn causes the event to be handled by the subscribers but who owns the data contract for each message well for the command it's the system that knows how to do the thing that defines what the message should look like it defines the format the object structure and the names of the properties only it knows what information it needs to do the thing like the ID for the payment or the name on the account the sender of a command must depend on that data contract to know exactly what pr
operties it can put on the command that it sends so the direction of dependency points in the same direction as the flow of control for the event it is also the system that knows how to do the thing that defines what the message should look like the subscribers of the event must depend on that data contract to know exactly what properties they can expect to find on the event when they receive it the direction of dependency is reversed it points in the opposite direction to the flow of control us
ing an event inverts the direction of dependency and you don't need a message bus to do this you can use the same patterns with any type of communication such as with an HTTP web API let's take an example imagine a third-party payment provider like PayPal you can integrate with their system by reading their API documentation and seeing what requests you can send to their apis you can send them an HTTP request to make a payment that's a command it's in the present tense second person imperative m
ood you depend on their data contract and you initiate the flow of information you might also subscribe to their web hooks API their API documentation says to give them a call back URL and when the payment is made they'll call it payment was made that's an event it's in past tense third person passive voice you still depend on their data contract but they initiate the flow of information so commands can exist outside of messaging when they do they're sometimes differentiated from queries such as
get payment which is another concept called cqrs you can watch my video about that on this channel here events also exist as a concept unrelated to messaging you might have heard of domain events in DDD you you can also use events to derive the state of your system which is called event sourcing and again I've got a video about that that you can watch here and as always thanks for watching this one

Comments

@AK-vx4dy

Crisp and clear, leaving no doubts or unanswered ifs. Great job. Draw us more boxes.

@sillycoder9690

you are awesome and so are your videos !!!

@danial_hamedi

thanks man, you are awesome!

@KhoaLe-wn5wb

Waiting for so long😊

@hatiko8227

Need video for Unit of Work pls