MX Records and MX Algorithm Part 2

MX algorithm is the logic behind MX records and email servers.

In part one I mentioned that MX record’s priority is used to avoid mail loops. In this post I will explain how it actually works.

So lets take a look at our example from previous part. MX records for gmail.com domain:

We have 5 mail servers listed. Let’s sort them out by priority:

gmail.com MX preference = 5, mail exchanger = gmail-smtp-in.l.google.com 
gmail.com MX preference = 10, mail exchanger = alt1.gmail-smtp-in.l.google.com 
gmail.com MX preference = 20, mail exchanger = alt2.gmail-smtp-in.l.google.com 
gmail.com MX preference = 30, mail exchanger = alt3.gmail-smtp-in.l.google.com 
gmail.com MX preference = 40, mail exchanger = alt4.gmail-smtp-in.l.google.com

When email server need to send email to gmail.com domain it looks up the MX records for that specific domain and will try to deliver email to gmail-smtp-in.l.google.com because it has the lowest priority (5).

Now imagine next situation gmail-smtp-in.l.google.com is down and is not responding. In that case email is sent to server with next lower priority 10 (alt1.gmail-smtp-in.l.google.com). alt1.gmail-smtp-in.l.google.com in its turn looks up all of the MX records for the gmail.com domain and  receives the same list of 5 MX records. It tries to deliver it again to server with lowest priority gmail-smtp-in.l.google.com(priority 5). Which  is down, as we know then it tries to send it to himself because he is next according to priority. After that a mail routing loop occur and server will try send email to itself indefinitely. Of course mail sending apps are smart enough to not send it forever and just drop it with the mail routing loop error code. 

To prevent this behavior email servers discard some MX records before deciding where to route email. First of all MX records are sorted by priority and checked for canonical domain name of itself. If local host is listed in MX records, mail server discards that MX record as well as records with the same priority value or higher. This helps do not send email messages to himself and to email servers which are further from the final destination.

Let’s get back to our example:

When email is received by alt1.gmail-smtp-in.l.google.comserver it discards all the records with priority 10 and higher(In our case 20, 30, 40). Leaving only gmail-smtp-in.l.google.com, which is down (as wee agreed at the beginning of our example).

Since server gmail-smtp-in.l.google.com is not responding and all other MX records have been discarded mail server alt1.gmail-smtp-in.l.google.com will defer delivery and queues email message. It will try to deliver email again after some time.

Thanks a lot for reading.

If you have any questions please leave them in a comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.