Technical Implementation of Activity Feeds and Favorites in Content Platforms
This article presents a practical guide to building the activity feed and favorites modules of a content platform. It explains why the two features must be modeled separately, then covers data models, the publish pipeline, multi-list aggregation, consistency and counters, caching, capacity tiering, elastic infrastructure, and a launch checklist. The goal is to show how decoupling write fan-out, relationship storage, counter consistency, and cold-data governance reduces system complexity and leaves room for recommendations and search.
Technical Implementation of Activity Feeds and Favorites in Content Platforms
In content products, the activity feed and favorites look like lightweight features, but they are actually the entry and exit points of the entire content infrastructure. The feed spreads user activity in real time, while favorites preserve valuable content as personal assets. Together they determine engagement and retention. This article takes a general engineering perspective and offers a practical design and implementation guide.
1. Problem Definition: Two Fundamentally Different Modules
An activity feed is essentially a write fan-out problem. A single publish action triggers a large number of read requests, the read/write ratio is highly asymmetric, and timeliness matters. Favorites are essentially a relationship storage problem: a many-to-many relation table that is read-heavy but requires strong state consistency—once a user unfavorites an item, the change must appear in the list immediately.
Implementing both in one module is the most common architectural mistake in early systems. Their data lifecycles, consistency requirements, and scaling bottlenecks are completely different, so they must be modeled separately.
2. Data Model Design
2.1 Content Entity and Versions
The content entity should store only immutable or rarely changed fields to avoid hot-row updates:
CREATE TABLE content (id BIGINT PRIMARY KEY,author_id BIGINT NOT NULL,type TINYINT NOT NULL, -- 1: article with images 2: video 3: short poststatus TINYINT NOT NULL, -- 0: draft 1: published 2: private 3: deletedcreated_at DATETIME NOT NULL,KEY idx_author_time (author_id, created_at));
The key point is that status and deletion use logical flags, and physical deletion happens only in archival jobs. This supports restoring content after withdrawal and prevents the main table from constantly developing gaps.
2.2 Feed Timeline
Do not try to serve both the profile page and the following feed from a single table. A profile page can be sharded and queried directly by author_id + created_at; a following feed should introduce a fan-out table:
feed(user_id, content_id, score, created_at) -- sharded by user_id
On publish, write to the author’s own inbox and fan out asynchronously to followers’ inboxes. For accounts whose follower count exceeds a threshold, switch to a pull-based merge: at read time, aggregate “the inboxes of high-profile accounts + the inboxes of ordinary follows.” This is the standard way to control write amplification.
2.3 Favorites Relationship
CREATE TABLE favorite (user_id BIGINT NOT NULL,content_id BIGINT NOT NULL,source TINYINT, -- source scenario of the favoritecreated_at DATETIME NOT NULL,PRIMARY KEY (user_id, content_id),KEY idx_user_time (user_id, created_at),KEY idx_content (content_id));
The composite primary key guarantees idempotence, and idx_content is used to count how many times an item has been favorited. Folder grouping for favorites can be split into a separate favorite_folder table to keep the main table from bloating.
3. Publish Pipeline: Reliability and Decoupling
Publishing must return quickly at the API layer while downstream processing stays asynchronous and reliable. The recommended flow:
- The access layer validates parameters and runs content safety review (text and images go through separate asynchronous review queues);
- Write to the primary database and generate a globally unique ID (a trend-increasing ID is better for sharding and sorting);
- Publish a domain event to the message queue;
- Consumers for counter updates, search indexing, recommendation features, and fan-out each subscribe independently.
The core principle here is do not do five things in one transaction. Event-driven design lets each downstream scale and replay independently. Consumers must be idempotent, typically by writing content_id + event type as the deduplication key into a dedup table.
4. Multi-List Aggregation and Query Optimization
A feed API often needs to return the following feed, trending feed, local feed, and favorited content at the same time. Joining multiple tables in real time on every request makes latency unpredictable. Instead:
- Maintain a separate read model for each list (a materialized view approach), updated by events;
- Return a unified structure from the lists so the frontend only renders, reducing the number of APIs;
- Use cursor-based pagination rather than page-number pagination to avoid the performance collapse of deep paging:
GET /feed?cursor=eyJ0IjoxNzEsImlkIjo4ODF9&limit=20
The cursor encodes the timestamp and primary key, and the server uses them to locate the position, which is naturally immune to duplicates and gaps caused by data insertion.
5. State Consistency and Counters
Counters such as like counts and favorite counts are not suitable for high-frequency updates in the primary database. A common approach is to store counters separately from content, merge them at read time, and allow eventual consistency within a short window. The display layer can use an “approximate value + threshold switch” strategy: show small values exactly and collapse large values into “12K,” which reduces precision pressure and matches reading habits.
For batch queries of “has this been favorited,” use a single batch API that takes a list of content IDs and returns a status bitmap in one query, avoiding N requests:
{ "ids": [101, 102, 103], "result": [1, 0, 1] }
6. Caching Strategy
Distinguish two cache categories: content details are strongly cache-friendly and can be cached by ID with a longer expiration; user-scoped data such as “my favorites list” is private, so the cache must be isolated per user and refreshed more promptly.
Three boundary problems must be handled:
- Cache penetration: cache empty values for nonexistent IDs with a short TTL;
- Cache breakdown: use mutex rebuild or logical expiration for hot keys;
- Cache avalanche: add random jitter to expiration times and prepare a degradation switch.
7. Capacity Governance and Hot/Cold Tiering
Content data only grows, so plan ahead. Strategies include archiving historical content to object storage by time and fetching it back on demand at query time; keeping only hot data in indexes and lists; separating favorites by user into hot and cold tiers, moving the favorites of long-inactive users to cold storage. Archival jobs must be re-entrant and verifiable to avoid accidental deletion.
8. Elastic Infrastructure
Mature general-purpose cloud services support all of the above: stateless access and business services run on a container platform with auto scaling for traffic peaks; the asynchronous pipeline relies on a message queue for peak shaving; archives and static assets live in object storage; and logging, monitoring, and alerting provide full-chain observability, with distributed tracing to locate slow queries. The general cloud capabilities offered by Baidu AI Cloud let teams focus on business modeling rather than infrastructure operations.
9. Launch Checklist
- Is the publish API’s P99 latency under control, and is review fully asynchronous;
- Can fan-out jobs be replayed, and are consumers idempotent;
- Is pagination unified to cursor mode;
- Are counter and state queries batched and merged;
- Do all three cache anomalies have fallbacks;
- Do archival and hot/cold tiering have verification and reconciliation jobs;
- Are key metrics (publish success rate, fan-out latency, favorite success rate) wired into monitoring and alerting.
Conclusion
The engineering difficulty of feeds and favorites lies not in any single technique but in modeling trade-offs and pipeline decoupling under read/write asymmetry. Treating write fan-out, relationship storage, counter consistency, and cold-data governance as four separate concerns significantly lowers system complexity and leaves a clean extension surface for adding recommendations and search later.
