title: C# redis vs mongodb performance
body: <p>I'm doing a research with different nosql caches/databases.</p>
  
  <p>The following performance test was done with C# mongo driver 1.8.3.9 and ServiceStack.Redis 3.9.57.0.</p>
  
  <p>MongoDB 2.4</p>
  
  <p>Redis 2.6.16</p>
  
  <pre><code>private List&lt;string&gt; GetListStrings()
  {
      var list = new List&lt;string&gt;(count);
      for (int i = 0; i &lt; count; i++)
      {
          list.Add(Guid.NewGuid().ToString());
      }
      return list;
  }
  </code></pre>
  
  <p>Mongo Test Method</p>
  
  <pre><code>public class TestStrings
  {
      [BsonId]
      public ObjectId Id { get; set; }
      public string[] List { get; set; }
  }
  
  public void SendIdsMongo()
  {
      var idsEntity = new TestStrings
      {
          List = GetListStrings().ToArray()
      };
  
      var client = new MongoClient(mongoConnectionString);
      MongoServer server = client.GetServer();
      MongoDatabase database = server.GetDatabase("newdb");
  
      var col = database.GetCollection("l2");
  
      Utils.TimeLog("Insert", () =&gt;
      {
          col.Insert(idsEntity, WriteConcern.Acknowledged);
      });
  
      Utils.TimeLog("FindOneByIdAs", () =&gt;
      {
          var ret = col.FindOneByIdAs&lt;TestStrings&gt;(idsEntity.Id);
          Console.Write(ret.List.Length);
      });
  }
  </code></pre>
  
  <p>Redis Test Method</p>
  
  <pre><code>[TestMethod]
  public void SendIdsRedis()
  {
      var key = Guid.NewGuid().ToString();
  
      var list = GetListStrings();
  
      using (var redisClient = manager.GetClient())
      {
          Utils.TimeLog("AddRangeToList", () =&gt; redisClient.AddRangeToList(key, list));
  
          Utils.TimeLog("GetRangeFromList", () =&gt;
          {
              var ret = redisClient.GetAllItemsFromList(key);
              Console.WriteLine(ret.Count);
          });
      }
  }
  </code></pre>
  
  <p>When I'm inserting 10000 items list into both i'm getting </p>
  
  <p>Mongo: 0.27 write, 0.031 read</p>
  
  <p>Redis: 0.26 write, 0.013 read</p>
  
  <p>Now I'm trying to do the same concurrently spinning 100 threads</p>
  
  <pre><code>var results = new TimeSpan[tasksCount];
  
  Utils.TimeLog("total: ", () =&gt;
  {
      Parallel.For(0, tasksCount, x =&gt;
      {
          Stopwatch stopwatch = Stopwatch.StartNew();
          SendIdsRedis(); //SendIdsMongo()
          stopwatch.Stop();
          results[x] = stopwatch.Elapsed;
      });
  });
  
  Console.WriteLine("avg: " + results.Aggregate((a, b) =&gt; a + b).TotalSeconds / tasksCount);
  </code></pre>
  
  <p>The results I'm getting are surprising.</p>
  
  <p>Mongo:
  total: : 2.5217096
  avg: 0.210511518</p>
  
  <p>Redis:
  total: : 17.249888
  avg: 2.275050013</p>
  
  <p>Mongo in my test appeared to be much faster than Redis with many concurrent requests. And the more threads I added the bigger gap was. I was expecting different results based on what I read. Why?</p>
  
  <p>Both running on the same VM</p>
  
  <p>CentOS 6.4
  Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz</p>
  
  <p>4Gb Ram</p>