0
0
Spring Bootframework~8 mins

DTO pattern for data transfer in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: DTO pattern for data transfer
MEDIUM IMPACT
This pattern affects the amount of data sent over the network and the processing time on both client and server during data transfer.
Sending data from server to client in a Spring Boot application
Spring Boot
public class UserDTO {
    private String username;
    private String email;
    // getters and setters
}

@GetMapping("/users/{id}")
public UserDTO getUser(@PathVariable Long id) {
    User user = userRepository.findById(id).orElse(null);
    if (user == null) return null;
    UserDTO dto = new UserDTO();
    dto.setUsername(user.getUsername());
    dto.setEmail(user.getEmail());
    return dto;
}
Only necessary fields are sent, reducing payload size and avoiding sensitive data exposure.
📈 Performance GainReduces payload size by 30-50%, speeds up JSON parsing and rendering.
Sending data from server to client in a Spring Boot application
Spring Boot
public class User {
    private Long id;
    private String username;
    private String password;
    private String email;
    private Date createdAt;
    // getters and setters
}

@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
    return userRepository.findById(id).orElse(null);
}
Sending the entire entity including sensitive and unnecessary fields increases payload size and risks exposing sensitive data.
📉 Performance CostIncreases payload size by 30-50%, blocks rendering longer due to larger JSON parsing.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Sending full entity as JSONN/AN/AHigh due to large JSON parsing[X] Bad
Sending DTO with minimal fieldsN/AN/ALow due to smaller JSON parsing[OK] Good
Rendering Pipeline
The DTO pattern reduces the data size sent from server to client, which decreases network transfer time and parsing time in the browser.
Network Transfer
Parsing
Rendering
⚠️ BottleneckNetwork Transfer and Parsing stages are most expensive due to large payloads.
Core Web Vital Affected
LCP
This pattern affects the amount of data sent over the network and the processing time on both client and server during data transfer.
Optimization Tips
1Always send only the data needed by the client using DTOs.
2Avoid sending sensitive or unnecessary fields in API responses.
3Smaller JSON payloads improve load speed and reduce parsing time.
Performance Quiz - 3 Questions
Test your performance knowledge
How does using a DTO pattern improve web app performance?
ABy reducing the size of data sent over the network
BBy increasing the number of database queries
CBy adding more fields to the JSON response
DBy delaying the server response
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and inspect the size of the JSON response payload.
What to look for: Look for smaller payload size and faster load times indicating efficient data transfer.