Serverless 'SPA' for tired cloud architects
Architecting applications for cloud is not an easy job. A few years back when we talked about 'going cloud', we had limited options like PaaS, IaaS etc. In last few of years, containers took the industry by storm and we started talking about microservices based architectures. As if this change was not disruptive enough, serverless architecture joined the party. Serverless architecture ( sometimes called FaaS ) is a very promising alternative to traditional Paas/IaaS approach. Serverless doesn't mean there are no servers. It only means servers are provisioned on demand by cloud providers. With serverless architecture, the burden of provisioning and maintaining the resources such as VMs, volumes, IP addresses, load balancers is take away from architects. Cloud providers such AWS, Microsoft, Google, IBM are already providing support for building serverless applications.
Another interesting paradigm which is around for a while is SPA - Single Page Applications. SPAs are web apps that load single html page and dynamically update the page as user interacts with the app. SPAs use Ajax/HTML/Javascript to create fluid and responsive web pages avoiding constant page reloads.
In this post, I will discuss how Serverless and SPA together make a great pair for building cloud-native applications based on microservices architecture. I will use an example of an imaginary movies booking application called www.i-love-movies.com where users can search movies, showtimes and book a show.
Architecture
Our movie application is built using AWS cloud ( No surprises ! ). Let's take a look at how we can use different AWS services to build application without provisioning even a single VM!

A] Hosting a website in S3: Our SinglePage application is built using AngularJS/HTML/Javascript/CSS technologies. But how do we server the content to users ? As we see in the digram, there are no VMs running webservers. Instead, Our application is hosted in S3 bucket with website hosting enabled. DNS records for www.i-love-movies.com are registered with AWS DNS service Route53. Optionally, we can use AWS CDN ( content delivery Network) for better performance.
B] Building backend microservices with Lambda and API Gateway: For dynamic interactions, we use AWS Lambda for building microservices such as movies service, showtime service etc. Advantage of Lambda is resources required to execute the functions are provisioned on demand and you pay only for the execution time and memory/CPU resources. Amazon API Gateway exposes the Lambda functions as REST APIs. Lambda function uses Amazon RDS ( database service ) for persistence.
C] AWS SDK for Javascript: This is another cool feature provided by AWS where one can interact directly with AWS services from javascript page ( See details ) . As shown in the diagram, SDK interacts with API gateway to call lambda functions. SDK also interacts with AWS Cognito to authenticate users through social identity providers such as Facebook, Twitter.
D] Deploying Serverless SPA with SAM: AWS cloud formation is a great tool for deploying AWS services as a single stack. Recently, AWS introduced Serverless Application Model ( SAM) which provides simplified syntax for expressing serverless resources. SAM extends AWS CloudFormation to add support for API Gateway, AWS Lambda etc. For monitoring and logs, AWS CloudWatch and CloudTrail support is available for Lambda functions.
Note: This is definitely not a complete architecture of movies application (e.g. payment gateway integration is missing.
Advantages
- No more headache of provisioning VMs, networking topology, firewalls, load balancing etc. No need to worry about autoscaling. AWS Lambda takes all the burden
- You pay only for the execution time and resources used by Lambda function. No more hourly billing.
- SAM simplifies overall deployment of application and Devops efforts
Drawbacks
- Migrating existing/legacy applications to Serverless architecture is not straightforward. This approach works well for new applications.
- Vendor lock in !
- Lambda function has certain limits. E.g. Time limit of 300 seconds. It is not meant for long running functions.
Conclusion
To summarize, Serverless Single Page application architecture provides a new way of building cool web applications. However, just like any new technology, it is not panacea. Happy Architecting !
References
- Martin Fowler: https://martinfowler.com/articles/serverless.html
- Serverless Application Model : http://blog.couchbase.com/2017/january/microservice-aws-serverless-application-model-couchbase
- AWS Lambda: http://docs.aws.amazon.com/lambda/latest/dg/welcome.html
- Single Page Applications : https://en.wikipedia.org/wiki/Single-page_application
Subscribe to:
Post Comments
(
Atom
)

No comments :
Post a Comment