Kafka learning path Level 2 This level · 6 of 7
Medium java spring

When would you choose REST over Kafka for communication between microservices

The 30 second answer

I use REST when the caller needs the answer right now to continue, like checking stock before confirming an order or reading a user's profile. That is a synchronous request and reply. I use Kafka when the caller only needs to announce that something happened and can move on, when several services react to the same event, or when the consumer may be down for a while. REST is simpler but couples availability; Kafka decouples services but adds eventual consistency, duplicates and ordering to think about.

Could you say this in an interview?

I decide from one question: does the caller need the result before it can continue?

REST (or gRPC) fits when the answer is part of the current request. microservices.io describes this as remote procedure invocation: a client sends a request and the service replies. Examples are reading data to render a page, validating a coupon, or checking stock before confirming an order. It is simple to build, test and debug, and errors come back to the caller straight away. The cost is coupling at run time: the pattern page lists "reduced availability since the client and the service must be available for the duration of the interaction". A chain of synchronous calls multiplies latency and failure, so each call needs a timeout, limited retries and often a circuit breaker.

Kafka fits when the caller is announcing a fact, not asking a question. In the messaging pattern, services exchange messages through a broker, and the client does not wait. The microservices.io page lists the benefits as loose coupling, better availability because the broker buffers messages until the consumer can process them, and support for many interaction styles. Examples are OrderPlaced triggering email, invoicing, analytics and search indexing; feeding audit logs; or smoothing a burst of writes. Kafka keeps the events after they are read, so a new consumer can be added later and replay history, and several consumer groups read the same topic independently.

What Kafka costs you. The flow becomes eventually consistent, so the user may not see the result at once. You must handle duplicates with idempotent consumers, ordering only within a partition, retries and dead-letter topics, schema evolution, and consumer lag. Debugging a flow across topics needs correlation IDs and tracing. You also need a reliable way to publish after a database write, which is usually the transactional outbox.

A rule I say in interviews: commands that need an answer go over REST; events that others may react to go over Kafka. Many real flows mix both. For example, the order service validates payment synchronously over REST, saves the order, and then publishes OrderPlaced to Kafka for everything that can happen later.

Alternate interview scenarios

"Give a real example of asynchronous communication between microservices."

A user places an order. The order service saves the order and an OrderPlaced row in an outbox table in the same database transaction, then returns 201 to the user. A relay publishes the event to Kafka with the order ID as the key, so all events for one order stay in order. The notification service sends the confirmation email, the inventory service reserves stock, and the analytics service updates dashboards, each in its own consumer group and at its own pace. If the email service is down for ten minutes, orders still succeed and the emails go out when it comes back, because its offset is still where it stopped. Each consumer stores processed event IDs so a redelivered event does not send a second email.

"The caller needs a result, but the work takes minutes. REST or Kafka?"

Neither alone. I accept the request over REST, return 202 Accepted with a job ID, do the work asynchronously through a queue or Kafka, and let the client poll a status endpoint or receive a callback or event when it finishes. Holding an HTTP request open for minutes ties up threads and breaks on timeouts.

Sources (4)
  1. microservices.io: Remote procedure invocation patternmicroservices.io
  2. microservices.io: Messaging patternmicroservices.io
  3. Kafka 4.4 introduction (events, topics, retention, consumer groups)kafka.apache.org
  4. microservices.io: Transactional outboxmicroservices.io
Previous
Next