As far as I understand SO keeps votes in a separate table and calculates the post votes based on that data.
So DB query can look like this assuming votes can be 1/-1.
SELECT SUM(value) FROM votes WHERE postId = [some post id]
In addition to vote count when a user opens a post it also show id the current user has voted up/down for that post, which can mean another DB query.
I would like to know the inner implementation details, since with such huge data it can be really slow to calculate votes all the time for each post. Is Stack Overflow caching this information, recalculating on background, etc.? If it is caching, then what? If it is calculating in background then how?