Lasso Soft Inc. > Home

  • Articles

Speeding up Email Sending with Asynchronous Tags

This article includes a set of tags that can speed of email sending within Lasso by using asynchronous tags. An -Async option is added to the [Email_Send] and [Email_Batch] ... [/Email_Batch] tags.

Email Performance

There are four stages in the delivery of an email message sent through Lasso.

  1. The message is queued for sending in Lasso using the [Email_Send] tag.
  2. The email sending system sends the message to an SMTP server.
  3. The SMTP server relays the message to the recipients MX server.
  4. The recipient downloads the message and reads it.

In Lasso 8.5 we optimized the email sending system to help ensure that steps two and three happened as fast as possible. The direct sending option eliminates the need for a relay by sending the email message directly to the recipients MX server. This reduces the latency which can be added by a busy local SMTP server. The queue was optimized so it could send more messages simultaneously and so that it would send higher priority messages faster.

This tip aims to address the delay that can be added in the first step, simply queuing up a message for sending. Delays in the [Email_Send] tag can reduce the apparent performance of your site since it takes longer for pages to be delivered to the site visitor. Lasso's email queue is SQLite based and while SQLite is a high performance database it does have some trade offs. In particular the performance of database INSERTs can be impacted by a busy table.

The download provided with this tip adds an -Async option to the [Email_Send] and [Email_Batch] ... [/Email_Batch] tags which allows the INSERT to be performed asynchronously so the site visitor does not need to wait for it to complete in order to view the Web page.

Installation

A file containing a new implementation of the [Email_Send], [Email_Queue], and [Email_Batch] ... [/Email_Batch] tags can be found here:

Email_Sending_with_Asynchronous_Tags.zip  

Unzip the file and install it in the LassoStartup folder in the "LassoStartup" folder within the Lasso Professional 8 application folder or a specific site folder. Lasso must be restarted for the tags to take effect.

Note that the tags function identically to the built-in tags unless the -Async option is specified. See below for details.

[Email_Send]

When you specify the -Async option in an [Email_Send] the SQL INSERT which queues a message for sending is pushed off to an asynchronous thread. The page can complete processing immediately and the asynchronous thread performs the insert a split second later.

!Insert Lassoscript here
  Email_Send(-To='joe@example.com',
      -From='webmaster@example.com',
      -Subject='Password Reminder',
      -Body='Your password is the name of your third grade teacher.',
      -Async);
?>

 

The advantage is in the apparent performance of the site. Since the page does not need to wait for the [Email_Send] tag to complete the site visitor can view the result of the page faster.

The primary disadvantage occurs if many email sends are pending and the Lasso site crashes since these messages will be lost. Normally a message is secure within the queue once the [Email_Send] completes so you can be sure it will be sent. Messages sent using the -Async option will be pending for another second or two.

The [Email_Result] tag will return Null if the -Async option is used. The [Email_Queue] tag also accepts an -Async option which functions identically to the [Email_Send] option.

[Email_Batch] ... [/Email_Batch]

When you specify the -Async option in an [Email_Batch] tag all of the [Email_Send] tags within inherit the option. The batch tags collect all of the messages that are queued within and then send them all at once when the closing tag is reached.

!Insert Lassoscript here
  Email_Batch(-Async);
    Inline(-FindAll, -Database='subscribers', -Table='marketing');
      Records;
        Email_Send(-To=Field('email'),
            -From='webmaster@example.com',
            -Subject='Important Message',
            -Body='Buy some stuff.');
      /Records;
    /Inline;
  /Email_Batch;
?>

 

The advantage is that the [Email_Send] tags are very fast. With the -async option 100 messages can be queued in two seconds, without it the same operation takes up to a minute.

The disadvantage is that the queue operation is simply pushed off. We can now queue up 100 messages in a couple seconds, but it still takes a minute for the asynchronous thread to queue those messages.

Note that the batch tags still temporily suspend the email queue, but they do so while the asynchronous thread is running.

The [Email_Result] tag will return Null for all tags within the batch if the -Async option is used. The -Async option will be ignored on all [Email_Send] tags within the batch.

Custom Asynchronous Operation

More control can be exerted over the email sending process if you write custom asynchronous code to send your email messages. Rather than making a site visitor wait while hundreds of messages are queued up you can trigger an asynchronous operation which queues up the messages and updates a status indicator so the visitor can monitor the progress.

In the following code a global $email_count is set to the current loop count as the email messages are sent in an asynchronous thread.

!Insert Lassoscript here
  {
    Inline(-FindAll, -Database='subscribers', -Table='marketing');
      Records;
        Email_Send(-To=Field('email'),
            -From='webmaster@example.com',
            -Subject='Important Message',
            -Body='Buy some stuff.');
        Global('email_count') = Loop_Count;
      /Records;
    /Inline;
  }->AsAsync;
?>

 

This global can be displayed on a page with a meta-refresh or in an AJAX region which is being updated periodically to let the visitor know how many messages have been sent so far.

Note that since we are already running this code asynchronously we do not want to use the -Async option on the [Email_Send] tag.

Author: Fletcher Sandbeck
Created: 15 Aug 2008
Last Modified: 2 Mar 2011

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft