Combining Web and Worker Role by Utilizing Worker Role Concept

I am very much excited to write this post as I believe not many people realized about it and I can tell that it would be very helpful in many scenario. Steve actually mentioned this in MIX 2010 in the session entitled 10 Things You Didn’t Know You Could Do with Windows Azure.

Always Start with an Introduction

Let refresh our mind that, Windows Azure Service Roles (Web Role and Worker Role) are is actually a provisioned VM, runs on Windows Azure.

  • The web role provides out-of-the-box IIS 7 environment which allows us to host our application.
  • While worker role is an “almost” empty VM which enables us to do whatever we like to do, specifically in the while loop within Run method.
public override void Run()
    // This is a sample worker implementation. Replace with your logic.
    Trace.WriteLine("WorkerRole1 entry point called", "Information");

    while (true)
        //put our code and logic here...

        Trace.WriteLine("Working", "Information");

What We Can Do with Worker Role

Worker role is indeed very flexible as we can do many things. Some common pattern that we can utilize worker roles are:

  • Performing background or batch processing
  • Hosting non-http service hosting (example: WCF)
  • Running other web / application server (eg: Ruby, Java, Mongoose)

For those compute-intensive task, it’s definitely fine to use spend dedicated worker role to perform the task. However, in many scenario, we only need to perform simple and non-compute intensive task. These could be:

Cost effective

It would be somehow too wasteful to create 2 instance of worker role just to run those task. (Remember in compute SLA, you are required to have at least 2 instance to meet the 99.95% uptime).

Considering the cost for small size VM,it could simply cost you about:

$ 0.12 X 24 hours X 30.5 days / month X 2 instance = USD 175.68

So,how can we do to save those cost?

Combining Web and Worker Role

I refer to the introduction that we’ve discussed above to refresh your mind on Web and Worker Role. For both Web and Worker Role, we generally have either WebRole.cs and WorkerRole.cs file within the project. The class in these two files inherit from RoleEntryPoint, which we can override some methods.

public abstract class RoleEntryPoint
    protected RoleEntryPoint();

    public virtual bool OnStart();

    public virtual void OnStop();

    public virtual void Run();

These methods are meant to be overridden when either on the role is starting (OnStart), the role is stopping (OnStop), and the role has done OnStart and ready to run (Run). This is pretty interesting since most people don’t realize that the entire role instance lifecycle is available in web roles just as it is in worker roles.

The idea is actually override the Run method in the WebRole, which generally provided in the WorkerRole.cs, but not in the WebRole.cs. However, you could simply override it, and would become something like this:

public class WebRole : RoleEntryPoint
    public override void Run()
        while (true)
            var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
            var context = new TaskDataServiceContext(account.TableEndpoint.ToString(), account.Credentials);

            context.AddTask(new Random().Next(100).ToString());
    public override bool OnStart()
        return base.OnStart();

In the example above, I override the Run method to perform a simple task, just to add a record in the table storage on every 3 seconds. Of course, you can add anything you want, typically just like what you’ve written in the worker role.

What’s actually happening on the Cloud is it will create a separate executable process that keep perform the Run method, while the IIS is still listening to the web traffic.

Is it an ideal solution?

Well, isn’t it an ideal solution? Well, the answer is pretty much depends!

If you are sure that your batch-processing jobs (inside Run) method won’t consume too much VM’s resource (e.g.: CPU / memory) AND you don’t mind to share the web resource with it, then that’s fine!

However, if you want to have separate VM that perform the particular task without affecting the web role, then you are encouraged to go with WorkerRole.

That’s all for this post. I hope it is useful to you!

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Combining Web and Worker Role by Utilizing Worker Role Concept

  1. nahog says:

    An important thing to add:

    You should add an App.config file to your web project if you are using config parameters as the web.config is not used for the code inside the Run() method.

  2. admin says:

    I got your point that settings in web.config is not readable from Run() method. This is due to Full IIS that runs Windows Azure Web Role separately from ASP.NET w3wp process.

    I don”t think App.config in web project is a common approach.

    You should consider putting the the config in Windows Azure Service Configuration.

Leave a Reply

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