Function as a Service and Java. Is that a good idea?
Evandro Pires
CTO | Serverless Guru Ambassador | AWS Serverless Hero | Palestrante internacional (EN/PT/ES) | Mentor | Podcaster | Diretor de tecnologia | Marido e pai de dois
In the last IT event that I went, I saw many talks about Function as a Service (it's also known as Serverless). That's a new kind of architecture and, as different as the name can suggest, you'll need servers, but you don't need worry about that. During my article I'll use the acronym FaaS for Function as a Service.
Below, the definition of FaaS in the Wikipedia:
Function as a service (FaaS) is a category of cloud computing services that provides a platform allowing customers to develop, run, and manage application functionalities without the complexity of building and maintaining the infrastructure typically associated with developing and launching an app
In love about that subject, after the event I started some PoCs using Java and IBM Cloud Functions.
In terms of concept and architecture I've liked so much of FaaS. That architecture forces you build codes very cohesive and, because of that, you can reuse the function easily for another functions.
The main principle of FaaS is that you don't need worry about servers. The vendors offer to you solutions while you just need to configure the scale, the level of availability and some others high level configurations.
The common vendors for that kind of architecture are: AmazonWS Lambda, Azure Functions, Google Cloud Functions and IBM Cloud Functions.
Proof of concept
Talking about my PoCs, I built two proof of concepts: the first one is two functions where one of that add a product and the other get the product. For that PoC I used Java 8, IBM Cloud Functions and Cloudant NoSQL Database. My second proof of concept is a function that returns a number in words with Java and Python. The idea was benchmark both functions to know what the best language to make my functions.
You can fork my repositories from GitHub (java-faas-ibm-cloud-example / number-in-words-function).
Pros and cons
Below a list of known pros and cons of FaaS.
Pros
- Cohesion
- Serverless: we don't need worry about servers
- High scalability
- Time to market
- Simplifies the administration
Cons
- Bandwidth
- Overhead: using HTTP protocol for every call even to simple functions
- Cold start: the cold start for Java is higher then another programming languages like Python
Use cases for FaaS
- IoT (Internet of Things)
- Chatbots
- Agile and Continuous Integration pipelines
PoC Results
#1: REST functions + NoSQL database
To test my functions I built a JMeter test that add an product and get the same product. I ran with 8 threads (limited of my free tier on IBM Cloud) and 100 iterations. The test ran in 2 minutes with a throughput of 6 reqs per second.
#2: Number in words
Java
The first sample ran in 1 minute and 17 seconds. It was 8 threads and 100 iterations. The result was 10,4 reqs per second. The second sample ran exactly equal.
Python
The first sample ran in 1 minute and 15 seconds, while the second sample ran in 1 minute and 3 seconds. Both executions were with 8 threads and 100 iterations. In the second sample with Python the performance was almost 20% better than the first sample.
Conclusion
When I started those tests, honestly, I was expecting a biggest difference between a load test with Java and Python. My intuition said me Java would be slower than Python, and isn't true. Python performs better in the second sample when the function are started, but not too much.
Even though my sample was tiny, with these results my conclusion is whatever your programming language. It's more about how you can build your code more cohesive and shorter.
If you want to use Java you'll need to concern oneself with the size of your package and the amount of memory you'll use.
But don't forget the pros and cons of FaaS, that's will guide you if you'll really need to use FaaS.
References
https://www.altexsoft.com/blog/cloud/pros-and-cons-of-serverless-architecture
Software Craftsman | Full Stack Developer | Blogger ???? | Java ?? | Ruby ?? | Node ? | Go ?? | Learning Generative AI with Langchain ???? | Mastering Dify and Coze AI for building Agentic workflows | Cursor AI
6 年I believe it is typically used when building microservices applications