Using the Redis NoSql database with .NET Part 13: transactions in the .NET client

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: