bradmontgomery / django-redis-metrics

Metrics for django apps backed by Redis.
http://django-redis-metrics.readthedocs.io
MIT License
93 stars 24 forks source link

Optimize redis writes for production #37

Closed odedfos closed 9 years ago

odedfos commented 9 years ago

Hi, In my system I had a latency issue between my the web-server and the redis redis. This led me to discover some inefficiency in the metrics logic when updating metrics.

checkout def metric(self, slug, num=1, category=None, expire=None)

in https://github.com/bradmontgomery/django-redis-metrics/blob/master/redis_metrics/models.py [331]

For each key a separate incrementation is performed which leads to multiple redis calls. Better to use a pipe here and update all keys in a single api call.

for example:

...
pipe = self.r.pipeline()

pipe.incr(hour_key, num)
pipe.incr(day_key, num)
pipe.incr(week_key, num)
pipe.incr(month_key, num)
pipe.incr(year_key, num)

pipe.execute()

Cheers!

bradmontgomery commented 9 years ago

Hi @odedfos, this seems like a great idea. Do you mind making the change, and sending me a pull request, so I can merge? Be sure to add yourself to AUTHORS.rst as well.

Thanks!