I'm trying to design a data updates mechanism in my micro-services architecture.
For the sake of simplicity, let's assume we have two micro-services A and B, B exposes an API for creating some tasks, using simple REST, POST /tasks, which creates a task and returns a unique task identifier to query on - task_id. Then any created task can be queried on status using another API endpoint: GET /tasks/{task_id}. Now A can create tasks and use polling mechanism to track progress.
The next improvement, we would like to add is "push API" - progress updates asynchronously using a message broker (e.g., RabbitMQ). Now, whenever the status has changed, B will publish a data update using a message broker and A will get this update instead of polling.
This is the expected flow:
- A requests B to create a task synchronously
- A subscribes to changes of
tasks.{task_id} - B publishes a change of
task_id
Steps 2 + 3 can be re-ordered causing A to miss updates or even never get any at all (if the task was completed before).
The only way to handle this race condition I can think of is to change step 2:
- A subscribes to changes of
tasks.{task_id} - A queries for current status
GET /tasks/{task_id} - For any received notification we need to check that it is a newer version than the state received in the manual querying (and vice-versa).
Is there another approach or a better practice for this problem?
tasksnot possible?