From synchronous RPC calls to event-driven communication using Java 21, Micronaut 4, gRPC, and Apache Kafka.
When building microservices, one of the first architectural decisions is how services should communicate.
Should one service call another and wait for a response?
Or should it publish an event and let other services process it independently?
In real-world systems, the answer is often both.
In this article, we'll explore how gRPC and Apache Kafka fit into a Java microservices architecture, why they solve different communication problems, and what developers should consider when combining them.
We'll also look at a practical Java 21 and Micronaut 4 starter project that you can explore and adapt.
- TWO COMMUNICATION PATTERNS, TWO DIFFERENT RESPONSIBILITIES
Let's start with a simple example.
Imagine an e-commerce application with three services:
• Order Service
• Payment Service
• Notification Service
When a customer places an order, several things happen.
The Order Service needs to process the request, while other services may need to react to the new order.
This is where synchronous and asynchronous communication become useful.
Synchronous communication with gRPC
gRPC is designed for direct service-to-service communication.
A client sends a request to a service and receives a response.
For example:
- The client sends a CreateOrder request.
- The Order Service processes the request.
- The service returns a response containing the order ID and status.
gRPC uses Protocol Buffers to define strongly typed service contracts and supports efficient communication over HTTP/2.
With Java and Micronaut, developers can build services with clearly defined interfaces and generated client/server code.
Typical use cases include:
• Request/response operations
• Service-to-service queries
• Operations requiring an immediate response
• Strongly typed API contracts
Asynchronous communication with Apache Kafka
Not every interaction requires an immediate response.
Once an order has been created, other services may need to know about it.
Instead of calling each service directly, the Order Service can publish an event to Kafka.
The communication flow is simple:
- The Order Service publishes an OrderCreated event.
- Kafka receives and stores the event in a topic.
- The Payment Service consumes the event.
- The Notification Service also consumes the event.
- Each service processes the event independently.
This provides a different communication model:
• Services can be less tightly coupled.
• Consumers can process events independently.
• Events can be retained and consumed asynchronously.
• New consumers can be introduced without changing the original producer.
The important point is that gRPC and Kafka are complementary technologies, not necessarily competing alternatives.
- A POSSIBLE ARCHITECTURE WITH JAVA 21 AND MICRONAUT 4
A practical architecture can combine both communication patterns.
Imagine the following sequence:
- A client sends a request to a Micronaut service through gRPC.
- The service receives the request and executes its business logic.
- When appropriate, the service publishes an event to Apache Kafka.
- Kafka makes the event available to independent consumers.
- Each consumer processes the event according to its own responsibilities.
In this model:
• gRPC handles direct communication.
• Micronaut provides the application framework.
• Java 21 provides the runtime.
• Protocol Buffers define the gRPC contracts.
• Kafka supports asynchronous event communication.
This is a conceptual architecture illustrating how the technologies can work together. The actual implementation details depend on the application and its reliability requirements.
- AN IMPORTANT CHALLENGE: THE DUAL-WRITE PROBLEM
Combining a database and Kafka introduces an important distributed-systems challenge.
Consider this sequence:
- A gRPC request arrives.
- The service saves data in its database.
- The service publishes an event to Kafka.
What happens if the database transaction succeeds but Kafka publication fails?
The database contains the new order, but other services never receive the corresponding event.
This is commonly known as the Dual-Write Problem.
The Transactional Outbox pattern
One established approach is the Transactional Outbox pattern.
Instead of writing directly to Kafka, the application:
- Saves the business data in the database.
- Saves the corresponding event in an Outbox table.
- Commits both changes in the same database transaction.
- Uses a separate process to publish Outbox events to Kafka.
Change Data Capture (CDC) tools such as Debezium can capture database changes and forward the relevant events to Kafka.
This approach helps avoid inconsistencies between database persistence and event publication.
It also introduces additional considerations, such as duplicate delivery, retries, and consumer idempotency.
These are important topics when moving from a simple gRPC service to a production-grade event-driven architecture.
- GETTING STARTED WITH A PRACTICAL JAVA gRPC PROJECT
Theory is useful, but understanding the components becomes easier when you can run and inspect an actual project.
I created GRPCMicroService, a Java 21 and Micronaut 4 starter project designed to provide a practical foundation for building gRPC microservices.
The project is available in two versions.
Free version — GRPCMicroService v1
The free version is a starting point for developers who want to understand the essential components of a Java gRPC microservice.
It includes:
• Java 21 and Micronaut 4
• gRPC client and server
• Protocol Buffers definitions
• Maven configuration and code generation
• Error-handling components
• Automated tests
• Technical documentation
Explore the source code:
https://github.com/frekost/grpc-micronaut-starter-kit
The repository is available under the MIT License.
Complete version — GRPCMicroService v2
For developers looking for a more complete starting point, the paid version extends the starter project with additional building blocks.
It includes:
• gRPC client and server
• Protocol Buffers
• Event handling
• Apache Kafka integration
• Logging
• Error handling
• Maven configuration
• Automated tests
• Technical documentation
The goal is to provide an organized foundation that developers can explore, debug, and adapt instead of assembling every initial component from scratch.
Explore GRPCMicroService v2:
https://frekost.gumroad.com/l/grpc2micronaut
- TRY IT YOURSELF
If you want to start with the free version, clone the repository.
Open a terminal and execute:
git clone https://github.com/frekost/grpc-micronaut-starter-kit.git
Then enter the project directory:
cd grpc-micronaut-starter-kit
Requirements:
• JDK 21
• Apache Maven
Build and verify the project with Maven:
mvn clean compile verify
Open the project in your preferred Java IDE and run the application using its main class:
org.frekost.micro.Application
The example includes a Greeter service that demonstrates a basic gRPC client/server interaction.
For detailed setup instructions, consult the project documentation.
- FINAL THOUGHTS
Building microservices is not just about choosing a framework or a messaging platform.
It's about understanding the communication patterns, reliability requirements, and responsibilities of each component.
gRPC provides a foundation for synchronous service communication.
Apache Kafka provides a foundation for asynchronous event-driven workflows.
Micronaut and Java provide the application environment in which these components can work together.
If you're exploring Java microservices, I hope these starter projects provide a useful starting point.
Start with the free project on GitHub:
https://github.com/frekost/grpc-micronaut-starter-kit
Looking for the more complete starter kit?
https://frekost.gumroad.com/l/grpc2micronaut
LET'S DISCUSS
How do you approach communication in your Java microservices?
• Do you use gRPC for synchronous calls?
• Do you use Kafka for asynchronous events?
• Have you implemented the Transactional Outbox pattern?
• Do you use CDC tools such as Debezium?
I'd be particularly interested in hearing how you handle reliability and consistency between databases and Kafka in production environments.
Top comments (0)