Using the Redis NoSql database with .NET Part 13: transactions in the .NET client
May 1, 2017 Leave a comment
Introduction
In the previous post we looked at a simple monitoring tool we can use to check which commands are executed on the Redis server. Running MONITOR in a production system might not make sense since there’s no filtering and the commands will just fly by in the command window in quick succession. Also, the monitor slows down the server performance. The command monitor is most useful in an alpha server with not much traffic or for debugging a Redis-based application locally where the target database sits on the localhost. We then took this monitoring tool and checked what commands the .NET client generates while working with the various client interfaces. The most interesting case was to see how it handles objects with various keys and sets to keep track of the object IDs.
In this post we’ll look at how to execute transactions in the .NET client.
Redis transactions in .NET
Executing transactions is very simple in the ServiceStack.Redis library. The two most important methods are QueueCommand and Commit. Queue command has 39 overloads in the current version of the .NET client. All of them accept either an Action or a Func which represent a command to be executed on the Redis server. You’ll probably use the simplest overload most frequently, i.e. the one which only takes a single Action. All Actions and Funcs accept a Redis client as an input parameter. The Commit() function executes the transaction and Rollback just dismisses it without any code execution.
Here comes an example with some commands that we tested earlier. You can open a MONITOR session in a command prompt to see which commands are generated against the Redis server:
private static void TryTransactions() { IRedisClientsManager pooledClientManager = new PooledRedisClientManager(0, "127.0.0.1:6379"); using (IRedisClient pooledClient = pooledClientManager.GetClient()) { string customerOneNameKey = "customer:1:name"; string setId = "languages"; string listId = "to-do-list"; var toDoList = pooledClient.Lists[listId]; IRedisTransaction transaction = pooledClient.CreateTransaction(); transaction.QueueCommand(c => c.SetValue(customerOneNameKey, "Great Customer")); transaction.QueueCommand(c => c.SetValues(new Dictionary<string, string>() { { "someKey", "someValue" }, { "someOtherKey", "someOtherValue" } })); transaction.QueueCommand(c => c.Sets[setId].Add("Greek")); transaction.QueueCommand(c => c.Sets[setId].Add("Albanian")); transaction.QueueCommand(c => c.Sets[setId].Add("Ukranian")); transaction.QueueCommand(c => toDoList.Clear()); transaction.QueueCommand(c => toDoList.Add("sleep")); transaction.QueueCommand(c => toDoList.Add("walk dog")); transaction.QueueCommand(c => toDoList.Add("eat")); transaction.QueueCommand(c => toDoList.Add("do absolutely nothing")); bool commit = transaction.Commit(); } }
Here are the generated commands
“MULTI”
“SET” “customer:1:name” “Great Customer”
“MSET” “someKey” “someValue” “someOtherKey” “someOtherValue”
“SADD” “languages” “Greek”
“SADD” “languages” “Albanian”
“SADD” “languages” “Ukranian”
“LTRIM” “to-do-list” “-1” “0”
“RPUSH” “to-do-list” “sleep”
“RPUSH” “to-do-list” “walk dog”
“RPUSH” “to-do-list” “eat”
“RPUSH” “to-do-list” “do absolutely nothing”
“EXEC”
We’ve seen all that before, there shouldn’t be any surprises.
We’ll continue with messaging in the .NET client in the next post.
You can view all posts related to data storage on this blog here.