~/codewithstu

Stateless Workers in Microsoft Orleans

Transcript

Hi, my name is Stu and in this video we're going to be taking a look at stateless workers in Microsoft Orleans. If you're new here, this channel is all about me breaking down different technologies into easily digestible chunks. This video builds upon the other videos in this Microsoft Orleans series, so if you haven't watched them yet I recommend you go and watch them after this.

So what are stateless workers? A stateless worker grain is a special type of grain that can automatically scale up and down based on the load in the system. Orleans manages this by creating new grains on the local node when either a grain doesn't exist or when all the existing ones are busy. By keeping the traffic on the local node, Orleans ensures that no extra network hops are introduced into the system. This also makes them a great choice in scenarios where the computation effort needs to remain close to the currently executing task. For example, caching or pre-computation tasks could be considered good use cases for stateless workers. You can constrain the number of a given stateless worker type per silo by adding an attribute on top of the class should you need to.

So let's take a look at how we can build a stateless worker grain today. First I'm going to create a new class called MyStatelessWorker. In this class we configure it much the same way as a normal grain, so let me go ahead and do that now. For this demo we're just going to wait an arbitrary amount of time before returning a random number from a random number generator.

Next we need to add the StatelessWorker attribute on top of the class. And if you wanted to limit how many instances of this stateless worker class that you wanted to run on a given silo, you can add this here by adding the maximum number of local workers. Note this is a per-silo limit, so you could have five of these on one silo and then three on a different silo and zero on another. It all depends on how busy that silo is and whether it needs to scale up or down.

So now that's done, we need to register this in the startup as we did before. And now we're going to go over to our HelloController so that we can invoke the stateless worker. For the purposes of this demo, we're just going to append the random number onto the string that we get back from our HelloWorld grain. We get the grain in much the same way and operate it in pretty much the exact same way as we do for the HelloWorld grain. One thing to call out here is that stateless workers typically don't have an identity, so here I've referenced it by the ID 0.

This is because I used the IGrainWithIntegerKey interface earlier when setting up the grain. With that done, we should now be able to hit F5 on our program and see the results. If I duplicate these tabs and then refresh them independently, you'll be able to see that they take different amounts of time to refresh and they have different values returned. I'll do this once more so you can see it again.

And that pretty much wraps it up for stateless workers.

If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.

// share_this