Boost Spring Boot 3 Performance: Caching, Actuator, Async & DB Tuning
This guide explains how to improve Spring Boot 3 applications by using Actuator for monitoring, enabling caching, optimizing database connections with HikariCP, and applying asynchronous request handling and tasks to achieve faster response times and better resource utilization.
1. Introduction
As applications grow, performance bottlenecks appear. Enhancing Spring Boot applications improves response speed, resource utilization, and resilience. This article presents key points and strategies for speeding up Spring Boot, focusing on Actuator, caching, and efficient database connections.
2. Optimization Strategies
2.1 Actuator
Use Spring Boot Actuator to monitor and control the application. Add the starter dependency and enable endpoints in the configuration file.
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</code>Configure endpoints in
application.yml(or .properties):
<code>management:
server:
port: 8002
endpoints:
web:
base-path: /ac
exposure:
include: '*'
endpoint:
health:
show-details: always
show-components: always
roles:
- ACTUATOR
</code>2.2 Caching
Enable caching to reduce load and latency.
<code>@SpringBootApplication
@EnableCaching
public class PackApplication {
public static void main(String[] args) {
SpringApplication.run(PackApplication.class, args);
}
}
</code>Add cache starter and provider (e.g., EhCache):
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
</code>@Cacheable : cache method return value.
@CachePut : update cache with method result.
@CacheEvict : remove entries from cache.
Example usage:
<code>@Resource
private UserRepository userRepository;
@Cacheable("users")
public Users queryUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
</code>2.3 Database Optimization
Database interaction is often a bottleneck. Use HikariCP for connection pooling and follow best practices such as proper indexing, batch operations, and lazy loading.
<code>spring:
datasource:
hikari:
minimumIdle: 100
maximumPoolSize: 100
autoCommit: true
idleTimeout: 30000
poolName: MasterDatabookHikariCP
maxLifetime: 1800000
connectionTimeout: 30000
connectionTestQuery: SELECT 1
</code>JPA/Hibernate tips: use
FetchType.LAZY, enable second‑level cache, write optimized JPQL/native queries.
<code>@Entity
public class Customer {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "order")
private Set<Order> orders;
}
</code>2.4 Asynchronous Requests
Leverage Servlet 3.0 async support, DeferredResult, Callable, or reactive WebClient to increase throughput.
<code>@PostMapping
public Callable<String> processUpload(final MultipartFile file) {
return () -> "success";
}
</code> <code>@Resource
private WebClient webClient;
public Flux<Map> chat(Mono<Map<String, Object>> body) {
return webClient.post()
.uri("/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?...")
.contentType(MediaType.APPLICATION_JSON)
.body(body, Map.class)
.retrieve()
.bodyToFlux(Map.class);
}
</code> <code>@GetMapping(path="/events", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter handle() {
SseEmitter emitter = new SseEmitter();
return emitter;
}
</code>2.5 Asynchronous Tasks
Use
@Asyncwith a dedicated thread pool for background work.
<code>@Configuration
@EnableAsync
public class AsyncConfig {}
@Async
public void dataTransform() {
// time‑consuming task
}
</code>Conclusion
Optimizing Spring Boot performance requires analysis, caching, efficient database access, and proper async handling. Applying these practices yields high‑performance, scalable applications.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.