Why do comment counts often disagree with what I see?

br3d@lemmy.world to Fediverse@lemmy.world – 217 points –
42

You are viewing a single comment

Getting the total number of all comments may be very resource heavy if there is a lot of comments.

If it's just 5 comments, then the computer can quickly get them all from database and count how many of them are there. Now imagine if there is 50 000 comments and suddenly, you me and entire website ask "how many comments are there for this post?"

Suddenly the computer is overwhelmed by the request and you may end up crashing it due to amount of tasks it has to do.

It's way faster if instead of all of that, the computer kept track of a number of all comments and simply adjust it when comment is added or removed. It does not have to get all the comments and count how many are there, just simply return the number and you are done.

But in the essence, you sacriface potential accuracy for speed. You may accidentally "desynchronize" the counter - if an user requests a removal of the same comment twice, and you don't check if that comment was not removed. Or, in theory, if two separate users add or remove a comment at the same time. This is called "race condition", which is common in multi-threaded computing.

4 more...