News for hackers. Stuff that matters.
Re:Caching at Reddit by chime on Thursday January 19, 2017 @02:19AM (#13433793)
> When you vote, your vote isn’t instantly processed—instead, it’s placed into a queue.

I remember looking into this a while ago and was bewildered to find that when I upvoted or downvoted, there was no XHR call to the backend! There was no hidden iframe/image, no silent form post. Absolutely no network activity. Yet when I refreshed, my vote was shown correctly. I thought I was going crazy.

This was long ago so I'm a bit fuzzy on the details but after a bit of digging, I found the most elegant data collection technique I've ever seen. Instead of sending network data when I voted, a local cookie was set with the link id and vote value. Then when I went to another page, my browser naturally sent the cookie to the server, where I believe it was processed, and then a fresh cookie was sent back to my browser. I could vote on 10 links, the local cookie would get large and then on the next page refresh, the backend would receive my batch of votes, process them, and send me a fresh cookie again.

I don't think they do that now and I've never seen anyone do something like this. Even HN just makes an XHR call on voting. After twenty years on the web, it's not often that I am surprised so this was quite a thrill.

Re:Caching at Reddit by dirtae on Thursday January 19, 2017 @02:42AM (#13433889)
What if the next page that you go to isn't on Reddit?
Re:Caching at Reddit by jhanschoo on Thursday January 19, 2017 @03:04AM (#13433988)
Then the next time you visit reddit, I guess.
Re:Caching at Reddit by test1235 on Thursday January 19, 2017 @04:08AM (#13434260)
Maybe I'm not a 'normal' user so it wouldn't have mattered to them, but I used to open loads of tabs at once, and slowly make my way across all the stories, then more often than not, I'd close the browser where all my cache, cookies and history and whatnot are cleared.

This would mean none of my votes would ever have made it to the server!

Re:Caching at Reddit by dspillett on Thursday January 19, 2017 @05:47AM (#13434734)
To an extent it might be worth letting those votes be lost to keep things simple, if only relatively few users browse that way.

There are ways around it if it were a problem though:

* Have a timeout on updates. If a vote has been sat in the local cookie send for some time send an XHR request to push the current queue to the server

* Use onunload event handlers to push any remaining queued votes when a tab closes. You could even try maintain a count of how many tabs are currently open (in the same cookie) and only send that request on the last one closing.

* If the amount of votes hits a certain length, also send a XHR request to register them.

Probably not perfect (I have in the past found onunload events to not be terribly reliable) but it would capture most of the otherwise lost votes you describe. It might be too much work to write/debug/maintain though, if the number of lost votes would be small anyway.

Re:Caching at Reddit by SlashmanX on Thursday January 19, 2017 @05:54AM (#13434766)
I'd guess they'd execute this function in onbeforeunload (or the equivalent) so the functionality is called when you go to leave the page rather than when you load a fresh one.
Re:Caching at Reddit by ThrustVectoring on Thursday January 19, 2017 @04:03AM (#13434244)
Getting only 90% of the votes people make doesn't even matter.
Re:Caching at Reddit by Tepix on Thursday January 19, 2017 @04:40AM (#13434394)
It does matter in small subreddits.

You could add an onunload handler that fires off a request with the cookie to mitigate this.

Re:Caching at Reddit by Kiro on Thursday January 19, 2017 @05:31AM (#13434648)
Can you really trust onunload handlers to finish?