0
0
Spring Bootframework~8 mins

Message serialization in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Message serialization
MEDIUM IMPACT
Message serialization affects the speed of data transfer between services and the time to process messages in the application.
Sending data between microservices
Spring Boot
public record User(String name, int age) {}

// Using JSON with Jackson
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
outputStream.write(json.getBytes(StandardCharsets.UTF_8));
JSON serialization is faster and produces smaller messages, improving network and CPU efficiency.
📈 Performance Gainreduces message size by 70%, processing time under 20ms
Sending data between microservices
Spring Boot
public class User {
  private String name;
  private int age;
  // getters and setters
}

// Using default Java serialization
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(user);
Default Java serialization is slow and produces large message sizes, increasing network latency and CPU usage.
📉 Performance Costadds 50-100kb per message, blocks processing for 50-100ms
Performance Comparison
PatternCPU CostMessage SizeNetwork ImpactVerdict
Default Java SerializationHighLarge (~50-100kb)High latency and bandwidth use[X] Bad
XML SerializationMediumVery LargeHigh latency and bandwidth use[!] OK
JSON SerializationLowMediumEfficient network use[OK] Good
Binary Serialization (Protobuf)Very LowSmallMinimal latency and bandwidth[OK] Good
Rendering Pipeline
Message serialization impacts the server processing stage before sending data over the network. Efficient serialization reduces CPU time and network payload size, speeding up data delivery.
Server Processing
Network Transfer
Client Processing
⚠️ BottleneckServer Processing (serialization CPU cost)
Optimization Tips
1Avoid default Java serialization for network messages due to size and speed issues.
2Prefer JSON or binary formats like Protobuf for faster serialization and smaller messages.
3Set explicit content types in APIs to ensure efficient serialization formats are used.
Performance Quiz - 3 Questions
Test your performance knowledge
Which serialization format generally produces the smallest message size?
ABinary formats like Protobuf
BXML
CDefault Java serialization
DPlain text
DevTools: Network
How to check: Open DevTools, go to Network tab, inspect the size and timing of API responses or messages.
What to look for: Look for large payload sizes and long transfer times indicating inefficient serialization.