In microservice architecture, there are multiple approaches how services could communicate. Two popular approaches are Kafka and REST API which begs a question: What determines when to use async Kafka and when sync REST API?
Here are some preliminary thoughts/criteria, in no particular order:
| Use async Kafka or sync REST API <br>for inter-service-communication? | REST API | Kafka |
| --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| Data consistency & storage | strongly consistent, client gets up-to-date data on-demand from REST server, client might not need to store data | eventually consistent, consumer might need to store local copy of data (e.g. a local list of customers based on incoming NewCustomer events) |
| Typical domain focus | Objects? | Events |
| Result of a request/message | Immediate with good usability, also for request validation | More complex: no validation out of the box, no built-in link between request and possibly response; however, not really a use-case for Kafka? |
| Availability | retry or scale the server | HA built-in |
| API documentation & contracts | swagger, contract testing, shared specs? | contract tests (asyncApi.com), shared specs, parsing versioned libs published by the server, but there's nothing out-of-the-box |
| Local development | requires REST server to be up (with seed data?), or use fake (in-mem?) servers | no need for consumer/producer, devs might produce kafka msg manually |
| Switching the server | requires an update of clients | no update need, producers/consumers are decoupled |
| | | |
Some other notes/criteria/questions:
- In Nubank - [they prefer](https://building.nubank.com/why-we-killed-our-end-to-end-test-suite/) Kafka for writes, and REST API for reads
- NATS to handle both sync and async processes
- Boring technology & data availability:
- see [[The value of boring technologies]]
- which one has been already used and the team knows about?
- which one is currently available to be used? any new interface needed?
- Does the REST API server make further REST API calls, resulting in a chain of API calls? Would async Kafka approach simplify such a chain or rather improved modelling, that is, alignment of services with the domain?