Building Dynamic Feeds and Favorites: Technical Practices for Content Platforms
This article breaks down the technical implementation of two core features in content platforms: dynamic activity feeds and favorites. It covers data modeling, index design, asynchronous writes, count consistency, hot and cold data tiering, and capacity planning. Backend and architecture developers will find practical guidance on separating strongly consistent state checks from eventually consistent fan-out and statistics.
In content platforms, “dynamic feeds” and “favorites” may look like two simple buttons, but they are foundational infrastructure for user retention and content distribution. This article breaks down both capabilities from the ground up, covering data modeling, index design, asynchronous writes, count consistency, hot and cold tiering, and capacity planning. It is written for backend and architecture developers.
1. Problem Definition: What Feeds and Favorites Actually Solve
A “dynamic feed” is usually a lightweight, time-ordered stream of user actions, such as publishing, liking, commenting, and following. “Favorites” are a retrievable index that users build over content, emphasizing stability, searchability, and bulk management. Both are write-heavy and read-heavy, strongly time-series in nature, and layered in their consistency requirements: counters can tolerate eventual consistency, while checks like “have I already favorited this” must be strongly consistent.
From a system perspective, four problems must be solved at once: write throughput and peak shaving; low read latency and stable pagination; consistency of counts and states; and storage cost plus hot and cold tiering under massive data volumes.
2. Data Model Design
Model “behavior events” and “relationship states” separately. Behavior events are append-only logs; relationship states are the current snapshot.
-- Behavior event table (append-only, partitioned by time)CREATE TABLE user_event (id BIGINT PRIMARY KEY AUTO_INCREMENT,user_id BIGINT NOT NULL,target_type TINYINT NOT NULL, -- 1:content 2:usertarget_id BIGINT NOT NULL,action_type TINYINT NOT NULL, -- 1:publish 2:like 3:favoritecreated_at DATETIME NOT NULL,KEY idx_user_time (user_id, created_at));-- Favorite relationship table (strongly consistent state)CREATE TABLE favorite_rel (user_id BIGINT NOT NULL,target_id BIGINT NOT NULL,created_at DATETIME NOT NULL,PRIMARY KEY (user_id, target_id),KEY idx_target (target_id));
The key point: the event table generates the feed and supports auditing, while the relationship table drives checks and lists. Both are triggered by the same business action, with a write order of relationship first, event second, and compensation on failure.
3. Write Path: Synchronous Check Plus Asynchronous Fan-Out
A favorite action must be “immediately queryable,” so the relationship write must complete synchronously. Feed fan-out, count updates, and notification delivery can be made asynchronous, using a message queue to decouple and shave peaks.
def favorite(user_id, target_id):# 1. Idempotent relationship write (relies on primary key conflict ignore)insert_ignore("favorite_rel", user_id=user_id, target_id=target_id)# 2. Publish domain event for asynchronous fan-out and countingmq.publish("topic.favorite", {"user_id": user_id, "target_id": target_id, "ts": now_ms()})return {"ok": True}
The consumer handles three things: appending the user’s dynamic feed entry, incrementally updating the content’s favorite count, and writing cache invalidation markers. Consumer logic must be idempotent, ideally implemented with a “unique event key plus dedup table” or “version number comparison.”
4. Read Path: Stable Pagination for Feeds
The most common feed defect is duplicate or missing items across pages, caused by offset-based pagination while new data keeps being inserted. Cursor-based pagination is recommended, using a composite cursor of timestamp and primary key.
SELECT id, target_id, action_type, created_atFROM user_eventWHERE user_id = ? AND (created_at, id) < (?, ?)ORDER BY created_at DESC, id DESCLIMIT 20;
For highly active users, an “inbox and outbox” dual-write model can be introduced: the publisher writes to their own outbox and asynchronously fans out to followers’ inboxes. On read, the inbox is checked first, falling back to source aggregation on a miss, balancing latency and cost.
5. Count and State Consistency
Do not put full validation of counts in the same strongly consistent transaction as the relationship write. Instead, use “incremental messages plus scheduled reconciliation.” The reconciliation job periodically compares the aggregated value from the relationship table against the count cache, and corrects deviations using the relationship table as the source of truth. Check-type reads can hit the cache first, falling back to the relationship table on a miss and backfilling. Cache keys must include a composite dimension of user and target to prevent unauthorized access and cross-wiring.
6. Capacity, Hot and Cold Tiering, and Cost
Feed and favorite data only grows, so tiering must be planned: keep hot data from the most recent months in high-performance storage, archive historical data to object storage, and rebuild indexes via offline jobs. The relationship table can be sharded by user dimension to avoid single-point hotspots. If using general cloud capabilities, you can combine a container platform for stateless services, a message queue for asynchronous decoupling, object storage for archiving, and logging plus monitoring and alerting for observability. Using general categories is enough to meet elasticity and cost goals.
7. Observability and Drills
Before launch, cover three types of validation: idempotency testing (repeated requests produce no duplicate feed entries), consistency drills (count convergence under message delay and duplicate delivery), and capacity load testing (peak writes and deep pagination reads). Monitoring metrics should include at least write failure rate, consumer backlog, cache hit rate, reconciliation deviation count, and deep pagination latency. When deviations appear, prioritize correct checks first, then repair counts asynchronously.
In short, the engineering essence of feeds and favorites is to layer “strongly consistent state checks” separately from “eventually consistent fan-out and statistics,” and to build a stable, scalable content management loop with a clear data model, idempotent writes, cursor-based reads, and reconcilable counts.
