Exploring Sorted Sets

Exploring Sorted Sets in Redis Using PHP

Welcome back! Building on our previous experience with Redis sets, today we are diving into sorted sets. Redis sorted sets combine the power of sets and lists, allowing us to handle collections where every member is unique but has an associated score. These scores ensure the elements are kept in a specific, sorted order.

What You'll Learn

In this lesson, we will focus on:

  1. Adding members and scores to a sorted set.
  2. Retrieving top members based on their scores.

Sorted sets in Redis are remarkable due to their efficiency and flexibility. You might find them particularly useful for scenarios like maintaining leaderboards, scheduling tasks, or storing time-series data.

Let’s start by connecting to your Redis server and adding some members to a sorted set:

Code Example

<?php

require 'vendor/autoload.php';

use Predis\Client;

$client = new Client();

// Adding scores and members to a sorted set
$client->zadd('leaderboard', 100, 'Alice');
$client->zadd('leaderboard', 400, 'Bob');
$client->zadd('leaderboard', 300, 'Charlie');
$client->zadd('leaderboard', 350, 'Alice');

// Retrieving top players
$topPlayers = $client->zrevrange('leaderboard', 0, 1, ['withscores' => true]);
echo "Top 2 players:\n";
foreach ($topPlayers as $member => $score) {
    echo "Name: $member, Score: $score\n";
}

// Retrieve players with the lowest scores
$lowPlayers = $client->zrange('leaderboard', 0, 1, ['withscores' => true]);
echo "Lowest 2 players:\n";
foreach ($lowPlayers as $member => $score) {
    echo "Name: $member, Score: $score\n";
}

// Removing members from a sorted set
$client->zrem('leaderboard', 'Alice');

?>

This code uses the zadd command to add members with their scores and the zrevrange command to get members in descending order of their scores.

Notice how we used the 'withscores' => true option to include scores in the result. This way, we can easily retrieve the top players along with their scores.

For this example, the output will be Top 2 players: Name: Bob, Score: 400, Name: Alice, Score: 350. Notice that the score for Alice is 350, not 100, as the last score is the one that is kept.

Similarly, the zrange command helps retrieve members in ascending order of their scores, which is useful when you want the lowest scores.

Both zrange and zrevrange functions take the name of the sorted set you want to query as the first argument, such as "leaderboard". For zrevrange, the second and third argument in the function call define the range of indices to retrieve, starting from the highest score and moving downwards. Conversely, zrange uses them to specify the range from the lowest score upwards.

Notice that sorted set functions all have a z prefix, which is a standardized prefix for sorted set operations in Redis. This makes it easier to identify and work with sorted set commands.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal