לדלג לתוכן

4.5 - פיד חברתי - תרגול

מימוש Hybrid Fan-out

תרגיל 1 - Fan-out Simulator

מימשו גרסה מפושטת של Hybrid Fan-out:

class SocialFeedSystem:
    CELEBRITY_THRESHOLD = 10  # low for demo purposes

    def __init__(self):
        self.posts = {}          # post_id -> post
        self.followers = {}      # user_id -> set of follower_ids
        self.following = {}      # user_id -> set of followed_ids
        self.feed_cache = {}     # user_id -> list of post_ids
        self._next_post_id = 1

    def follow(self, follower_id: int, followee_id: int):
        ...

    def get_follower_count(self, user_id: int) -> int:
        ...

    def publish_post(self, author_id: int, content: str) -> int:
        """Returns post_id"""
        ...

    def get_feed(self, user_id: int, limit: int = 10) -> list[dict]:
        """Returns posts sorted by time"""
        ...

בדיקה:

system = SocialFeedSystem()

# create a celebrity with many followers
for i in range(1, 15):
    system.follow(follower_id=i, followee_id=100)  # celebrity

# user 1 also follows user 2
system.follow(follower_id=1, followee_id=2)

# publish posts
system.publish_post(2, "פוסט של חבר רגיל")
system.publish_post(100, "פוסט של celebrity!")

# user 1's feed should contain both posts
feed = system.get_feed(user_id=1)
print(len(feed))  # 2

תרגיל 2 - שאלות עיצוב

  1. מה היתרון של Fan-out on Write על Fan-out on Read עבור משתמשים רגילים?
  2. למה celebrities נטפלים אחרת? מה יקרה אם לא?
  3. Beyonce Problem: כשcelebrity עם 100 מיליון עוקבים מפרסמת פוסט - מה יקרה אם נשתמש בFan-out on Write? איך פותרים?
  4. אם רוצים לתמוך בposts של תמונות כבדות (10MB כל אחת) - מה צריך לשנות בארכיטקטורה?

תרגיל 3 - עיצוב סיום

תכננו מערכת פיד ל-TaskFlow: "Activity Feed" שבו משתמש רואה עדכונים על משימות ופרויקטים שמעורב בהם.

גדרו:
1. אילו events מופיעים בפיד (task created, assigned, commented, completed...)
2. מי מקבל כל event?
3. איזו גישה (push/pull/hybrid) מתאימה ולמה?
4. כמה זמן שומרים עדכונים בפיד?