I am implementing a leaderboard for my game and since it needs to be fast and run-time I am planning to use Redis. I haven’t use Redis before so I am confused about one thing. As far as I know Redis is maninly used as a cache, but in my scenario I am planning it like a database. Can I do this or should I store the leaderboard data in a database as well. What would be the best practice in this situation?
3
Answers
Simple answer would be have leaderboard data in a reliable database and load it into Redis as a secondary database.
However, it is possible to configure Redis persistence RDB + AOF and with that option you can use only Redis, but be aware that having RDB + AOF will be impacting Redis performance for write operations and there might be another issues mapping you in-memory data to disk.
Redis is in-memory storage and, as you already know, there are some hurdles making it primary database. Also you can try ready-to-go solutions like AWS MemoryDB, which is reliable database and Redis at the same time. If you can’t use solutions like that, I don’t recommend focusing effort try store data only on Redis and have data backed up by regular, well understood database.
There are better choices for persistent storage: relational databases (PostgreSQL, MySQL etc) or NoSQL (MongoDB, Firebase etc). In addition to persistence, they offer a lot of query-related functionality as well as concurrent user access and transactions.
Redis is an excellent choice for leaderboards due to its Sorted Sets data structure, which allows for maintaining the leaderboard in a sorted state. Additionally, Redis provides commands to retrieve the top or bottom ranges easily. For more details read the article
It is perfectly valid to use Redis as an operational NoSQL database, as well as a data structure server (e.g., counter, stack, queue, priority queue, set, leaderboard, …) with enterprise capabilities (controllable distribution, scalability, atomicity, isolation, durability, and consistency).
I’m for Redis. Many of our paying Redis Enterprise Software and Redis Cloud customers, including game industry leaders, use Redis as a primary database and data structure server to store their leaderboards (See here).