News for hackers. Stuff that matters.
Re:View Counting at Reddit by tudorconstantin on Saturday May 27, 2017 @04:23PM (#14431780)
Wouldn't it had been easier to simply increment a counter for each visit and then set a short lived cookie in the browser for that post? And put the spam detection system before the counter increment
Re:View Counting at Reddit by heifer2822 on Saturday May 27, 2017 @10:20PM (#14432853)
I think they said the product decision was made primarily to prevent abuse. I don't think a cookie could stop a sophisticated abuser.
Re:View Counting at Reddit by frenchie14 on Saturday May 27, 2017 @06:22PM (#14432206)
A browser cookie that can be trivially deleted by the client? What's the purpose of the cookie?
Re:View Counting at Reddit by tudorconstantin on Sunday May 28, 2017 @12:24AM (#14433081)
Indeed it can easily be deleted, that's why I said to put the spam detection before the counter.

I assume most of the visits are from normal users, not spammers, so if a normal user has the cookie for the post set then it means it's a page refresh, so don't increase the counter.

Identifying sophisticated spammers accurately is more complicated though. You can't rely on any client side info (user agent, cookies, browser history, screen resolution, OS, etc) because they can all be modified. You can't rely on IP address either, because there are public hotspots used by genuine users also. I think their spam detector is more complicated than this and they have to use it for HLL also.

So, for the genuine users, a counter increased based on the cookie mechanism would've worked just fine.

Re:View Counting at Reddit by Kluny on Sunday May 28, 2017 @04:50AM (#14433587)
> I assume most of the visits are from normal users

That's not a safe assumption. If spam prevention controls aren't working well enough, bots start to outnumber humans very quickly.

Re:View Counting at Reddit by nl on Sunday May 28, 2017 @06:07AM (#14433771)
I suspect the number of Reddit users who run AdBlockers and/or aggressively block cookies is non-trivial.

Indeed, one of the main reasons for this architecture is to measure the difference between what it measures and what the cookies method gives.

Re:View Counting at Reddit by bognition on Saturday May 27, 2017 @05:14PM (#14432007)
How do you concurrently update a counter?
Re:View Counting at Reddit by eriknstr on Saturday May 27, 2017 @05:29PM (#14432057)
I think the main problem is not concurrently updating the counter. After all updating the HLL must also be done concurrently.

The primary motivation on their part for using HLL is given in the intro.

>In order to maintain an exact count in real time we would need to know whether or not a specific user visited the post before. To know that information, we would need to store the set of users who had previously visited each post, and then check that set every time we processed a new view on a post. A naive implementation of this solution would be to store the unique user set as a hash table in memory, with the post ID as the key.

>This approach works well for less trafficked posts, but is very difficult to scale once a post becomes popular and the number of viewers rapidly increases. Several popular posts have over one million unique viewers! On posts like these, it becomes extremely taxing on both memory and CPU to store all the IDs and do frequent lookups into the set to see if someone has already visited before.

Re:View Counting at Reddit by BillinghamJ on Saturday May 27, 2017 @05:26PM (#14432045)
Redis writes are atomic - you just use the increment function
Re:View Counting at Reddit by bognition on Saturday May 27, 2017 @07:29PM (#14432420)
Writes are atomic in redis because redis is single threaded. So you are bounded by how fast redis can write. If you try to write any faster then redis can handle you'll get queueing or errors.
Re:View Counting at Reddit by antirez on Saturday May 27, 2017 @11:18PM (#14432955)
The wonderful thing about HyperLogLogs is that you can split the counter in N servers and "merge" the registers later, in case you want an architecture that shards the same counter in multiple servers. But sharding directly by resource looks simpler actually...
Re:View Counting at Reddit by btmorex on Saturday May 27, 2017 @07:50PM (#14432480)
Run enough redis servers to handle the load. Choose a server by hashing a user id. Total = sum of counts from all servers.
Re:View Counting at Reddit by jjirsa on Tuesday May 30, 2017 @12:21AM (#14442990)
It's also got cassandra behind the scenes, which has fast, concurrent, distributed counters.
Re:View Counting at Reddit by diziet on Saturday May 27, 2017 @08:56PM (#14432644)
Yes, that was my idea as well. They must have some sort of cache system for serving basic user meta-data at scale when a page is loaded, and they could add a time-expiring list post ids of the posts viewed by a user to do detection on a per user basis on the backend.

I think they want to break it into different services for (whatever) reasons.

Running a counter across a sharded in memory cache implementation (like Redis) for something like post views should be ok, but they might have some weird rule-sets for spam detection that are slow.

Re:View Counting at Reddit by sethammons on Saturday May 27, 2017 @10:42PM (#14432892)
I'm not following your solution for how it handles the unique visitor counting. You have to know when to increment the counter. Thus why they used the HLL.
Re:View Counting at Reddit by diziet on Monday May 29, 2017 @02:30AM (#14437714)
Ah, sorry if it wasn't clear. By storing an expiring list post IDs in the close cache for loading the user metadata, you could filter out the posts that were already visited by that user.
Re:View Counting at Reddit by gniv on Saturday May 27, 2017 @10:24PM (#14432859)
That might work, but the cookie would be huge for people who read a lot of reddit threads, no?
Re:View Counting at Reddit by tudorconstantin on Sunday May 28, 2017 @12:10AM (#14433053)
That depends on their definition of 'short lived'.

How many posts can a user visit in, lets say, an hour?

They said they want to avoid increasing the views when a user refreshes the page in a short interval