Designing Dynamic Feeds and Favorites Systems for Content Platforms

    This article explains how to redesign the dynamic feed and favorites features on content platforms as two independent subsystems. It covers a layered data model for posts, the three key steps of the publishing pipeline (validation and rate limiting, idempotent writes, and asynchronous fan-out), and the trade-offs between push and pull fan-out. It then treats favorites as semantic assets rather than simple likes, covering data modeling, count and list consistency with cursor pagination, and handling of invalidated content. The article also addresses visibility checks on the read path, hot-cold data separation for cost control, and a pre-launch checklist. Readers will gain a practical blueprint for stable reads and writes at controllable cost.

    1. Background: Why Feeds and Favorites Need to Be Redesigned

    On content-driven communities, a “dynamic” (a user post or update) is the vehicle for self-expression and relationship-driven distribution, while “favorites” are the entry point for building a personal knowledge base. Both look simple, yet they simultaneously face four pressures: high write volume, high read volume, strong consistency expectations, and long-tail storage. The seemingly simple “feed / favorites” entry on a page actually exposes two long-underestimated engineering problems: the scalability of the publishing pipeline and the accuracy of favorite semantics.

    Many teams initially treat a post as an ordinary record and a favorite as a simple relationship write. As the user base grows, the problems erupt together: write jitter during publishing peaks, chaotic ordering in favorite lists, private content being misread, and historical data slowing down queries. Both features therefore need to be governed at the architecture level as independent subsystems.

    2. The Feed Subsystem: From “Writing a Record” to a “Diffusable Event Stream”

    2.1 Layered Data Model

    It is recommended to split a post into three layers: the content body, publishing metadata, and the diffusion index. The content body stores text and media references; the publishing metadata stores the author, visibility scope, timestamp, and status; the diffusion index serves the read amplification problem of the “following feed.”

    1. -- Content body
    2. CREATE TABLE post (
    3. post_id BIGINT PRIMARY KEY,
    4. author_id BIGINT NOT NULL,
    5. body TEXT,
    6. visibility TINYINT NOT NULL DEFAULT 0, -- 0 public, 1 followers, 2 private
    7. status TINYINT NOT NULL DEFAULT 1,
    8. created_at BIGINT NOT NULL
    9. );
    10. -- Following-feed diffusion index (simplified illustration)
    11. CREATE TABLE feed_index (
    12. viewer_id BIGINT NOT NULL,
    13. post_id BIGINT NOT NULL,
    14. score DOUBLE NOT NULL,
    15. created_at BIGINT NOT NULL,
    16. PRIMARY KEY (viewer_id, post_id)
    17. );

    The value of layering is that content changes and index rebuilds are decoupled: recomputing rankings will not pollute the body data.

    2.2 Three Key Actions in the Publishing Pipeline

    The first is validation and rate limiting. Apply upfront constraints on frequency, length, and media count to prevent abnormal traffic from penetrating to the storage layer. The second is idempotent writes. The client carries a request identifier, and the server deduplicates with a unique key to prevent duplicate posts caused by retries on weak networks.

    1. def publish(req):
    2. key = f"post:{req.author_id}:{req.client_token}"
    3. if not cache.set(key, 1, nx=True, ex=600):
    4. return {"code": 0, "msg": "duplicated"}
    5. post_id = id_gen.next()
    6. db.insert_post(post_id, req)
    7. mq.send("feed_build", {"post_id": post_id})
    8. return {"code": 0, "data": {"post_id": post_id}}

    The third is asynchronous diffusion. A successful publish returns immediately, and index building is handed off to a message queue and consumers. This compresses the synchronous path to a single primary-database write and defers work that can be delayed.

    2.3 Trade-offs Between Push and Pull Fan-out

    Push fan-out generates an index for every follower at publish time; reads are extremely fast, but write amplification is severe. Pull fan-out aggregates the timelines of followed accounts at read time; writes are light, but read amplification is obvious. In engineering practice, a hybrid strategy is more common: use push fan-out for authors with a normal follower count, and use pull fan-out with caching for very large accounts.

    The threshold should not be set arbitrarily; instead, use “follower count × average publishing frequency” as a capacity metric, observe it continuously, and adjust dynamically.

    3. The Favorites Subsystem: Treating “Favorites” as Semantic Assets

    3.1 Favorites Are Not Likes

    A like is a monotonic, revocable emotional signal; a favorite is a collection of assets the user actively builds, which naturally requires categorization, retrieval, and long-term availability. The favorites table therefore needs to carry the source type, target identifier, folder, and remark.

    1. CREATE TABLE favorite (
    2. user_id BIGINT NOT NULL,
    3. target_type TINYINT NOT NULL, -- 1 post, 2 article, 3 video
    4. target_id BIGINT NOT NULL,
    5. folder_id BIGINT,
    6. remark VARCHAR(128),
    7. created_at BIGINT NOT NULL,
    8. PRIMARY KEY (user_id, target_type, target_id)
    9. );

    The composite primary key naturally guarantees idempotency: repeated favorites produce no dirty data, and unfavoriting is a physical delete or a status-bit flip.

    3.2 Consistency of Counts and Lists

    The favorite count is a typical high-frequency update counter. The recommended approach is to write to the primary database and then asynchronously increment a counter cache, reading primarily from the cache with periodic calibration as a fallback. The list page should paginate in reverse chronological order by creation time, using a cursor rather than an offset to avoid the performance collapse of deep pagination.

    1. def list_favorites(user_id, cursor, size=20):
    2. rows = db.query(
    3. "SELECT target_id, created_at FROM favorite "
    4. "WHERE user_id=%s AND created_at < %s "
    5. "ORDER BY created_at DESC LIMIT %s",
    6. (user_id, cursor, size + 1))
    7. has_more = len(rows) > size
    8. items = rows[:size]
    9. return {"items": items,
    10. "next_cursor": items[-1]["created_at"] if has_more else None}

    3.3 Invalidations and Availability

    Favorited content may be deleted or switched to private. The design should retain the favorite relationship and only mark it as invalid at the presentation layer, avoiding silently wiping out a user’s assets. For invalidated targets, provide a “bulk cleanup” entry point that returns the choice to the user.

    4. Privacy and Visibility: The Most Overlooked Correctness Issue

    Visibility scope must be re-validated at read time and cannot rely only on the write-time check. A recommended approach is to filter uniformly before returning a list:

    1. def visible(post, viewer_id):
    2. if post.visibility == 0:
    3. return True
    4. if post.author_id == viewer_id:
    5. return True
    6. if post.visibility == 1:
    7. return relation.following(post.author_id, viewer_id)
    8. return False

    At the same time, the default visibility of posts and favorites should lean conservative. A private-by-default favorites list is a widely accepted industry practice; making it public requires the user to explicitly enable it.

    5. Capacity Governance and Cost Control

    Hot-cold separation is the core approach: keep data from the last three months in high-performance storage, move historical data to low-cost object storage, and restore query capability by rebuilding indexes. Combined with logging services and monitoring alerts, build dashboards for write latency, diffusion backlog, and cache hit rate.

    On Baidu AI Cloud, a container platform can host the stateless publishing service, a message queue can handle diffusion decoupling, object storage can hold historical and media data, and logging and monitoring services can complete the observability loop. This supports the layered design above with a general-purpose product combination, without binding to any proprietary interface.

    6. Pre-Launch Checklist

    • Is the publishing API idempotent, and will retries produce duplicate content?
    • Does the diffusion pipeline have backlog alerts and a degradation switch?
    • Does the favorites list use cursor pagination?
    • Is the visibility scope re-validated on the read side?
    • Does the counter have a periodic calibration task?
    • Do historical data have hot-cold tiering and a recovery path?

    In summary, “feeds” test diffusion and peak-shaving capability, while “favorites” test semantic modeling and long-term availability. Governing the two as independent, layered subsystems, with three main threads running through them—asynchronization, idempotency, and hot-cold separation—delivers stable read and write performance and a good user experience at a controllable cost.