Data store for capturing votes & aggregating them
-
22-09-2019 - |
Question
I'm interested in using Node.js as a socket server for stream-based communication with tens of thousands of clients. Clients will be sending votes for particular pieces of content and receiving near-real-time updates of aggregated vote tallies from the server.
The datastore needs to support:
- Storing the votes
- Summarising the votes in near-realtime
- Preventing multiple votes within an arbitrary time period (e.g. clients can only vote once for a piece of content every 1 minute)
Something that already has client libraries for Node.js would be preferable.
Solution
I would highly recommend Redis. It is a key/value store with set and list operations. It also supports atomic operations for counters. There is a client for Redis for Node.js available on github. Here is how I would implement your features:
Storing the votes
INCR votes:option:<option>
Summarizing the votes
MGET votes:option:<option1> votes:option:<option2> ... votes:option:<optionN>
Preventing multiple votes (create an expiring lock key for client IP)
EXPIRE lock:<encoded ip> 60
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow