← Del monolito a Kafka + ClickHouse: cómo rediseñé un analytics con millones de eventos por día
Del monolito a Kafka + ClickHouse: cómo rediseñé un analytics con millones de eventos por día
Español ↔ English
El cuello de botella no era el dashboard
Cuando un dashboard se vuelve lento, la primera reacción suele ser culpar a la consulta, al frontend o al índice que supuestamente falta.
En VMageste, el problema era más estructural.
La plataforma necesitaba consolidar datos de Meta, Google y TikTok para múltiples clientes. El volumen llegó al orden de millones de eventos por día, mientras la misma base de datos también respondía por las rutinas transaccionales del producto.
La cuenta dejó de cerrar por una razón simple: le estaba pidiendo al mismo componente resolver dos tipos de carga que quieren cosas muy diferentes.
PostgreSQL funcionaba muy bien para autenticación, configuración, usuarios, tenants, integraciones y estados transaccionales. Pero las agregaciones continuas sobre grandes volúmenes de eventos de marketing empezaron a competir con ese trabajo.
El dashboard lento era el síntoma. La disputa entre OLTP y analytics era la enfermedad.
La arquitectura que funcionaba con menos volumen dejó de funcionar
Al principio, un flujo síncrono parecía perfectamente razonable:
API externa
↓
Node.js
↓
normalización
↓
PostgreSQL
↓
consulta agregada
↓
dashboard
Esa arquitectura tiene una ventaja enorme: es fácil de entender.
Sigo defendiendo empezar simple cuando la escala todavía no justifica distribución.
El problema apareció cuando tres cosas crecieron al mismo tiempo:
- más cuentas conectadas;
- más eventos ingeridos;
- más consultas analíticas concurrentes.
La escritura quería throughput. El dashboard quería lecturas rápidas. Lo transaccional quería baja latencia y previsibilidad.
Cuando todo pasa por el mismo camino, optimizar una punta empieza a perjudicar otra.
Ahí fue cuando la arquitectura tuvo que cambiar.
La separación más importante fue entre evento, procesamiento y consulta
La versión simplificada de la arquitectura hacia la que empecé a moverme fue esta:
┌──────────────────────┐
Meta ────────→│ │
Google ──────→│ capa de ingestión │
TikTok ──────→│ │
└──────────┬───────────┘
↓
Apache Kafka
↓
┌──────────────────────┐
│ consumers en Go │
│ normalizan │
│ deduplican │
│ enriquecen │
└───────┬──────────────┘
│
┌──────────┴───────────┐
↓ ↓
PostgreSQL ClickHouse
estado del producto eventos / analytics
│ │
└──────────┬───────────┘
↓
Dashboard
Kafka no entró para hacer que el diagrama se viera más sofisticado.
Entró para desacoplar la velocidad con la que llegan los datos de la velocidad con la que puedo procesarlos.
Esa diferencia es fundamental.
Si una API externa entrega una ráfaga, el resto de la plataforma no tiene que acompañarla exactamente en ese instante. El evento puede esperar en el log hasta que un consumer tenga capacidad.
Un pico de tráfico se convierte en backlog observable en lugar de timeouts dispersos.
Kafka se volvió un amortiguador, no la base de datos principal
Un error común en arquitectura orientada a eventos es empezar a tratar el broker como solución universal.
Prefiero darle un papel más específico:
- el productor publica un hecho;
- Kafka preserva y distribuye ese hecho;
- los consumers ejecutan trabajo;
- bases especializadas guardan el estado que después será consultado.
La documentación de Apache Kafka describe justamente la propiedad que me interesaba: dentro de un consumer group, las particiones se reparten entre consumers, lo que permite paralelizar el procesamiento sin que todos procesen el mismo registro.
Conceptualmente:
topic: ad-events
partition 0 ─→ consumer A
partition 1 ─→ consumer B
partition 2 ─→ consumer C
partition 3 ─→ consumer D
El detalle importante es que el límite de paralelismo no es solamente la cantidad de procesos que puedo levantar; también es la cantidad de particiones disponibles para ese grupo.
Eso cambia la forma de dimensionar.
Levantar veinte consumers para un topic con diez particiones no crea veinte workers útiles dentro del mismo consumer group.
La clave de partición es una decisión de dominio
Es tentador elegir la clave que mejor distribuya la carga. Pero distribución no es el único requisito.
Kafka preserva el orden dentro de una partición. Por eso la clave también determina qué eventos necesitan mantener una relación temporal entre sí.
En un sistema multi-tenant de analytics, una estrategia simplificada podría ser:
const key = `${tenantId}:${adAccountId}`
Eso no significa que haya sido literalmente la clave de todos los flujos de VMageste. Es el tipo de razonamiento que uso: equilibrar distribución y localidad lógica.
Si particiono solo por tenantId, un cliente enorme puede concentrar tráfico. Si particiono al azar, gano distribución, pero puedo complicar procesamientos que dependen del orden por cuenta.
Ese tipo de decisión no aparece en un benchmark de frameworks. Es arquitectura de dominio.
El evento necesitaba suficiente contexto
Otro aprendizaje fue dejar de publicar mensajes demasiado pobres.
Un evento como:
{
"id": "123"
}
parece elegante hasta que el consumer necesita tres round trips para descubrir qué significa.
En el otro extremo, copiar la entidad completa crea payload innecesario y acoplamiento.
El equilibrio que prefiero es un envelope estable:
type MarketingEvent<T> = {
eventId: string
eventType: string
tenantId: string
source: 'meta' | 'google' | 'tiktok'
occurredAt: string
schemaVersion: number
payload: T
}
Cuatro campos hacen una diferencia enorme en operación:
eventId, para idempotencia y trazabilidad;tenantId, para aislamiento y observabilidad;occurredAt, porque el tiempo de negocio y el tiempo de procesamiento no son iguales;schemaVersion, porque los contratos también envejecen.
"Exactly once" no reemplaza la idempotencia de negocio
En sistemas distribuidos, prefiero asumir que un mensaje puede procesarse de nuevo.
Eso cambia la pregunta de:
¿cómo garantizo que este evento jamás se repita?
por:
si este evento llega dos veces, ¿cómo garantizo que el resultado siga siendo correcto?
Un consumer puede registrar un identificador de procesamiento, usar claves naturales o implementar upserts según el caso.
Un patrón simplificado:
if (await alreadyProcessed(event.eventId)) {
return
}
await transaction(async () => {
await applyEvent(event)
await markAsProcessed(event.eventId)
})
El código real puede ser distinto y el costo de esa tabla debe considerarse. El principio es más importante: el reprocesamiento debe estar previsto, no tratarse como un accidente imposible.
Por qué Go entró en los consumers
El backend principal estaba fuertemente basado en Node.js y NestJS. No reescribí todo en Go.
Usé Go donde el perfil del servicio lo justificaba: workers pequeños, concurrentes, con poca superficie HTTP y mucha responsabilidad de ingestión y transformación.
Para mí esa distinción es importante.
Elegir otro lenguaje no necesita ser una declaración ideológica. Puede ser simplemente una frontera.
El producto mantuvo partes relevantes en Node/Nest; los consumers de alto throughput ganaron un runtime muy cómodo para concurrencia y binarios simples de operar.
El costo de un segundo lenguaje solo vale la pena cuando la frontera está clara. Si cada servicio usa una stack distinta por gusto personal, el sistema se convierte en un zoológico.
ClickHouse resolvió un problema diferente de PostgreSQL
Reemplazar PostgreSQL por ClickHouse en todo habría sido otro error.
Lo que necesitaba era dejar de pedirle a la base transaccional que también se comportara como motor analítico.
ClickHouse es una base columnar orientada a analytics. Eso importa porque una consulta de dashboard suele leer pocas columnas de muchas filas y hacer agregaciones.
Una tabla simplificada podría seguir esta lógica:
CREATE TABLE ad_events
(
tenant_id String,
source LowCardinality(String),
campaign_id String,
occurred_at DateTime,
impressions UInt64,
clicks UInt64,
spend Decimal(18, 4)
)
ENGINE = MergeTree
ORDER BY (tenant_id, source, occurred_at, campaign_id);
No copies ese ORDER BY a producción sin entender tus patrones de consulta. En ClickHouse, el diseño físico debe acompañar las queries que quieres acelerar.
El punto es otro: los datos analíticos pasaron a vivir donde la agregación a gran volumen es una preocupación de primera clase.
PostgreSQL pudo volver a hacer lo que mejor hacía dentro del producto.
Strangler Fig evitó la reescritura heroica
No quería un viernes en el que alguien apretara un botón y todo el tráfico cambiara del sistema viejo a la arquitectura nueva.
La migración fue incremental, alineada con el patrón Strangler Fig: crear caminos nuevos junto a los existentes, mover partes gradualmente y observar cada transición.
Eso permitió:
- migrar un flujo por vez;
- comparar resultados;
- revertir una etapa específica;
- seguir entregando producto mientras cambiaba la arquitectura.
Una reescritura total se ve limpia en la pizarra. En un sistema vivo, concentra riesgo.
La modernización incremental distribuye ese riesgo en el tiempo y, sobre todo, lo vuelve medible.
El backlog es una métrica de salud
Cuando existe una cola, "está funcionando" deja de ser binario.
Un consumer puede estar vivo y aun así perder la carrera contra la entrada de eventos.
Por eso empecé a mirar cosas como:
- lag por consumer group;
- tasa de producción;
- tasa de consumo;
- tiempo medio por evento;
- errores por tipo;
- retries;
- dead letters, cuando corresponde;
- edad del evento más antiguo todavía no procesado.
Imagina dos escenarios:
entrada: 2.000 eventos/s
salida: 2.100 eventos/s
El backlog tiende a bajar.
Ahora:
entrada: 2.000 eventos/s
salida: 1.600 eventos/s
Nada "se cayó", pero el incidente ya empezó.
Ese es uno de los grandes cambios mentales de los sistemas asíncronos: también monitoreas la distancia entre el presente y el punto que el sistema logró terminar de procesar.
Lo que haría antes de agregar más microservicios
Después de vivir este tipo de migración, me volví más conservador con la distribución.
No empezaría un producto nuevo con esta arquitectura solo porque "puede crecer".
Antes de romper un monolito intentaría responder:
- ¿existe carga realmente independiente?
- ¿hay un cuello de botella que no puedo aislar dentro del proceso actual?
- ¿los equipos necesitan desplegar partes a ritmos distintos?
- ¿la falla de un flujo necesita quedar contenida?
- ¿se justifica el costo operacional de colas, schemas, retries y observabilidad?
En VMageste, la respuesta pasó a ser sí porque el volumen y el perfil de las cargas lo exigieron.
En muchos SaaS iniciales, la respuesta sigue siendo no.
La principal lección: escalar es una propiedad del flujo
La experiencia hizo que dejara de hablar de "un sistema que escala" como si fuera una sola cualidad.
¿Qué escala?
- ¿la ingestión?
- ¿la transformación?
- ¿la escritura?
- ¿la agregación?
- ¿la lectura del dashboard?
- ¿el aislamiento entre tenants?
Cada tramo tiene un límite diferente y, muchas veces, una tecnología distinta es apropiada para cada uno.
Kafka, Go y ClickHouse fueron importantes. Pero la decisión que realmente cambió el sistema ocurrió antes de elegir herramientas:
separar el camino de ingestión del camino analítico y dejar que cada componente asuma una responsabilidad que realmente pueda sostener.
Esa es la parte de la arquitectura que llevaría a cualquier otro producto de datos, aunque la stack elegida fuera completamente distinta.
Referencias
The bottleneck was not the dashboard
When a dashboard gets slow, the first reaction is usually to blame the query, the frontend, or the index that is supposedly missing.
In VMageste, the problem was more structural.
The platform had to consolidate Meta, Google and TikTok data for multiple customers. The volume grew to millions of events per day, while the same database was also responsible for the transactional routines of the product.
The equation stopped making sense for a simple reason: I was asking the same component to handle two workloads that want very different things.
PostgreSQL was a great fit for authentication, configuration, users, tenants, integrations and transactional state. But continuous aggregations over large volumes of marketing events started competing with that work.
The slow dashboard was the symptom. OLTP and analytics fighting over the same resources was the actual problem.
The architecture that worked at a smaller scale stopped working
At first, a synchronous flow looked perfectly reasonable:
external API
↓
Node.js
↓
normalization
↓
PostgreSQL
↓
aggregated query
↓
dashboard
That architecture has one huge advantage: it is easy to understand.
I still believe in starting simple when scale has not yet justified distribution.
The problem appeared when three things grew at the same time:
- more connected accounts;
- more ingested events;
- more concurrent analytical queries.
Writes wanted throughput. The dashboard wanted fast reads. Transactional operations wanted low latency and predictability.
When everything shares the same path, optimizing one end starts hurting another.
That was the point where the architecture had to change.
The most important separation was between event, processing and query
The simplified architecture I started moving toward looked like this:
┌──────────────────────┐
Meta ────────→│ │
Google ──────→│ ingestion layer │
TikTok ──────→│ │
└──────────┬───────────┘
↓
Apache Kafka
↓
┌──────────────────────┐
│ Go consumers │
│ normalize │
│ deduplicate │
│ enrich │
└───────┬──────────────┘
│
┌──────────┴───────────┐
↓ ↓
PostgreSQL ClickHouse
product state events / analytics
│ │
└──────────┬───────────┘
↓
Dashboard
Kafka was not added to make the diagram look more sophisticated.
It was added to decouple the speed at which data arrives from the speed at which I can process it.
That distinction matters a lot.
If an external API delivers a burst, the rest of the platform does not need to keep up at that exact moment. The event can wait in the log until a consumer has capacity.
A traffic spike becomes observable backlog instead of scattered timeouts.
Kafka became a buffer, not the primary database
A common mistake in event-driven architecture is turning the broker into a universal solution.
I prefer giving it a narrower responsibility:
- a producer publishes a fact;
- Kafka preserves and distributes that fact;
- consumers perform work;
- specialized databases keep the state that will later be queried.
The Apache Kafka documentation describes the property I cared about: within a consumer group, partitions are distributed among consumers, which allows processing to scale without every process handling the same record.
Conceptually:
topic: ad-events
partition 0 ─→ consumer A
partition 1 ─→ consumer B
partition 2 ─→ consumer C
partition 3 ─→ consumer D
The important detail is that the parallelism ceiling is not just how many processes I can start; it is also how many partitions are available to that consumer group.
That changes capacity planning.
Starting twenty consumers for a topic with ten partitions does not create twenty useful workers inside the same consumer group.
The partition key is a domain decision
It is tempting to choose whichever key produces the most even distribution. Distribution, however, is not the only requirement.
Kafka preserves ordering inside a partition. The key therefore also determines which events need to preserve temporal locality with one another.
For a multi-tenant analytics system, a simplified strategy might look like this:
const key = `${tenantId}:${adAccountId}`
That does not mean this was literally the key used in every VMageste flow. It illustrates the reasoning I use: balance distribution with logical locality.
Partitioning only by tenantId can let one huge customer dominate a partition. Random partitioning improves distribution but may make workflows that depend on account-level ordering harder.
You will not find that trade-off in a framework benchmark. It is a domain architecture decision.
The event needed enough context to stand on its own
Another lesson was to stop publishing messages that were too poor in context.
An event like this:
{
"id": "123"
}
looks elegant until the consumer needs three round trips just to discover what it means.
At the other extreme, copying an entire entity creates unnecessary payload and coupling.
The balance I prefer is a stable envelope:
type MarketingEvent<T> = {
eventId: string
eventType: string
tenantId: string
source: 'meta' | 'google' | 'tiktok'
occurredAt: string
schemaVersion: number
payload: T
}
Four fields make an enormous operational difference:
eventId, for idempotency and tracing;tenantId, for isolation and observability;occurredAt, because business time and processing time are not the same;schemaVersion, because contracts age too.
"Exactly once" does not replace business idempotency
In distributed systems, I prefer assuming that a message can be processed again.
That changes the question from:
how do I guarantee this event is never repeated?
into:
if this event arrives twice, how do I guarantee the result is still correct?
A consumer can record a processing identifier, rely on natural keys or use upserts depending on the case.
A simplified pattern:
if (await alreadyProcessed(event.eventId)) {
return
}
await transaction(async () => {
await applyEvent(event)
await markAsProcessed(event.eventId)
})
Real code may differ, and the cost of a processed-events table has to be considered. The principle matters more: reprocessing should be designed for, not treated as an impossible accident.
Why Go entered the consumer layer
The main backend was heavily based on Node.js and NestJS. I did not rewrite everything in Go.
I used Go where the service profile made sense: small concurrent workers with very little HTTP surface and a lot of ingestion and transformation work.
That distinction is important to me.
Choosing another language does not need to be an ideological statement. It can simply define a boundary.
The product kept substantial Node/Nest parts; high-throughput consumers gained a runtime that is very comfortable for concurrency and simple binaries to operate.
The cost of a second language is only worth it when the boundary is clear. If every service uses a different stack because somebody likes it, the system becomes a zoo.
ClickHouse solved a different problem from PostgreSQL
Replacing PostgreSQL with ClickHouse everywhere would have been another mistake.
What I needed was to stop asking the transactional database to also behave like an analytical engine.
ClickHouse is a column-oriented database built for analytics. That matters because a dashboard query often reads a few columns from many rows and performs aggregations.
A simplified table might follow this shape:
CREATE TABLE ad_events
(
tenant_id String,
source LowCardinality(String),
campaign_id String,
occurred_at DateTime,
impressions UInt64,
clicks UInt64,
spend Decimal(18, 4)
)
ENGINE = MergeTree
ORDER BY (tenant_id, source, occurred_at, campaign_id);
Do not copy that ORDER BY into production without understanding your own query patterns. In ClickHouse, physical design needs to follow the queries you want to accelerate.
The point is that analytics data moved somewhere where high-volume aggregation is a first-class concern.
PostgreSQL could go back to doing what it did best for the product.
The Strangler Fig pattern avoided the heroic rewrite
I did not want a Friday afternoon where someone pressed a button and all traffic suddenly moved from the old system to the new architecture.
The migration was incremental, aligned with the Strangler Fig pattern: build new paths next to the existing ones, move pieces gradually, and observe every cutover.
That let me:
- migrate one flow at a time;
- compare results;
- revert one specific step;
- keep shipping product while the architecture changed.
A full rewrite looks clean on a whiteboard. In a live system, it concentrates risk.
Incremental modernization distributes that risk over time and, more importantly, makes it measurable.
Backlog is a health metric
Once queues exist, "it is running" stops being a binary statement.
A consumer can be alive and still be losing the race against incoming events.
So I started caring about metrics such as:
- lag per consumer group;
- production rate;
- consumption rate;
- average processing time per event;
- errors by type;
- retries;
- dead letters, when applicable;
- age of the oldest event that has not been processed yet.
Imagine two scenarios:
input: 2,000 events/s
output: 2,100 events/s
Backlog tends to shrink.
Now:
input: 2,000 events/s
output: 1,600 events/s
Nothing has technically "gone down", but the incident has already started.
That is one of the major mindset shifts in asynchronous systems: you also monitor the distance between now and the point the system has managed to finish processing.
What I would do before adding more microservices
After living through this kind of migration, I became more conservative about distribution.
I would not start a new product with this architecture just because it "might grow".
Before breaking a monolith apart, I would try to answer:
- is there truly independent load?
- is there a bottleneck I cannot isolate inside the current process?
- do teams need to deploy parts at different cadences?
- does one flow need failure isolation from another?
- is the operational cost of queues, schemas, retries and observability justified?
In VMageste, the answer eventually became yes because the volume and workload profile required it.
For many early-stage SaaS products, the answer is still no.
The main lesson: scale is a property of the flow
The experience made me stop saying "a system that scales" as if scalability were one single property.
What is scaling?
- ingestion?
- transformation?
- writes?
- aggregation?
- dashboard reads?
- tenant isolation?
Each segment has a different limit, and sometimes a different technology is appropriate for each one.
Kafka, Go and ClickHouse were important. But the decision that really changed the system happened before choosing those tools:
separate the ingestion path from the analytical path, and let each component own a responsibility it can actually sustain.
That is the part of the architecture I would carry into any other data product, even if the stack were completely different.