AWS Lambda Java Vs Python –
which is a better Programming language choice for Lambda ?
AWS Lambda Java Vs Python :
Goal of this article is to identify better programming language between Java and Python to run serverless programs in the AWS Lambda environment. I am looking at the following metrics to determine which one saves time and money, which will be important criteria especially in the long run.
- Initialization time (cold start time)
- Invocation time (function run time)
- Total Billed Duration time.
- Ability of processing large files in short time (tied to #2)
In order to gather some data, I have developed a simple Lambda program in both Java 8 and Python 3.7, which listens to a put event of a S3 bucket and prints the meta data and copies the object into a different destination S3 bucket. It is that simple and there is no processing of any data.
I have created 2 source buckets and 2 destination buckets two for each programming language and ran repeated tests on both Lambda functions using same set of files and captured Cloud Watch logs and X-Ray traces.
Pre-requisites to test sample Lambda code
- S3 buckets with public read write permissions (not recommended other than for testing)
- One Python 3.7 Lambda function with S3 PUT event listener and for files larger than 6 MB, enable “Multipart upload completed” event.
- One Java 8 Lambda function with S3 PUT event listener and for files larger than 6 MB, Multipart completed event is needed.
- New IAM role with permission to following services
- S3 Full Access
- ClowdWatchFullAccess
- AWSXrayFullAccess
- Enable “Enable active tracking” for Lambda to capture X-ray traces
- Increment timeout to 15 minutes as larger file takes may little longer to process
- Increment Lambda memory allocation to full capacity 3008 MB
- Network – No VPC
Source code @ GIT
Java Code
Python Code
Test #1 (cold start) File size: 95 KB, Event : S3 Put
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #2 (Warm Standby) File size: 365 KB, Event : S3 Put
Language : Python
Cloud watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #3 (Warm Standby) File size: 2.2 MB, Event : S3 Put
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #4 (Warm Standby) File size: 5.7 MB, Event : S3 Put
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #5 (Warm Standby) File size: 11.8 MB, Event : S3 Multipart upload completed
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #6 (Cold start) File size: 39.2 MB, Event : S3 Multipart upload completed
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #7 (Warm standby – repeat of Test #6 in Warm standby mode ) File size: 39.2 MB, Event : S3 Multipart upload completed
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Test #8 (Cold Start ) File size: 330 MB, Event : S3 Multipart upload completed
Language : Python
Cloud Watch Logs
X-Ray traces
Language : Java
Cloud Watch Logs
X-Ray traces
Conclusion
As per the Billed Duration metric from Cloud Watch logs for each lambda invocation, it is evident that Java is about slower than Python, especially during the cold start initializations the difference is much wider and while processing large files Python is about much faster than Java.
AWS Lambda Java vs Python Other factors to consider
- Ease of development
- Popularity of the language among developers community and availability of programmers
- Speed of development and deployment
- Ability to modify and debug code in AWS environment (Lambda console and Cloud9), Python is an interpreted language and has a clear edge.
Let’s look at some facts as for as programming language popularity itself.
2018 Google Trends Programming language comparison :
2019 Google Trends Programming language comparison :
Also, look at this article that provides a great solution that detects sentiment using Amazon Comprehend from an audio file by converting audio to text using Amazon Transcribe.