#MongoDB, Redis and Leaderboards

5 messages · Page 1 of 1 (latest)

mortal birch
#

I am working on a project that has some gamefication features, including a leaderboard. The project uses a MongoDB database, and I store in it user points and xp. After a short research, I found out that Redis is a perfect solution for leaderboards because of the speed. However, I dont really understand how to integrate Redis into my current stack, because whenever a user has points to be updated, I should also update the Redis db, so I can have a real time leaderboard. Because of that, I am not sure how I can guarantee data consistency between both databases.

What are your thoughts on that ? Do you know a good way to implement this ? Are there any useful resources / examples I can check

untold glade
#

Hi Maxim, what kind of data are you storing in Mongo? And what are you planning to store in Redis? How often does your leaderboard change?
As for data consistency, you can’t guarantee it, without some distributed transaction mechanism.

indigo bronze
mortal birch
# untold glade Hi Maxim, what kind of data are you storing in Mongo? And what are you planning ...

so the main idea is we have users, and they have points. We have daily leaderboards, weekly leaderboards and overall leaderboards. So the ideea would be to find the mechanism that would allow the leaderboards to work fast, as we know that we need the daily and weekly points only for leaderboards, but to quarantee consistency. The leaderboard, ideally, would update every time a user updates his own points

dense perch
#

Hi @mortal birch
You're on the right track — use Redis as a cache layer for fast leaderboard reads, and treat MongoDB as your source of truth. Every time a user earns points, update both Mongo and Redis in the same operation (a small wrapper function can help). Since Redis doesn't need to be perfectly consistent (only fast), you can periodically sync it from Mongo to handle any edge cases. Libraries like redis-rank are great for handling time-based leaderboards efficiently!