当前位置:网站首页>Every day we say we need to do performance optimization. What are we optimizing?

Every day we say we need to do performance optimization. What are we optimizing?

2020-11-06 01:15:49 Yin Jihuan

During the interview, I am often asked about :

  • Have you ever done performance optimization ?
  • What aspects have been optimized ?
  • How to optimize ?
  • What is the effect of optimization ?

The serial gun asked , For old drivers who have done optimization , It must be able to resist . For Xiaobai, who has not really done optimization , I'm sure I can't bear this series of questions , In the end, the interview failed .

So what exactly is performance optimization optimizing ? Let's take a look at some common optimization methods .

SQL Optimize

When the response time of your developed interface exceeds 200ms When it comes to optimization , Of course 200ms It's not absolute , It depends on the application scenario . With App give an example , Call in a page 5 Interface ( Digression : You can also do aggregation ), So the total is 1s Time for , For users, the experience is OK , Of course, the faster the response, the better .

The interface takes 200ms, One of the most important is the operation of the database , There will be... In an interface N Secondary database operation . So optimize SQL Speed priority is the highest , A lot of slow SQL It's going to take down the whole system .

About SQL Optimization of is not the focus of this article , Most of them are slow SQL It still has something to do with your usual habits of development . Most of them are writing SQL I don't think much about performance , Just write it out ,join I'll be with you , It doesn't comb the query fields , Do not add index , It's OK to start online , Wait until the concurrency amount , It's cool when it comes to data .

You can refer to this article about the use of database :https://mp.weixin.qq.com/s/mFsK7YSKcG6T7jpPnK92tg

When the amount of data is large, we must do read-write separation and sub database sub table , This is also the only way to optimize . Related articles can also refer to some of my previous writing :http://mp.weixin.qq.com/mp/homepage?__biz=MzIwMDY0Nzk2Mw==&hid=4&sn=1b96093ec951a5f997bdd3225e5f2fdf&scene=18#wechat_redirect

Reduce duplicate calls

Another fatal problem with poor performance is repeated calls , The same logic repeats database queries in different ways , Repeated calls to RPC Service etc. .

Consider the following code :

  
  1. skuDao.querySkus(productId).stream().map(sku -> {
  2. skuDao.getById(sku.getId());
  3. })

It's clear that the data has been found , According to ID I went to check again , The more the number of , The more time you waste . This is just an example , I believe that there are a lot of duplicate queries in real projects , I wrote an article before , Explain how to solve this repeated query problem , Those who are interested can check this article :https://mp.weixin.qq.com/s/1k4OtNYIoOasrXAF1ZhcGg

Query on demand

A lot of business logic is not complex function , But the response was slow . It's often when you write code that you don't think about , Just call any existing method , Resulting in slower overall response , In summary : Most of the performance issues are written in code .

Say a scene , Everyone must have seen . The parameter is a commodity ID, The function is on the shelf , We need to judge the State , Only when you meet the requirements can it be put on the shelves . In this scenario, you just need to get the status of the goods to judge , Sometimes you see the code in the following way :

  
  1. GoodsDetail goods = goodsService.detail(id);
  2. if (goods.getStatus() == GoodsStatusEnum.XXXX) {
  3. }

detail There's a lot of logic in , In addition to basic product information , There's a lot more , That's why it's slow .

Parallel call

For an interface , If you design to multiple interiors RPC Services or multiple external interfaces , When there is no association between interfaces , We can use parallel calls to improve performance .

CompletableFuture It is very suitable for parallel call scenarios , About CompletableFuture This article does not elaborate on the use of , do Java You have to be able to use .

except CompletableFuture outside , For the processing of set class , It can be used parallelStream To implement parallel calls .

In microservices, there is a layer dedicated to aggregation API, The aggregation layer is very suitable for parallel calls , A function or a page presentation involves multiple interfaces , Interface aggregation and data tailoring in the back end through aggregation layer , Respond to the front end together .

Cache on

Caching is also the most commonly used optimization , The effect is most obvious , The cost is not big either . For caching , And don't abuse , Not all scenarios can rely on heap caching to improve performance .

First of all, for business scenarios with low real-time requirements, cache can be used preferentially , And don't worry too much about updating , It's just natural .

Business scenarios with high real-time requirements , Cache must have a complete cache update mechanism , Otherwise, it is easy to cause inconsistency between business data and cache data .

The recommended approach is to subscribe to binlog To update the cache uniformly , Don't update or invalidate the cache in your code , The simple scene is OK , The entrance is just a few , No big problem . Some data is used in multiple scenarios , There are too many entries to update ,

Asynchronous processing

Some logic , Without real-time feedback to users, it can be handled in the background asynchronously .

The most common way of asynchronous processing is to add tasks to the thread pool for processing , Thread pool needs to consider the capacity and monitoring of some indicators , For related articles, please see my article :https://mp.weixin.qq.com/s/JM9idgFPZGkRAdCpw0NaKw

In addition to monitoring some indicators , Another problem to be concerned about is the persistence of tasks . If your data is already stored , Then read it out and execute it through the thread pool . If there is no persistence, it is directly dropped into the thread pool for execution , There is a possibility of loss , Such as service restart and so on .

About persistence , Whether it's a thread pool or EventBus such , Will meet , Therefore, for asynchronous scenarios, I suggest that you use message queues .

Message queuing can store task information , Guaranteed not to lose . The message in the queue is consumed separately for logical processing , If you want to speed up consumption , You can also use the thread pool for multithreading consumption on the consumer side of the queue , Multi thread consumption should also avoid message loss , You can check out my article :https://mp.weixin.qq.com/s/Bbh1GDpmkLhZhw5f0POJ2A

JVM Parameter adjustment

JVM Parameter adjustment , In general, we don't have to adjust . Sometimes some code is not written well , Causing memory overflow , At this time, I will make some adjustments and optimize the code .

Parameter adjustment is mainly to reduce GC The pause problem caused by , If your program is always GC, There's been a pause , Your interface is slow .

As long as there is no frequent Full GC, In optimizing this JVM You can adjust the parameters of , Give priority to SQL Optimize these .

With the machine

Adding machines is the ultimate trick , When concurrency goes up , How do you optimize single machine and single database to resist concurrency is also limited , At this time, it can only be expanded horizontally .

If it's an early start-up , And it's growing fast , Adding machines is the most direct way to optimize , Although the cost goes up , But developing resources is also a cost , Saving can achieve more business requirements . Wait until the mid-term is stable before considering the architecture , Overall optimization and refactoring of performance .

It's like playing a game , Only equipped players can be arrogant , The same is true for back-end applications , High configuration machine , High configuration database configuration , High configuration cache and so on .

About author : Yin Jihuan , Simple technology enthusiasts ,《Spring Cloud Microservices - Full stack technology and case analysis 》, 《Spring Cloud Microservices introduction Actual combat and advanced 》 author , official account Ape world Originator .

I have compiled a complete set of learning materials , Those who are interested can search through wechat 「 Ape world 」, Reply key 「 Learning materials 」 Get what I've sorted out Spring Cloud,Spring Cloud Alibaba,Sharding-JDBC Sub database and sub table , Task scheduling framework XXL-JOB,MongoDB, Reptiles and other related information .

版权声明
本文为[Yin Jihuan]所创,转载请带上原文链接,感谢