
News feed, timeline, fan-out, phân trangNews feeds, timelines, fan-out, pagination
Chín giờ ra mắtThe nine o'clock launch.Hãy hình dung một mạng xã hội Việt Nam lúc 9 giờ: Lan đăng ảnh Đà Lạt cho 300 follower, còn một ca sĩ công bố MV cho 2,1 triệu người. Cả hai đều mong bài xuất hiện nhanh trên bảng tin.Picture a Vietnamese social app at 9 AM: Lan shares a Da Lat photo with 300 followers while a singer announces a music video to 2.1 million. Both expect their posts to appear quickly in followers' feeds.
Theo chân ảnh Đà Lạt của Lan: Post Service lưu bài rồi CDC chuyển sự kiện qua Kafka để worker phân phối ID, không bắt Lan chờ từng follower. Khi bạn mở app, Feed Service lấy feed có sẵn, trộn bài người nổi tiếng rồi lấy nội dung và kiểm tra quyền xem trước khi trả về. Hãy thử các luồng bên dưới để thấy vì sao bài của ca sĩ không cần hàng triệu lần ghi, cursor giữ chỗ khi cuộn, và Ranking hết thời gian chờ vẫn không làm mất bảng tin.Follow Lan's Da Lat photo: the Post Service saves it, then CDC carries the event through Kafka so workers distribute IDs without making Lan wait for every follower. When you open the app, the Feed Service reads the precomputed feed, merges celebrity posts, and loads bodies with visibility checks before responding. Try the flows below to see why the singer's post avoids millions of writes, how a cursor holds your place, and how a Ranking timeout still leaves a usable feed.
1Lan (u-42) đăng ảnh cho 300 follower; request chỉ cần lưu bài bền vững, không chờ phân phối đến từng người.Lan (u-42) posts a photo for 300 followers; the request only needs to save it durably, without waiting for distribution to everyone.
Đẩy lúc ghi cho 2 triệu follower là 2 triệu ZADD và vài phút trễ. Mô hình lai: dưới ngưỡng (ví dụ 100 000 follower) thì đẩy; trên ngưỡng thì chỉ lưu vào timeline của tác giả và trộn lúc đọc. Người đọc theo dõi thường chỉ vài chục người nổi tiếng, nên trộn rẻ.Pushing to 2 million followers is 2 million ZADDs and minutes of delay. Use a hybrid: below a threshold (say 100,000 followers) push; above it store only in the author's timeline and merge on read. A reader usually follows only a few dozen celebrities, so the merge is cheap.
Post Store và Follow Graph là nguồn; feed trong Redis là bản chiếu dựng từ sự kiện, mất thì dựng lại được. Ghi qua Post Service, đọc qua Feed Service, hai đường mở rộng độc lập.The Post Store and Follow Graph are the source; the Redis feed is a projection built from events and can be rebuilt if lost. Writes go through the Post Service and reads through the Feed Service, and each scales on its own.
Bài mới chen vào đầu làm mọi vị trí dịch xuống. Dùng cursor (score, id): đọc score ≤ cursor, cùng score thì id nhỏ hơn. Với feed xếp hạng, chụp lại danh sách id đã xếp cho phiên (feed_session:{sid}, TTL 30 phút) rồi phân trang trên bản chụp.New posts at the top shift every position down. Use a (score, id) cursor: read score ≤ cursor and, on an equal score, smaller ids. For a ranked feed, snapshot the ranked id list per session (feed_session:{sid}, TTL 30 min) and page over the snapshot.
Feed chỉ chứa id và score, khoảng 50 byte mỗi phần tử; giới hạn 800 phần tử là khoảng 40 KB mỗi người. 50 triệu người hoạt động là khoảng 2 TB, nên bỏ feed của người không hoạt động 30 ngày và dựng lại khi họ quay lại. Nội dung bài lưu một lần, mọi feed trỏ tới cùng một bản.A feed holds only ids and scores, about 50 bytes an entry; an 800-entry cap is about 40 KB per user. 50 million active users is about 2 TB, so drop feeds of users inactive for 30 days and rebuild them on return. Post bodies are stored once and every feed points at the same copy.
Không xoá ngay khỏi hàng triệu feed. Đánh dấu tombstone trong Post Store, lọc khi lấy nội dung bài; job nền ZREM dần khỏi feed. Bài bị ẩn hoặc tác giả bị chặn được lọc theo danh sách riêng của từng người đọc.Never delete from millions of feeds synchronously. Mark a tombstone in the Post Store and filter at hydration time; a background job ZREMs it from feeds over time. Hidden posts and blocked authors are filtered per reader from their own lists.
Theo dõi mới: lấy 20 bài gần nhất của người đó từ Post Store và ZADD vào feed (backfill). Bỏ theo dõi: không quét feed ngay, lọc theo danh sách following khi đọc và để các phần tử cũ tự bị đẩy ra theo giới hạn 800.New follow: take the followee's 20 latest posts from the Post Store and ZADD them into the feed (backfill). Unfollow: do not scan the feed at once; filter by the following list on read and let old entries fall off with the 800 cap.
Lấy khoảng 3 lần số bài cần hiển thị làm ứng viên, chấm điểm theo độ mới, mức thân thiết với tác giả, tương tác dự đoán; cache điểm 5 phút. Ranking chậm hoặc lỗi thì trả theo thời gian, không làm hỏng trang.Take about three times the page size as candidates and score them on recency, affinity with the author and predicted engagement, caching scores for 5 minutes. If ranking is slow or fails, fall back to time order rather than breaking the page.
| BảngTable | Cột chínhKey columns | Ghi chúNotes |
|---|---|---|
posts_by_author (Cassandra) | author_id, post_id (Snowflake, clustering DESC), body, media, visibility, deleted | Partition theo tác giả; đọc 20 bài mới nhất của một người là một lần đọc partitionPartitioned by author; the 20 latest posts of one person are a single partition read |
feed:{uid} (Redis ZSET) | member = post_id, score = created_ms | Giới hạn 800 bằng ZREMRANGEBYRANK, bỏ sau 30 ngày không hoạt độngCapped at 800 with ZREMRANGEBYRANK, dropped after 30 inactive days |
posts:{celebrity_id} (Redis ZSET) | member = post_id, score = created_ms | Timeline của người nổi tiếng, 200 bài; đọc rất nhiều nên có replica và cache cục bộ vài giâyA celebrity's timeline of 200 posts; read heavily, so it has replicas and a local cache of a few seconds |
follow | follower_id, followee_id, created_at · follower_count trên user | Hai chỉ mục: theo follower (đang theo dõi ai) và theo followee (để fan-out)Two indexes: by follower (who I follow) and by followee (for fan-out) |
Lớp FeedService trong Java gộp hai hộp của sơ đồ: fanOut (@KafkaListener) là hộp Fan-out Workers, chọn đẩy vào feed:{uid} hay chỉ ghi posts:{author} theo số follower; page và read là đường đọc theo thời gian của hộp Feed Service. Bản Go chỉ có phía đọc: FeedService.Page đọc song song mọi nguồn bằng readAfter, trộn theo Cursor (score, id) và đọc hết nhóm trùng created_ms ở ranh giới. Mã lược bỏ bước lấy nội dung, lọc quyền xem, Ranking và bản chụp phiên như sơ đồ.The Java FeedService class covers two boxes in the diagram: fanOut (@KafkaListener) is the Fan-out Workers box, pushing into feed:{uid} or writing only posts:{author} depending on follower count; page and read are the chronological read path of the Feed Service box. The Go version shows only the read side: FeedService.Page reads every source in parallel with readAfter, merges by Cursor (score, id) and reads the whole created_ms tie group at the boundary. The code omits body loading, visibility filtering, Ranking and the session snapshot shown in the diagram.
package com.example.feed;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class FeedService {
private static final long CELEBRITY = 100_000;
private static final int CAP = 800;
private final StringRedisTemplate redis;
private final FollowGraph graph;
@KafkaListener(topics = "post.created")
public void fanOut(PostCreated e) {
if (graph.followerCount(e.authorId()) >= CELEBRITY) {
redis.opsForZSet().add("posts:" + e.authorId(), e.postId(), e.ts()); // merged on read
return;
}
for (List<Long> batch : graph.activeFollowers(e.authorId(), 1_000)) {
redis.executePipelined((RedisCallback<Object>) c -> {
for (Long f : batch) {
byte[] key = ("feed:" + f).getBytes();
c.zSetCommands().zAdd(key, e.ts(), e.postId().getBytes());
c.zSetCommands().zRemRange(key, 0, -CAP - 1); // keep newest 800
}
return null;
});
}
}
public FeedPage page(long userId, Cursor cur, int limit) {
List<Entry> merged = new ArrayList<>(read("feed:" + userId, cur, limit));
for (long celeb : graph.followedCelebrities(userId)) // usually < 20
merged.addAll(read("posts:" + celeb, cur, limit));
List<Entry> page = merged.stream()
.sorted(Entry.NEWEST_FIRST) // score desc, id desc
.distinct().limit(limit).toList();
return new FeedPage(page, page.isEmpty() ? null : Cursor.of(page.get(page.size() - 1)));
}
private List<Entry> read(String key, Cursor cur, int limit) {
List<Entry> out = new ArrayList<>();
if (cur != null) {
redis.opsForZSet().reverseRangeByScoreWithScores(key, cur.score(), cur.score())
.stream().map(Entry::of).filter(en -> en.isBefore(cur))
.limit(limit).forEach(out::add); // complete boundary group
}
if (out.size() == limit) return out;
double max = cur == null ? Double.POSITIVE_INFINITY : cur.score() - 1; // integer ms
redis.opsForZSet()
.reverseRangeByScoreWithScores(key, Double.NEGATIVE_INFINITY, max, 0, limit - out.size())
.stream().map(Entry::of).forEach(out::add);
return out;
}
}package main
import (
"context"
"fmt"
"strconv"
"github.com/redis/go-redis/v9"
"golang.org/x/sync/errgroup"
)
type Cursor struct {
Score float64
ID string
}
func (s *FeedService) Page(ctx context.Context, uid int64, cur *Cursor, limit int) ([]Entry, *Cursor, error) {
celebs, err := s.graph.FollowedCelebrities(ctx, uid)
if err != nil {
return nil, nil, err
}
keys := []string{fmt.Sprintf("feed:%d", uid)}
for _, c := range celebs {
keys = append(keys, fmt.Sprintf("posts:%d", c))
}
parts := make([][]Entry, len(keys))
g, gctx := errgroup.WithContext(ctx)
for i, k := range keys {
g.Go(func() error { // fan-out: one read per source
var err error
parts[i], err = s.readAfter(gctx, k, cur, limit)
return err
})
}
if err := g.Wait(); err != nil {
return nil, nil, err
}
page := mergeNewest(parts, limit) // fan-in: k-way merge by (score, id), dedup
if len(page) == 0 {
return page, nil, nil
}
last := page[len(page)-1]
return page, &Cursor{Score: last.Score, ID: last.ID}, nil
}
func (s *FeedService) readAfter(ctx context.Context, key string, cur *Cursor, limit int) ([]Entry, error) {
max := "+inf"
out := make([]Entry, 0, limit)
if cur != nil {
score := strconv.FormatFloat(cur.Score, 'f', -1, 64)
ties, err := s.rdb.ZRevRangeByScoreWithScores(ctx, key, &redis.ZRangeBy{Max: score, Min: score}).Result()
if err != nil { return nil, err }
for _, z := range ties {
e := Entry{ID: z.Member.(string), Score: z.Score}
if e.Before(*cur) { out = append(out, e) }
if len(out) == limit { return out, nil }
}
max = "(" + score // exclusive: boundary group was handled above
}
zs, err := s.rdb.ZRevRangeByScoreWithScores(ctx, key, &redis.ZRangeBy{
Max: max, Min: "-inf", Count: int64(limit - len(out)),
}).Result()
if err != nil { return nil, err }
for _, z := range zs {
out = append(out, Entry{ID: z.Member.(string), Score: z.Score})
}
return out, nil
}| Fan-out on write (push)Fan-out on write (push) | Fan-out on read (pull)Fan-out on read (pull) | |
|---|---|---|
| Chi phí đọcRead cost | Một ZREVRANGE, khoảng 1 msOne ZREVRANGE, about 1 ms | Đọc và trộn timeline của mọi người đang theo dõiRead and merge the timeline of everyone followed |
| Chi phí ghiWrite cost | Tỉ lệ với số follower, bùng nổ với người nổi tiếngProportional to followers; explodes for celebrities | Một lần ghi vào timeline tác giảOne write to the author's timeline |
| Bộ nhớMemory | Một feed cho mỗi người đọc, kể cả người ít mở appOne feed per reader, even for those who rarely open the app | Chỉ timeline tác giả, không có feed tính sẵnOnly author timelines, no precomputed feeds |
| Chọn khiPick it when | Tác giả có ít follower, người đọc mở app thường xuyênAuthors have few followers and readers open the app often | Tác giả có hàng triệu follower; kết hợp với push thành mô hình laiAuthors have millions of followers; combine with push as a hybrid |
Worker có thể chết sau khi ghi Redis nhưng trước khi commit offset Kafka, nên cùng sự kiện sẽ chạy lại. Dùng post_id làm member và created_ms gốc làm score cho ZADD, chỉ commit sau khi mọi lô thành công; retry có backoff và theo dõi tuổi sự kiện chưa xử lý để cảnh báo trước mốc 5 giây.A worker can crash after writing Redis but before committing its Kafka offset, so the same event runs again. Use post_id as the ZADD member and the original created_ms as its score, commit only after every batch succeeds, and retry with backoff while monitoring event age to alert before the 5-second target.
Chuyển sang pull tránh hàng triệu lần ghi nhưng posts:c-1 vẫn có thể nhận hàng chục nghìn lượt đọc mỗi giây trên một shard. Cache timeline cục bộ 1–2 giây, gộp các lần tải đồng thời và đo QPS cùng p99 theo key; chấp nhận độ trễ cache trong ngân sách xuất hiện bài dưới 5 giây.Pull avoids millions of writes, but posts:c-1 can still receive tens of thousands of reads per second on one shard. Cache the timeline locally for 1–2 seconds, coalesce concurrent fetches, and measure per-key QPS and p99 while keeping cache staleness within the 5-second delivery budget.
Nếu hơn 40 bài cùng mili giây, lấy limit + 20 rồi lọc theo ID có thể trả trang rỗng dù vẫn còn bài cũ hơn. Đọc hết nhóm cùng score tại ranh giới rồi lấy phần có score thấp hơn, hoặc dùng chỉ mục hỗ trợ cursor ghép; kiểm tra cả trường hợp mọi ID trong trang có cùng timestamp.If more than 40 posts share a millisecond, fetching limit + 20 and filtering by ID can return an empty page while older posts remain. Read the entire boundary score group before fetching lower scores, or use an index supporting composite keyset pagination; test a page whose IDs all share one timestamp.
Khi Ranking vượt ngân sách 30 ms, trả theo thời gian nhưng vẫn phải lọc bài xoá, bài riêng tư, tác giả bị chặn và quan hệ đã bỏ theo dõi. Vô hiệu hoá cache nội dung khi quyền xem đổi, lấy bù ứng viên bị lọc và ghi metric ranking_fallback_total để trang nhanh không che khuất lỗi kéo dài.When Ranking exceeds its 30 ms budget, return time order while still filtering deleted posts, private posts, blocked authors, and unfollowed accounts. Invalidate body caches on visibility changes, refill filtered candidates, and record ranking_fallback_total so fast pages do not hide a persistent outage.
Mỗi pattern mở bài học tương ứng trong menu Pattern.Each one opens its lesson in the Patterns menu.
Cân giữa thời gian fan-out và chi phí trộn lúc đọc. Với 1 000 ZADD mỗi lô mất vài mili giây, 100 000 follower là vài trăm mili giây, chấp nhận được. Trên ngưỡng đó thì pull. Ngưỡng là cấu hình, đo bằng độ trễ fan-out p99 và số timeline trung bình phải trộn.Balance fan-out time against read-time merge cost. At a few milliseconds per 1,000-ZADD batch, 100,000 followers take a few hundred milliseconds, which is acceptable. Above that, pull. The threshold is configuration, tuned on p99 fan-out delay and the average number of timelines to merge.
Dựng lại theo kiểu pull: lấy danh sách following, đọc vài bài mới nhất của mỗi người từ Post Store, trộn và ghi vào feed:{uid}. Trang đầu có thể chậm hơn (200–500 ms); các bài sau đó lại được đẩy như bình thường.Rebuild by pulling: get the following list, read a few latest posts per followee from the Post Store, merge them and write feed:{uid}. The first page may be slower (200–500 ms); later posts are pushed as usual.
Một bài nằm trong hàng nghìn feed; lưu nội dung sẽ nhân bộ nhớ lên hàng nghìn lần và sửa hoặc xoá bài phải cập nhật hàng nghìn bản. Lưu id thì nội dung có một bản, cache theo post_id, lấy bằng multi-get khi hiển thị.One post sits in thousands of feeds; storing bodies would multiply memory by thousands and make every edit or delete touch thousands of copies. With ids, the body exists once, is cached by post_id and is loaded with a multi-get at display time.
Điểm xếp hạng thay đổi giữa các lần gọi nên cursor (score, id) không còn ổn định. Xếp hạng một lần cho khoảng 200 ứng viên, lưu danh sách id vào feed_session:{sid} với TTL 30 phút; cursor là vị trí trong bản chụp đó. Hết bản chụp thì xếp hạng tiếp phần cũ hơn.Ranking scores change between calls, so a (score, id) cursor is no longer stable. Rank about 200 candidates once and store the id list in feed_session:{sid} with a 30-minute TTL; the cursor is a position in that snapshot. When it runs out, rank the next older slice.
Phím 1–4 để chọn đáp án, Enter để kiểm tra.Press 1–4 to choose an answer, Enter to check.
Ôn tập ngắt quãng (spaced repetition): thẻ được lặp lại sau 1 → 3 → 7 → 14 → 30 ngày để củng cố trí nhớ dài hạn.Spaced repetition: cards are repeated after 1 → 3 → 7 → 14 → 30 days to build long-term memory.