2

I have to add N (independent) items frequently in redis cache using stackexhange.redis C# each with different expiration time so that there is minimum time at client side and min blocking & cost at server side. Redis server will receive hundreds of get requests per second so I don't want to mess with the get time at all.

I have read the documentation here and answer here. I could not find a single method that does this operation. Considering different options:

  1. Using transaction- This will block any operation at server side. So this should not be right solution
  2. Using batching- This will block any operation at client side till all the batch operation is complete. This should not be right solution.
  3. Using pipelining - This will not block any operation at client side and server side. But it can send multiple requests (packets less than N) may consume more network but may increase memory consumption at client side which may induce latency.
  4. Using fire and forget - This also will not block any operation at client side and server side. But it will send multiple requests (more packets than pipelining) which may consume more network bandwidth but no memory consumption at client side.

Which should be the best approach? I assumed competing operations means 2 inserts and one get and insert cannot go together though they may be accessing different keys. Am I correct in this otherwise what does it mean?

Community
  • 1
  • 1

2 Answers2

0

Redis is single-threaded when it comes to either read or write on a database.

What's the best solution in your case? Who knows, it might depend on a lot of variables and each use case should be analyzed separately to implement the right solution.

Redis MULTI can't be avoided unless you want to corrupt your data if something goes wrong in your application layer. Actually, if you want to avoid many requests to Redis, you should use Lua scripts instead.

In the other hand, the point of Redis is trying to make many operations but be sure that those are as small as possible because of the Redis single-threaded nature. Right, it's blazing fast, unless you execute an operation that takes too much time.

In summary, I wouldn't be too concerned about sending many requests as its an in-memory database and works at the lightspeed. Also, consider the wonders of Redis Cluster (i.e. sharding) to being able to optimize your scenario.

Finally, I would take a look at this Redis tutorial: Redis latency problems troubleshooting

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

You should add to your list of options, Lua scripting. See EVAL.

Also, consider the data structure that you will use. For example you can use MSET to send multiple values in Redis with one hop.

georgeliatsos
  • 1,168
  • 3
  • 15
  • 34