Our project is Springboot + hibernate, each time I deploy the project to the cloud(not only the cloud, but also in the local dev), the first POST/PUT API need long time to execute.
Discover
Our project is Springboot + hibernate, each time I deploy the project to the cloud(not only the cloud, but also in the local dev), the first POST/PUT API need long time to execute.
We make several assumptions:
- DNS lookup
- TCP Handshake and SSL Handshake
- AWS RDS performance
- Code problem
Assumption
Assumption 1 — DNS lookup
In Postman, obviously it’s an ideal time. So, move to the next.
Assumption 2 —TCP/SSL Handshake
Check the speed of handshake. Here is CLI, there is no any exception. So, move to the next.
curl -w "TCP handshake: %{time_connect}, SSL handshake: %{time_appconnect}\n" -so /dev/null https://{your domain}
TCP handshake: 0.057688, SSL handshake: 0.100967
Assumption 3 — AWS RDS
How to troubleshoot this problem? I run the server locally and connect to the AWS RDS as a DB source. as long as the server run locally, there is no any delay between RDS and backend, in other words, the problem is not related to RDS. Definitely code leads to potential bugs.
Assumption 4
This screenshot already told me where is the problem happend there.
However, troubleshooting in the backend is tedious.
Firstly, Warping the execution code:
|
|
For example:
In controller:
|
|
In service :
|
|
Print all the sql statement, we need to change some configuration in yml file:
|
|
Then package the project run in inbuilt tomcat. Run this CLI in terminal under the target folder.
|
|
After the program start, head to Postman, run “create an order api”.
It takes 17s!!!!! insert
method takes 16s, so let’s head to source code.
In the save method, before execute the creaete an order(add a new record to DB), the inner method will check the entity is already in the DB or not, so it will query in the DB. In fact, when I execute [persist] method. It faster than before. So I suppose, something happened in isNew function.
I found a solution finally. Just overwrite the isNew function in each entity class.
When I depoly to the cloud and execute the create function, it only takes 1s in the initial request!
I am not sure it is a temporary fix or not…..