понедельник, 11 февраля 2019 г.

Cross-services communication

Recently I was involved in short but efficient discussion on the topic, and decided to consolidate some thoughts here.

In very short there are 3 kinds of communication types between services:
  • Query
  • Command
  • Event
Additionally, there can be synchronous and asynchronous implementations.
 
Event is async by nature. Query can be either sync or async in general though it's more about sync interaction (heavy async Query should be refactored in Command in most cases). Command is ok for both.

Sync calls are usually done via RPC, i.e. via REST. Asyn calls are better via Topics or Queues (of course one can implement them through REST as well though it's usually over complicated).
 
Context for Command - many producers, one consumer (I mean anatomically, even if there are several instances of the same logical consumer); therefore guaranteed delivery and retry in case of consumer failure are required.
 
Context for Event - one producer, many consumers. Consumers can be online or offline at the moment of events firing, but nevertheless they are interested to receive events. Guaranteed delivery and messages order are also important.
 
Therefore,
Message Brokers fit Commands since they had been created exactly for these needs. In addition, there are plenty of libraries to achieve required metrics.
Kafka is very ok for Events since it's easy, straightforward, and provides configurable retention time; guaranteed delivery and correct order are available by design.

It is possible to implement Events via Message Brokers and Commands via Kafka though it seems to be not the common way.

So, finally