How I refactor my 3-tier application to Serverless Architecture - Part 2
Tags: serverless, refactoring, stepfunctions, lambda
Published: 2024-01-13 13:16:29

In [Part 1](https://dev.to/cheesiong/how-i-refactor-my-3-tier-application-to-serverless-architecture-part-1-4pj0), I shared my journey as a Social Entrepreneur and how I refactored my 3-tier architecture design for SociallyClick from monolithic to a more loosely coupled design using AWS Lambda. That said, there were a couple of issues that made the architecture less than ideal.

In this Part 2, I will talk about the Serverless Architecture, and share why it is a better architecture than any of my previous designs.

## Cons of Monolithic Architecture
One of the earliest designs of SociallyClick was a 3-tier architecture using EC2. Although 3-tier design separates the presentation, application and data logic so that they can be developed and maintained separately without impacting the other tiers, there are a couple of issues, such as:

- **Monolithic** - It is a single unified application, and you cannot scale individual components easily. For instance, within the application tier, even if it is just one component that needs to be scaled to handle the load, the entire application is scaled as well. This is wasteful and not lightweight, when I need to spin off another EC2 instance(s) to handle the spike in some components and not the entire application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlypr2vf67dide41pqhz.jpg)

- **Affects Entire Application** - Since it is monolithic, if there is error in one component that leads to a crashing, it would bring down the entire application as well. 
- **Less Flexible** - I was constrained by the same technology stack for the 3-tier architecture. For example, I need to use the same programming language for the application logic for the entire application, even there are better options for certain parts of the application.
- **Tightly Coupled** - Changes in one module could impact the other modules or the rest of application drastically.
- **Single Deployment** - A change in one of the components would usually result in the deployment of the entire application. But why do I need to deploy those codes that I did not modify?

## Cons of my Lambda-based Architecture
Likewise, there were also a couple of issues for my architecture based on Lambda, such as:

- **Chaining of Lambda** - My design chained a number of Lambda functions together as I need to orchestrate the workflow. This is an [anti-pattern for Lambda](https://docs.aws.amazon.com/lambda/latest/operatorguide/orchestrator.html). This can be resolved using AWS Step Functions, which was launched in Dec 2016 (about 2 years after I was no longer with SociallyClick Pte Ltd).
- **Lack of Asynchronous flow** - I did not design for a more scalable architecture. Some functions can be made asynchronous so that they [do not block further execution while one or more functions are in progress](https://docs.aws.amazon.com/lambda/latest/operatorguide/synchronous-waiting.html).

## How the new Serverless Architecture be looked like for SociallyClick

For the new architecture, I would use AWS Step Functions to orchestrate the workflow, instead of chaining the Lambda functions.

AWS Step Functions is an AWS service that allows you to coordinate multiple AWS services into serverless workflows. It uses a visual workflow editor to build and execute state machines, which are made up of a series of steps that can include tasks, conditions, and error handling. With Step Functions, you can create workflows that are easy to understand, debug, and update, making it a useful tool for building and deploying complex, multi-step applications and microservices on AWS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no99o1qzqdkz2stkwsm3.jpg)

## Blue Ocean Strategy
If we evaluate the key attributes for successful application development, you will find that Serverless Architecture hits a sweet spot for the Blue Ocean Strategy. It eliminates or reduces the undifferentiated heavy lifting parts such as capacity planning of the servers, and the management of them, and elevate it with greater flexibility, able to scale massively and on a pay-as-you-use model. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qcctlme4cbecd3mtgj1.jpg)

In summary, Serverless Architecture allows you to create and run applications without the need to manage the underlying infrastructure. This enables you to develop applications with greater speed and efficiency, as well as lower costs (e.g., no more idling EC2 instances). By eliminating the need to maintain infrastructure, developers can focus their time and resources towards creating high-quality, scalable, and reliable products.

## About the Author
Chan Chee Siong is an AWS Community Builder with many years of IT professional experience. He can be connected at http://www.linkedin.com/in/chancheesiong.