Persisting Microsoft Orleans Grain State With DynamoDB
Transcript
Hello, my name is Stu. If you're new here, this channel is all about breaking down different technologies into easily digestible chunks. In this video, we're going to take a look at adding DynamoDB as a persistence layer to our Hello World grain.
Just to refresh your memory from the previous video, our Hello World grain has a simple SayHelloTo method that has the invocation count on it. To start adding state to the Hello World grain, one of the things we will need to do is create a new class to manage the state. So let's do that now. I'm going to create a new class called HelloState. The property is InvocationCount, which mirrors what we have in our Hello grain.
Next, we're going to change the grain type to a Grain of HelloState, and this now gives us a few new properties and methods such as the State property and allowing us to store the state. So the first thing we're going to need to do is remove this private field for invocation count and make the method asynchronous in nature. Then we can do var account = State, which gives us all the properties on our state such as InvocationCount, and then we can do the plus plus as we have done on the line below. Next, we can await the saving of the state. This ensures that we always have the correct state saved in DynamoDB. Next, we can simply return the string and replace the invocation count with the count from above.
So the next thing that we're going to need to do is add a new NuGet package. So I'll right click on the project and click Manage NuGet Packages. Then for simplicity, I'm going to search for "Orleans Dynamo" and you will see the Microsoft.Orleans.Persistence.DynamoDB. So I'm going to click install and that is now installed.
Next, we need to modify the Program.cs file. This is where we have previously configured the Orleans silo. We're going to add a new line on line 23 and we're going to call builder.AddDynamoDBGrainStorageAsDefault and pass in an options lambda which we're going to go and configure. On the options, we need to set the Service property equal to http://localhost:4566. This tells Orleans that the DynamoDB instance that we're talking to is on the localhost and it is on port 4566.
The next thing we need to do is make sure that we have an instance up and running in DynamoDB. I have already got this DynamoDB instance set up through the LocalStack image. If you're not familiar with LocalStack, it is a Docker image which mimics the running of AWS services, and this is running on port 4566 and we're only running the DynamoDB service.
So the next thing I'm going to do is just ensure that my Docker image is running by running docker compose up. Now that is running, I can click run on my Hello World and you can see that my invocation count is zero. As I refresh, the count is saved. So we're going to go up to 10 and then we're going to close the window. The application will shut itself down because we're in debug mode. If I press F5 to run again, the application will start up and the state is loaded, and we have now called hello 11 times. So it's remembered the previous 10 times that we've gone through and run this demo.
Now we can take a look at what the state is actually storing using the Amazon NoSQL Workbench which I have open here. You can add a new connection, select DynamoDB Local, and name it whatever you want. Make sure you put in the port 4566. When you have that, you can have a look on the table section on the left hand side. Click on the table to open up and then you can see that we have something stored inside of here, and we have the state stored for us. As you can see from the screen, we have a column called binary state.
You can also store the state as a JSON object, which may make it easier for you to use things like DynamoDB Streams later on in the future. So let's go and configure that now. We'll go back to our Orleans application, go to Program.cs, and add a new line to these options and say options.UseJson = true. We will need to restart our docker compose, so let me do that. If you're following along at home and you switch between binary state and JSON state, then you may need to clear your state and repopulate it in order to do the migration.
So now we have this change, I'm going to hit F5, refresh the page a couple of times, and then I'm going to go and look in the workbench and refresh the table. Now you can see I have the JSON string. So hopefully now you understand how to add DynamoDB as a persistence layer in Microsoft Orleans.
If you enjoyed this video, consider subscribing to the YouTube channel for more content like this. In the next video, we're going to take a look at clustering with DynamoDB. See you next time.