Lesson 61 — Mini-project: Blog Backend

Tổng kết Tier 5: Postgres (CRUD + transaction) · Redis cache · full-text search · migration — sau một interface swap được sang DB thật.

Module 1 — Kiến trúc (bấm vào từng tầng)

Service chỉ phụ thuộc interface của 4 tầng. Bấm từng hộp để xem tầng đó làm gì và có thể swap sang backend thật nào.

handler — lớp HTTP (request → JSON)
service — LOGIC (cache-aside · index · transaction · invalidation)
(qua interface)
storage
Repository
cache
Cache + TTL
search
InvertedIndex
migration
version tracker
Bấm một tầng để xem chi tiết.

Module 2 — Request flow

Mô phỏng hai luồng quan trọng nhất. Bấm nút để chạy từng bước.

2a. Create post → write storage + index search

1. validate — title không rỗng?
2. repo.CreatePost — gán id, lưu storage (Postgres)
3. index.AddOrUpdate — tokenize title+body → posting list
4. (không invalidate) — post mới chưa từng được cache
→ 201 Created

2b. Get post → cache-aside (cache → miss → storage → populate)

hits=0 · misses=0
cache.Get("post:3")
repo.GetPost(3) — chỉ chạy khi MISS
cache.Set("post:3", ttl=5m) — populate (chỉ sau MISS)
→ trả post

Lần 1: cache trống → MISS → xuống storage → nạp cache. Lần 2: HIT → không chạm storage.

Module 3 — Search demo (inverted index + TF-IDF)

Corpus 4 bài (N=4). Nhập từ khoá → tra inverted index → tính điểm TF-IDF → xếp hạng.

idtitlebody (rút gọn)tags
1Go concurrency patternsgoroutines channels concurrentgo, concurrency
2Postgres indexing tipsbtree index query writepostgres, database
3Redis cache strategiescache aside pattern read heavyredis, cache
4Full text search inverted indexinverted index term document tfidfsearch, database
Thử nhanh: