← build log
feedpattern 08Feed architecture

Candidates, features, ranking: three jobs that must stay three services

Folding scoring into recommendation-service would save one hop and cost testability.

The problem

After candidate generation, a home feed request still needs feature hydration (interaction history, social distance, recency) and a scoring pass before it’s ranked. All three — gathering candidates, hydrating features, scoring — could live in one service, since recommendation-service already touches the request end to end. The question is whether collapsing them saves more than it costs.

Approaches considered

One service, inline scoring. recommendation-service loads the LightGBM model directly and scores candidates itself after hydration. Fewest hops, no gRPC call, one deploy for the whole pipeline.

Two services, feature hydration duplicated. Split scoring into ml-service, but let each candidate source also compute its own version of relevant features. Avoids a shared hydration contract but produces inconsistent feature values depending on which source a candidate came from.

Three concerns, three owners, one gRPC boundary. recommendation-service gathers candidates and hydrates a single consistent feature set; ml-service receives that feature set over gRPC and only scores.

Trade-offs

The three-service split costs a network hop and a serialization boundary that inline scoring wouldn’t need. It also means a scoring change and a candidate-source change can’t ship in the same deploy even when it might be convenient to bundle them.

The decision

Keep ml-service narrow: it accepts hydrated features over gRPC and returns scores, nothing else. Candidate generation and feature hydration stay entirely inside recommendation-service.

Why

Each concern changes for a different reason and on a different schedule — a new candidate source is a recommendation-service change, a new feature is a hydration change tied to what the current model expects, a new model architecture is purely an ml-service change. Collapsing them means any one of those changes risks the other two by accident. Keeping ml-service ignorant of Neo4j, Qdrant, and ClickHouse also means it can be tested and load-tested with synthetic feature vectors, with no dependency on the candidate pipeline being up at all — which matters more once training and deployment cadence differ between the two services.

Implementation

// libs/contracts/recommendation.proto — the boundary ml-service
// sees. No mention of graph, vector, or trending sources.
service ScoringService {
  rpc ScoreCandidates(ScoreRequest) returns (ScoreResponse);
}

message ScoreRequest {
  repeated CandidateFeatures candidates = 1;
}

message CandidateFeatures {
  string post_id = 1;
  double social_distance = 2;
  double interaction_score = 3;
  double recency_hours = 4;
}
// recommendation-service — hydrates the feature set, then calls
// ml-service purely as a scoring function.
func (r *RecommendationService) Rank(ctx context.Context, userID string, candidates []Candidate) ([]RankedPost, error) {
	features := r.hydrateFeatures(ctx, userID, candidates)

	resp, err := r.scoringClient.ScoreCandidates(ctx, &pb.ScoreRequest{
		Candidates: toProtoFeatures(features),
	})
	if err != nil {
		return nil, err
	}
	return rerank(candidates, resp.Scores), nil
}
# ml-service — receives feature vectors, knows nothing about
# where they came from.
def ScoreCandidates(self, request, context):
    features = np.array([
        [c.social_distance, c.interaction_score, c.recency_hours]
        for c in request.candidates
    ])
    scores = self.champion_model.predict(features)
    return ScoreResponse(scores=scores.tolist())