My applications hosted on Amazon Elastic Container Service (Amazon ECS) experience slow response times.
Resolution
Identify reasons for slow response times
Review Amazon ECS service events to determine the reasons for stopped tasks.
Resource resource constraint errors
You might receive one of the following error messages in the application logs:
- CPU issues: Thread starvation, clock leap, or execution timeouts
- Memory issues: OutOfMemoryError or GC overhead limit exceeded
When the application experiences a resource issue, the container might stop. Amazon ECS discards in-progress operations before they complete. The container loses data, and responses to requests fail.
To resolve issues with resource constraints, complete the following steps:
- Open the Amazon ECS console.
- In the navigation pane, choose Task definitions.
- Choose Create a new task definition.
- In the Container details section, define the resources for your workload.
Note: For more information, see What do I need to know about CPU allocation in Amazon ECS?
- Select the Amazon Elastic Compute Cloud (Amazon EC2) container instance types to allocate resources.
- Optimize your task launch times.
For more information about CPU and memory utilization, see Service level CPU and memory utilization.
Resolve I/O and networking errors
You might receive one of the following error messages because of connection errors or timeouts:
- Connection timeouts and retries: SocketTimeoutException: connect timed out
- DNS resolution failures: Could not resolve host: api.example.com
- Network unreachable errors: Network is unreachable (Host : 'database.example.com'. Port: 5432)
- Connection refused errors: java.net.ConnectException: Connection refused (Connection refused)
- SSL/TLS errors: SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
To troubleshoot I/O and networking issues, take the following actions:
- Check your DNS settings.
- Verify your security group rules.
- Confirm that you correctly configured SSL/TLS.
Resolve database errors
For database issues, you might receive one of the following error messages in your application logs:
- Connection pool exhaustion: java.sql.SQLException: Too many connections
- Query timeouts: ERROR: canceling statement due to statement timeout
- Deadlocks: Deadlock found when trying to get lock; try restarting transaction
- Connection issues: Cannot connect to MySQL server on '*' (111)
To resolve database issues, optimize database queries and rebuild your indexes. Review connection pool settings and query execution plans to identify bottlenecks.
Monitor load balancer metrics and health checks
Use Amazon CloudWatch to monitor load balancer and target metrics such as TargetResponseTime, UnHealthyHostCount, and HTTPCode_Target_5XX_Count.
Review application responses
You might receive one of the following 5xx status codes:
- HTTP 500 Internal Server Error
- HTTP 502 Bad Gateway
- HTTP 504 Gateway Timeout
To resolve these issues, see Common Error Types.
Review application logs for error messages
Use CloudWatch to review your Amazon ECS logs for errors such as, Connection timeout, Database query exceeded time limit, or Memory limit exceeded. To identify errors and analyze bottlenecks and response times, use application trace data.
Optimize performance
Use the following methods to improve the performance of your Amazon ECS hosted applications.
Cache your data
Use a caching system to reduce response latency. Examples of caching systems include Redis, Memcached, or Amazon CloudFront.
If you use the EC2 launch type, then configure ECS_IMAGE_PULL_BEHAVIOR: prefer-cached as the Amazon ECS container agent pull behavior. For more information, see Optimize Amazon ECS task launch time.
Use Application Auto Scaling
Use Application Auto Scaling to adjust task counts based on demand and configure scaling policies. For more information, see Tutorial: Create your first Auto Scaling group.
Optimize container images
Use multi-stage builds to reduce image size, and cache your image layers. For more information, see Linux containers on Fargate container image pull behavior for Amazon ECS.
Implement efficient logs and monitors
Use the LogConfiguration API to set log levels. Use Amazon CloudWatch Application Signals to implement distributed tracing and identify latency bottlenecks.
Related information
Amazon ECS service event messages
CloudWatch metrics for your Application Load Balancer
Centralized container logs with Amazon ECS and Amazon CloudWatch Logs
How can I configure Amazon ECS service auto scaling on Fargate?