Microservice to use Commands or Events ?

 In a distributed application, it is likely to use a combination of commands, events, and queries to implement business use cases. Hence, it is necessary to thoroughly thought through and identify all possible service to service communication based on the use cases to decide upon which messaging to construct to use.


When to use commands

Consider using Commands for applications or services with below use cases suits;


  • End to end message delivery guarantee between message producer and consumer.
  • A consumer must process a message exactly once, not more than once.
  • A consumer expects to process messages in the order they were published.
  • Producer of a command expects a response from the receiver to confirm the completion of a task; as a way of assuring the integrity of a system.
  • Using a message queue to deliver commands between components will address the above features. As an added advantage, message queues offer competing consumer pattern based message consumption at the receiving end. That allows scaling out the command processors while making sure that each command is processed only by a single consumer.

When to use events

Consider using Events for applications or services with below use cases suits;


  • Loose coupling between event producers and consumers where producers are not aware of the recipients of events at all.
  • To broadcast state change events to interested services or components in a fire and forget style.
  • An event will be discarded if there are no subscriptions exist. Doing so shouldn’t impact the business.
  • Messages brokers that provide pub-sub features are the best way to implement event broadcasting use cases. That way, the subscription handling is taken care of by the broker, allowing you to focus only on building the event publishing or receiving logic.

Comments

Popular posts from this blog

Interview Questions to Ask the Employer

Place .NET DLL in GAC

Windows Communication Foundation - FAQ