We’ve seen before a Redis snippet for storing the social graph, this one in PHP:
The trick to implementing the social graph is using the built-in Set datatype in Redis. In short, a set is an unordered collection of binary-safe strings. These strings are grouped together using a key as the index. The great thing about sets is that the most used operations are performed in O(1) (constant) time. More complex operations have slightly worse runtimes, such as determining common followers among a set of users, which runs in O(n * m).
As regards those “more complex operations, you might want to check the Redis SINTER/set intersection performance.
Original title and link: Implementing a Social Graph using Redis (NoSQL databases © myNoSQL)