With today's modern applications, we end up with lots of asynchronous operations happening and we somehow have to let the user know what is happening. This, of course, depends on the requirements and business needs.
Let's look at the Amazon case. When you buy something on Amazon, Amazon tells you that your order has been taken. However, it doesn't really mean that you have actually purchased it as the process of taking the payment has not been kicked off yet. The payment process probably goes through a few stages and at the end, it has a few outcome options like success, faΔ±lure, etc. Amazon only gives the user the change to know about the outcome of this process which is done through e-mail. This is pretty straightforward to implement (famous last words?) as it doesn't require things like real-time process notifications, etc..
On the other hand, creating a VM on Microsoft Azure has different needs. When you kick the VM creation operation through an Azure client like Azure Portal, it can give you real-time notifications on the operation status to show you in what stage the operation is (e.g. provisioning, starting up, etc.).
In this post, I will look at the later option and try to make a few points to prove that the implementation is a bit tricky. However, we will see a potential solution at the end and that will hopefully be helpful to you as well.
Let's look at a problematic flow:
We have a few problems with the above flow:
The solution to all of the above problems boils down to persisting the events and opening up an HTTP endpoint to pull all the past events per each operation. Let's assume that we are performing payment transactions and want to give transparent progress on this process. Our events endpoint would look similar to below:
GET /payments/eefcb363/events [ { "id": "c12c432c", "type": "ProcessingStarted", "message": "The payment 'eefcb363' has been started for processing by worker 'h8723h7d'.", "happenedAt": "2016-07-30T11:05:26.222Z" "details": { "paymentId": "eefcb363", "workerId": "h8723h7d" } }, { "id": "6bbb1d50", "type": "FraudCheckStarted", "message": "The given credit card details are being checked against fraud for payment 'eefcb363' by worker 'h8723h7d'.", "happenedAt": "2016-07-30T11:05:28.779Z" "details": { "paymentId": "eefcb363", "workerId": "h8723h7d" } }, { "id": "f9e09a83", "type": "ProgressUpdated", "message": "40% of the payment 'eefcb363' has been processed by worker 'h8723h7d'.", "happenedAt": "2016-07-30T11:05:29.892Z" "details": { "paymentId": "eefcb363", "workerId": "h8723h7d", "percentage": 40 } } ]
This endpoint allows the client to pull the events that have already happened, and the client can mix these up with the events that it receives through the notification channel where the events are pushed in the same format.
With this approach, suggested flow for the client is to follow the below steps in order to get 100% correctness on the progress and events:
Obviously, at any display stage, the client needs to honor that there are special event types which give an indication that the operation has ended. The client needs to know about these types and handle them specially (e.g. show the completed view, etc.). Besides this, whenever the client receives an event which corresponds to operation end event, it should unsubscribe from the notification channel for that specific operation.
However, the situation can be a bit easier if you only care about percentage progress. If that's case, you may still want to persist all events but you only care about the latest event on the client. This makes your life easier and if you decide that your want to be more transparent about showing the progress, you can as you already have all the data for this. It is just a matter of exposing them with the above approach.
Under any circumstances, I would suggest you to persist operation events (you can even go further with event sourcing and make this a natural process). However, your use case may not require extensive and transparent progress reporting through the client. If that's the case, it will certainly make your implementation a lot simpler. However, you can change your mind later and go with a different progress reporting approach since you have been already persisting all the events.
Finally, please share your thoughts on this if you see a better way of handling this.