There’s nothing like a leaderboard to trigger feelings of sincere satisfaction or motivational malaise. Much like Pavlov’s dogs, we start salivating at the sight this engrossing stimulus. Whether we’re motivated by the recognition of our ambitious achievements or by a dreadful sense of judgment and self-reproach, leaderboards manage to hit nerves and rev engines. What was once a staple of the gaming community is now a tool that drives engagement across a variety of platforms. , for instance, gamified the process of ‘committing’ to repositories by publicly displaying contribution stats on a calendar. As a developer, I can attest to the power of sprawling swaths of green. GitHub Today we’re happy to release Contributions: a new addition to profile pagesthat lets you see what everyone has been up to on GitHub…The contributions calendar shows how frequently you’ve been contributing over the past year…You’re making contributions to projects all over GitHub and we want to show everyone what you’re doing… Show off the fancy repositories you’ve created. — GitHub Blog, 2013 Image Credit The Art of Crafting a Leaderboard Crafting effective leaderboards has become something of an art as a wider range of disciplines explore and deploy various gamification mechanisms. Fields from education and health to managing and marketing are leveraging leaderboards to elicit positive behaviors and enhance initiative. In fact, building transparency has become such a popular tactic that countless resources are devoted to detailing how to do it right. Broadly speaking, there are two types of leaderboards: . While absolute leaderboards display the top X number of people, relative leaderboards position users among others of a similar rank, mitigating feelings of discouragement and deflation. The spellbinding psychology of winning and losing offers insights into what makes a highly effective table that captures the nuances of motivation and averts disengagement. Relative and Absolute The satisfaction of completing a challenge might evoke a ripple of euphoria. Similarly, the relief of not coming in last often conjures a wave of delight. These insights are invaluable in the context of leaderboard development, but other factors weigh just as heavily on successful implementation. It would be foolish to ignore the significance of speed and time in a society driven by instant gratification. Image Credit For the Love of Gaming, Update in Real-Time Fending off frustration and assuaging impatience takes a special kind of database. Here’s the deal; . When Space Invaders led the way in the late 1970s, leaderboards catered to a niche community and were capable only of featuring the top ten players and their baller scores. The badass boards of today, however, are responsible for an astounding amount of data and possess a wealth of capabilities that make the underlying technology particularly complex. Leaderboards are deceptively difficult to implement Nowadays, developers have to consider factors like , , and . Users expect to see their latest leaderboard stats across multiple devices and appreciate being updated when their standings switch. Impressive leaderboards also grapple with that could require complicated calculations. A single application might need a global leaderboard, as well as boards distinguished by location, level, or time. A solution that optimizes for all these requirements needs to be fast and flexible. cross-platform potential social integration notifications multiple attributes Traditional, relational databases struggle to serve mammoth volumes of active users. The more users, the slower the queries. When an application uses an external data source, it’s at risk of performing sub-optimally due to data transmission and latency. “High load” environments demand more flexible infrastructure that can effectively process millions of events in a short span of time. A backend leaderboard solution must deal with continuously adding users, removing users, updating scores, finding rankings, etc. Luckily, Redis lends itself to the task! Redis to the Rescue Image by Redis High volumes of read and write operations are likely to exceed the capabilities of traditional databases. But by keeping frequently needed data close to the application, Redis is capable of drastically accelerating response time. Redis works from an , which makes it uniquely qualified to be leveraged for leaderboards. Rather than repeatedly hitting the database, developers can rely on Redis’ to generate real-time responses. in-memory dataset caching layer Caching is one of the coolest tools on the block when it comes to scalability, and is often the first step taken when performance needs to be improved. It’s advantageous in terms of speed because it translates to far fewer database accesses and much less traffic. For these reasons, developers have been turning to NoSQL data management solutions, like , for years. Memcached is a well-established and high-performance caching system, but there’s a reason why Redis has been coined, “Memcached on steroids.” Memcached One way Redis is unique is that it offers fine control over Developers can choose between various eviction policies to create more room for new information, like updated rankings on a leaderboard. Redis as a cache has tremendous potential, but it’s not the only thing that sets it apart from its alternatives. Unlike Memcached, which only supports the simple key-value structure, Redis supports data eviction. strings, hashes, lists, sets and sorted sets. Photo by RedisLabs Redis is fantastically flexible regarding the . With five primary data structures to pick from, it provides sophisticated ways to tackle complicated tasks. , in particular, help Redis kick ass at adding, removing, and updating data at lightning speed. This data structure falls somewhere between a traditional set and a hash. It’s a non-repeating collection of strings, but the elements inside are mapped to a value, not ordered. Sorted sets are especially powerful because they are able to , making it incredibly easy to access any span of scores, ranks, etc. objects that can be cached Sorted Sets operate on ranges In the case of leaderboards, each unique user is associated with a score (that can be repeated) and different sorted sets represent various types of rankings. Built-in Redis commands make it easy to sort and access the exact information, or range of information, that you need. Let’s wrap things up by walking through a simple implementation of ! ZSET (sorted set) ZSET Implementing ZSET We’re going to record scores in a sorted set by setting a value for a for a given key. Scores can be modified with the command. ZADD redis> ZADD "scores" 100 "anna"(integer) 1redis> ZADD "scores" 95 "david"(integer) 1 Now let’s query the scores and retrieve them by rank. We’ll use to return all the elements in a sorted set at a specific key, with a score between a specified and . ZRANGEBYSCORE min max redis> ZRANGE 'scores' 0 11) "david"2) "anna" We can even return users alongside their scores by using : WITHSCORES redis> ZRANGE 'scores' 0 1 WITHSCORES1) "david"2) "95"3) "anna"4) "100" Common Commands Cheat Sheet Refer to this list of common commands for additional clarification. ZSET Photo by Redis References _Redis Labs the home of Redis delivers full-featured, geo-distributed, Redis databases benchmarked as the world's…_redislabs.com Redis Labs | Database for the Instant Experience _Strings are the most basic kind of Redis value. Redis Strings are binary safe, this means that a Redis string can…_redis.io Data types - Redis _COMMAND INFO command-name [command-name ...] Get array of specific Redis command details_redis.io Command reference - Redis _Today we're happy to release Contributions: a new addition to profile pages that lets you see what everyone has been up…_github.blog Introducing Contributions - The GitHub Blog _One of the central features of my browser game, Directive Alpha, is the daily leaderboard, which is automatically reset…_blog.directivealpha.com [Making a Browser Game] Daily Leaderboard with Redis _Leaderboards are probably the oldest social feature used in games and are used to increase the level of competition…_www.gamesparks.com Leaderboards - The original and best social feature _One of biggest draws to Redis for me is the fact that it has more data structure types than just key / value. These…_joshtronic.com How to Build a Leaderboard with PHP and Redis _Redis is different than other database solutions in many ways: it uses memory as main storage support and disk only for…_oldblog.antirez.com How to take advantage of Redis just adding it to your stack