Combine WorkerRole and WebRole Windows Azure

Sometimes is cost efficient to combine a WebRole and WorkerRole. In this way you use the complete capacity of the VM/Server, without the need of extra instances.

Assume this scenario. You got a WebRole with 2 Websites and a WorkerRole. But now the WorkerRole could do with a smaller VM. Of course you can chose a smaller VM, but what if you already are using the smallest instance (XS)? And besides that the WebRole with 2 Websites also not using the complete capacity of the VM.

clip_image002

In this case it looks interesting to combine the two roles.

clip_image004

This combination can be done quite simple, by moving the WorkerRole logic to the WebRole. This can be done by adding the method Run() to WebRole.cs.

Remark: of course you also have to move the specific settings from the app.config or Role configuration from the WorkerRole to the WebRole web.config or Role configuration.

public override void Run()
{
   
Trace.WriteLine(“WorkerRole entry point called”, “Information”
);
   
while (true
)
    {
       <own logic>

      
Trace.WriteLine(“Working”, “Information”
);
    }
   
base.Run();
}

Here a more detailed example. 

clip_image006

In the Local Development Emulator you will see the actual Traces from the Roles. Meanwhile the website is opened in a browser and fully functional. Of course as a user you do not notice that a process is running in the background. If you do, then you need to splits it again Winking smile.

clip_image008

The other way around is also possible. You can add the WebRole logic to a WorkerRole. But then logic for starting a Website is necessary. The first is the simplest one.

Leave a Reply