Skip to content

Docs: improve documention regarding the change to async when migrating from BatchProcessorSync to BatchProcessor #4760

@Tohaker

Description

@Tohaker

Expected Behavior

Migrating from BatchProcessorSync and processPartialResponseSync to BatchProcessor and processPartialResponse with the processInParallel flag set to false should make no change to behaviour.

Current Behavior

Using BatchProcessor and processPartialResponse with the processInParallel flag set to false processes records in parallel, which means we lose data where before we didn't.

Code snippet

const processor = new BatchProcessor(EventType.SQS);

const recordHandler =
  (qtyMap: Record<string, number>) => (record: SQSRecord) => {
    const { product } = extractDataFromEnvelope(
      record,
      'powertools_json(body)',
    );

    const productCode = product.code;
    const items = product.items;

    for (const item of items) {
      const id = item.itemId;
      const qty = Math.ceil(item.quantity);

      if (!qtyMap[id]) {
        qtyMap[id]= [
          qty,
        ];
      } else {
        qtyMap[id]?.push(
          qty,
        );
      }
    }
  };

const handler = (event: SQSEvent) => processPartialResponse(
    event,
    recordHandler(elementQuantityMap),
    processor,
    { processInParallel: false }
  );

Steps to Reproduce

With some records that have a list of item IDs and quantities, you can run the above and see that records get overwritten because they're being process in parallel still, even when specified not to.

e.g.

{
  "product": { "id": "1" "items": [{
  itemId: 'sku1',
  quantity: 1.00000000, 
},
{
  itemId: 'sku2',
  quantity: 1.08300000, 
},
{
  itemId: 'sku3',
  quantity: 5.00000000, 
},
{
  itemId: 'sku4',
  quantity: 4.00000000, 
},] }

{
  "product": { "id": "2" "items": [{
  itemId: 'sku1',
  quantity: 1.00000000, 
},
{
  itemId: 'sku2',
  quantity: 1.08300000, 
},
{
  itemId: 'sku3',
  quantity: 5.00000000, 
},
{
  itemId: 'sku4',
  quantity: 4.00000000, 
},] }

I'd expect a map like;

{
  "sku1": [1, 1],
"sku2: [2, 2]
... etc
}

But instead I just get something like;

{
  "sku1" : [1],
"sku2: [2]
... etc, some keys even misisng
}

Possible Solution

No response

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

Lambda Layers

Execution logs

Metadata

Metadata

Assignees

Labels

confirmedThe scope is clear, ready for implementationdocumentationImprovements or additions to documentation

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions