← build log
feedpattern 07Feed architecture

Fan-out-on-read: the home feed's real-time scatter/gather

There's no timeline to precompute when relevance depends on what's true right now.

The problem

The home feed ranks content by relevance — social proximity, semantic similarity, trending momentum, freshness — rather than by recency alone. Relevance isn’t a fixed property of a post the way “who posted it” is; it depends on inputs only known at the moment of the request, like what the user just engaged with in this session. The following feed’s trick — precompute the answer when a post is written, read it cheaply later — doesn’t have anything to precompute here, because the answer changes with every request even if no new posts exist.

Approaches considered

Periodic batch ranking. Recompute a ranked list per user on a schedule — hourly, say — and serve it from a cache, the same shape as the following feed. This is cheap to read but stale by definition, and stale is exactly what “relevant right now” can’t be.

Single-source ranking at read time. Rank based on one signal — trending, say — computed fresh per request. Cheap and fast, but a home feed built on one signal is a trending page wearing a different name, not a personalized feed.

Fan-out-on-read across multiple sources. At request time, query several independent candidate sources in parallel, merge, and rank the combined pool.

Trade-offs

Every home feed request now costs real work: several network calls, a merge step, feature hydration, a scoring call. There’s no equivalent of the following feed’s single Redis lookup — the cheapness the following feed enjoys was never available to a feed that has to be current.

The decision

recommendation-service fans out in parallel to four candidate sources on every home feed request: graph-service over Neo4j for social proximity, Qdrant directly for embedding similarity, ClickHouse for trending momentum, and a raw pool for freshness.

Why

Four independent, uncorrelated sources cover different failure and staleness modes that no single source can — graph relationships change slowly and predictably, trending scores shift by the hour, embeddings capture similarity trending can’t see. Running them in parallel rather than sequentially is the specific reason recommendation-service is written in Go: goroutines make “four calls at once, proceed with whatever’s back by the deadline” cheap to express, where a sequential implementation would pay the sum of four latencies instead of the max.

Implementation

// recommendation-service — four candidate sources queried in
// parallel; a slow one degrades the result, it doesn't block it.
func (r *RecommendationService) GatherCandidates(ctx context.Context, userID string) ([]Candidate, error) {
	var wg sync.WaitGroup
	results := make(chan []Candidate, 4)

	sources := []func(context.Context, string) ([]Candidate, error){
		r.graphCandidates,       // graph-service — social proximity
		r.vectorCandidates,      // Qdrant, direct — semantic similarity
		r.trendingCandidates,    // ClickHouse — recent momentum
		r.explorationCandidates, // freshness pool
	}

	ctx, cancel := context.WithTimeout(ctx, 150*time.Millisecond)
	defer cancel()

	for _, source := range sources {
		wg.Add(1)
		go func(fetch func(context.Context, string) ([]Candidate, error)) {
			defer wg.Done()
			if cands, err := fetch(ctx, userID); err == nil {
				results <- cands
			}
		}(source)
	}

	go func() { wg.Wait(); close(results) }()

	var merged []Candidate
	for cands := range results {
		merged = append(merged, cands...)
	}
	return dedupe(merged), nil
}