High Availability Architecture Design Logic Layer Six



 
Responsibilities of the logic layer, functionally :
User related: user login and logout, user information setting query.
Friends related: add friends, delete friends, modify friends.
Message related: send and receive messages from friends, send and receive messages from strangers, etc.
When A sends a message to B, it first determines whether B has blocked A. If the message is blocked, it will be discarded directly. If not, it is also necessary to determine whether the message contains pornographic political viruses and other content, and this part of the verification will be handed over to the anti-spam system for processing. If it passes, then judge whether the number of users B is online, and send it to the online queue online. Offline, send to offline queue.
The overall architecture of the logic layer :
ALL IN ONE (vertical division of business) method ---- a separate business and a component (directory and file). Advantages: independent business, reduced coupling, independent development between businesses, high development efficiency, and relatively simple operation and maintenance. Disadvantages: Physically a module, the compilation cost is high, a business modification, re-launch, restart affects all businesses. Use scenarios, Internet companies use more, 58, Baidu.
Physical Cui Zhi division between services, each service has an independent service module (process).
How to design the stateless business logic layer by dividing users, commodities, search, transaction, recommendation, community, operation activities, and customer service into different independent processes (independent deployment) ?
What is stateless, the system does not store the upper and lower information of the business, and only carries out corresponding business logic processing according to the data carried by each request. Full symmetry between multiple modules (subsystems). Requests are submitted to any server, and the processing structure is exactly the same.
The key factors of the design: the business logic layer does not save the request state, the business logic layer does not save data, and all the business logic layer servers are completely symmetrical. When one or more downtime requests are submitted to any available server in the cluster, the business logic layer passes the load. Balanced high availability.
load balancing: The mechanism of real-time monitoring of high server availability status, the mechanism of automatic swivel chair failure tasks (machines), the high request volume and data, and the ability to distribute traffic and data to multiple servers in the cluster. It is found that the downstream server is unavailable through the heartbeat mechanism and is eliminated. Recovery can be automatically rebuilt once the server is available.
Synchronous call: The caller is blocking mode and does not return without a result.
Asynchronous call: return immediately after the call, and notify the caller through status, notification, and return when the result is complete. Use is non-thread blocking mode.
If an asynchronous call is made: for example: a new user registration request, it is assumed that two steps are required, one is to write the username and password to the database, and the other is to send a registration success email. Asynchronous implementation: a new user registration request is written to the message queue, and the success is directly returned to the requester. The business logic layer reads the request from the message queue and executes the first and second steps asynchronously. The caller blocks step by step, with high efficiency and high performance.
Asynchronous call scenario: I/O model: blocking I/O model, polling non-blocking I/O model, I/O multiplexing model (the number of monitored file handle IDs should be less than 1024)



 

High-performance pure asynchronous network call design
server-side connection pool | server-side sending and receiving queue, client-side connection pool | client-side sending and receiving queue; timeout queue and timeout manager; context manager + state machine.



 
Hierarchical management
hardware: use good machines for the core system, and poor machines for edge bears.
At the deployment level: service deployment is isolated to avoid chain reactions caused by failures. Core systems are deployed on physical machines, core systems are deployed in different computer rooms, edge systems are deployed on virtual machines, and edge systems are deployed on shared machines.
At the monitoring level: more types of monitoring for core services (process, semantics, error logs), more detailed monitoring, email and SMS notifications.
At the level of response: rapid response to core service development, rapid response to core service launch, rapid response to core service operation and maintenance, and rapid response to core service launch problems.
Set a reasonable timeout
The business logic layer interacts with the downstream modules a lot. It is very important to set a reasonable timeout. The downstream service is down, the thread is deadlocked, etc., the request cannot be responded, the request occupies resources, the caller cannot receive a response, and the user experience is poor.
The timeout setting of the request is 2 times the average response delay of the request, the high response delay can be set to 3S, and the low response delay can be set to 100MS. After the downstream request times out, the business layer retries according to the preset scheduling policy, generally 3 times, multiple requests are not beneficial (will cause downstream pressure, avalanche), and request the swivel chair to the same downstream service provider.
The service of the business logic layer is degraded
during the peak period of the design website, and the amount of concurrency is large. Limited service capacity, performance degradation, service downtime, and system avalanches.
Ensure that core services are available (orders, transactions), non-core services are weakly available, or even unavailable, downgrade design solutions (reject some requests, close requests)
Reject some requests: reject calls to low-priority services (comments, messages, private messages) , when the message arrives in the queue, the worker thread will receive the queue. If it is found that the message queue has exceeded 1S, the message can be discarded. In addition, there is another way to directly discard non-core business messages. Reduce the number of concurrent service calls, randomly discard a certain percentage of messages, or discard messages by priority.
Shut down some services: Non-core services are closed directly, and the upstream does not directly call downstream services.
Server idempotent design: The request will be retried if it fails, ensuring that the service retry call has the same result as a call (idempotency). It is tragic not to guarantee idempotent results: transfers, transactions, payments.
Natural idempotency: Offline messages are set to read, and the settings are the same multiple times.
Non-idempotent requires idempotent design: payment ID, payment status, these two must be atomic. The payment status is judged before each payment, the unpaid status continues, and the payment is terminated. Idempotent designs can also be handled in a documented manner.
Design of Timeout Manager: Timeout management for sending downstream packets, avoiding infinite waiting, separate thread, timed scanning, and timeout processing triggering its callback.
Context manager: request context, the request will bring a unique package_key, timeout, etc. to delete the context, so be sure to store a package_key when the upstream call is made, and it can only correspond to the callback.
State machine manager: state machine for asynchronous calls, state machine for flag requests, state machine for serial execution


 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326459078&siteId=291194637