For the understanding of the message queue.

We in the development process, will encounter a situation like this:

  For example, we need to develop a function to send messages to registered users. In fact, it is not difficult to make, nothing more than two steps,

    1, need to get the mailbox to send mail, and send content to fill.

    2, sent.

This function is simple to implement, it is also written, is not it!

So, now the question is.

I now have a good million concurrent, millions of users are registered at the same time (of course, this assumes). The case of such a high concurrency, we still use a synchronous manner to perform the above two steps 1 and 2.

I am responsible to tell you, there will certainly be the case failed to send, but will be a lot.

Then how to solve this problem?

message queue! ! !

Here we introduce the concept of message queues. Before understanding this concept, let's look at life in a small example:

  We all went to the restaurant for dinner, right, waiter and chef cook single point, it must be much faster than the waiter carte chef cook. So the question again here, if the waiter and chef cook single point of synchronization is single-threaded execution.

  So we, as customers, will be of significant complaints. Because we have to wait on a single point after Sarkozy it is on course to ordering.

  But in real life, not a restaurant like this. Restaurant staff, usually a single point and not directly related to cooking the cook, the waiter just a single point, the single dot on a successful recording medium (herein assumed to be recorded on paper), and then passed to Houchu, then in accordance with the list of Houchu before and after making the order.

  In fact, we have gone through the above every day this case is a typical message queue: Post Owner (Waiter), the message is stored in the message queue (single dot paper records), news executives (Cook) and get messages from the message queue carried out.

Read the example above, I believe the vast majority of people on the message queue should have a certain understanding of.

The following is an abstract message queue of FIG. (Producer: Waiter, consumers: the chef)

  

Here, we are speaking about how to send mail messages using the queue to achieve one million concurrent (to ensure that every message can be sent successfully, unless the customer fill out a wrong e-mail address, here we do not consider this case)

It should be noted that the media has a lot to store messages, we can choose according to the needs, not rigidly adhere to the redis List . Let us choose where mysql as the storage medium of the message.

We create a new table for storing messages. It shows as MQ, comprising three fields id, u_email (user mailbox), post_content (transmit content).

1, when the user application for registration, the user's mailbox and send content (content can be sent, as the case may be, the logic here is to analyze specific issues Kazakhstan) into MQ table, after the success we can immediately credited to the user feedback. For example (go to your mailbox to see if the message has been sent to your mailbox ......)

2, implemented with a timer php, the timing data is extracted from the MQ table, mail transmission. If you delete this record (of course, we usually use the tombstone) from the data table MQ success, if not successful, it will not be deleted.

advantage:

1, running in the background, foreground without waiting, can immediately react to the customer, the higher the degree of friendship.

2, recording a high success rate, failure will automatically retransmit until it succeeds.

The work will encounter many programmers new to the message queue, the list is mistaken redis message queue. In fact, the list just redis a data type, it is possible to realize the message queue functions only.

Make a message queue summary :

  Message (Message) refers to data transfer between applications, a message can be very simple, for example, only contains the text string, it may be more complex, and may contain embedded objects.
  Message Queue (Message Queue) is a communication between an application, the message transmission may return immediately, with a system to ensure reliable mail message information, the message just publishers publish messages to the MQ regardless of who take,

  Message consumers just take messages from the MQ whoever posted, so publishers and users do not know about each other.

Recommend a messaging middleware queue, we have time to get to know: RabbitMQ

We want to help people in need.

Guess you like

Origin www.cnblogs.com/573734817pc/p/10931632.html