具有快速转储数据能力的计数器的键值存储

Let's imagine that, we have 300K keys with simple counters in our storage Counter key names, for example: counter1_2014-03-25_00:01 counter2_2014-03-25_00:01 counter3_2014-03-25_00:01

Each counter is collecting data for 1 minute, thus one key per one minute: counter1_2014-03-25_00:01, counter1_2014-03-25_00:02, counter1_2014-03-25_00:03 etc.

Every next minute I have to dump all counters for previous minute and remove them from storage. My current implementation is really really simple and using Redis hashes (HINCR, HGETALL, DEL).

//incrementing counter $Redis->hincr('counters_2014-03-25_00:02', 'counter1');

//dumping and removing $result = $Redis->multi() ->hgetall('counters_2014-03-25_00:02') //dumping ->del('counters_2014-03-25_00:02') //removing hash ->exec()

Everything is fine with Redis, but dumping with HGETALL is getting very and very slow with large number of counters because HGETALL have O(N) complexity.

Right now I'm looking for approach that will allow:

  • atomically incremented counters doing as fast as Redis do
  • quickly dump counter values collected for previous minute
  • remove counters collected for previous minute from storage
  • no 100% persistence required, but it will be a great plus

UPDATE: Fast dumping is required because I need to transfer all locally collected data from backend servers to master server.

One solution can be to create simple keys, increment using INCR and each time you create a key set an expiry time with it using EXPIRE.