Sammy Roberts
Sammy Roberts

Reputation: 657

How to handle "View count" in redis

Our DB is mostly reads, but we want to add a "View count" and "thumbs up/thumbs down" to our videos.

When we stress tested incrementing views in mysql, our database started deadlocking.

I was thinking about handling this problem by having a redis DB that holds the view count, and only writes to the DB once the key expires. But, I hear the notifications are not consistent, and I don't want to lose the view data.

Is there a better way of going about this? Or is the talk of redis notifications being inconsistent not true.

Thanks,

Sammy

Upvotes: 1

Views: 1541

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50052

Redis' keyspace notifications are consistent, but delivery isn't guaranteed.

If you don't want to lose data, implement your own background process that manually expires the counters - i.e. copies to MySQL and deleted from Redis.

There are several approaches to implementing this lazy eviction pattern. For example, you can use a Redis Hash with two fields: a value field that you can HINCRBY and a timestamp field for expiry logic purposes. Your background process can then SCAN the keyspace to identify outdated keys.

Another way is to use Sorted Sets to manage the counters. In some cases you can use just one Sorted Set, encoding both TTL and count into each member's score (using the float's integer and fractional parts, respectively), but in most cases it is simpler to use two Sorted Sets - one for TTLs and the other fur values.

Upvotes: 2

Related Questions