Let’s start with a question: Is only one interface slow, or is the interface of only one service slow, or is the interface of multiple services slow in the system?
If the system has multiple service interfaces are slow, it may be the result of insufficient system Shared resources, such as database connection number too much, slow database has a large number of queries, some mutual dependence of the downstream service performance issues, etc., can view the dosage in service in the system, whether it call volumes in database and developed into the bottleneck, Whether it leads to performance problems of the downstream services that are called together, whether there are a large number of slow queries caused by this service in the database, etc., to do targeted optimization
- If the concurrency of the database reaches the bottleneck, you can consider using read and write separation, library and table, read cache and other ways to solve the problem
- If a performance problem occurs on the downstream interface, the downstream service needs to be notified to optimize and the degrade switch needs to be added
- If there are a lot of slow queries in the database, you need to change the SQL or add indexes
If the interface of only one service is slow, it is necessary to analyze the service, check whether its CPU usage and GC frequency are abnormal, and make targeted optimization (troubleshooting and solving ideas of soaring CPU usage, write a separate article to talk about). There could also be a performance problem with the downstream service on which the service depends independently
If only one interface is slow, analyze this interface. There may be a problem with the performance of the downstream service that this interface depends on alone. It could also be that the code itself is written incorrectly and needs to be optimized
- For example, fetching remote data in a loop can be changed to just one call, fetching data in batches
- For example, if the same remote interface is invoked multiple times in a link to obtain the same data, the data can be cached after the first invocation and obtained directly from the cache
- For example, in order to obtain a complex model, it is necessary to call multiple interfaces to obtain information, but the current interface only needs part of the relatively simple data, so it is necessary to rewrite the acquisition method of a simple Model according to the actual situation
- For example, if multiple time-consuming operations are executed in serial, but they have no dependencies, you can change serial to parallel, using countDownLatch
- In addition, if it is impossible to reduce the time of request processing, you can also consider optimizing the product logic, such as changing the original synchronous request to asynchronous, and polling the result of request processing at the front end