preface

The special

Question 23 Redis23

2020 Internet Java backend interview essential parsing – JVM21 question

The interview does not understand these 10 Spring questions, go back to wait for notice

2020 Internet Java backend interview essential analysis — SpringCloud20 questions

Learning map

Share RabbitMQ interview questions! Use XMind to draw a map to record the RabbitMQ study notes and some interview analysis.

1. Tens of millions of messages are stuck in MQ for hours without being resolved:

  1. Fix the problem of the consumer first, ensure that they restore consumption speed, and then stop all existing consumers;
  2. Create a new topic, partition is 10 times the original, temporarily create 10 times or 20 times the original number of queues;
  3. Then write a temporary consumer program that distributes data, which is deployed to consume the backlog. After consumption, no time-consuming processing is done, and directly uniform polling is written to the temporarily established queue of 10 times.
  4. Then 10 times as many machines are temporarily requisitioned to deploy consumers, each consuming a temporary queue of data;
  5. This temporarily increases the queue and consumer resources by 10 times, consuming data at 10 times the normal rate.
  6. Once the backlog is quickly consumed, the original deployment architecture needs to be restored to consume messages with the original consumer machine.

Conclusion:

  1. Fixed and stopped the consumer
  2. Create a new topic with 10 times as many partitions and create a temporary queue with 10 or 20 times as many partitions.
  3. Write a temporary consumer program that temporarily commandeers 10 times as many machines to consume data;
  4. After the consumption is completed, restore the original consumer;

2. Some messages are lost when rabbitMQ is set to expire:

Take the batch redirect approach: import the missing batch of data queries into MQ.

3. Is there a limit to the number of messages stored in a queue on RabbitMQ?

You can think of it as unlimited because the limit depends on the memory of the machine, but too many messages can lead to inefficient processing.

4. Distributed deployment

RabbitMQ does not tolerate latency between data centers, but distributed deployment can be implemented in three ways: Federation and the Shovel.

5. How do I ensure that messages are sent to RabbitMQ correctly?

RabbitMQ uses sender confirmation mode to ensure that messages are correctly sent to RabbitMQ.

** Sender confirmation mode: ** If the channel is set to Confirm mode (sender confirmation mode), all messages published on the channel will be sent

Will be assigned a unique ID. Once a message has been posted to the destination queue or written to disk (persistent

The channel sends an acknowledgement to the producer (containing the unique ID of the message). If RabbitMQ has an internal error

The message is lost and an NACK (not acknowledged) message is sent.

The sender confirmation pattern is asynchronous, and producer applications can continue sending messages while waiting for confirmation. When the acknowledgement message arrives at the producer application, the producer application’s callback method is triggered to process the acknowledgement message.

6. How to ensure that message recipients consume messages?

** Recipient message acknowledgement mechanism: ** Consumers must acknowledge each message they receive (message receipt and message acknowledgement are two different operations). RabbitMQ can safely remove a message from the queue only if the consumer confirms it.

The timeout mechanism is not used and RabbitMQ only confirms that the Consumer needs to resend the message if the connection is broken. That is, RabbitMQ gives the Consumer enough time to process the message as long as the connection is not broken.

Special circumstances:

  1. If a consumer receives a message and disconnects or unsubscribes before confirming it, RabbitMQ will assume that the message has not been distributed and redistribute it to the next consumer that subscribes. (There may be a hidden problem of repeated message consumption, which needs to be deleted according to bizId)
  2. If a consumer receives a message without an acknowledgement and the connection is not broken, RabbitMQ considers the consumer to be busy and will not distribute any more messages to the consumer.

7. How to avoid repeated message delivery or consumption?

During message production, MQ internally generates an inner-msG-ID for each message sent by the producer as a basis for de-duplication and idempotent (message delivery failure and retransmission) to prevent duplicate messages from entering the queue. In message consumption, there must be a bizId (globally unique for the same business, such as payment ID, order ID, post ID, etc.) in the message body as the basis for deduplication and idempotent, so as to avoid repeated consumption of the same message.

8. What transport is the message based on?

The creation and destruction of TCP connections is expensive and the number of concurrent connections is limited by system resources, resulting in performance bottlenecks.

RabbitMQ uses channels to transmit data. A channel is a virtual connection established within a real TCP connection, and each

There is no limit to the number of channels on a TCP connection.

  1. RabbitMQ uses a non-blocking I/O (NIO) approach to reuse TCP connections to reduce performance overhead and facilitate management.
  2. Each thread holds a channel, so the channel takes the TCP Connection of Connection. RabbitMQ also ensures that each thread is private, as if it were a separate connection.

9. How are messages distributed?

If at least one consumer subscribes to the queue, the message is sent to the consumer in a round-robin fashion. Each message is distributed to only one subscribing consumer (provided the consumer can process the message and confirm it properly).

10. How to route messages?

** Conceptually, a message route must have three parts: ** switch, route, and binding. The producer publishes the message to the exchange; Bindings determine how messages are routed from the exchange to a particular queue; The message eventually reaches the queue and is received by the consumer.

  1. When a message is published to the exchange, it will have a routing key, which is set when the message is created.
  2. Queues can be bound to switches through queue routing keys.
  3. When the message arrives at the exchange, RabbitMQ matches the routing key of the message with the routing key of the queue (different routing rules apply to different switches).
  4. If a queue can be matched, the message is delivered to the corresponding queue. If no queue is matched, the message goes into a “black hole”.

11. How to ensure that messages are not lost?

The premise of message persistence is that the durable switch/queue attribute is set to true. It indicates that the switch/queue is durable and does not need to be re-created after a server crashes or restarts (the switch/queue is automatically created).

If the message wants to recover from the Rabbit crash, the message must:

  1. Before a message is published, mark it as persistent by setting its Post mode option to 2 (persistent)
  2. Sends the message to the persistent exchange
  3. The message arrives in the persistent queue

RabbitMQ ensures that persistent messages can be recovered from a server restart by writing them to a persistent log file on disk. When a persistent message is posted to the persistent exchange, Rabbit sends a response only after the message is committed to the log file. (If the message is routed to a non-persistent queue, It is automatically removed from the persistent log. Once a consumer consumes a persistent message from the persistent queue, RabbitMQ marks it in the persistence log as waiting for garbage collection. If RabbitMQ restarts persistent messages before they can be consumed, Rabbit will automatically rebuild the exchange and queues (and bindings) and replay the messages from the persistent log file to the appropriate queue or exchange.

12. What are the benefits of using RabbitMQ?

  1. Application decoupling (System split)
  2. Asynchronous processing (After the appointment registration service is successfully processed, SMS, message push, and log are sent asynchronously to greatly reduce the response time)
  3. Message delivery
  4. ** Peak traffic shaving: ** Requests are sent to queues, and short peak backlogs are allowed.
  5. Message buffer

13. What are the disadvantages of message queues?

  1. ** Reduced system availability: ** Message queue problems affect services;
  2. ** Increased system complexity: ** Joining the message queue, there are many issues to consider, such as: consistency, how to ensure that the message is not repeated consumption, how to ensure the reliable transmission of messages, etc.

14. How to select MQ?

  1. Medium – and small-sized companies prefer RabbitMQ: simple management interface and high concurrency.
  2. Large companies can choose RocketMQ: Higher concurrency and customized development for RocketMQ.
  3. Log collection function, kafka is preferred, specially prepared for big data.

15. How can message queues be highly available?

1. The cluster:

  • The cluster can extend the throughput of message traffic, but it does not back up messages, which are backed up through mirrored queues.
  • Queues are stored on a single node and switches are stored on all nodes.

**2. Mirrored queues: ** Can implement RabbitMQ by making a mirrored queue that requires consumption exist on multiple nodes

HA high availability. The effect is that message entities actively synchronize between mirror nodes, rather than the normal pattern,

Data is read temporarily while the consumer consumes it. The disadvantage is that synchronous communication within the cluster consumes a lot of network bandwidth.

16. How to ensure the sequential nature of messages?

  1. An algorithm that places messages in the same message queue (partition in Kafka, queue in rabbitMq) in order of precedence. The queue is then consumed by only one consumer.
  2. You can do this by adding a global ordered identity to the message body.

17. Use RabbitMQ to increase the rest service throughput.

18. What are the types of RabbitMQ switches?

  • ** FANout switch: ** It routes all messages sent to the switch to all queues bound to the switch;
  • ** Direct exchanges: The routing rules for direct exchanges are simple, routing messages to queues where bindingkeys and Routingkeys match exactly;
  • ** Topic switches: ** Matching rules are more flexible than direct.
  • ** Headers exchange: ** Matches the HEADERS attribute of the sent message content (not practical due to poor performance)

Commonly used switches are divided into the following three types:

  • **direct: ** If the routing keys match exactly, the message is delivered to the appropriate queue
  • ** FANout: ** If the switch receives a message, it broadcasts it to all bound queues
  • ** Topic: ** enables messages from different sources to reach the same queue. When using topic switches, you can use wildcards, such as “*” to match any text at a particular location, “. The routing key is divided into several parts, “#” matches all rules, etc. Note that messages sent to the topic exchange cannot be arbitrarily set to a selection key (routing_key) and must consist of a series of identifiers separated by “.”

19. How does RabbitMQ ensure data consistency?

  1. ** Producer acknowledgement mechanism: ** Asynchronous callback notifies the producer after the message is persisted to ensure that the message has been sent;
  2. ** Message persistence: ** sets message persistence;
  3. ** Consumer confirmation mechanism: ** After the consumer successfully consumes the message, it will confirm manually to ensure that the message has been consumed.

20. RabbitMQ consumers automatically expand the number

SimpleMessageListenerContainer quantity can be according to the RabbitMQ build-up is automatically extended consumers.

21, RabbitMQ structure:

  • Broker: Simple message queue server entity.
  • Exchange: message switch, which specifies the rules by which messages are routed to which queue.
  • Queue: Message Queue carrier, each message is put to one or more queues.
  • Binding exchanges and queues according to routing rules.
  • Routing Key: The Key used by Exchange to deliver messages.
  • Vhost: Virtual host. Multiple vhosts can be set up within a broker to separate permissions between different users.
  • Producer: A program that delivers messages.
  • A consumer is a program that receives messages.
  • Channel: Message channel. In each connection of the client, multiple channels can be established. Each channel represents a session task.

22. Relationship between rabbitMQ queues and consumers?

  1. A queue can bind multiple consumers;
  2. Messages are sent to consumers in a loop by default;
  3. Consumers receive messages automatically by default, or can change to manual confirmation.

eggs

Welcome everyone to pay attention to my public account [A wind architecture notes], 2020 interview essential Java backend advanced interview questions summarized some study document notes and interview analysis, a total of 2000 pages, the article will be updated in it, collated data reply [2020] to receive!