This document discusses how Redis can be used to solve various data and application scenarios for a social Q&A platform. It describes 6 scenarios including using Redis strings for counters, Redis lists for a message queue, Redis hashes for user activity tracking, Redis bits for feature flags, Redis sorted sets for a social wall, and Redis pub/sub for real-time monitoring. It provides examples of how Redis data structures map to each use case and discusses capabilities like performance, scalability and data consistency.
11. COUNTERS STRING
MESSAGES QUEUE LIST
USER ACTIVITY MATRIX HASH
FUNCTIONAL SWITCHES BIT OPERATIONS
THE WALL SORTED SET
REAL-TIME MONITORING PUB / SUB
57. SELECT * FROM questions q
LEFT JOIN followships f ON (q.user_id = f.friend_id)
WHERE f.user_id = :my_user_id
ORDER BY q.answered_at
LIMIT 0,25
SOLUTION
58. LATER REQUIREMENTS
LIKES INTRODUCED
SHOW RETWEETS
UNIQUENESS OF ANSWERS
ORDERED BY FIRST OCCURRENCE
PAGINATION NEEDED
DO NOT SHOW OWN POSTS
SHOW RETWEETS SINCE STARTED FOLLOWING A
FRIENDS
59. MORE REQUIREMENTS
DO NOT SHOW RETWEETS IF ANSWERER OR
RETWEETER IS DISABLED
SHOW LATEST FRIENDS WHO LIKED A QUESTION
62. NON REPEATING COLLECTIONS OF
STRINGS
EACH MEMBER ASSOCIATED WITH
SCORE
QUICKLY ACCESS
ELEMENTS IN ORDER
FAST EXISTENCE TEST
FAST ACCESS TO ELEMENTS IN THE
MIDDLE
REDIS SORTED SET
63. user/:user_id/wall
score_1 score_2 ... score_N
question_id_1 question_id_2 ... question_id_N
score - timestamp, when the question_id first occurred in a set
STRUCTURE
64. ● GET USERS WALL
○ ZREVRANGEBYSCORE - O(log(N)+M)
● USER ANSWERED A QUESTION
○ ZADD - O(log(N))
● LIKE
○ ZRANK - O(log(N))
○ ZADD - O(log(N))
● REMOVE ANSWER
○ ZREM - O(M*log(N)
OPERATIONS
74. Time complexity: O(N+M) where N is the number of clients
subscribed to the receiving channel and M is the total
number of subscribed patterns (by any client).
SCALING