Question

I use batsd ( which use statsd) library with jeremy/statsd-ruby client for my ruby web application (rails). And I have to keep simple visits statistic. Great! I use statsd.increment('users.visits') method from the above gem.

Then I noticed, this operation once create new sorted set(zset) and add one element(it looks like "1338932870<X>1) every time. Why statsd use this approach? Would not be easer and faster use HINCRBY method with simlpe hash(not zadd to zset)?

I know, statsd is good and well-known instrument, but I wonder, is it the counters standart pattern in redis? I'm new to redis and nosql at all, thank you!

Was it helpful?

Solution

I'm not familiar with the package, but if you just use HINCRBY, you will just calculate the last value of the metric and keep it in Redis. I guess a statistical package may need to store the evolution of the metric (in order to plot graph over time or something similar).

Using a zset is a way to store the events ordered by timestamp (i.e. a time serie), and therefore to keep an history of the evolution of this metric. It is slower and consume much more memory than just keeping the last value, but you have the history. See Noah's comment below for the full story.

Using HINCRBY or INCRBY to aggregate counters in real time, and using zset to store time series are two common Redis patterns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top