Home > Backend Development > PHP Tutorial > 15 PHP interview questions about high concurrency (summary)

15 PHP interview questions about high concurrency (summary)

青灯夜游
Release: 2023-04-09 10:02:01
forward
11517 people have browsed it

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template