4 ms·
> 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 tha
by chime 10y ago
> 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.
- dirtae 10y agoWhat if the next page that you go to isn't on Reddit?
- jhanschoo 10y agoThen the next time you visit reddit, I guess.
- test1235 10y agoMaybe 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!
- dspillett 10y agoTo 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.
- ThrustVectoring 10y agoGetting only 90% of the votes people make doesn't even matter.
- SlashmanX 10y agoI'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.