当前位置:网站首页>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 :
skuDao.querySkus(productId).stream().map(sku -> {
skuDao.getById(sku.getId());
})
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 :
GoodsDetail goods = goodsService.detail(id);
if (goods.getStatus() == GoodsStatusEnum.XXXX) {
}
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]所创,转载请带上原文链接,感谢
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World