r/programming • u/shrink_and_an_arch • May 25 '17
View Counting at Reddit (x-post /r/redditdata)
https://redditblog.com/2017/05/24/view-counting-at-reddit/
1.6k
Upvotes
r/programming • u/shrink_and_an_arch • May 25 '17
126
u/shrink_and_an_arch May 25 '17 edited May 25 '17
I'll be hanging around in this thread answering questions.
Since I somehow failed to include this in the post, we are hiring.
Edit: Thanks /u/powerlanguage for fixing ^
39
u/Retsam19 May 25 '17
Is HLL conceptually similar to a bloom filter? That was my first thought in how to prevent duplicate view counts, without needing to store an entire list of ids.
9
u/[deleted] May 25 '17
Why are you only counting registered users? It seems like if the goal is measure popularity it should include non registered users, too.
8
u/Cidan May 25 '17
This is super interesting. We too, wrote a counter service called Abacus, but we took a slightly difference approach.
The service is hit directly via http to increment or decrement a counter. When you increment, we queue the increment into RabbitMQ with a transaction before we return. Backend workers then slurp up the queue and apply the counters.
The unique thing is we can guarantee that all counts will be counted eventually (sub-second), but we can also ensure that any count is only processed once, even if you hit the http endpoint multiple times. We do this by keeping an atomic transaction log in Google's Spanner, ensuring that counters are always 100% right.
I imagine you could do the same with CockroachDB, and I'm curious as to how Reddit will solve duplicate counters and lost batches/writes!
8
u/excitedastronomer May 25 '17
r/counting is going to love this.
6
u/ArsenalOnward May 25 '17
Great read. Thanks for this!
Are you guys using Redis in cluster mode or standalone? Was curious if cluster mode (particularly at scale) is still as easy-to-use/crazy performant as in standalone.
6
u/HariSeldonPlan May 25 '17
Thanks for the write-up u/shrink_and_an_arch and team. This is very interesting. I was wondering if you could expand a little on the rules processing that is done in the Nazar section. Are you using a formal rules evaluation engine (like drools) with persisted data to redis? Or are did you do a "custom" solution using redis for values to compare against? Or something different?
19
u/novelisa May 25 '17
Can someone ELI5 HyperLogLog
105
u/sh_tomer May 25 '17
Great post, enjoyed the read. A question out of curiosity: Why wouldn't you consider dropping the requirement of "Each user must only be counted once within a short time window."? Wouldn't doing that will simplify this problem a lot, so you won't have to track users at all? I know that the counts would be more as impressions and not unique views, but if the goal is to measure popularity, I think that on average every post will have the same multiple of re-visits, so it's something that can be neglected from consideration. There might be something I'm missing here, so will be great to hear your thoughts on that. Thanks again for sharing!
4
u/r888888888 May 25 '17
How do you prune the HLL counters in Redis so that it doesn't run out of space? Just expire based on last access?
And do you do anything special about the Redis keys? I know you could do things like partition them by date although that makes managing them harder.
4
u/ReallyAmused May 25 '17
Out of curiosity, what language is Abacus written in? How are write-backs queued back to Cassandra?
We have a similar thing at where we work, but not for tracking view counts, but it sits as a logical layer infront of Cassandra and does write-through caching and counting.
6
u/crylicylon May 25 '17
Will view counts be only used for posts or will they be used for comments so it would be something similar to Twitter's Tweet Activity?
2
u/duanehutchins May 25 '17
You already track the links/threads I view. Couldn't you just increment the counter when appending to that list?
For non-logged users, I would think a session cookie could suffice the same as above. Sure, there's room for fudging if someone keeps wiping the cookie, but that would be a statistical minority.
2
u/kaiyou May 25 '17
Probably a stupid question, but did you consider storing in-memory viewed posts per user over a finite time window to avoid duplicating views? The hash table would roughly occupy the same space as indexing per post but each set would be a lot smaller and save read operations upon lookup.
Also, my understanding is that duplicate views over time could have a very predictable distribution, e.g. most duplicates happen in the first few seconds following the initial view (page refresh, quick tab browsing). In that case, other structures like circular list could be more efficient that hash table maybe?
2
u/jpflathead May 25 '17
A very interesting technical discussion that teaches me a lot, but re:
What is the business reason for this? How are real time counts vs. hourly aggregates that much better for your or user needs?
2
u/JungleJesus May 26 '17
I'd like to see content-heavy subs defining their own ranking algorithms based on depth/credibility/etc.
For example, on advice-oriented subs, posts containing credible advice should be given higher priority than the clickbait article that everybody viewed.
32
u/HeterosexualMail May 25 '17
Will the view count ever be publicly visible?
9
u/warlock1992 May 25 '17
Why is the view only visible to content creators and moderators? Already posts on popular,all are curated based on popularity as prime key. Would making view count visible to regular users, affect viewership of other posts? Whats the thinking?
8
u/lonestar136 May 25 '17
I just took my first algorithms course last quarter and it is really interesting to see how much space you saved by using the HLL implementation. Great to see concepts I learned and practiced like space and time complexity can have a serious impact on my future projects.
8
u/cubicpolynomial3 May 25 '17
Wow that HyperLogLog algorithm is crazy! :o
8
u/UnfortunateDwarf May 25 '17
Will this data be available via the api? I imagine it could be useful for some the the subreddit bots.
1
u/[deleted] May 26 '17
Is Nazar kafka open source consumer or something custom to reddit? What is the scale of Kafka cluster on AWS? Do you have smaller clusters for kafka or you do one big cluster? How do you deal with kafka HA and cluster replication
1
u/vba7 May 26 '17
I alway wonder if someone will not game the system if you explain it (easier to do when all details are explained, obviously could still be done without explanation). Unless you have some manipulation detector that was ommitted
2
u/[deleted] May 25 '17
This post blew my mind. I had to figure out this for my website as well. I thought for a long time how to do it. I came up with a simple key of "name"+"id" and store it in a set in Redis redis.set("key") then store the same key in the user session. if the key is not store in the user session then I will add 1 to the key with Redis padd(). I was thinking of a better way to do this because I also store the session data in redis and don't want it to grow too big.
2
u/tabarra May 26 '17
/r/counting
2
u/DonaNobisPacman May 26 '17
Your naming conventions are A+. Who would think to name a system after the evil eye?
2
u/lexeous May 26 '17
here's a question when are you gonna ban the_donald
1
u/Gusfoo May 25 '17
Discussion of this post at Hacker News here.
1
u/Kal_Ho_Na_Ho May 26 '17
Would localization be supported for view count? For example in India we use the Indian numbering system. So on u/powerlanguage's profile the numbers are displayed like this when viewing from India
1
u/autotldr May 27 '17
This is the best tl;dr I could make, original reduced by 93%. (I'm a bot)
Extended Summary | FAQ | Theory | Feedback | Top keywords: count#1 post#2 HLL#3 event#4 Redis#5
1
u/timmyotc May 25 '17
The inaccuracy of HLL has a beneficial effect in that shadowbanned accounts can't view a post for an
ack1
u/Recl May 25 '17
I hope this has some effect on brigading.
-1
u/[deleted] May 25 '17
Hmm, seems the long term goal for Reddit at the moment is to become somewhat of a corporate Tumblr. I think their end goal with stuff like this and the new profile system is to allow for companies to have a profile where they can engage with users while controlling the whole community and narrative. This addition is so they can talk to potential companies and say "look, we have engagement numbers now!"
Definitely a direction I do not like, though I appreciate how they are (for now) keeping subreddits quite separate from all this new junk. If I don't want to see what Blizzard or whoever is up to, I can just not go to their profiles and stick to the relevant subreddits like I have been.
-8
u/PlayerDeus May 25 '17
This is awesome because I literally have decided to stop voting, it seems broken most of the time.
0
u/rivade May 25 '17
What determines if a view is unique?
0
u/Buckwheat469 May 25 '17
Can we just get the upvote and downvote counts back?
Asking for the re-enabling of Reddit Uppers and Downers Enhanced.
-4
u/[deleted] May 25 '17
An interesting and well written article, thanks guys! The only thing I find a bit upsetting is the technology bias of the article - Kafka this, Kafka that. Kafka makes no practical difference in this architecture, any queue* would do.
The highscalability.com articles are a bit better in this regard, they tend to focus on concepts, and not web-scale-hipster-technologies.
(* - although Kafka is not even a proper queue...)
0
u/[deleted] May 25 '17
[deleted] — view deleted comment
0
u/[deleted] May 25 '17
[deleted] — view deleted comment
0
u/[deleted] May 25 '17
[deleted] — view deleted comment