specifically to the PHP code level, improve the high concurrency of the measures are what

Source: Internet
Author: User
Tags php framework
1, today asked a question: what is the specific code level of PHP to improve the high concurrency measures?

In the face of high concurrency problems I first think of clusters, caches (APT, redis, mem, memory ...) , but specifically to the PHP code level in addition to the thought queue, reduce network requests, think nothing else, so open this discussion paste, hope that the great God left a workable solution.

Reply content:

1, today asked a question: what is the specific code level of PHP to improve the high concurrency measures?

In the face of high concurrency problems I first think of clusters, caches (APT, redis, mem, memory ...) , but specifically to the PHP code level in addition to the thought queue, reduce network requests, think nothing else, so open this discussion paste, hope that the great God left a workable solution.

I think the PHP code level, then directly from PHP the relevant knowledge point of departure

    • The processing mechanism of the project SESSION , Files->db->cache

    • Record of the project Log , files->db

    • Resource reuse, long-time operation asynchronous processing, singleton, global data sharing, queues, etc.

    • Byte-code cache, APC XCACHEOPCACHE

    • Data (view) cache, file cache (files), local cache (YAC), distributed cache (Memcache,redis)

    • Frame selection, micro-framework (lumen), Full stack PHP framework (SF,LARAVEL), extended Framework (YAF,PHALCON), parallel framework (YAR)

    • PHPVersion selection, PHP5.X->HHVM->PHP7

Is it a question at the interview?

It is estimated that the interview company in the actual development of the relevant problems encountered, as an interview problem

Specific to the code aspect, now usually talk about improving concurrency, you can draw on the idea of Nodejs, is asynchronous, non-blocking access to ensure high concurrency, so you can talk about PHP has what asynchronous operation extension, such as I searched for a swoole called the extension.
There is more to the computing task to the front end, reduce server-side computing, improve response speed.
There is the operation of the database, may talk about how to modify the organization of the database (reduce joins and other operations) is also good

It's just not breaking!

10ms a request, than 100ms a request, the pressure will be 10 times times smaller, because your concurrency is limited, if a connection comes in, delay, will occupy the resources are not released, prone to occur at the same time there are a large number of concurrent connections. When the CPU and memory are full, the vicious cycle will die, so set the maximum number of connections.

Then, the vast majority of people have this problem (our company is full of this code), not optimized for large amounts of data I/O:

// 取用户的订单列表$orders = api_get_orders($uid);// 可是, 订单里没有商品的详情, 怎么做呢?foreach($orders as $i=>$order){    // api_get_good_by_id 是个缓存或mysql IO    $orders[$i]['good'] = api_get_good_by_id($order['goodid']);}return $orders;

The loop generates a lot of Io, and if the average user has 20 order data at a time, 10 concurrent requests Generate 200 database/cache IO

Combine them into one IO:

// 取用户的订单列表$orders = api_get_orders($uid);// 提取goodid序列$goodids = array_map(function($o){ return $o['goodid']; }, $orders);// 一次性把商品全部取出来$goods = api_get_goods_by_ids( $goodids );// 封装到$ordersforeach($goods as $i=>$g){    $orders[$i]["good"] = $g;}return $goods;

Cache driver design Layer Runtime cache

Sometimes a lot of reading from the cache of the same key data, if we add a level runtime cache, there is a cache Io, the next time directly from the PHP variable to take, wouldn't it be faster? Similar to how we use static to cache some stable data within a method. Of course, don't forget to erase or update the runtime cache while encapsulating the cache delete, set.

Yes, everything is designed to reduce IO and improve reuse, and what sort of algorithms can be faster, less meaningful, except for SQL statement optimizations.

Object resource reuse such as singleton mode instantiating a database object this object is shared
Reduce IO overhead on data

I also understand the relatively shallow, I know, can less request the database as little as possible request, the storage of data if not urgent to put Redis, and then do the scheduled task to run.

In general, the most feared should be the same data update issues, such as with the new inventory. This adoption queue can be solved.

  • 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.