How I refactor my 3-tier application to Serverless Architecture - Part 1
Tags: serverless, refactoring, lambda, architecture
Published: 2024-01-08 05:54:05

I was a Social Entrepreneur in 2013 after I started a social enterprise known as SociallyClick Pte Ltd (UEN: 20145992E as at [https://www.bizfile.gov.sg](https://www.bizfile.gov.sg)/) to provide one-stop e-commerce solution to other social enterprises. It was one of the 4-5 companies that I have setup between 2010 to 2014. 

My vision for SociallyClick was "**To nurture shopping for a good cause. One social enterprise, one shopper, and one click at a time**”, hence the name "SociallyClick".

SociallyClick was a social business founded on the basis of providing a one-stop e-Commerce platform such that various charity organisations and social enterprises could simply leverage it instead of reinventing the wheel themselves. In a nutshell, it was like the "Shopify for social enterprises". The key motivation was to reduce the duplication of work among these organisations and hence save significant IT and manpower costs so that these organisations could focus on their key competencies or businesses to fulfil their social obligations in a greater manner.

## 3-tier Architecture
I first started work in the 1990's and many of the systems I implemented for my professional career were typically using 3-tier architecture design. 3-tier architecture basically separates the presentation, application and data logic so that they can be developed and maintained separately without impacting the other tiers. For instance, you can update the look and feel of the website without necessarily changing the business logic or data access modules.

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

The first release of SociallyClick was implemented in 2013 using PHP, and subsequently, I refactored the 3-tier architecture design using AWS services such as EC2, and RDS. Multiple Availability Zones were used to protect the platform from the failure of a single location.

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

The solution was refactored a few times when newer AWS services were launched. Web servers were also moved to the private subnet for better security.

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

## AWS Lambda, Lambda and Lambda
While my solution was running in AWS, and it could scale when there was a sudden spike in web traffic, the 3-tier architecture design was less than ideal. It was a monolithic application. Period. Moreover, the application would still be incurring costs for the idling EC2 instances doing nothing productive at all.

So, when AWS Lambda was launched in 2014, I refactored the architecture once again. This refactoring allowed me to adopt a microservice architecture where I could achieve a lesser coupled design than the 3-tier architecture. I could also have some cost-savings as well since I would not have any idling EC2 instances anymore.

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

I would be honest to admit that at that time I was new and learning Lambda, Microservices, and Event-driven architecture, hence I was probably not doing a great job for the refactoring. Granted, the architecture was based on Lambda, but I ended up creating a spider web cob of Lambda functions and chaining many of them together. 

Yucks, this was ugly. And I think I did not understand Event-driven architecture enough then to incorporate AWS services as such Amazon SQS in my architecture design.

## Serverless Architecture
I was no longer running SociallyClick by early 2015, when a few months ago, I became the IT Assistant Director for a small and mid-size enterprise (SME) specialising in providing hardware and software solutions to some Travel Agencies in Singapore. Fair enough, I was not allowed to hold another Director position in another company as there could be a conflict of interests.

But I always love to reflect on my past projects to see how I can correct the past mistakes, learn from them and improve and simplify the solutions. Certain implementations may not be possible previously due to technical limitations but are no longer the issues today.

SociallyClick is always on my mind all these years. If I were managing it now, I would adopt the Serverless Architecture using AWS Step Functions, Amazon EventBridge etc., to name a few. This would overcome some of the limitations in my Lambda-based architecture for SociallyClick. I shall share the design and rationale behind it in Part 2.

Stay tuned!

## 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.