In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to solve the problem of high concurrency in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
One: question
First of all, we have to consider why to solve the high concurrency, where the high concurrency bottleneck occurs, friends who have known must know that it is in the database, because in a large number of requests to operate the database, there will be data confusion, oversold, system crash, mysql deadlock and so on.
Second: ideas
1. Static page: store the entire page in redis and read the page value in redis the next time you visit
2. Cdn: mainly accelerate the static resource files of the whole website, such as pictures, css,js, etc. (go to Ali to see tutorials)
3. Mathematical CAPTCHA: when calculating the result of CAPTCHA, users can reduce a large number of requests to enter at the same time, and reduce the pressure on redis, mysql and server.
4: inventory identification: this is a huge optimization, through the logo to determine whether the redis inventory is sufficient, if insufficient, interrupt to read the redis inventory. Example: boolean over = map.get (goodsId); when our map reads the value as true through key, we return an error prompt to the user, if (over) {return Result.error ('insufficient inventory');}. In this way, no matter how many requests enter later, only two lines of code will be run, and the following operations cannot be entered.
5. Generate dynamic url: mainly to prevent malicious users from killing products in advance through a fixed url. (security issues should not be taken lightly. The following actions are in vain if you do not do a good job in security measures.)
6. Redis pre-reduced inventory: go to redis to obtain the current inventory quantity before the product is killed, and then directly subtract the inventory stored in redis during the second kill (rest assured that the Redis and MySQL data are synchronized here, and as long as you enter the MQ queue to complete the order, the MySQL database will-1 quantity), so as to avoid going to MySQL to read inventory data.
7. MQ message queue: it is an intermediate message key that sends messages to consumers through the producer to perform business operations, while the producer does not need to know the execution result, that is, the user clicks the second kill and waits for the processing result, and then polls the query processing result (asynchronous operation), thus avoiding constant requests to operate the database. (the polling query here is also queried directly from redis, because after the second kill is successful, the second kill result will be put into redis, and the polling will be queried through key.)
8. Nginx: a good way to solve the problem of high concurrency is to add a few more tomcat servers. When the user accesses, the request can be submitted to the idle tomcat server.
9. Database cluster, database table hash
① large websites have complex applications, and these applications must use databases, so in the face of a large number of visits, the bottleneck of the database will soon appear, and a database will soon be unable to meet the applications, so we need to use database clusters or database table hashing.
② in terms of database clustering, many databases have their own solutions. Oracle, Sybase, etc., all have good solutions. The commonly used Master/Slave provided by MySQL is also a similar solution. What kind of DB you use, you can refer to the corresponding solution to implement.
The database cluster mentioned above in ③ is limited by the type of DB in terms of architecture, cost and expansibility, so we need to improve the system architecture from the perspective of application. Database table hashing is the most common and effective solution.
④ We install business and application or functional modules in the application to separate the database, different modules correspond to different databases or tables, and then carry out smaller database hashing of a page or function according to a certain strategy, such as user tables, hashing tables according to user ID, so that the performance of the system can be improved at low cost and has good scalability.
10. Load balancing
Load balancing will be a high-end solution for large websites to solve high-load visits and a large number of concurrent requests.
11. Reverse proxy
The server that the client accesses directly is not the server that provides the service directly. It takes resources from other servers and returns the results to the user.
Proxy server and reverse proxy server:
The proxy server visits the resources on our behalf and returns the results. For example, a proxy server that accesses the extranet. The reverse proxy server is when we normally access a server, the server itself calls another server.
Reverse proxy means that the user's request is sent to the load balancer device, and the load balancer device distributes the request to the idle application server for processing, and then returns it to the user through the load balancer device after processing. In this way, the subsequent distribution is not visible to the user.
Implementation of reverse proxy
1) A load balancing device is required to distribute user requests to idle servers.
2) the server returns its own service to the load balancer device
3) load balancer returns the service of the server to the user
Proxy server we take the initiative to use, is for our service, do not need to have their own domain name; reverse proxy is the server's own use, we do not know, has its own domain name.
This is the end of the content of "how to solve the problem of high concurrency in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.