I needed to implement the "Top Contributors" feature of StackShare, that gets updated whenever someone posts a decision. I hesitated between 2 solutions for data storage:
-
Store real time updates in a Redis sorted set. It's like having a big in-memory hash, but in Redis.
- Pros: Best for pure real time feedback. Also, Redis sorted sets are cool!
- Cons: Web devs may not be familiar with Redis sorted sets. Also, we still have to query our traditional DB to enrich Redis results.
-
Add a
cached_scorecolumn for the PostgreSQL records shown in the leaderboard. This column is recalculated in our app whenever a record is updated. Make that column indexable for sorting, sort the records by descending order, and voilà, you have leaderboards. Cache the result for X seconds to keep DB queries low while still keeping a more or less "real time" feel.- Pros: Web devs are more familiar with traditional DB sorting code.
- Cons: Every X seconds, you still have to do a DB sort on a big table.
We ended up choosing solution 2 with PostgreSQL for a couple reasons. 1) We currently don't treat Redis as persistent storage and don't have redundancy in place, so data could disappear. 2) Up-to-the-second real time updates weren't mandatory. #Leaderboards #StackDecisionsLaunch

