当前位置:网站首页>Millions of visits - resolution of high concurrency problems

Millions of visits - resolution of high concurrency problems

2022-06-11 01:17:00 Quiet in the forest, the moon and the immortal

One 、 Single server + database ( original )

The original architecture

Two 、 Add reverse proxy

Reverse proxy

A proxy is a process of receiving and forwarding requests . Under normal circumstances ,「 Forward agency 」 The object of the proxy is the client ,「 Reverse proxy 」 The object of proxy is the server , It accomplishes these functions :

 Health check function , Make sure our server is always running 
 Routing forwarding function , Forward the request to the correct service path 
 Authentication function , Ensure that the user has access to the back-end server 
 Firewall function , Ensure that users can only access the allowed network parts, etc 

3、 ... and 、 Introducing load balancer

Introducing load balancer

Reverse proxy has another function : They can also act as load balancers .

Nginx After the configuration , You can reverse proxy multiple servers .

Deployment of multiple stations Nginx( Reverse proxy ), Prevent downtime , Improve the stability of the system .

Nginx Reverse proxy and load balancing configuration reference :nginx Configure the reverse proxy example - Sixi - Blog Garden

Four 、 Extended database

The use of load balancer enables us to distribute load among multiple servers , But now all servers still use a database to store and retrieve data . We can do it the same way , Expand a few more databases , Reduce storage retrieval pressure , But there is a problem of data consistency .

(1) Master-slave mode or single instance write multi copy read

One of the databases is responsible for data writing and modification , Other servers are responsible for reading , The advantage of this scheme is to ensure consistency , Because data can only be written to a single instance database , Then synchronize the written data to other parts , So this scheme is suitable for the situation of reading more and writing less .

I、 How to synchronize ?

Synchronization mechanism

Asynchronous data synchronization by using message queuing , To achieve the final consistency of data . Of course, various exceptions of the message queue will also cause data inconsistency , So we introduced real-time monitoring services , Calculate the data difference between the two clusters in real time , And do consistency synchronization .

II、 The disadvantages of master-slave mode

Master-slave mode architecture

When the main library DB1 When something goes wrong ,DBA Will DB2 Switch to main library , And inform the project team , The project team uses DB2 Replace the original main library DB1, restart web The server , such web The service will use the new master library DB2, and DB1 Will no longer be accessed , The entire database service is restored , etc. DBA Repair DB1 when , then DB1 As DB2 From the library .
There's a big problem here , No matter what happens to the master database or the slave database , Need to be DBA Cooperate with the project team to complete database service recovery , It's hard to automate , And the restoration work is too slow .

So how to make the database highly available ?

High availability HA(High Availability) It is one of the factors that must be considered in the design of distributed system architecture , It usually means , Reduce the time that the system cannot provide service by design .

Database high availability Architecture

As shown in the figure above ,web The server will no longer be directly connected to the main library DB1, It's about connecting KeepAlive A virtual thing ip, Make this virtual again ip Map to main library DB1 On , Simultaneous addition DB_bak Slave Library , Real time synchronization DB1 Data in . Under normal circumstances web Still DB1 Read and write data in , When DB1 After downtime , The script will automatically DB_bak Set as main library , And will virtual ip Mapping to DB_bak On ,web The service will use healthy DB_bak Read and write access as the main library . It only takes a few seconds , Can complete the main database service recovery .
alike ,web The server will no longer connect directly from the library DB2 and DB3, It's about connecting LVS Load balancing , from LVS Connect from library . The advantage of this is LVS Can automatically sense whether the slave library is available , Slave Library DB2 After downtime ,LVS The read data request will not be sent to DB2. meanwhile DBA When you need to add or remove slave nodes , Just operate independently LVS that will do , There is no longer a need for project groups to update configuration files , Restart the server to cooperate .

(2) Operation of sub warehouse and sub table

Reference resources : High concurrent database design - zjone391 - Blog Garden
Problems and solutions to be considered in dividing databases and tables - Simple books

5、 ... and 、 Microservices

Put all the services on one server , In a JAR package , Reduced complexity , But as the scale increases , Things get complicated and inefficient , More developers join in , Develop in the same project code on the same server , There will be more and more conflicts , Interdependence is too high , At the same time, it is not conducive for new developers to read and understand the code .
At this time, the microservice architecture came into being .

Microservice architecture

Each service can be extended separately , Better adapt to needs
The development teams are independent of each other , Each team is responsible for its own microservice lifecycle ( establish , Deploy , to update ) etc. .
Each microservice has its own resources , Like databases , It further alleviates the problem of database .
The division of microservices is mostly based on business , Achieve low coupling .

6、 ... and 、 Caching and content distribution networks (CDN)

Cache architecture

A large part of network applications are composed of static resources , Such as images 、CSS The style file 、JavaScript Scripts and some pages rendered in advance for specific products , By using caching , For some customer requests , It doesn't have to be dealt with all over again , Use the cache , Improve access speed . An enhanced version of caching is called a content distribution network (Content Delivery Network), Massive cache across the world . This allows users to access web content physically close to them , Instead of moving data from the source to users every time .

7、 ... and 、 Message queue

Message queue

Put the customer's task requests into a queue , Carry out management tasks , advantage :

Decoupled the task from the process . Sometimes you have to deal with a lot of pictures , Sometimes very little . Sometimes a large number of services are available , Sometimes it is rarely available . Simply add tasks to your to-do list instead of working on them directly , This ensures that the system remains responsive and that tasks are not lost .
It can be expanded on demand . Starting a large number of services is time-consuming , So start the service when a large number of users upload pictures , It's too late . We add the task to the queue , We can postpone the provision of additional processing capacity .

8、 ... and 、 summary

The greatest truths are the simplest , No matter what plan is adopted , The essence of optimization is to divide and rule , Make a big deal small , Small things become small , There is nothing a middleware cannot solve , If there is , Then add one more !

原网站

版权声明
本文为[Quiet in the forest, the moon and the immortal]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110012424053.html