Engineering
Why DropAsecret splits every count into pieces
When a crowd votes on a poll at once, every vote lands on the same number. Here's why that slows things down, and how splitting each count into pieces lets everyone vote without waiting on each other.
Hexer
Co-Founder & CEO
3 min read

Post a poll on your story and a lot of your friends might vote in the same minute. Every vote has to be counted. Your link's view count goes up, your stats for the day change and your DAS score moves. All of those land on the same few numbers at the same moment.
That creates a problem most people never think about. Here's what it is, and how we solved it by never keeping a count in one place.
Why one number gets crowded
Our database runs every change as a transaction. It reads what it needs, makes its changes and saves them all together, or not at all. That's what makes sure no vote is ever lost or counted twice.
The catch is what happens when two changes touch the same record at the same moment. Only one can win. The other has to start again with the new number.
If a poll's result lived in one record, every vote would be fighting over it. Two or three at once is fine. A crowd arriving together means changes starting over again and again, waiting on each other, and votes that take longer and longer to go through.
Split the number into pieces
So we don't keep one count. We keep four.
A poll's results live in four separate records. Each vote picks one of them at random and adds to it. When someone looks at the results, we add the four together.
In plain JavaScript, the idea is this:
1 2 3 4 5 6// Writing: add to one piece, picked at random const piece = Math.floor(Math.random() * 4); pieces[piece] += 1; // Reading: add all the pieces up const total = pieces.reduce((sum, count) => sum + count, 0);
Four votes arriving together now usually land on different records, so they don't wait on each other. Nothing about the total changes: it's the same number, stored in four places.
Where we use it
Poll results. Four pieces per poll. Each vote is also saved on its own, so nobody can vote twice, but those are never shown to anyone. A vote can't be traced back to the voter, and people only ever see the totals.
Your link's stats. Views, messages, replies and reactions are counted per day, and each day is split into four. When you look at your last 30 days, we add up to 120 pieces.
Your DAS score. Four pieces per link.
Campaign links. The message count for each campaign is split into four.
Our own dashboard. The numbers we watch for the whole site, like visits, messages and new links, are split into 16 pieces each. Every visit to every page adds to them, so they're the busiest counts we have. Once a day is over, a scheduled job copies its finished totals into a single record. Looking back over a month then reads one record per day instead of every piece.
Why four, and not forty
More pieces mean less waiting when people write, but more records to read and add up every time someone looks.
For one person's link, four pieces is plenty. Even a busy poll spreads its votes thin enough that they rarely collide. The site-wide counts take every visit from everyone, so they get sixteen. We picked each number for how busy that count actually gets, not the biggest one we could think of.
What it costs
Picking at random isn't perfectly even. Now and then two votes still pick the same piece, and one of them waits a moment. That's the trade we chose: occasionally one vote waits a moment, instead of a whole crowd queueing behind a single number.
Running a poll of your own? Here's how Drops and polls work on DropAsecret, and what your stats show.
From Hexerize
See what we're building.
Hexerize is a technology company from Ghana. Take a look at our apps, or get in touch.


