3
votes

I'm new to Microsoft Azure and I'm not really getting the idea of Worker Roles in the Cloud Services. The Web Roles are kind of easy to grasp: they are vm's running IIS so that I don't have to manage it myself, so when I deploy to a web role I would be deploying something to run in IIS.

Now, worker roles everyone says "it's like windows services, it runs on background" and I'm not really getting when and why I would need this. Until today I never needed to develop a windows service, so perhaps because of this I'm not getting the point with worker roles.

Also, I've seem people deploying WebAPI apps on worker roles, but WebAPI is something that I would run on IIS, so it seems much more like web role than worker role.

Anyway, what are the real use cases of azure worker roles? When should we use it and why? What are common examples of it's usage?

3

3 Answers

3
votes

As @Thiago said, there are tons of uses.

Think of YouTube for instance. How would you implement it right now... You'd probably set up a front-end when users would watch and upload videos.

Once the video is uploaded you need to perform some task on it. Convert it to a specific format, look for copyright material, take snapshots... or whatever it is.

You definitely don't want to block the user while you do that, and you don't want to have the user with the browser open while you do that . That's when a worker role comes into play... there's no need for UI and it does not depend on anything from the user, you just run the processes you need there and notify the user when the video is ready. Is he still there? show him a badge or something, did he close the browser? send him an email.

This is just an example that helped my understand worker roles... hope it helps you too

1
votes

There are a lot of uses for worker roles.

You can use it to send emails, to dequeue items from a azure service bus queue and process it, you can self host a webapi (you don't need IIS). You can insert items in a table...

1
votes

In a nut-shell,

  • Web role - if you plan to run IIS in Azure
  • Worker Role - if you want to run middle-tier application without IIS
  • VM role - if you want to do in Azure is beyond the scope of Web or Worker roles

Startup tasks are only available in Web or Worker roles; VM role cannot manage startup tasks. It is one of the reason you are using Worke role.

Basically, web role must be able serve HTTP content; however, there is no such limitation in worker role.

what are the real use cases of azure worker roles?

  • You can run Startup task such as a console application or a batch file that can start one or more Windows PowerShell scripts.
  • You can schedule a lot of maintenance process running in the background such as
    • checking orphaned data by running queries against the database
    • indexing database
    • pull messages from queue sent by web roles

Ideally, you want web roles to be as responsive as possible by deferring all expensive transactions/work by inserting work items into a queue, and have them performed in a worker role.

Also, I've seem people deploying WebAPI apps on worker roles, but WebAPI is something that I would run on IIS, so it seems much more like web role than worker role.

WebAPI can be self-hosted using OWIN without IIS. If you just want to host WebAPI itself without documentation, you can save money by combining with tasks in Worker roles.