Back to Home

Microservices Communication Tools Kafka Feign

The article breaks down microservices interaction tools: synchronous HTTP clients WebClient and Feign, real-time WebSocket, asynchronous Kafka broker. Describes Reactor reactivity, delivery guarantees, Flux code examples. Recommendations for selection based on load and scenarios.

How microservices communicate: Kafka vs Feign vs WebClient
Advertisement 728x90

Microservices: Communication Protocols and Tools

Microservices exchange messages via two main models: synchronous through direct HTTP requests to another service's endpoints, and asynchronous through brokers like Kafka or RabbitMQ. In the synchronous model, the sender waits for a response, knowing the receiver's URL and endpoints. The asynchronous model uses topics to store messages without direct interaction.

Synchronous works best for request-response scenarios, while asynchronous shines for events where reliable delivery trumps speed.

Synchronous HTTP Clients

For direct calls, several tools stand out:

Google AdInline article slot
  • RestTemplate: A blocking client that's simple but outdated for new projects due to thread blocking.
  • WebClient: Reactive on Project Reactor, non-blocking, ideal for high-throughput systems with async processing.
  • Feign Client: Declarative—define an interface with annotations, and Spring generates the HTTP client implementation. Synchronous with caching support, it cuts down on boilerplate code.

Feign simplifies things: just annotate an interface with @GetMapping or @PostMapping, and you're set. WebClient edges it out in throughput thanks to non-blocking I/O.

Reactive Programming with WebClient

Reactivity on Project Reactor uses Mono (single value) and Flux (stream of values). Key differences from traditional async:

  • Handles data streams over time with backpressure: consumers signal producers when ready.
  • Declarative operators like map, filter, flatMap, and buffer for transformations.
  • Lazy evaluation: operations kick off only on subscription.

Example processing users from a repository:

Google AdInline article slot
Flux<User> users = userRepository.findAll();
Flux<String> names = users
    .filter(u -> u.getAge() > 18)
    .map(User::getName)
    .take(10);
names.subscribe(name -> {
    System.out.println("Received name: " + name);
});

Here, database queries start only on .subscribe(). Reactor integrates seamlessly with Spring WebFlux for reactive web apps. Use it for high loads or streaming data; stick to synchronous tools for basic CRUD.

WebSocket for Event-Driven Exchange

WebSocket creates a persistent connection for bidirectional event exchange without repeated requests. No request-response cycle: services push messages over the open channel.

Perfect for:

Google AdInline article slot
  • Chats and real-time notifications.
  • Games and stock tickers with instant updates.
  • Time-series streams without polling.

Downside: no delivery guarantees if the connection drops. It's all about speed, not rock-solid reliability.

Kafka: Asynchronous Message Broker

Kafka is a distributed system for storing and streaming events. Producers send to topics (partitioned logs), consumers read from partitions.

Delivery guarantees:

  • At-least-once: Messages arrive at least once (duplicates possible on commit failures).
  • Exactly-once: Via idempotent producers and transactions.

Idempotency ensures repeated requests yield the same result. Broker replication prevents data loss. Ordering is preserved within partitions by key (e.g., user ID).

Consumer groups spread the load: multiple consumers handle partitions in parallel.

Kafka use cases:

  • Async events like emails, analytics, or bonuses.
  • Replaying history for recalculations.
  • Streaming (Kafka Streams, CDC from databases).

Drawbacks: Complex cluster setup (KRaft simplifies it over ZooKeeper), and higher latency than HTTP.

Key Takeaways

  • Pick Feign for simple synchronous calls with minimal code.
  • Use WebClient for reactive high-load systems with Flux/Mono.
  • WebSocket for real-time bidirectional exchange without delivery guarantees.
  • Kafka for reliable async event-driven communication with full history.
  • Mix them: synchronous for CRUD, asynchronous for events.

— Editorial Team

Advertisement 728x90

Read Next