r/apachekafka • u/frekost • 19h ago
Tool gRPC + Kafka: A practical architecture for Java microservices (Micronaut 4 & Java 21)
Title: gRPC + Kafka: A practical architecture for Java microservices (Micronaut 4 & Java 21)
How do you decide between synchronous and asynchronous communication in a microservices architecture?
I've been exploring a combination of gRPC and Apache Kafka using Java 21 and Micronaut 4.
The idea is straightforward:
gRPC for synchronous request/response communication.
Kafka for asynchronous domain events and communication between independent services.
Protocol Buffers for strongly typed service contracts.
But combining the two introduces some interesting engineering challenges:
-Event serialization and schema evolution
-Consumer groups and idempotency
-Retry policies and dead-letter topics
-Transactional Outbox and consistency
-Observability across distributed services
I've put together a practical implementation starting with a simple Greeter service.
GitHub: https://github.com/frekost/grpc-micronaut-starter-kit
A question for the community:
How do you combine gRPC and Kafka in production?
Do you publish Kafka events directly after handling a gRPC request, or do you prefer an Outbox-based approach?
I'd love to hear about your experiences and architectural decisions.
1
u/fatduck2510 11h ago
your question is unclear. 1st you mentioned decide between sync and async, but later you mentioned combining gRPC + Kafka + protobuf.
In microservices architecture, there are part of the system is asynchronous, the other parts are synchronous. You don't decide whether it's one way or another. There should be both. The main thing to note here is around protobuf. It's a protocol for gRPC and supported by Kafka but there are challenges around using protobuf in Kafka (compare to avro):
• No true null (proto3): Unset primitives default to 0, "", or false, making it impossible to distinguish "missing" from "default" without extra wrappers. Avro enforces explicit null unions. • Silent wire collisions: Protobuf groups multiple types under broad wire formats (e.g., int32, bool, and enum all use varint). Incompatible changes or reused field numbers silently decode into corrupt values instead of failing, whereas Avro strictly validates exact types.
2. Fragile Compatibility
• Field number traps: Deleting a field without marking its number reserved risks silent data corruption if that number is ever reused. Avro avoids this by matching fields by name and aliases. • Rigid evolution: Changing field types breaks wire compatibility, and renaming fields breaks JSON mappings. Avro natively supports safe type promotions (e.g., int to long) and field aliases. • The required lock-in (proto2): Marking a field required made it permanently impossible to remove without breaking existing consumers.
3. Versioning Friction
• Unversioned payloads: Protobuf bytes contain no schema metadata, requiring an external Schema Registry to track versions. Avro files embed their schema directly. • Compile-time lockstep: Updates require regenerating and redistributing code across services, unlike Avro’s dynamic runtime schema resolution.
1
u/mightegas 15h ago
Pretty much default to transactional outbox pattern. gRPC clients invoke domain logic for manipulating data, write result and outbox events back to database, then use a database-specific CDC connector to pipe the events to Kafka, usually with an Avro mapping in between.
1
u/aayush_satna_dev 6h ago
I will stick with the orthodox pattern and just ensure that the dual write problem is eliminated at the first place.