3

I'm using Redis Servicestack in C#. Currently, the way I'm storing data is the following:

var listTypedRedis = db.As<MyObject>();

foreach (var obj in myObjects)
{
       listTypedRedis.AddItemToList(listTypedRedis.Lists["urn:list"], obj);
}

I successfully store about 6000 records. I have problems retrieving the records as it appears to be slower than SQL.

Took Redis 138ms, Took SQL 105ms

The way I'm retrieving data is the following:

return (db.As<MyObject>().Lists["urn:list"].GetAll());

Is there any problems with my code above? Is it because of the deserialization that is causing it to be so slower? Thanks!

slee
  • 344
  • 3
  • 5
  • 15

1 Answers1

1

Whenever dealing with a remote data store consider using batch methods where possible. e.g. in ServiceStack.Redis you can use the AddRangeToList to add multiple items to a list.

Each operation in Redis makes a network call which you want to optimize as much as possible. There are many batch operations in the RedisClient and it also supports Pipelining and Transactions which are both pipelined (redis docs) and allows you to batch multiple operations in much fewer socket writes.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • thanks a lot for your answer. AddRangeToList only accepts List types. Is there a batch method that takes in List ? – slee Oct 09 '12 at 20:41
  • 1
    Ultimately everything in redis is stored as bytes (RedisNativeClient), strings are stored in Utf8 (RedisClient) and POCO types are just serialized to JSON before stored as a TPoco -> JSON/string -> bytes (RedisTypedClient). If you want a typed API you need to use a Typed client, e.g. See `RedisClientList.AddRange` https://github.com/ServiceStack/ServiceStack.Redis/blob/master/src/ServiceStack.Redis/Generic/RedisClientList.Generic.cs#L176 – mythz Oct 09 '12 at 21:12
  • got it. thanks again. currently, im doing everything locally, so i guess i wouldn't see much difference whether if i do it in batch or not, right? also, when i look at this [link](https://github.com/ServiceStack/ServiceStack.Redis#speed), it looks like it takes 132.0076ms to get 3203 records, SQL takes less time to get that many records/objects. any idea why? – slee Oct 09 '12 at 22:07
  • 2
    No, using a batch-ful reads/writes matters everytime, each operation translates to socket writes - when its localhost it uses the loopback address but still goes through the TCP stack. Try taking your list of entities and use `StoreAll()` and see how fast it is? – mythz Oct 09 '12 at 22:29