C# redis vs mongodb performance
I'm doing a research with different nosql caches/databases.
The following performance test was done with C# mongo driver 1.8.3.9 and ServiceStack.Redis 3.9.57.0.
MongoDB 2.4
Redis 2.6.16
private List<string> GetListStrings()
{
var list = new List<string>(count);
for (int i = 0; i < count; i++)
{
list.Add(Guid.NewGuid().ToString());
}
return list;
}
Mongo Test Method
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", () =>
{
col.Insert(idsEntity, WriteConcern.Acknowledged);
});
Utils.TimeLog("FindOneByIdAs", () =>
{
var ret = col.FindOneByIdAs<TestStrings>(idsEntity.Id);
Console.Write(ret.List.Length);
});
}
Redis Test Method
[TestMethod]
public void SendIdsRedis()
{
var key = Guid.NewGuid().ToString();
var list = GetListStrings();
using (var redisClient = manager.GetClient())
{
Utils.TimeLog("AddRangeToList", () => redisClient.AddRangeToList(key, list));
Utils.TimeLog("GetRangeFromList", () =>
{
var ret = redisClient.GetAllItemsFromList(key);
Console.WriteLine(ret.Count);
});
}
}
When I'm inserting 10000 items list into both i'm getting
Mongo: 0.27 write, 0.031 read
Redis: 0.26 write, 0.013 read
Now I'm trying to do the same concurrently spinning 100 threads
var results = new TimeSpan[tasksCount];
Utils.TimeLog("total: ", () =>
{
Parallel.For(0, tasksCount, x =>
{
Stopwatch stopwatch = Stopwatch.StartNew();
SendIdsRedis(); //SendIdsMongo()
stopwatch.Stop();
results[x] = stopwatch.Elapsed;
});
});
Console.WriteLine("avg: " + results.Aggregate((a, b) => a + b).TotalSeconds / tasksCount);
The results I'm getting are surprising.
Mongo: total: : 2.5217096 avg: 0.210511518
Redis: total: : 17.249888 avg: 2.275050013
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?
Both running on the same VM
CentOS 6.4 Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz
4Gb Ram
Here is some cases which can affect your test.
In general Mongo is better for that test. Look at mongo concurrency specification.
MongoDB uses a readers-writer 1 lock that allows concurrent reads access to a database but gives exclusive access to a single write operation.
When a read lock exists, many read operations may use this lock. However, when a write lock exists, a single write operation holds the lock exclusively, and no other read or write operations may share the lock.
Locks are “writer greedy,” which means writes have preference over reads. When both a read and write are waiting for a lock, MongoDB grants the lock to the write.
And redis concept is
The fact that Redis operations are atomic is simply a consequence of the single-threaded event loop. The interesting point is atomicity is provided at no extra cost (it does not require synchronization). It can be exploited by the user to implement optimistic locking and other patterns without paying for the synchronization overhead.
ps Another thing in redis is snapshoting settings if you are using RDB as persistence storage type. This strategy means that you save db to disk each N seconds or if Y keys was changed. For example each 10 seconds or each 1000 keys.
链接地址: http://www.djcxy.com/p/86416.html下一篇: C#redis vs mongodb性能