auth0 / node-auth0

Node.js client library for the Auth0 platform.
MIT License
638 stars 309 forks source link

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

Open uchiyama-mesh opened 2 months ago

uchiyama-mesh commented 2 months ago

Checklist

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)