0

I want to trigger lambda with a websocket. I have deployed a EC2 instance of websocket producer which is throwing all its data through SQS FIFO and SQS triggering lambda with same messageGroupId. But sometimes lambda is executing concurrently, I am expecting lambda to be executed sequentially. Because data is coming in a Queue. Since it is a cryptocurrency exchange websocket, data frequency is really high. And I checked one message from the websocket takes 3ms in lambda to get processed. I was expecting lambda to run only 1 process not concurrently (which is causing wrong data calculation). Can anyone tell me what config in Queue should I configure or is there any other method to achieve this goal. Thanks

Edit: Attaching config for fifo enter image description here

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Mani Kant Tiwari
  • 390
  • 2
  • 7
  • 19
  • 1
    What value are you provide for the `MessageGroupId` on each message? Are you wanting **every** message to be processed sequentially, or only those of the **same MessageGroupid**? – John Rotenstein Mar 25 '22 at 10:19
  • Basically My requirement is to process every message sequentially. But most of the times it is sequential process, most sometimes lambda gets invoked concurrently, which is causing bad data calculation. – Mani Kant Tiwari Mar 27 '22 at 08:00

3 Answers3

0

There are two types of Amazon SQS queues: first-in, first-out (FIFO) and standard queues.

In FIFO queues, message strings remain in the same order in which the original messages were sent and received. FIFO queues support up to 300 send, receive or delete messages per second.

Standard queues attempt to keep message strings in the same order in which the messages were originally sent, but processing requirements may change the original order or sequence of messages. For example, standard queues can be used to batch messages for future processing or allocate tasks to multiple worker nodes.

The frequency of message delivery differs between standard and FIFO queues, as FIFO messages are delivered exactly once, while in standard queues, messages are delivered at least once.

Suggestion : check your que type and change it to FIFO.

0

You need to set the maximum lambda concurrency to 1.

https://aws.amazon.com/about-aws/whats-new/2017/11/set-concurrency-limits-on-individual-aws-lambda-functions/

concurrency

Jérémy Caré
  • 315
  • 3
  • 12
0

To process each message sequentially:

  • When sending a message to the Amazon SQS queue, specify the same MessageGroupId for each message
  • In the Lambda function, configure the SQS Trigger to have a Batch size of 1

When using a FIFO queue, if a message is 'in-flight' then SQS will not permit another message with the same MessageGroupId to be processed. It will, however, allow multiple messages with the same MessageGroupId to be sent to a Lambda function, which is why you should set the Batch Size to 1.

See:

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470