Week of Java: Part 3 - Designing and Implementing a Multi-Tier Lambda App
Tags: java, tutorial, aws, serverless
Published: 2022-05-11 19:14:53

Posted at [Serverless](https://www.serverless.com/blog/week-of-java-part-3-designing-and-implementing-a-multi-tier-lambda-app) by [Juan Urrego](https://medium.com/@juancho088)

![](https://cdn-images-1.medium.com/max/3200/0*pwtBLoML3_IcOjUU.png)

In part 1 and 2 of this series, we have covered the basics to set up our serverless project and development environment using emulation software. Now that we already understand the basics and tools, let’s go in deep with our architecture.

## Old Fashion Design Patterns Still Work

In the article [“Silence of the Lambdas”](https://www.3pillarglobal.com/insights/silence-lambdas-5-anti-patterns-aws-lambda), Dan Greene states that we shouldn’t stop regular best practices just because it’s serverless. The fact that we no longer need to maintain and set up servers, doesn’t mean that we stop thinking about[ quality attributes](https://medium.com/tech-travelstart/designing-with-your-head-not-with-your-guts-1ca2c961218f): performance, security, modifiability, etc.

Therefore, let’s take a look at a possible reference architecture that can help us in future serverless-based projects, as well as the tools and design patterns to achieve that.

## N-Tier Architecture

A [multi-tier or n-tier pattern](https://msdn.microsoft.com/en-us/library/ee658120.aspx) is a way to **distribute and group our logical components along our deployment nodes **for the sake of an independent execution structure. In an[ N-Tier architecture](https://docs.microsoft.com/en-us/azure/architecture/guide/architecture-styles/n-tier) the **logical layers** play a critical role, given that each layer, as a unit of implementation, **separate responsibilities and manage dependencies of common functionalities**. To give you an example, one tier can host multiple logical layers and, in other cases, there could belayers per tier. The next diagram exposes the previous statement:

![](https://cdn-images-1.medium.com/max/2000/0*3wddm3hmXhxoKA23.png)

In the previous diagram, the 1-Tier architecture is a typical example of an old fashion desktop app, where we used to have all the logical components deployed in the same execution node. The second case, the 4-tier architecture, is a modern case where the visual components and UI logic reside on the client side, while the business logic components are distributed in backend deployment nodes (as well as the data components).

That means that our **serverless backend may have multiple layers that are going to be deployed in the same AWS Lambda function**, but also we may have other lambda functions that could deploy other layers. One of the common anti-patterns that I have seen in [**Function As a Service](https://en.wikipedia.org/wiki/Function_as_a_service)**(FaaS) projects, is that developers put all the logic in the handling function without creating a separation, resulting in a single file with thousands of lines of code.

In a multi-layer pattern the most common practice is to use a **closed layer architecture**, meaning that a layer can only call the next layer immediately down, avoiding cyclic dependencies and improving security. However, some people have found benefit in an **open layer architecture**, where a layer can call any of the layers below. Personally, this latter tactic makes the source code messy and insecure, so **I recommend avoiding it**.

## Serverless Reference Architecture

Now that we already understand the main architectural pattern to use, let’s take a look at a **Serverless Reference Architecture**.

![](https://cdn-images-1.medium.com/max/2000/0*oc2TdUFt7BAgg1gB.png)

‍As you can see in diagram 2, I’m using a multi-tier architecture for the complete serverless product. In this case we have a 4-tier architecture conformed by the next tiers.

## Client Tier

![](https://cdn-images-1.medium.com/max/2000/0*Z-kZZmhEMujDofJy.png)

This tier renders the UI components of our product. Using [Client Side Rendering](https://threewill.com/what-is-client-side-rendering-and-why-would-i-use-it/) (CSR) via a[ Single Page Application](https://en.wikipedia.org/wiki/Single-page_application) (SPA) framework just like React, Angular, or Vue, is one of the most common ways to work here. I’m a true believer that **client devices are powerful machines that we need to take advantage of to reduce our on demand infrastructure costs** and state management that a [Server Side Rendering](https://codeburst.io/when-to-use-server-side-rendering-ssr-in-vue-js-projects-697bd925d57b) (SSR) generates. In terms of performance and SEO behavior, there’s still a[ debate about using CSR or SSR](https://medium.com/walmartlabs/the-benefits-of-server-side-rendering-over-client-side-rendering-5d07ff2cefe8), however I’m not going to go in detail about that.

These visual components need to be retrieved from a distribution server that is located in the next tier: **the presentation tier**, so let’s take a look at it.

## Presentation Tier

![](https://cdn-images-1.medium.com/max/2000/0*jmjoM82c8Fe-7KVJ.png)

This tier is responsible for exposing the front-end services that our clients will require to perform operations. In this case, I recommend having a [Content Delivery Network](https://en.wikipedia.org/wiki/Content_delivery_network) (CDN) such as[ Cloudfront](https://aws.amazon.com/es/cloudfront/) to cache and distribute your UI resources (located in our application tier). However, there are some non-AWS solutions such as [Cloudflare](https://www.cloudflare.com) that can provide not only caching services but also some security services such as[ Web Application Firewalls](https://en.wikipedia.org/wiki/Web_application_firewall) (WAF) and security rules to prevent [DoS and DDoS](https://en.wikipedia.org/wiki/Denial-of-service_attack) attacks (AWS also provides[ some of them](https://aws.amazon.com/waf/)).

![](https://cdn-images-1.medium.com/max/2000/0*ytmVIP3C_v3wf54I.png)

In this Tier we can find the [**API gateway](http://microservices.io/patterns/apigateway.html), **responsible for exposing our API services to our client apps and eventually third party systems. In the case of the[ AWS API Gateway](https://aws.amazon.com/api-gateway/), the lambda functions will be accessed using events, so even when the client communicates with the API using a [request-reply](https://en.wikipedia.org/wiki/Request%E2%80%93response) approach, behind the scenes we have an[ Event Driven Architecture](http://microservices.io/patterns/data/event-driven-architecture.html) (EDA) to manage our requests.

![](https://cdn-images-1.medium.com/max/2000/0*MdCHiBRNnAFC9yCb.png)

Finally, we’ll require an **authentication & authorisation service** for our clients. As usual, you can develop your own, but I recommend to use out-of-the-box solutions that use the best practices, most common frameworks and industry standards. In the case of AWS, we can use [Amazon Cognito](https://aws.amazon.com/cognito/) as our[ **Authentication as a Service](https://medium.freecodecamp.org/evaluating-authentication-as-a-service-providers-6903895a8450)** (AaaS) provider. Cognito will help us with the complete authentication and authorisation life cycle of our system’s users. There are other non-AWS services such as[ Auth0](https://auth0.com) or [Back&](https://www.backand.com/aws-lambda/) that you could also explore.

Remember that **this tier** **should be the only[ Demilitarised Zone](https://en.wikipedia.org/wiki/DMZ_(computing)) (DMZ) of your infrastructure**, therefore **the ****following ****tiers won’t have public access**.

## Application Tier

![](https://cdn-images-1.medium.com/max/2000/0*UWyqyU6LrA3ZbIpT.png)

Also called Business Tier, the Application Tier is in charge of grouping all the core components of our architecture, meaning: our functions. A critical concept in this tier, which I’m going to explain in detail in part 4, is the **Function as a Microservice** (FaaM), where every Lambda function should behave as a small microservice.

In a FaaM project, every communication between our functions should be [event driven](https://en.wikipedia.org/wiki/Event-driven_programming), meaning that we must reduce as much as possible the request-reply interaction between them, otherwise we could create an[ unnecessary coupling between our microservices](https://es.slideshare.net/AntonUdovychenko/going-serverless-with-cqrs-on-aws). To achieve this, we can use services such as [SQS](https://aws.amazon.com/sqs/),[ SNS](https://aws.amazon.com/sns/) or [Kinesis](https://aws.amazon.com/es/kinesis/).

![](https://cdn-images-1.medium.com/max/2000/0*p_DWTZM0TNDGF1H_.png)

Typically in non-serverless architectures, a Front-end web server would be used as a solution to host our UI resources and UI logical components. However, by using SPAs and CSR we can easily host our visual resources in [S3](https://aws.amazon.com/s3/?sc_channel=PS&sc_campaign=acquisition_ZA&sc_publisher=google&sc_medium=s3_b&sc_content=s3_e&sc_detail=aws%20s3&sc_category=s3&sc_segment=192064243191&sc_matchtype=e&sc_country=ZA&s_kwcid=AL!4422!3!192064243191!e!!g!!aws%20s3&ef_id=Wvr-MwAABXUeNQkf:20180515153931:s) to have a fully serverless architecture. By using a framework like React or Angular it’s just as easy as running **npm run build** and uploading the resulting files to an S3 Bucket. All the UI logic and state will be managed by the client side, thus eliminating the need of a web server.

**Note:** Don’t forget to create [security policies](https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) for your AWS Lambda functions and S3 Buckets. In the case of Lambda, you can locate the functions in a private network and secure them creating specific security groups and roles with just enough permissions.

## Data Tier

![](https://cdn-images-1.medium.com/max/2000/0*MO44E5ISrMGaZwf0.png)

The data tier is responsible for hosting all the services that will persist and manage our data ( [OLTP](https://en.wikipedia.org/wiki/Online_transaction_processing) databases,[ OLAP](https://en.wikipedia.org/wiki/Online_analytical_processing) databases, caches, etc). The diagram above is showing us an [RDS](https://aws.amazon.com/rds/) database using a[ multi-tenant pattern](https://d1.awsstatic.com/whitepapers/Multi_Tenant_SaaS_Storage_Strategies.pdf) in bridge: single database with multiple schemas (one per function/microservice), as well as DynamoDB tables for caching and keeping operational states. It’s important to keep in mind that, even when some people [embrace having a complete data tier with DynamoDB](https://read.acloud.guru/six-months-of-serverless-lessons-learned-f6da86a73526), the answer will be “it depends”.

## Support Area

![](https://cdn-images-1.medium.com/max/2000/0*_UsQfDa5UY-xtPJU.png)

‍

The support area contains Cloudformation S3 buckets, IAM policies, Route 53 Hosted Zones, Cloudwatch, among others. In general, it **audits, logs and monitors the actions of the other tiers**. We can use S3 buckets to persist structured log files as well as Cloudwatch to monitor our lambda functions and database efficiency.

**Final Thoughts**

This article presented a basic high-level web reference architecture for a serverless project. Based on that, it’s important to take into consideration the next points:

* The data tier is not necessarily 100% serverless and we need to understand the tradeoffs that those decisions might have.

* New technologies and trends shouldn’t replace/avoid the basics of software engineering and software architecture.

* Architectural and design patterns shouldn’t be forgotten.

* Keep your functions communication as asynchronous as possible using EDA patterns.

The next articles will explain in detail the **application and data tiers**. For that reason, the client and presentation tiers are out of the scope of this series. If you need help with those tiers, there are other [articles](https://hackernoon.com/hosting-static-react-websites-on-aws-s3-cloudfront-with-ssl-924e5c134455) that can help you.

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpeh44n0g9nr6z8huhet.png)](https://console.serverless.com/?view=register&utm_campaign=Console%20Signup&utm_source=dev.to&utm_medium=post&utm_content=Week%20of%20Java%3A%20Part%203%20-%20Designing%20and%20Implementing%20a%20Multi-Tier%20Lambda%20App)

Originally published at [Medium](https://medium.com/tech-travelstart/using-kotlin-in-a-serverless-architecture-with-aws-lambda-part-2-setting-up-your-local-4c5b40df85d5)

*Published at [https://www.serverless.com](https://www.serverless.com/blog/week-of-java-part-3-designing-and-implementing-a-multi-tier-lambda-app).*