Synchronization with 3rd party using commands, events and read model.
Author: Jonasz WiÄ…cek
Problem context
Sometimes we have to synchronize with 3rd party service like Blockchain, HubSpot etc. This feature may dramatically increase the complexity of application and introduce a lot of bugs. Especially when it comes to the blockchain synchronization where data once sent cannot be edited it is crucial to send valid data.
Additionally, communication may take a long time, 3rd party service may be down or for some reason we're sending invalid data. Common approach I see is to set up some retry policy on request. The problem with that simple retry solution is no tracing and no way to manually retry the communication later.
Bad example: Mint some cryptocurrency:
const mintMyCoin = async (coinId: number, amount: number, tx: Transaction) => {
// Save the state in our DB for faster reads.
await coinRepository.mintCoin(coinId, amount, tx);
// We cannot pass our transaction to 3rd party service. That means:
// - synchronization may fail -> we have state mismatch between our app and blockchain
// - even if we retry our synchronization some other operations may occur and may have already been synchronized
// - synchronization may happen out of order which will produce incorrect state in 3rd party service
await blockchainService.mintCoint(coinId, amount);
};