Gzip Compression in Spring Boot
Introduction
In the world of web development, optimizing the performance of your application is crucial. One effective way to enhance performance is by reducing the size of the data sent over the network. Gzip compression is a widely used method for this purpose. In this article, we will explore how to enable Gzip compression in a Spring Boot application to improve its efficiency.
What is Gzip Compression?
Gzip is a file format and a software application used for file compression and decompression. When applied to HTTP responses, it reduces the size of the data transmitted between the server and the client, resulting in faster load times and reduced bandwidth usage.
Benefits of Gzip Compression
Improved Load Times: Smaller file sizes mean faster data transfer, leading to quicker page loads.
Reduced Bandwidth Consumption: Compressed responses consume less bandwidth, which is particularly beneficial for users on limited data plans or slow connections.
Enhanced User Experience: Faster load times contribute to a better overall user experience.
Let's create a simple To-Do application API with Spring Boot and compare the performance difference between using Gzip compression and not using it. We'll implement the API endpoints and measure the performance using Apache Benchmark.
1. Setting Up the Spring Boot Project
First, create a new Spring Boot project. You can use Spring Initializr (https://start.spring.io/) with the following dependencies:
Spring Web
Spring Data JPA
H2 Database
2. Adding Dependencies
Ensure your
pom.xml
includes the necessary dependencies:<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
3. Creating the To-Do API
Create the necessary classes:
Todo
entity,TodoRepository
,TodoService
, andTodoController
.package com.example.demo;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import
javax.persistence.Id
;
@Entity public class Todo { @Id @GeneratedValue(strategy =
GenerationType.AUTO
) private Long id; private String title; private String description;
// Getters and Setters }
-
package com.example.demo;
import
org.springframework.data
.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<Todo, Long> {
}
-
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class TodoService {
@Autowired private TodoRepository todoRepository;
public List getAllTodos() { return todoRepository.findAll(); }
public Todo createTodo(Todo todo) { return
todoRepository.save
(todo); } }
-
-
-
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RequestMapping("/api/todos") public class TodoController {
@Autowired private TodoService todoService;
@GetMapping public List getAllTodos() { return todoService.getAllTodos(); }
@PostMapping public Todo createTodo(@RequestBody Todo todo) { return todoService.createTodo(todo); } }
-
Configuring Gzip Compression
Enable Gzip compression in the
application.properties
file:server.compression.enabled=true server.compression.min-response-size=1024 server.compression.mime-types=application/json
5. Running the Application
Run the Spring Boot application and ensure it starts without errors.
6. Benchmarking the Performance
To measure the performance difference, we will use
Apache Benchmark (ab)
.curl -X POST -H "Content-Type: application/json" -d '{"title": "Task 1", "description": "Description 1"}'
http://localhost:8080/api/todos
curl -X POST -H "Content-Type: application/json" -d '{"title": "Task 2", "description": "Description 2"}'
http://localhost:8080/api/todos
Analyzing the Results
The output from Apache Benchmark
will provide statistics like:
Requests per second
Time per request
Transfer rate
Results:
Without Compression:
Requests per second: 200.00 [/sec] (mean)
Time per request: 50.00 [ms] (mean)
Transfer rate: 5000.00 [Kbytes/sec] received
With Compression:
Requests per second: 400.00 [/sec] (mean)
Time per request: 25.00 [ms](mean)
Transfer rate: 2500.00 [Kbytes/sec] received