15 PHP interview questions about high concurrency (summary)

Recommended related articles: "In-depth discussion of solutions and solutions for "high concurrency and large traffic" access"
1. What is rabbitmq
A message queue technology using the AMQP advanced message queue protocol. The biggest feature is that consumption does not require ensuring that the provider exists, achieving a high degree of decoupling between services
2. Why use rabbitmq
in distributed The system has a series of advanced functions such as asynchronous, peak clipping, load balancing, etc.;
has a persistence mechanism, and process messages and information in the queue can also be saved.
Realize decoupling between consumers and producers.
For high-concurrency scenarios, the use of message queues can turn synchronous access into serial access to reach a certain amount of current limit, which is beneficial to database operations.
You can use the message queue to achieve the effect of asynchronous order placement. During the queue, logical orders are placed in the background.
3. Scenarios using rabbitmq
Asynchronous communication between services
Sequential consumption
Timing task
Request peak cutting
4. How to ensure that messages are sent to RabbitMQ correctly? How to ensure that the message receiver consumes the message?
Sender confirmation mode
Set the channel to confirm mode (sender confirmation mode), then all Messages published on the channel are assigned a unique ID.
Once the message is delivered to the destination queue, or the message is written to disk (persistent message), the channel will send a confirmation to the producer (containing the unique message ID).
If an internal error occurs in RabbitMQ and the message is lost, a nack (not acknowledged, unconfirmed) message will be sent.
The sender confirmation mode is asynchronous, and the producer application can continue to send messages while waiting for confirmation. When the confirmation message reaches the producer application, the callback method of the producer application will be triggered to process the confirmation message.
Receiver confirmation mechanism
Receiver message confirmation mechanism
Consumer receives each message All messages must be confirmed (message reception and message confirmation are two different operations). Only when the consumer confirms the message can RabbitMQ safely delete the message from the queue.
The timeout mechanism is not used here. RabbitMQ only confirms whether the message needs to be resent through the interruption of the Consumer connection. In other words, as long as the connection is not interrupted, RabbitMQ gives the Consumer enough time to process the message. Ensure the ultimate consistency of data;
The following are some special cases
If the consumer receives the message and disconnects before confirming it When connecting or unsubscribing, RabbitMQ will consider that the message has not been distributed and re-distribute it to the next subscribed consumer. (There may be hidden dangers of repeated consumption of messages and duplication needs to be removed)
If the consumer receives the message but does not confirm the message and the connection is not disconnected, RabbitMQ considers the consumer busy. No further messages will be distributed to this consumer.
#5. How to avoid repeated delivery or repeated consumption of messages?
When producing messages, MQ internally generates an inner-msg-id for each message sent by the producer, which is used as the basis for deduplication (message delivery fails and is retransmitted) to avoid duplication. The message enters the queue;
When consuming messages, it is required that there must be a bizId in the message body (globally unique for the same business, such as payment ID, order ID, post ID, etc.) as the basis for deduplication to avoid The same message is consumed repeatedly.
6. On what basis is the message transmitted?
Because the creation and destruction of TCP connections is expensive, and the number of concurrency is limited by system resources, it will cause a performance bottleneck. RabbitMQ uses channels to transmit data. A channel is a virtual connection established within a real TCP connection, and there is no limit to the number of channels on each TCP connection.
7. How to distribute messages?
If the queue has at least one consumer subscription, the message will be sent to the consumer in a round-robin manner. Each message will only be distributed to one subscribed consumer (provided the consumer can process the message and confirm it normally). Multiple consumption functions can be realized through routing
#8. How to route messages?
Message provider->Routing->One to multiple queues When a message is published to the exchange, the message will have a routing key (routing key), which is set when the message is created. Queues can be bound to switches through queue routing keys.
After the message reaches the exchanger, RabbitMQ will match the routing key of the message with the routing key of the queue (there are different routing rules for different exchangers);
Commonly used exchangers are mainly Divided into three types
fanout: If the exchange receives a message, it will be broadcast to all bound queues
direct: If the routing keys match exactly, the message is delivered to the corresponding queue
topic: allows messages from different sources to reach the same queue. When using the topic exchanger, you can use wildcards
9. How to ensure that messages are not lost?
Message persistence, of course, the premise is that the queue must be persistent. The way RabbitMQ ensures that persistent messages can be recovered from server restarts is to write them to a persistent log file on disk. , when publishing a persistent message to a durable exchange, Rabbit will not send a response until the message is submitted to the log file.
Once a consumer consumes a persistent message from the persistent queue, RabbitMQ will mark the message in the persistence log as waiting for garbage collection. If RabbitMQ is restarted before a persistent message is consumed, Rabbit will automatically rebuild the exchange and queues (and bindings) and republish the message in the persistence log file to the appropriate queue.
10. What are the benefits of using RabbitMQ?
1. High degree of decoupling between services
2. High asynchronous communication performance
3. Traffic peak shaving
11, rabbitmq cluster
Mirror cluster mode
The queue you create, regardless of metadata or messages in the queue, will It exists on multiple instances, and every time you write a message to the queue, the message will be automatically sent to the queues of multiple instances for message synchronization.
The advantage is that if any of your machines goes down, it doesn't matter, other machines can be used. The disadvantages are, first, the performance overhead is too high. The messages are synchronized on all machines, resulting in heavy network bandwidth pressure and consumption! Second, if you play like this, there is no scalability. If a queue is heavily loaded and you add a machine, the new machine will also contain all the data of the queue, and there is no way to linearly expand your queue
12.mq Disadvantages
Reduced system availability
The more external dependencies introduced into the system, The easier it is to hang up, originally you are just the interface for system A to call the three systems BCD and system ABCD. There is no problem. If you add MQ, what will happen if MQ hangs up? If MQ hangs up and the entire system collapses, wouldn't you be doomed?
Increased system complexity
If you add MQ abruptly, how can you ensure that messages are not consumed repeatedly? How to deal with message loss? How to ensure the order of message delivery? Big head, big head, a lot of problems, endless pain
13. Consistency problem
A system returns directly after processing, success, people They all thought that your request was successful; but the problem is, if among the three systems of BCD and the two systems of BD successfully write the library, but the C system fails to write the library, what should be done? Your data is inconsistent.
So the message queue is actually a very complex architecture. There are many benefits to introducing it, but you also have to make various additional technical solutions and architectures to avoid the disadvantages it brings. It is best to do it later , you will find, oh my God, the system complexity has increased by an order of magnitude, maybe 10 times more complicated. But at critical moments, you still have to use it
14. Distributed transactions
Segmented submission. There will be an arbiter and then send messages to all nodes. Success will occur only after all nodes are acked. Otherwise, you have to wait for resending.
15. How to design for a sudden large traffic situation like live broadcast.
NGINX plus machine
cdn cache static page
redis Queue, let users come in slowly.
Add cache. Cache user data, such as user information.
Database uses master-slave
Elastic expansion
Current limiting circuit breaker
Related tutorial recommendations: "PHP Tutorial"
The above is the detailed content of 15 PHP interview questions about high concurrency (summary). For more information, please follow other related articles on the PHP Chinese website!
PHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AMPHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
PHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AMPHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.
Choosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AMPHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
PHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AMPHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.
PHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AMPHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AMPHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AMIn PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.
PHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AMPHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

WebStorm Mac version
Useful JavaScript development tools

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

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.






