MYOC - Offload compute intensive tasks on Azure using the Offline Processing pattern
In this post on the MYOC cloud development series, I will share an offline processing design pattern where certain computation tasks are offloaded to another execution task using queues and that can help reducing the overall processing time of online transactions. This is a very useful pattern to use, if you plan to build highly scalable and compute intensive application on the web today. This patterns is also used by many popular websites. Here I will demonstrate how we've used this pattern to help reduce the poll creation time.
In MYOC (Make Your Opinion Count), whenever a new poll is created,a user can send invite notifications to people for participating in the poll. These notifications can be sent through SMS, e-mail or Twitter.
As shown in the figure below, when a user creates a poll and invites his/her friends to participate in poll, the poll gets created and subsequently a notification may need to be send out. These notifcations are first send out as a Notification messages into a queue. The Notification message contains instructions on the mode of alerts which need to be sent out say SMS, e-mail or Twitter. A work role, the Notification processor, processes these messages asynchronoously in an offline fashion. It picks up the notification messages from the queue, process the message and extracts the channels to which the notifications have to be send. Once the notification channels are identified the processor calls the respective notifcations services along with the information required by the service. The poll creation transactions completes without waiting for completion of sending invite notifications to invitees and significantly reducing the overall time of the transaction improving the application throughput.
As of now MYOC supports three types of invite notifications to participants i.e. e-mail, SMS and tweet. Other notification services can also be easily incorporated by setting a filter for them in the notification processor and passing the message to the services.
The overall process of sending the notification comprises of three steps –
1. Putting notification messages in the queue after creation of the poll
a. Once the poll is created, create an entity for notification with the details of participants i.e. e-mail ID and cell number, twitter ID and password of poll creator to put status on, type of notification i.e. e-mail, SMS and tweet.
b. Form the message to be sent to the participants with poll URL. For SMS and tweets, considering the limitation of message size, convert the URLs into tiny URL using TinyURL API.
c. XML serialize the notification entity and encode it to form a string message.
d. Create notification message queue using StorageClient API and set queue properties.
e. Put the message in the notification message queue.
f. If any of the above steps fails then get the error/exception details and write it to Azure table storage.
2. Processing of the notification by the Worker Role
a. Worker role keeps running in the background and checks if notification message queue has been created or not.
b. Create a dead letter queue to store the message which could not be processed by the worker role and wait until any queue is created in the storage account.
c. If notification message queue exists then keep checking if there is any notification message in the queue after specified time interval.
d. If there is any message in the queue, worker role will pick it up for processing.
e. Deserialize the message in the Notification entity and get message details.
f. Check the notification message to process it ; if it contains twitter authorization key, then call twitter notification service from the webrole to put status on specified twitter account.
g. If the notification message contains an SMS message, then call SMS notification service from webrole to send SMS to specified mobile number.
h. If the notification message contains e-mail IDs, then call Live notification service from webrole to send e-mail to specified e-mail IDs.
i. If any of the above steps fails then get the error/exception details and write it to Azure table storage.
3. Sending messages through various notification services to the participants
a. Notification services are RESTful WCF services created in the webrole for various notification mechanisms in MYOC. Whenever a service is called with valid inputs, it will call the concerned method to send invite notification to the participants.
b. Follow the link to know how a tweet will be sent to a specified twitter account
c. SMS service will send SMS to specified mobile numbers
d. Follow the link to know how an e-mail will be sent to specified e-mail IDs
e. If any of the above steps fails then get the error/exception details and write it to Azure table storage.
Offloading the notification transaction to a background process has helped in improving the overall throughput of the application. Architects and developers need to keep an eye on scenarios which exhibit such characteristics in their overall process and simultaneously design to offload such activities to an offline mode