Skip to content

Bug: idempotency in-memory cache doesn't read correct value #2309

Closed
@dreamorosi

Description

@dreamorosi

Expected Behaviour

When using the Idempotency utility with in-memory cache enabled, the utility should return the result of a previous operation from cache provided that the request is handled by the same execution environment.

Current Behaviour

When the in-memory cache is enabled the utility doesn't return the correct value from the cache, but instead causes a runtime error.

Code snippet

To reproduce you can use the first example found in our docs with the only change of enabling the local cache:

import { randomUUID } from "node:crypto";
import {
  makeIdempotent,
  IdempotencyConfig,
} from "@aws-lambda-powertools/idempotency";
import { DynamoDBPersistenceLayer } from "@aws-lambda-powertools/idempotency/dynamodb";
import type { Context } from "aws-lambda";

export type Request = {
  user: string;
  productId: string;
};

export type Response = {
  [key: string]: unknown;
};

export type SubscriptionResult = {
  id: string;
  productId: string;
};

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: process.env.IDEMPOTENCY_TABLE_NAME!,
});

const createSubscriptionPayment = async (
  event: Request
): Promise<SubscriptionResult> => {
  // ... create payment
  return {
    id: randomUUID(),
    productId: event.productId,
  };
};

export const handler = makeIdempotent(
  async (event: Request, _context: Context): Promise<Response> => {
    try {
      const payment = await createSubscriptionPayment(event);

      return {
        paymentId: payment.id,
        message: "success",
        statusCode: 200,
      };
    } catch (error) {
      throw new Error("Error creating payment");
    }
  },
  {
    persistenceStore,
    config: new IdempotencyConfig({
      useLocalCache: true,
    }),
  }
);

Steps to Reproduce

Then run the function with the same payload twice and observe the error (shown below) being thrown at the second execution.

Possible Solution

N/A

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

START RequestId: 2aca7871-ce8f-4bb0-99f9-e7ab38b136e1 Version: $LATEST
2024-04-03T12:09:58.833Z        2aca7871-ce8f-4bb0-99f9-e7ab38b136e1    ERROR   Invoke Error    {"errorType":"Error","errorMessage":"There is already an execution in progress with idempotency key: lambda-powertools-playground-fn#iqkUdwjZtMfikGMWbtq/mw==","stack":["Error: There is already an execution in progress with idempotency key: lambda-powertools-playground-fn#iqkUdwjZtMfikGMWbtq/mw==","    at IdempotencyHandler.determineResultFromIdempotencyRecord (/var/task/index.js:1891:15)","    at IdempotencyHandler.<anonymous> (/var/task/index.js:1853:21)","    at async IdempotencyHandler.handle (/var/task/index.js:1937:24)"]}
END RequestId: 2aca7871-ce8f-4bb0-99f9-e7ab38b136e1
REPORT RequestId: 2aca7871-ce8f-4bb0-99f9-e7ab38b136e1  Duration: 17.59 ms      Billed Duration: 17 ms  Memory Size: 128 MB     Max Memory Used: 82 MB
XRAY TraceId: 1-660d4716-760593296fc433313071b913       SegmentId: be6e6dbcc62ba80d     Sampled: true

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcompletedThis item is complete and has been merged/shippedidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions