Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis performance: Many queries returning small objects vs one query returning large objects

I am fairly new to Redis, so far I really like it. I started to wonder however if it is better - performance wise - to use a single query that returns a large object (storing information in JSON) or if I should use more smaller queries, that return smaller objects?

like image 672
stiller_leser Avatar asked Dec 15 '15 13:12

stiller_leser


1 Answers

Redis is single threaded application. Each query would be executed strictly one by one.

The answer depends on your needs and size of query response. If you try to get large ammount of keys in one query (with MULTI or with LUA script) you may block your server to accept new queries. One query allow you to keep total time as small as possible.

Each query is:

  1. Parse query.
  2. Get data.
  3. Send it with network.

For example:

-------------------------------------------------------------------> time 
           |           |                           |             |
client send query(Q)   |                           |           got it(G)!
redis             execute(E, server blocked)  send response(SR)  

However, if you do a lot of small queries, the total time for information to be longer.

-------------------------------------------------------------------> time 
         |  |  |  |  |          |  |  |  |  |
client   Q  |  |  G  |    ...   Q  |  |  G  |     ...
redis       E  SR   idle           E  SR   idle 

The answer is (if you have high loaded system):

  • If you need to receive data on several tens of keys prefer to get them a few queries.
  • If each your key has amount of data (many kilobytes for example) use many queries.

Also if you want to save the JSON think about the mandatory use of some kind of serialization (for example messagepack or lz) to minimize memory consumption.

like image 79
Nick Bondarenko Avatar answered Oct 11 '22 18:10

Nick Bondarenko