It might be because of different reasons. Performance differences between local and EC2 usually come down to deployment environment rather than Spring Boot itself. Some common things to check:
- Network latency: Hitting an EC2 instance over the internet will always be slower than calling
localhost. - Instance size: Small EC2 types (t2.micro, t3.micro) have limited CPU credits and can throttle under load.
- Database location: If Postgres is not in the same VPC/region, every query adds network overhead.
- Cold starts or low memory: Spring Boot apps need enough RAM and CPU to avoid GC pressure and slow startup.
- Reverse proxy or TLS termination overhead: Nginx, ALB, or TLS can add milliseconds compared to local calls.
Compare local vs EC2 using internal VPC requests and monitor CPU, memory, and DB latency to pinpoint the bottleneck.