Spring Boot Actuator is a powerful module that provides production-ready features to help you monitor and manage your Spring Boot application.
📌 What is Actuator?
Spring Boot Actuator exposes several built-in endpoints that give insights into your application:
-
Health
-
Metrics
-
Environment
-
Beans
-
Thread dumps
-
HTTP trace
-
Custom endpoints
✅ Step-by-Step Setup
🧱 1. Add Dependency
Maven:
Gradle:
⚙️ 2. Configuration (application.yml or .properties)
application.yml (Recommended)
If using .properties
🚀 3. Access Endpoints
Once Actuator is enabled and running (on default port 8080), you can hit these endpoints:
| Endpoint | Description | URL Example |
|---|---|---|
/actuator | List all enabled endpoints | http://localhost:8080/actuator |
/actuator/health | Application health status | http://localhost:8080/actuator/health |
/actuator/metrics | Performance metrics (memory, etc.) | http://localhost:8080/actuator/metrics |
/actuator/beans | All Spring beans | http://localhost:8080/actuator/beans |
/actuator/env | All environment variables | http://localhost:8080/actuator/env |
/actuator/info | Project info (from info.*) | http://localhost:8080/actuator/info |
/actuator/loggers | Log levels (change at runtime) | http://localhost:8080/actuator/loggers |
🔐 4. Security with Spring Security (Optional)
By default, Actuator endpoints are secured if Spring Security is on the classpath.
To allow all endpoints without auth (not for production):
Login using admin/admin123 in browser when accessing endpoints.
➕ Custom Info Endpoint
Add this to application.yml:
Then check: http://localhost:8080/actuator/info
➕ Custom Health Indicator
🛠 Expose Actuator on a Different Port (Optional)
Now actuator is accessible at http://localhost:9090/actuator.
📦 Use Cases
| Use Case | Benefit |
|---|---|
| Kubernetes liveness/readiness | Check app status automatically |
| Prometheus/Grafana | Integrate metrics and monitoring |
| Debugging | View beans, thread dumps, env vars |
| DevOps Friendly | Health, metrics, restart, shutdown |
📌 Summary
| Feature | Default Status | Configurable |
|---|---|---|
/actuator/health | ✅ Enabled | Yes |
/actuator/info | ✅ Enabled | Yes |
Others like /metrics, /beans | ❌ Disabled by default | ✅ Enable via config |
✅ 2. Expose Custom Metrics using Micrometer
📌 Custom Metric Example
Use orderMetrics.incrementOrderCount() inside your service when an order is created.
✅ 3. Write a Custom Actuator Endpoint
📦 Step-by-Step Custom Endpoint
a. Add a dependency for custom actuator endpoint support (optional)
No extra dependency needed unless you're using older Spring Boot (<2.0)
b. Custom Endpoint Code
This exposes: http://localhost:8080/actuator/customstatus
You must explicitly expose it:
✅ Bonus: Custom HealthIndicator
It becomes part of /actuator/health.
---------------------
2.Spring boot performance issue
What: Spring Boot performance issues typically arise due to excessive memory usage, slow startup times, inefficient database access, blocking I/O, or suboptimal configurations.
Common Causes & Solutions:
| Problem Area | Description | Solution |
|---|---|---|
| Slow Startup | Caused by loading too many beans, unnecessary classpath scanning | Use @SpringBootApplication(exclude = {...}), reduce @ComponentScan scope |
| Memory Leaks | From poorly managed resources or caches | Use tools like VisualVM or Eclipse MAT to identify leaks |
| Blocking Calls | Using blocking I/O in a reactive context | Switch to reactive WebFlux or use @Async where appropriate |
| Large Fat JARs | Slows down class loading | Trim dependencies, remove unused starters |
| Database Latency | Inefficient queries, N+1 problems | Use pagination, optimize queries, enable query logging |
| Inefficient Logging | Too much logging (INFO/DEBUG) | Reduce log level in application.properties |
-
Enable actuator:
/actuator/metrics,/actuator/heapdump -
Use caching (
@Cacheable) -
Profile with JProfiler or Flight Recorder
-
Minimize autowiring unused beans
✅ Best Way to Handle "Lack of Records" Efficiently (High Performance)
🔹 1. Check Record Count Before Fetching Data
Don't load full records if there are none.
⏱️ count() is faster because it just returns a number and can use an index.
🔹 2. Use Pagination Always
Never use findAll() directly in real applications — it loads the entire table, even if it’s huge.
✅ Pagination avoids full memory load and keeps your service fast.
🔹 3. Use DTO Instead of Full Entity (avoid LazyInitialization)
Heavy JPA entities with relations (@OneToMany, etc.) can cause slow loading. Use DTO to fetch only required fields.
🔹 4. Add Database Indexing
Ensure your database columns (especially those used in filters, joins, and sort) are indexed properly.
🔹 5. Use @Transactional with Read-Only Flag
This avoids locking and ensures better performance.
🔹 6. Use Caching for Static or Rarely Changing Data
For data that doesn’t change often, cache it using @Cacheable.
✅ Final Optimized Controller Example
✅ Summary: Best Practices Table
| Practice | Benefit |
|---|---|
count() check | Quick exit when no data |
Pagination (Pageable) | Avoids full table load |
| DTO projections | Avoids heavy entity fetch |
@Transactional(readOnly) | Faster and safer DB access |
| Database indexing | Boosts query performance |
| Caching | Skips DB for repeat requests |