Closed
Description
At the moment when we send data to Kafka using helper.postEvent
we usually send raw data which were received from the user:
await job.update(data) // save raw data form the user
await helper.postEvent(config.TAAS_JOB_UPDATE_TOPIC, { id, ...data }, { oldValue: oldValue }) // send raw data to the Kafka
This leads to several inconsistencies:
- When we are creating a record there are no values for
updatedAt
andupdatedBy
. So object is send to Kafka without these values and they are indexed withupdatedBy = undefined
andupdatedBy = undefined
. Though in the DB these values are stored asnull
. This leads to the inconsistency between data in DB and ES. While generally we shouldn't rely on the difference betweennull
andundefined
sometimes we do. Also, our commands to reindex data from DB to ES would index these fields withnull
because the data would be taken from the DB. - If if
PATCH
data, then we can pass just a few fields and as a result payload for the Kafka Event would have only a few fields. In my opinion, it would be better for consistency if Kafka events always have the whole document in the payload.
So to make data consistent between DB and ES, we have to always pass to the Kafka Events data from DB, and the code above should be replaced with something like:
const updatedJob = await job.update(data) // save raw data form the user
await helper.postEvent(config.TAAS_JOB_UPDATE_TOPIC, updatedJob.toJSON(), { oldValue: oldValue }) // send raw data to the Kafka
Another benefit of such approach is that if we implement some logic at the model level it would automatically reflected in the event payloads, though for now I prefer not to rely on it. So for now we still have to implement custom logic explicitly like here https://github.com/topcoder-platform/taas-apis/issues and not to include it inside the models.
- This has to be done for all the places where we pass data to the Kafka events during creating and updating
- As we use method
helper.postEvent
to send a message not only to Kafka, but also to our internal event handler, so this would make event bodies unified there too, which seems good too