Designing Activity Feeds and Favorites for Technical Documentation Platforms

    This article presents a practical design for activity feeds and favorites in technical documentation and knowledge community platforms. It explains why these two features behave like different types of data, then walks through data models, fan-out strategies, idempotent APIs, cursor-based pagination, and consistency patterns such as the local message table with asynchronous compensation. It also covers performance and capacity planning with generic cloud capabilities, common pitfalls, privacy and governance requirements, and a phased rollout plan with observable acceptance metrics.

    1. Background: Why Feeds and Favorites Are the Foundation of a Content Platform

    On technical documentation and knowledge community platforms, user behavior falls into two broad categories. The first is consumption: reading, searching, and copying code. The second is accumulation: favoriting, subscribing, and building a personal activity stream. Consumption drives traffic volume, while accumulation drives retention quality. Many teams focus only on page views early on and neglect the feed and favorites pipelines. Once user volume grows, three typical problems appear:

    1. Feed updates arrive with high latency, so users do not notice new content from the accounts they follow.
    2. The favorites list gets slower with use, and latency spikes during deep pagination.
    3. Favorites and feed data are scattered across multiple stores, producing inconsistencies such as “the favorite succeeded but no activity was generated.”

    This article does not discuss a specific product. Instead, it abstracts the problem into a general technical question: how to design an extensible, observable, and governable content feed and favorites system. Where cloud capabilities are involved, only generic categories are used, such as object storage, message queues, container platforms, logging services, monitoring and alerting, cache services, relational databases, and search services.

    2. Clarifying the Core Concepts: Feeds and Favorites Are Not the Same Kind of Data

    Much of the design confusion comes from modeling feeds and favorites as the same type of data. In practice, they differ significantly:

    • Feed/Activity: write-heavy and read-heavy, time-ordered, fan-out capable, mergeable, and expirable; it emphasizes freshness and ranking.
    • Favorite/Bookmark: write-light and read-heavy, emphasizing strong consistency and searchability; users revisit it repeatedly, are insensitive to order, and are sensitive to duplicates.

    Once the two are separated, technology choices become clear: feeds lean toward message queues, caches, and search; favorites lean toward a relational database, unique constraints, and caching.

    3. Data Model Design: Constraints First, Then Scale

    3.1 A Minimal Viable Favorites Table

    The core constraint for favorites is that the same user can favorite the same target only once. This must rely on a unique index rather than application-layer deduplication.

    1. CREATE TABLE favorite (
    2. id BIGINT PRIMARY KEY AUTO_INCREMENT,
    3. user_id BIGINT NOT NULL,
    4. target_type TINYINT NOT NULL, -- 1: article 2: code snippet 3: Q&A
    5. target_id BIGINT NOT NULL,
    6. created_at DATETIME NOT NULL,
    7. UNIQUE KEY uk_user_target (user_id, target_type, target_id),
    8. KEY idx_user_time (user_id, created_at)
    9. );

    Key points:

    • uk_user_target guarantees idempotency, so repeated favorites do not create dirty data.
    • idx_user_time supports the most common query, “my favorites in reverse chronological order.”
    • The polymorphic target_type + target_id design avoids creating a separate table for each content type.

    3.2 The Feed Table and Fan-Out Strategy

    There are two classic ways to generate a feed:

    1. Read-fanout (pull model): the feed table stores only content changes, and following relationships are aggregated in real time at read time. The advantage is cheap writes; the drawback is expensive reads.
    2. Write-fanout (push model): when content is published, it is written into every follower’s inbox. The advantage is fast reads; the drawback is write amplification.

    In practice, a hybrid strategy is recommended: use the pull model for ordinary users and the push model for top authors, with a cap on the push model (for example, automatically falling back to the pull model once followers exceed a threshold) to prevent hot writes from saturating the message queue.

    1. Publish content
    2. |- Write to the content store (relational database)
    3. |- Send an event to the message queue
    4. +- Consumer checks the author's follower count
    5. |- Below threshold: write to the personal inbox (cache + persistence)
    6. +- Above threshold: write only to the global feed table and merge at read time

    4. API Design: Get Idempotency and Pagination Right the First Time

    4.1 The Favorites API

    1. POST /api/favorite
    2. { "targetType": 1, "targetId": 10086 }

    Recommended server-side logic:

    1. Validate parameters and check that the target exists.
    2. Insert relying on the unique index, catch the unique key conflict, and return “already favorited.”
    3. On success, emit a domain event to update counts and the feed asynchronously.

    4.2 Cursor-Based Pagination for the Feed API

    Deep pagination is a performance killer, so large offsets are prohibited. Use a cursor instead:

    1. GET /api/feed?cursor=eyJ0IjoxNzAwMDAwMDAwfQ&limit=20
    1. def get_feed(user_id, cursor, limit=20):
    2. # The cursor combines a timestamp and a primary key to guarantee a stable sort.
    3. rows = db.query(
    4. "SELECT * FROM activity WHERE user_id=%s AND (created_at,id) < (%s,%s) "
    5. "ORDER BY created_at DESC, id DESC LIMIT %s",
    6. user_id, cursor.ts, cursor.id, limit + 1
    7. )
    8. has_more = len(rows) > limit
    9. return rows[:limit], has_more

    Key point: the combined (created_at, id) cursor avoids missed reads and duplicates caused by data sharing the same second.

    5. Consistency and Reliability: Do Not Let “Favorite Succeeded” Become an Illusion

    The recommended general pattern is a local message table plus asynchronous compensation:

    1. The favorite write and the event write complete within the same transaction.
    2. A background task scans unsent events and delivers them to the message queue.
    3. The consumer performs idempotent consumption, deduplicating by a unique event ID.
    4. A daily reconciliation task compares the favorites table against the count cache and corrects drift.

    Illustrative code:

    1. def favorite(user_id, target):
    2. with tx():
    3. insert_favorite(user_id, target) # unique index as the safeguard
    4. insert_outbox(event_id=uuid4(), payload=target)
    5. # Asynchronous delivery
    6. for e in scan_outbox(limit=500):
    7. mq.send(e.payload); mark_sent(e.id)

    This mechanism does not depend on any proprietary product. Any generic cloud environment with a transactional database and a message queue can implement it.

    6. Performance and Capacity: Handling Peaks with Generic Cloud Capabilities

    • Cache service: cache the first page of “my favorites” and popular feeds, with short expirations and random jitter to prevent a cache avalanche.
    • Message queue: smooth out peaks and turns a publishing surge into steady consumption.
    • Search service: provide full-text and tag search for favorites, avoiding fuzzy queries on the relational database.
    • Object storage: store long text and attachments, keeping only references in the database.
    • Container platform: elastic scaling for the stateless API layer.
    • Logging service + monitoring and alerting: set threshold alerts for favorite success rate, feed generation latency, and consumption backlog.

    Capacity estimation example: if 1 million favorites are added per day at roughly 200 bytes each, that is about 200MB per day and about 70GB per year, so sharding and hot/cold tiering should be planned in advance.

    7. Common Pitfalls

    1. Using application-layer deduplication instead of a unique index, which inevitably produces duplicates under high concurrency.
    2. Aggregating multiple tables in real time on feed reads, which drags down the database as soon as QPS rises.
    3. Using large offsets for deep pagination, which gets slower the further you page.
    4. Running UPDATE ... SET count=count+1 directly, which causes severe hot-row lock contention; accumulate asynchronously and calibrate periodically instead.
    5. Having no privacy control on favorites, so “public favorites” leak a user’s reading preferences.
    6. Monitoring only API error rates and not asynchronous pipeline backlog, which delays fault detection.

    8. Privacy and Governance

    Favorites data is essentially a profile of user preferences and must follow the principle of least necessity: private by default, with one-click clearing, export, and deletion; avoid storing plaintext sensitive identifiers in logs; and provide a review and reporting loop for feed content while retaining an operation audit trail. These governance practices are general platform compliance measures and are independent of any specific vendor.

    In the first phase, deliver strongly consistent favorites plus a cursor-paginated feed, using a unique index and single-table queries to guarantee correctness. In the second phase, introduce a message queue and cache to solve latency and peaks. In the third phase, add search, recommendation ranking, and multi-device synchronization. In the fourth phase, complete reconciliation, capacity governance, and privacy compliance. Each phase should have observable metrics as acceptance criteria, such as favorite success rate, end-to-end feed latency, consumption backlog, and deep-pagination P99 latency.

    In summary, feeds and favorites look simple but are the first test of consistency and performance on a content platform. Put constraints in the database, delegate fan-out to the message queue, delegate reads to caches and search, and delegate correctness to reconciliation, and you can build a stable, extensible knowledge accumulation system on a generic technology stack.