rediscounter-cache

batch video views to redis instead of directly to the db


The idea is to move video views counters from inserting directly to the db to Redis using INC/HINCRBY, and, every x sec get a batch of them and insert in one go to the db. As I see it, there are 2 options:

  1. set key per id and INC
  2. use hash and HINCRBY

Both approaches have a disadvantage as I see it - there is no POP/ atomic command to get and reset the views counter. Which makes me look at transaction of sort - pipeline or LUA for the GET + DEL times the batch size..

Are those my only options? Is there a better option? What is the best practice?

BTW, maybe this is for a different topic, but I need same logic for date instead of counter (last active..), so it will be best to have one logic for both.


Solution

  • The way we decided to implement it is to push all the updates to a list and pop them every x sec into one transaction. Improvement is 10x then the current approach which each one is a transaction by itself.

    Trying to aggregate the counter and generate update from it later of is too much of a hassle.