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

v4 processing on lambda is 1000ms slower than v3 processing #1040

Open
5 tasks done
uchiyama-mesh opened this issue Sep 24, 2024 · 0 comments
Open
5 tasks done

v4 processing on lambda is 1000ms slower than v3 processing #1040

uchiyama-mesh opened this issue Sep 24, 2024 · 0 comments
Labels
bug This points to a verified bug in the code

Comments

@uchiyama-mesh
Copy link

uchiyama-mesh commented Sep 24, 2024

Checklist

  • I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Description

Hello.
When using node-auth0 v4.3.1 and v3.7.2 in Lambda, there's a 1000ms difference in processing time during Lambda's cold start.
My Lambda function is developed using Node.js v20 + TypeScript + SAM. I've also added node-auth0 to a layer and imported it into my Lambda function.

Here are the Lambda function and layer codes for both v4.3.1 and v3.7.2.
Although it's simple code using node-auth0 and X-Ray (for measuring processing time), when executing the Lambda function, the processing time for the v4.3.1 function is 1000ms slower. Why is this?
I've managed to achieve the same processing time as v3.7.2 by increasing the Lambda's memory, but this incurs additional costs. If possible, I'd like to reduce the cost while maintaining the same processing time as v3.7.2.

Thanks.

v.4.3.1

lambda code

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { AWSXRay } from '/opt/nodejs/lib/x-ray';
import { getUsersByEmail } from '/opt/nodejs/lib/auth0';

export const lambdaHandler = async (
  event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
  const segment = AWSXRay.getSegment();
  if (!segment) {
    const error = new Error('segment is undefined');
    console.error(error);
    throw error;
  }

  const getUserByEmailSegment = segment.addNewSubsegment('getUserByEmail');
  let users;
  try {
    const email = JSON.parse(event.body!).email;
    if (!email) {
      const error = new Error('email is not found');
      console.error(error);
      throw error;
    }

    users = await getUsersByEmail(email);
  } catch (error: any) {
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    };
  } finally {
    getUserByEmailSegment.close();
  }
  segment.close();

  return {
    statusCode: 200,
    body: JSON.stringify(users),
  }
};

layer code

import { ManagementClient } from 'auth0';

const managementClient = new ManagementClient({
  domain: process.env.AUTH0_DOMAIN!,
  clientId: process.env.AUTH0_CLIENT_ID!,
  clientSecret: process.env.AUTH0_CLIENT_SECRET!,
});

export const getUsersByEmail = async (email: string) => {
  try {
    const result = await managementClient.usersByEmail.getByEmail({
      email: email,
    });
    return result.data;
  } catch (error: any) {
    throw error;
  }
};

v3.7.2

lambda code

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { AWSXRay } from '/opt/nodejs/lib/x-ray';
import { getUsersByEmail } from '/opt/nodejs/lib/auth0';

export const lambdaHandler = async (
  event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
  const segment = AWSXRay.getSegment();
  if (!segment) {
    const error = new Error('segment is undefined');
    console.error(error);
    throw error;
  }

  const getUserByEmailSegment = segment.addNewSubsegment('getUserByEmail');
  let users;
  try {
    const email = JSON.parse(event.body!).email;
    if (!email) {
      const error = new Error('email is not found');
      console.error(error);
      throw error;
    }

    users = await getUsersByEmail(email);
  } catch (error: any) {
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    };
  } finally {
    getUserByEmailSegment.close();
  }
  segment.close();

  return {
    statusCode: 200,
    body: JSON.stringify(users),
  }
};

layer code

import { ManagementClient } from 'auth0';

const managementClient = new ManagementClient({
  domain: process.env.AUTH0_DOMAIN!,
  clientId: process.env.AUTH0_CLIENT_ID!,
  clientSecret: process.env.AUTH0_CLIENT_SECRET!,
});

export const getUsersByEmail = async (email: string) => {
  try {
    const result = await managementClient.getUsersByEmail(email);
    return result;
  } catch (error: any) {
    throw error;
  }
};

Reproduction

  1. Create Lambda code and layer code using v4.3.1 with SAM
  2. Create Lambda code and layer code using v3.7.2 with SAM
  3. Deploy both versions
  4. Send requests via API Gateway
  5. The API response time for v4.3.1 is 1000ms slower than v3.7.2

Additional context

Processing time for v4.3.1 as measured by X-Ray

The getUserByEmail process takes 2040ms

Screenshot 2024-09-24 14 35 27

Processing time for v3.7.2 as measured by X-Ray

The getUserByEmail process takes 1010ms

Screenshot 2024-09-24 14 36 36

node-auth0 version

4.3.1 and 3.7.2

Node.js version

nodejs20.x(lambda)

@uchiyama-mesh uchiyama-mesh added the bug This points to a verified bug in the code label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This points to a verified bug in the code
Projects
None yet
Development

No branches or pull requests

1 participant