Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dreamorosi opened this issue Apr 3, 2024 · 5 comments · Fixed by #2311
Closed

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

dreamorosi opened this issue Apr 3, 2024 · 5 comments · Fixed by #2311
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped idempotency This item relates to the Idempotency Utility

Comments

@dreamorosi
Copy link
Contributor

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
@dreamorosi dreamorosi added bug Something isn't working idempotency This item relates to the Idempotency Utility confirmed The scope is clear, ready for implementation labels Apr 3, 2024
@dreamorosi dreamorosi self-assigned this Apr 3, 2024
@dreamorosi dreamorosi linked a pull request Apr 3, 2024 that will close this issue
9 tasks
Copy link
Contributor

github-actions bot commented Apr 5, 2024

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Apr 5, 2024
Copy link
Contributor

This is now released under v2.0.4 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Apr 10, 2024
@mrerichoffman
Copy link

@dreamorosi I'm seeing this bug for the DynamoDBPersistenceLayer too. Does a similar fix for the in-memory cache need to be addressed for the DDB layer too?

@dreamorosi
Copy link
Contributor Author

Hi @mrerichoffman - The DynamoDB persistence layer is the only one we have.

If you are seeing a bug, please open a new bug report and if possible, provide a sample of the code you are using.

@mrerichoffman
Copy link

Link to new issue - #2517

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped idempotency This item relates to the Idempotency Utility
Projects
2 participants