ankit shukla
ankit shukla

Reputation: 357

Using redis to maintain a queue of counts of page views and update the DB with collective values instead of updating on every page view

I want to use redis to store the count of profile views by different users. Instead of updating the table on every page view(increasing the count with 1) I am thinking of storing and increasing the count(of views) for that profile in a list or something on redis.Then at regular intervals pop the collective count from the list and update the table. So the number of queries to the DB could be reduced. Would lists be a better to use or any other data structure.Could there be any other better way to do so?

Upvotes: 0

Views: 430

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50052

A List is less suitable in this case, if only because its members are immutable and you're looking to update counts.

Instead, consider using a Hash in which each field is represents a profile and the value is the counter for that page.

Each page view triggers an HINCRBY to the relevant field's value. Periodically you can read that Hash's contents, delete it and add the deltas to your database.

Upvotes: 1

Related Questions