As you make two separate calls Redis, without the use of transaction block (MULTI / EXEC) your operations StringGetand StringSetare not atomic. Running threads can alternate between Get and Set operations.
Think of one possible execution sequence:
- Thread1 Reads myKey1 = 40
- Thread2 myKey1 = 40
- Thread2 myKey 41
- Thread2 myKey1 = 41
- Thread2 myKey 42
- Thread1 myKey1 41
, StringGet StringSet StringIncrement:
RedisConnection.Default.Database.StringSet("mykey1", 0);
Parallel.For(0, 50000, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (i) =>
{
RedisConnection.Default.Database. StringIncrement("mykey1");
});
Console.WriteLine("Result" + RedisConnection.Default.Database.StringGet("mykey1"));