How to process events in order but with only 1 active process for 1 employee?

26 Views Asked by At

I am working on a usecase where we have multiple events incoming from up streams and it is possible that we will have multiple events for 1 employee. But we want to make sure that there is only 1 active process is there for 1 employee and we want to delay processing of other events with same employeeID but events for different employees can process. While doing this we still want to maintain the order of events for an employee.

I was thinking of below approach

FIFO SQS → Lambda - In this scenerio we can use a lock on basis of employee(maybe in dynamoDB) and lambda can requeue event if there is an active lock on that employee. But I am not sure how we can maintain order here? If we requeue events in FIFO won't new events get processed first before requeued events?

1

There are 1 best solutions below

0
John Rotenstein On

It is simpler than that.

Simply use an Amazon SQS FIFO queue and specify the Employee ID in the MessageGroupId.

From Using the Amazon SQS message group ID - Amazon Simple Queue Service:

MessageGroupId is the tag that specifies that a message belongs to a specific message group. Messages that belong to the same message group are always processed one by one, in a strict order relative to the message group (however, messages that belong to different message groups might be processed out of order).

If the batch size is greater than 1, then it is possible to retrieve multiple messages with the same MessageGroupId in a single ReceiveMessages() call, but they will be in order. If the batch size is 1, then only one consumer will be given a message for a given MessageGroupId.