


Specifically at the PHP code level, what are the measures to improve high concurrency?
1. I was asked a question today: What are the measures to improve high concurrency at the PHP code level?
When faced with high concurrency issues, the first thing I thought of was clusters and caches (apt, redis, mem, memory...), but when it came to the PHP code level, I couldn't think of anything else besides queues and reducing network requests, so I opened this Discussion post, I hope the master will leave a feasible solution.
Reply content:
1. I was asked a question today: What are the measures to improve high concurrency at the PHP code level?
When faced with high concurrency issues, the first thing I thought of was clusters and caches (apt, redis, mem, memory...), but when it came to the PHP code level, I couldn't think of anything else besides queues and reducing network requests, so I opened this Discussion post, I hope the master will leave a feasible solution.
I think at the code level of PHP
, we should start directly from the relevant knowledge points of PHP
Project’s
SESSION
processing mechanism, files->DB->CacheProject’s
Log
records the situation, files->DBResource reuse, asynchronous processing of long-term operations, singletons, global data sharing, queues, etc.
Bytecode cache
APC
,XCACHE
,OPCACHE
Data (view) cache, file cache (files)->Local cache (Yac)->Distributed cache (MEMCACHE, REDIS)
Framework selection, micro-framework (Lumen)->Full-stack PHP framework (SF, LARAVEL)->Extended framework (Yaf, Phalcon)->Parallel framework (Yar)
PHP
Version selection, PHP5.x->hhvm->php7
Is this a question during the interview?
It is estimated that I encountered related problems during the actual development of the interview company, so I used them as interview questions
Specifically in terms of code, when it comes to improving concurrency, we can learn from nodejs's ideas, which is asynchronous, non-blocking access to ensure high concurrency, so you can talk about what extensions of asynchronous operations PHP has, such as me After searching, there is an extension called swoole.
Also, more computing tasks are handed over to the front end, reducing server-side calculations and improving response speed.
The other thing is to operate the database. Maybe it would be good to talk about how to modify the organizational form of the database (reduce operations like join)
Only speed will never break!
A request once every 10ms will be 10 times less stressful than a request once every 100ms, because your concurrency is limited. If a connection comes in and is not completed for a long time, resources will be occupied and not released, and it is easy to occur at the same time. A large number of concurrent connections. When the CPU and memory are full, there will be a vicious cycle of stuck, so you need to set an upper limit on the number of connections.
Then, most people have this problem (our company has such code everywhere), unoptimized large data I/O:
<code>// 取用户的订单列表 $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;</code>
A large amount of IO is generated in the loop. If on average a user receives 20 order data at a time, 10 concurrent requests will generate 200 database/cache IO
Combine them into one IO:
<code>// 取用户的订单列表 $orders = api_get_orders($uid); // 提取goodid序列 $goodids = array_map(function($o){ return $o['goodid']; }, $orders); // 一次性把商品全部取出来 $goods = api_get_goods_by_ids( $goodids ); // 封装到$orders foreach($goods as $i=>$g){ $orders[$i]["good"] = $g; } return $goods;</code>
Cache driver design a layer of runtime cache
Sometimes a large amount of data for the same key is read from the cache. If we add a level of runtime cache and have a cache IO, wouldn't it be faster to get it directly from the PHP variable next time? Similar to what we do in the method Use static to cache some stable data. Of course, don’t forget to encapsulate the cache delete and set, clear or update the runtime cache.
Yes, everything is to reduce IO and improve reuse. It doesn’t make much sense to worry about which algorithm can make sorting faster, except for sql statement optimization
Object resource reuse. For example, singleton mode instantiates a database object. This object is shared
Reduce the IO overhead of data
I also know a little bit about it. I know that if you can make fewer requests to the database, make as few requests as possible. If the stored data is not urgent, put it in redis, and then run scheduled tasks.
Generally, the most feared thing is the update of the same data, such as new inventory. This can be solved by using queues.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
