← build log
reliabilitypattern 03Reliability & contracts

One writer per store: why ingestion-service owns all of ClickHouse

Five services could each write their own analytics rows. Only one of them should.

The problem

Vitrin’s ClickHouse cluster holds every analytics-relevant event — post creation, likes, comments, feed impressions, scroll depth, library ratings — feeding both trending queries and the ML training pipeline. Half a dozen services produce those events. The naive design lets each one write directly: post-service inserts its own post.created row, feed-service inserts its own feed.served row, library-service inserts its own library.rated row.

Approaches considered

Direct writes from every producer. Fewest hops, least code per service. Also five independent opinions about column names and types, five different retry/backoff behaviors hitting the same cluster, and no single place to add deduplication if any of them redelivers.

Shared client library, still multiple writers. A common libs/clickhouse-client package standardizes the insert calls. It fixes the column-naming problem but not the ownership problem — five services still hold ClickHouse credentials, and a schema migration still means coordinating five deploys.

Single-writer service. One service consumes all analytics events from RabbitMQ and is the only one with ClickHouse credentials at all.

Trade-offs

Single-writer adds a network hop and a queue between every producer and the data actually landing — post.created isn’t queryable in ClickHouse the instant it’s inserted into Postgres, it’s queryable after RabbitMQ delivers it and ingestion-service processes it. For trending queries and training datasets, that lag is fine; for anything wanting the write to be visible to itself immediately, it wouldn’t be.

The decision

ingestion-service is the only service with ClickHouse credentials. Every producer publishes to RabbitMQ and stops caring what happens next.

Why

Ownership, not hops, was the actual constraint. With one writer, there’s one schema definition, one place enforcing the inbox dedup from the previous post, and one deploy to coordinate for a migration. With five writers, “what does the posts_events table actually look like” stops having one answer, and nobody can add deduplication without touching all five. The hop is a fixed, known cost; schema drift across five independently-deployed producers is not.

Implementation

// ingestion-service — the only process in the system holding
// ClickHouse write credentials. Everything else only knows RabbitMQ.
func main() {
	ch := clickhouse.MustConnect(os.Getenv("CLICKHOUSE_ADDR"))
	inbox := NewInbox(ch)

	consumer := NewConsumer(rabbitConn, inbox, ch)
	consumer.Bind("post.created", handlePostCreated)
	consumer.Bind("post.liked", handlePostLiked)
	consumer.Bind("post.commented", handlePostCommented)
	consumer.Bind("feed.served", handleFeedServed)
	consumer.Bind("feed.scrolled", handleFeedScrolled)
	consumer.Bind("library.rated", handleLibraryRated)

	consumer.Run(context.Background())
}

// post-service, feed-service, library-service — never import a
// ClickHouse client. They only ever do this:
func publishPostCreated(ch *amqp.Channel, post Post) error {
	body, _ := json.Marshal(PostCreatedEvent{
		EventID:  uuid.NewString(),
		PostID:   post.ID,
		AuthorID: post.AuthorID,
	})
	return ch.Publish("domain-events", "post.created", false, false, amqp.Publishing{
		Body: body,
	})
}