[Design] High concurrency and async for back-end programs

Source: Internet
Author: User
Tags message queue

Since the concept of high concurrency is involved, there are a few concepts to be talked about, concurrent number, multi-process, multi-threading, co-scheduling, load balancing.

Operating system on the concurrency is a few programs on the operating system at the same time, the single-core CPU on the micro-CPU is scheduled to execute, non-simultaneous execution, multi-core CPU is the real parallel in the micro.

Internet product concurrency usually refers to the number of concurrent connections, the number of simultaneous users access, which factors can affect the concurrency capability, both the programming model, as well as the server load capacity.

PHP relies on multiple processes to resolve concurrent numbers, which is the most primitive and resource-intensive way.

Take 8G memory server For example, a PHP process consumes 20M of memory, up to hundreds of processes, if the task is more time-consuming and high concurrency, then the new request will soon be unresponsive.

For concurrent access to PHP to provide curl_multi_* series functions, the pure interface of concurrent calls do not need to write their own multi-process, multi-threaded program.

Java is a multithreaded model, where each process can create multiple threads, threads use the context of the process, and each thread requires only a small subset of running resources, so it is much lighter than the process.

Processes and threads are supported by system calls that depend on the operating system, so this part of the scheduling overhead is unavoidable.

The process is a program-level scheduling model, the lightest, like the framework-level implementation of the same as the operating system can be free of program interruption, scheduling, such as the use of C-setjmp series functions can be interrupted.

No matter how advanced the programming model is, a single machine always reaches the concurrency limit, and if you want to break the limit, load balancing must be introduced to solve the problem by scaling.

The programming models mentioned above are inherently language-based, and cannot be solved in the short term, which extends from the application level:

Why is high performance inseparable from asynchrony, such as Swoole? The goal is to increase response time and improve QPS.

In our usual business development process, logic code is generally synchronous blocking mode, on the one hand it is easy to understand, on the other hand, it is convenient to carry out some tests.

These advantages, coupled with the fact that most business scenarios have no high requirements for concurrency, are acceptable.

But for a large web site, as well as for scenarios where responsiveness and concurrency are high, it's time to do some optimizations to make the blocking operations as asynchronous as possible.

To do an asynchronous scenario with Message Queuing:

Acceleration response: For example, after the registration of the reminder message, the registration operation will send a message to the queue, directly return information to the user, write the queue very fast, and then by the asynchronous task of the subscription processing mail sent.

Application decoupling: For example, users post to his fans to push the post, when the real-time requirements are not high, you can write the message of the new posts to the queue, and then by the queue handler operation.

Traffic cutting peak: such as the second kill activity, we do not need to deal with some of the purchase logic in real-time, as long as the user request to write to the message queue, the length of the limit prompts the user has ended, the subsequent program to queue content processing.

Log collection: The log is generally required to write disk operations, large traffic to the I/O pressure, reduce program performance; At this point, the log can be written to a message queue, which is consumed by the handler subscribing to the queue.

Broadcast Chat: Users subscribe to a channel to get the latest published messages.

Example Demo

<?PHP/** * pub.php * * @author farwish*///1. Connect and select a database$redis=New\redis ();$bool=$redis->connect (' 127.0.0.1 ', 6379, 2.5,NULL, 100);if(!$bool) {     die(' Connection failed ');}$bool=$redis->select (0);//The simulation puts a task in the queue and publishes//$phone = 13199999999;//$redis->lpush (' Sms-signin ', $phone);//$redis->publish (' Sms-signin ', $phone);////2. Simulate generating batch data for($i= 0;$i< 10;$i++) {    $phone=Mt_rand(10000000000, 99999999999); Echo $phone.Php_eol; //simulating each task takes 100ms of time, using publish instead of a scheme to process//echo $phone in subscribe.    Php_eol; Usleep (100000);    $redis->publish (' Signin-sms ',$phone);}

If you do not write the message to the queue, but do it yourself each time, the response time is long and the user experience is not good.

The task is handled asynchronously by the subscriber, and the user is not aware and the experience is good.

<?PHP/** * sub.php * * @author farwish*///1. Connect and select a database$redis=New\redis ();$bool=$redis->connect (' 127.0.0.1 ', 6379, 2.5,NULL, 100);if(!$bool) {     die(' Connection failed ');}
* Block Redis Read Timeout$redis->setoption (Redis::opt_read_timeout, 1);//2. Subscribe to time consuming tasks and process//if the tasks of this subscription are more important, there will be requirements for availability, log collection, etc. can be used. $redis->subscribe ([' signin-sms ', ' signin-mail ', ' crawler-task-1 '),function($redis,$chan,$msg) { Switch($chan) { Case' Signin-sms '://time consuming 1s, sending and recording database Sleep(1); Echo"{$msgSend a registration message \ n "; Break; Case' Signin-mail ': Break; Case' Crawler-task-1 '://other time consuming tasks that are performed by passing parameters $msg Break; }});

Other common Message Queuing products are RabbitMQ, ZeroMQ, ActiveMQ, Kafka.

Link:www.cnblogs.com/farwish/p/9513100.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.