Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is bcryptjs slower on AWS Lambda than in local docker?

I have Lambda written in NodeJS. I noticed it takes several seconds to complete. I added logs and found that bcrypt is quite slow.

Packages:

"dependencies": {
  "bcryptjs": "^2.4.3",

Source code:

const bcrypt = require('bcryptjs');

console.log("User was found"); // following part takes more than 1 second!
if (bcrypt.compareSync(password, user.password)) {
    console.log("Password verified"); //

This is a log from AWS LogWatch:

2020-01-13T20:25:30.951 User was found
2020-01-13T20:25:32.670 Password verified

and

2020-01-13T20:31:20.192 User was found
2020-01-13T20:31:21.550 Password verified

So it takes 1.7 seconds. I ran the same code in docker on my machine

2020-01-13T20:09:48.109 User was found
2020-01-13T20:09:48.211 Password verified

Locally it takes just 120 ms. AWS uses NodeJS 10.x, local docker image is probably 8.x. I do not know how to tell docker to reflect changes in packaged.yaml.

Is this NodeJS regression? Or some issue on AWS configuration?

like image 335
Leos Literak Avatar asked Oct 15 '22 07:10

Leos Literak


1 Answers

Encryption performance is typically CPU bound. AWS Lambda CPU is proportional to RAM, so you should choose the largest (3008 MB) and re-test.

When I run this inside a Lambda function handler on a 3008 MB RAM Lambda in us-east-1, the compareSync call consistently takes 90-100ms. With a 128 MB Lambda, it takes a little over 1s.

On a related note, it's helpful to understand that choosing the lowest (128 MB) RAM option, simply because it is cheaper per GB-s, is not always the best thing to do. While the highest RAM option (with proportionally higher CPU and network) is certainly more expensive per GB-s, it also completes Lambda functions a lot quicker. So, for example, you might be able to complete your task in 1/10th of the time at only 1.75x the cost. In a lot of situations, that can be very valuable.

There is a project that can help you tune price/performance for your Lambdas: alexcasalboni/aws-lambda-power-tuning

like image 65
jarmod Avatar answered Dec 28 '22 11:12

jarmod