Serverless architecture is a design pattern that allows developers to build and run applications and services without having to worry about infrastructure or compute resources. The name "serverless" in itself is a bit misleading, as it's not that servers are not needed in this architecture, but servers are abstracted away from the end user. Devs don't have to think about underlying servers, scale-ups, deployments, etc, they can just focus on the business logic writing functions, for this reason, this architecture is also called "Functions as a Service" or "FaaS".
The main components of a serverless architecture are functions and event-driven triggers. Functions are small code executed in response to an event or trigger. These functions are typically executed in a stateless environment, meaning they do not maintain any persistent state or memory between executions.
AWS Lambda is a way of building serverless applications because it abstracts away many of the underlying infrastructure details and makes it easy to run code in response to a wide range of triggers.
AWS Lambda functions are written in a variety of languages, including Python, Nodejs, Golang, Java, etc. Here's an example of a simple AWS Lambda function written in Python, this function returns a JSON object with a status code of 200 and a body containing the message "Hello, World!".
When to use Serverless architecture?
A few days back, I wrote a small script, where I wanted to see if devs in our team are putting estimates and due dates in their assigned Jira tickets. I wrote a simple script that integrates Jira API and then sends a slack message to our team channel. I used node js for this script and after writing the code, I needed to deploy the code, and deploying this code on full-fledged servers did not make much sense as the server has to be on 24 hours, which is not needed. I just wanted to run this script once a week and lambda perfectly fitted the use case. I simply created a weekly trigger and uploaded this function in AWS lambda. Let's look at other examples.
- Microservices: Serverless architecture is well-suited for building and deploying microservices, which are small, independent units of functionality that can be composed into larger applications.
- Event-driven computing: Serverless architecture is often used to build event-driven systems, which perform certain actions in response to specific events. For example, a serverless function might be triggered by a new file being uploaded to a cloud storage service, or a message being sent to a message queue.
- Web and mobile applications: Serverless functions can be used to build web and mobile applications, by providing the backend logic and integrations required by the frontend client.
Advantages of Serverless architecture:
- Cost Savings: You only pay for the specific actions or requests performed, rather than paying for a fixed amount of computing power. This can lead to significant cost savings, especially for applications with variable or infrequent traffic.
- Flexibility: Serverless architectures are highly scalable, so they can handle sudden spikes in traffic without any downtime. This makes them ideal for applications with unpredictable workloads.
- Simplified Maintenance: With a serverless architecture, you don't have to worry about maintaining servers or infrastructure. This can free up time and resources that can be better spent on developing and improving your application.
Disadvantages of serverless architecture:
While serverless architecture has many benefits, it also has some limitations. Let's look at a few issues.
- Cold start latency: Serverless functions may experience longer start-up times, known as "cold start latency," when they are invoked after a period of inactivity. This can be a problem for applications that require very low latencies or high levels of performance.
- Vendor lock-in: Be it AWS lambda, google functions, or other serverless architecture. Applications and software that run in the serverless environment are by default locked to a specific cloud vendor. Therefore, serverless can cause multiple issues during migration.
- Complexity: Serverless architecture can be more complex than traditional architecture, as it involves multiple moving parts and requires a different way of thinking about how your application is built and deployed. This can make it more difficult to debug and troubleshoot issues, and it may require a different set of tools and processes.