Microservices and the Serverless Architecture have changed the way we think about web applications and partitioning logic. In this post, we'll share the best ways to structure your Serverless applications by applying the patterns directly on a simple example app we’re building — A Serverless Social Network. This fictional social network could have many features, but we’ll focus on the users and comments features to keep things simple.
Following months of experimentation and feedback we’ve summarized our findings into four patterns that the Serverless Framework embraces perfectly:
1. Microservices Pattern
2. Services Pattern
3. Monolithic Pattern
4. Graph Pattern
Let’s explore how we can build our Serverless Social Network using each one of these patterns, while outlining the benefits and drawbacks of each along with the configuration required for each pattern.
In the Microservices Pattern each job or functionality is isolated within a separate Lambda function. In the case of our example app, each Lambda function would also have a single http endpoint that serves as the entry point for that function.
service: serverless-social-network provider: aws functions: usersCreate: handler: handlers.usersCreate events: - http: post users/create commentsCreate: handler: handlers.commentsCreate events: - http: post comments/create
Benefits of Microservices Pattern:
Drawbacks of Microservices Pattern:
In the Services Pattern, a single Lambda function can handle a few (~4) jobs that are usually related via a data model or a shared infrastructure dependency. In our example app, all operations on the Users data model are performed in a single Lambda function, and multiple HTTP endpoints are created for all CRUD operations.
service: serverless-social-network provider: aws functions: users: handler: handler.users events: - http: post users - http: put users - http: get users - http: delete users comments: handler: handler.comments events: - http: post comments - http: put comments - http: get comments - http: delete comments
You can inspect the incoming HTTP request’s path and method by parsing the event body in your code, and then perform the correct operation in response. It’s like having a small router in the beginning of your Lambda code.
Benefits of Services Pattern:
Drawbacks of Services Pattern:
In the Monolithic Pattern your entire application is crammed into a single Lambda function. In our example app, our entire app is in a single Lambda function, all HTTP endpoints point to that Lambda function.
service: serverless-social-network provider: aws functions: socialNetwork: handler: handler.socialNetwork events: - http: post users - http: put users - http: get users - http: delete users - http: post comments - http: put comments - http: get comments - http: delete comments
Benefits of the Monolithic Pattern:
Drawbacks of the Monolithic Pattern:
The Graph Pattern is similar to the Monolithic Pattern, but it allows you to take advantage of GraphQL to reduce your entire REST API and all of its endpoints into 1–2 endpoints. As a result, your entire application will be composed of a single function and a 1–2 endpoints that handle GraphQL queries. GraphQL will then fetch the correct data in any form you need.
service: serverless-social-network provider: aws functions: socialNetwork: handler: handler.socialNetwork events: - http: get query
Benefits of the Graph Pattern:
Drawbacks of the Graph Pattern:
Our team is currently playing with the Graph pattern, except we’ve isolated each GraphQL query into a second tier of Lambda functions. This helps retain a microservices architecture w/ GraphQL. It is what we’re internally calling a Graph Gateway pattern. We’ll write more on this in the near future, as it’s still in the testing phase.
We’ve explored four patterns that you can use to build your Serverless applications. Everyone has different requirements and preferences, so we’ve made sure the Serverless Framework can support all of the above patterns, easily. Enjoy, and good luck!
About the Author: Eslam Hefnawy is a senior developer at Serverless Inc. He leads the team in charge of building and maintaining the Serverless Framework — an application framework for building web, mobile and IoT applications powered by AWS Lambda, AWS API Gateway and in the future other FaaS providers. Connect with Eslam at http://eahefnawy.com/ or on Github.
Eslam Hefnawy is a senior developer at Serverless Inc.
guides-and-tutorialsoperations-and-observability - 11.01.18
We'll show you how to process a video file that extracts a thumbnail in Amazon ECS using Fargate and Lambda
written by Rupak Ganguly
operations-and-observabilityguides-and-tutorialsnews - 04.12.17
Learn what Serverless Aurora is, what it means for serverless developers, and why we think it's the future of data. Solving the serverless data layer.
written by Alex DeBrie