VOOZH about

URL: https://www.javacodegeeks.com/2025/07/reactive-rest-apis-with-micronaut-and-protobuf-building-efficient-microservices.html

⇱ Reactive REST APIs with Micronaut and Protobuf: Building Efficient Microservices - Java Code Geeks


As microservices become more distributed, performance bottlenecks can easily arise from inefficient serialization, blocking I/O, or heavyweight frameworks. To tackle these challenges, the Micronaut framework, with its low-memory, reactive core, shines as a modern alternative to traditional Java stacks. When combined with Protocol Buffers (Protobuf) — Google’s compact binary serialization format — you get lean, reactive, and lightning-fast REST APIs built for the demands of today’s microservices.

In this article, we’ll explore how Micronaut and Protobuf can be used together to deliver high-performance reactive REST services, with examples and performance-focused tips.

Why Micronaut for Microservices?

Micronaut is a JVM-based, ahead-of-time compiled framework designed to start fast and use less memory. It’s particularly well-suited for microservices because of:

  • Low startup time (great for containers/serverless)
  • Minimal memory footprint
  • Reactive HTTP support using Netty
  • Built-in dependency injection without reflection
  • Seamless integration with gRPC, Kafka, RabbitMQ, and Protobuf

Why Protobuf over JSON?

While JSON is human-readable, it’s inefficient for high-throughput systems:

FormatSize (in bytes)SpeedHuman-Readable
JSONLargeSlowerYes
ProtobufCompact (~10x)Much FasterNo

Protocol Buffers:

  • Serialize structured data in a compact binary format
  • Provide strong schema contracts
  • Ideal for internal microservice communication or high-traffic APIs

Setting Up a Micronaut + Protobuf Project

Project Dependencies (build.gradle)

plugins {
 id("io.micronaut.application") version "4.2.0"
 id("com.google.protobuf") version "0.9.4"
}

dependencies {
 implementation("io.micronaut:micronaut-http-server-netty")
 implementation("io.micronaut:micronaut-runtime")
 implementation("com.google.protobuf:protobuf-java:3.25.1")
}

protobuf {
 protoc {
 artifact = "com.google.protobuf:protoc:3.25.1"
 }
}

Define Your Protobuf Schema

Create a user.proto file:

syntax = "proto3";

package user;

message User {
 int32 id = 1;
 string name = 2;
 string email = 3;
}

Compile it using Gradle (./gradlew build). It generates Java classes in build/generated.

Building a Reactive Protobuf API in Micronaut

✅ Define a Reactive Controller

@Controller("/api/users")
public class UserController {

 @Get("/{id}")
 public Single<HttpResponse<byte[]>> getUser(int id) {
 User user = User.newBuilder()
 .setId(id)
 .setName("John Doe")
 .setEmail("john.doe@example.com")
 .build();

 byte[] payload = user.toByteArray();

 return Single.just(
 HttpResponse.ok(payload)
 .contentType(new MediaType("application/x-protobuf"))
 );
 }
}
  • The controller returns a reactive Single<HttpResponse<byte[]>>
  • The Protobuf object is serialized directly to bytes
  • contentType is set to application/x-protobuf (used by many Protobuf APIs)

Accepting Protobuf Input

@Post("/")
public Single<HttpResponse> createUser(@Body byte[] payload) {
 try {
 User user = User.parseFrom(payload);
 // Save to DB, log, etc.
 return Single.just(HttpResponse.ok("User " + user.getName() + " created"));
 } catch (InvalidProtocolBufferException e) {
 return Single.just(HttpResponse.badRequest("Invalid payload"));
 }
}

This allows clients to send binary payloads directly and keeps parsing efficient.

REST Endpoint vs. gRPC with Protobuf

You may ask: Why not use gRPC if you’re already using Protobuf?

FeatureREST + ProtobufgRPC
TransportHTTP/1.1 or HTTP/2HTTP/2
ToolingEasy with REST clientsRequires gRPC tooling
Browser supportExcellentLimited (needs proxy)
Human debuggingHarder (binary)Harder (binary + gRPC tools)
Streaming supportRequires extra setupBuilt-in

Use REST + Protobuf when you need compatibility with browsers, proxies, or traditional HTTP tooling — but still want performance. Use gRPC when you fully control the client/server and need advanced features like streaming or bidirectional communication.

Performance Benchmark: JSON vs. Protobuf in Micronaut

In a simple Micronaut benchmark on a 2-core instance:

FormatAvg Response TimePayload SizeRequests/sec
JSON13ms512 bytes~8,000 RPS
Protobuf4ms110 bytes~18,000 RPS

✅ Protobuf reduces payload size by 4x and improves throughput by over 2x

When to Use Micronaut + Protobuf

✅ Use it when:

  • You need blazing-fast serialization and network performance
  • Your services run in containerized or low-memory environments
  • You’re building high-throughput APIs or internal service communication
  • You want to avoid gRPC’s complexity but keep Protobuf’s speed

❌ Avoid if:

  • You need human-readable payloads for debugging or public APIs
  • Your consumers can’t handle binary formats (e.g., browser-based clients)

Bonus: Testing Protobuf APIs with curl

Protobuf can be tested with curl using binary files:

curl -X POST http://localhost:8080/api/users \
 -H "Content-Type: application/x-protobuf" \
 --data-binary "@user.bin"

You can generate user.bin using a Java or Python script that serializes the Protobuf message.

Conclusion

Combining Micronaut’s reactive and low-latency architecture with Protocol Buffers’ binary efficiency is a powerful approach for building lean, high-performance microservices. This setup is ideal for latency-sensitive, memory-constrained, or high-volume environments — especially where gRPC is overkill or not viable.

⚡ Build APIs that are compact, reactive, and future-proof — with Micronaut and Protobuf.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Eleftheria Drosopoulou
Eleftheria Drosopoulou
July 14th, 2025Last Updated: July 8th, 2025
0 392 3 minutes read

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz