Distributed System Refreshers :
Best Practices for Developing Microservices
Stateful Architecture vs Stateless Architecture
Limited Offer First Time 40% off , End Tomorrow .
What is Serverless Architecture?
Serverless architecture is an approach to software design that allows developers to build and run services without having to manage the underlying infrastructure. Developers can write and deploy code, while a cloud provider provisions servers to run their applications, databases, and storage systems at any scale. In this article, we’ll cover how serverless architecture works, the benefits and drawbacks of using it, and some tools that can help you go serverless.
HOW IT WORKS:
We know how a regular back end works. We have the client-side browser making a request for the server-side logic to access a central database.
In a serverless system, the client-side browser talks directly to the authentication service associated with the BAAS(Back-end As A Service). Once access is granted, another BAAS function allows the browser access to a products database fully hosted by the third party. We essentially have a single page application – the third party keeps track of the client’s session, reading of the database, and translating into a usable view by offering a specific service package.
Instead of having third party servers that are always running, we can use FAAS to generate an application programming interface gateway. The user can now choose between other functions they have created themselves – for argument’s sake, let’s call one search, and one purchase.
The search function links to the same product database as the BAAS, with the results populating in the browser. The search code has been ported in the original Java and JavaScript without a complete rewrite – but the process is much faster, and variable function can be added at the developer’s discretion.
A selection is made in the “shopping cart” style function of the browser, before another FAAS function links to the offside purchase database. From there payment is authenticated via another third-party vendor like PayPal and success – the browser informs the user of their purchase.
The FAAS system has broken up different logic processes into separately deployed commands that are more flexible and easier to modify or change. Each click generates a new compute container, managing the microservices in a more efficient manner. BAAS functions like security, user authentication, and database optimization are left to run as background processes.
Cloud Providers services for Serverless App
Key Component of Serverless Architecture
What is Functions as a Service (FaaS)?
Functions as a Service (FaaS) is a cloud computing model that enables developers to run individual functions or pieces of code in a stateless, event-driven environment. FaaS eliminates the need to manage infrastructure, allowing developers to focus solely on writing and deploying code. It’s the backbone of serverless computing, enabling the execution of code in response to specific events.
Key Characteristics of FaaS
Event-Driven Execution:
Functions are triggered by specific events such as HTTP requests, database updates, file uploads, or message queue events.
No Server Management:
The cloud provider manages all infrastructure tasks, including server provisioning, scaling, and maintenance.
Automatic Scaling:
Functions automatically scale up or down based on the number of incoming requests.
Statelessness:
Each function execution is independent and does not retain state between invocations. If state is required, it must be stored in external systems like databases or caches.
Pay-As-You-Go:
Billing is based on the time a function runs and the resources it consumes, measured in milliseconds or seconds.
Short Execution Time:
Functions are designed for short-lived tasks, with execution times often capped by the cloud provider (e.g., 15 minutes in AWS Lambda).
How FaaS Works
Trigger:
An event occurs (e.g., an HTTP request or database change), which invokes the function.
Function Execution:
The function runs in a lightweight container environment provided by the cloud provider.
Response:
The function performs its task (e.g., processing data or returning a response) and then terminates.
Scaling:
If multiple events occur simultaneously, the cloud provider scales the function horizontally by running multiple instances.
Serverless platforms
Serverless platforms need infrastructures where they can be executed, provider agnostic frameworks provide a platform agnostic way to define and deploy Serverless code on various cloud platforms or commercial services.
Serverless Framework (Javascript, Python, Golang)
Apex (Javascript)
ClaudiaJS (Javascript)
Sparta (Golang)
Gordon (Javascript)
Zappa (Python)
Up (Javascript, Python, Golang, Crystal)
The Serverless App
A Serverless solution consists of a web server, Lambda functions (FaaS), security token service (STS), user authentication and database.
Benefits of serverless architecture
Serverless architecture is changing software development, offering startups a way to maximize efficiency and innovation. By removing the need to manage servers, serverless computing allows companies to concentrate on developing serverless apps and backend code without the complexity of handling server hardware.
1. Reduced operational costs
Adopting a serverless framework translates to a reduction in operational costs. Without the need to provision and manage physical servers, businesses save on hardware expenses and ongoing maintenance. FaaS models provided by cloud providers mean you pay only for the computing resources used, cutting down the financial waste associated with idle server capacity.
2. Increased scalability
Serverless functions offer increased scalability. The serverless platforms automatically adjust computing resources to match the demand of serverless applications, allowing seamless scaling without the need for manual intervention. This elasticity ensures that serverless apps can handle varying loads, making it ideal for startups that experience unpredictable traffic.
3. Faster development
Serverless environments expedite the software development process. Developers can focus on writing business logic rather than getting bogged down by the infrastructure setup. The serverless service models offer out-of-the-box backend code solutions that accelerate the creation and deployment of web apps.
4. Flexibility
The flexibility offered by serverless computing is another benefit. Developers can deploy serverless functions or entire serverless applications quickly and with ease, making it possible to experiment with new features without extensive planning or risk. This agility empowers startups to adapt rapidly to market changes or user feedback.
5. Maintenance-free
With serverless architecture, the cloud providers handle the maintenance of the serverless environment. There’s no need for startups to dedicate resources to manage servers, as the underlying infrastructure, security patches, and updates are all taken care of, leading to a maintenance-free operation for serverless apps and serverless application development.
6. High availability
Serverless platforms are designed to provide high availability for serverless applications. By taking advantage of the distributed nature of serverless computing, web applications benefit from reduced downtime and increased resilience, as the serverless framework ensures that computing resources are always available when needed.
Drawbacks of Serverless Architecture
Cold Starts: Initial delays when functions are idle for some time. Mitigated by keeping functions warm.
Limited Execution Time: Functions often have a maximum runtime (e.g., 15 minutes). Long tasks need alternative designs.
Vendor Lock-In: Tied to specific cloud platforms, making migrations difficult.
Statelessness: No built-in state retention; requires external storage like databases or caches.
Debugging Challenges: Distributed systems complicate debugging and monitoring.
Unpredictable Costs: High-frequency usage can lead to unexpected expenses.
Limited Control: Developers can’t customize underlying infrastructure.
Scaling Issues: Concurrency limits can throttle performance, and rapid scaling might overload downstream services.
Security Concerns: Misconfigurations and shared infrastructure pose risks.
Learning Curve: Requires familiarity with event-driven programming and cloud-specific features.
Use cases of serverless architecture
Serverless architectures are not a one-size-fits-all solution, yet they shine in scenarios where traditional infrastructure may falter. Some of the top use cases for serverless architectures include:
Web and mobile backends: Serverless architecture excels at powering the backend of web and mobile applications by processing APIs, handling database events, managing user authentication, sending notifications, and more. It enables developers to build and scale these backend services without directly dealing with server management or provisioning.
Stream processing: Serverless functions can respond in real-time to data streams from sources like Kafka, allowing for the seamless processing of streaming data without the burden of managing the underlying infrastructure. This capability is essential for applications that rely on immediate data processing and analytics.
ETL pipelines: Serverless computing transforms how ETL (Extract, Transform, Load) pipelines are constructed by allowing data transformation jobs to be scheduled as functions that are triggered at specified intervals. This serverless approach to ETL jobs eliminates the need for dedicated servers to run these processes continuously.
Event processing: Serverless functions are ideally suited for event-driven processing, executing code in response to various triggers such as file uploads, monitoring signals, or REST API calls. This reactivity enables applications to be both efficient and responsive to user or system events.
Cron jobs: Serverless functions offer a modern alternative to traditional cron jobs by allowing developers to schedule function calls that run at regular intervals. This method is more scalable and reliable than managing cron jobs on individual servers.
Microservices: Serverless architecture supports the microservices approach by allowing complex services to be decomposed into individual functions that work in concert. Each function can scale independently, providing flexibility and efficiency in resource usage and maintenance.
Serverless is best for event-driven, lightweight applications but may not suit tasks requiring long runtimes, stateful operations, or high customization.