4
Design of Follower using REDIS Following questions needs to be answered. List of follower for given Objects (Game, Icon, Movie ) List of Objects user is following. Total count of following for a given object. Velocity of object following (Hourly, Daily, Weekly ) How do we know two friend or parents are following same “Object”? How do we know difference of objects following between two friends/parents. How to implement using REDIS List of Followers for a given object REDIS SET is a perfect data structure. Key name for Object is “Object Name”

Follower-REDIS

Embed Size (px)

Citation preview

Page 1: Follower-REDIS

Design of Follower using REDIS

Following questions needs to be answered.

List of follower for given Objects (Game, Icon, Movie ) List of Objects user is following. Total count of following for a given object. Velocity of object following (Hourly, Daily, Weekly ) How do we know two friend or parents are following

same “Object”? How do we know difference of objects following between

two friends/parents.

How to implement using REDIS

List of Followers for a given object

REDIS SET is a perfect data structure. Key name for Object is “Object Name” qualified with name space. Set data structure guarantees uniqueness (Takes care if a user follows object more than once ..)

1. Adding member to a set – SADD – Complexity O(n)2. Find out how many total follower for a given Object

– SCARD Complexity O(1)

Page 2: Follower-REDIS

3. If we want to support “unfollow” functionality – SREM – Complexity O(n)

Alternatively, if we want to track, follower’s should be listed in reverse chronological order, then, Sorted Set ZADD, is right data structure and timestamp (Unix Epoch Time Stamp) as a score.

List of Object user is following.

REDIS SET is a perfect data structure. User name can be namespace qualified. Name space is “SWID”.

1. User adds object to follow – SADD – Complexity to O(n)

2. User Decides to “unfollow” – SREM – Complexity to 0(n)

3. Total number of Object user is following – SCARD – Complexity O(1).

Here, also if want to track order of following then Sorted Set ZADD is right data structure and Epoch time is a score. Complexity is O(log(n))

Velocity of Object following. (Daily, Hourly, Weekly)

This feature is accomplished by using Sorted set. As elements are inserted and indexed by epoch time, ZCOUNT ( Complexity O(log(N)+M ) command returns number of elements between min and max. Following is example to retrieve Daily velocity.

1. Get current time in Epoch.

Page 3: Follower-REDIS

2. Total seconds in 24 hour ( 26 * 60 * 60 ) = 86400.3. Epoch24hr-before = current Time in EPOCH –

86400 )4. Issue command, ZCOUNT “key” Epoch24hr-before

current-time-in-Epoch, this results in count of follower in last 24 hours.

Same methodology can be used to find out velocity of any time duration, e.g. Weekly, Monthly, Yearly.

How do we know two friend or parents or group are following same “Object”?

This should be accomplished by intersection between two or more sets. SINTER is a command. Complexity of this command is 0(N * M). Where N is cardinality of smallest set where as M is number of sets.