More Related Content Similar to Building Real-time Serverless Backends (20) More from Amazon Web Services (20) Building Real-time Serverless Backends1. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Heitor Lessa
Specialist Solutions Architect, Amazon Web Services
Building Real-time Serverless Backends
with GraphQL
@heitor_lessa
2. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Why do we need real-time and offline
capabilities in the first place?
3. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Brief intro to Serverless
4. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
5. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Build and run
applications
without thinking about
servers
… pay per request not
for idle
“ Scales with usage
Never pay for idle
High availability
built-in
No servers
to provision
or manage
A serverless world…
6. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda is for all application types
Analytics
Operational management
Live Dashboards
Data workflows
Content management
ETL workflows
Interactive Backends
Mobile and web apps
Webhooks and Bots
Autonomous IT
Policy engines
Infrastructure management
7. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Building blocks for serverless applications
AWS Lambda
Amazon DynamoDB
Amazon SNS
Amazon API Gateway Amazon SQS Amazon Kinesis
Amazon S3
Orchestration and State Management
API Management and Real-time Backend Messaging and Queues Analytics
Monitoring and Debugging
Compute Storage Database
AWS X-RayAWS Step Functions Amazon Cognito
User Management and IdP
AWS AppSync Amazon Athena
AWS Lambda@Edge
8. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Building blocks for serverless applications
AWS Lambda
Amazon DynamoDB
Amazon SNS
Amazon API Gateway Amazon SQS Amazon Kinesis
Amazon S3
Orchestration and State Management
API Management and Real-time Backend Messaging and Queues Analytics
Monitoring and Debugging
Compute Storage Database
AWS X-RayAWS Step Functions Amazon Cognito
User Management and IdP
AWS AppSync Amazon Athena
AWS Lambda@Edge
9. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Has anyone said GraphQL?
10. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL APIs
Open, declarative data-fetching specification
!= Graph database
Use NoSQL, Relational, HTTP, etc.
Traditional data-fetching GraphQL
/posts
/postInfo
/postJustTitle
/postsByAuthor
/postNameStartsWithX
/commentsOnPost
/posts
11. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Schema
{
"id": "1",
"name": "Get Milk",
“priority": "1"
},
{
"id": “2",
"name": “Go to gym",
“priority": “5"
},…
type Query {
getTodos: [Todo]
}
type Todo {
id: ID!
name: String
description: String
priority: Int
duedate: String
}
query {
getTodos {
id
name
priority
}
}
Model data with
application schema
Client requests what it
needs
Only that data is
returned
12. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Types
Schema
type Event {
id: ID!
name: String
where: String
when: String
description: String
comments: [Comment]
}
type Comment {
commentId: String!
eventId: ID!
content: String!
createdAt: String!
}
13. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Mutation
Schema
Mutation
type Mutation {
createEvent(
name: String!,
when: String!,
where: String!,
description: String!
): Event
deleteEvent(id: ID!): Event
commentOnEvent(
eventId: ID!,
content: String!,
createdAt: String!
): Comment
}
14. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Query
Schema
Mutation
Query
type Query {
getEvent(id: ID!): Event
listEvents(
limit: Int,
nextToken: String
): EventConnection
}
15. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Subscription
Schema
Mutation
Query
Subscription type Subscription {
onComments(eventId: String!): Comment
@aws_subscribe(mutations: ["commentOnEvent"])
}
16. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL Spec - Subscription
Schema
Mutation
Query
Subscription type Subscription {
onComments(eventId: String!): Comment
@aws_subscribe(mutations: ["commentOnEvent"])
}
type Mutation {
…
commentOnEvent(
eventId: ID!,
content: String!,
createdAt: String!
): Comment
}
17. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraphQL - Summary
Schema
Mutation
Query
Subscription
Realtime? YES
Batching? YES
Pagination? YES
Relations? YES
Aggregations? YES
Search? YES
Offline? YES
18. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync – Intro
19. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What is AWS AppSync?
AWS AppSync is a managed service for
application data using GraphQL with real-time
capabilities and an offline programming model.
Real-time
Collaboration
Offline Programming
Model with Sync
Flexible Database
Options
Fine-grained
Access Control
20. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
How does AWS AppSync work?
21. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync and GraphQL Subscription
Near Realtime updates of data
Event based mode, triggered by Mutations
- Scalable model, designed as a platform for common use-cases
Can be used with ANY data source in AppSync
- Lambda, DynamoDB, Elasticsearch
mutation addPost(
id: 123
title: ”New post!”
author: ”Nadia”)
{
id
title
author
}
data: [{
id: 123,
title: ”New Post!”
author: ”Nadia”
}]
MQTT over Websockets
22. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync – Data Sources, Auth and more
DynamoDB
Table
Lambda
Function Elasticsearch
Service
GraphQL
Schema
Upload
Schema
GraphQL
Query
Mutation
Subscription
Real-time
Offline
AppSync
API
Cognito
User Pool
Legacy
Application
23. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
How does AppSync connect to my data sources?
24. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync – Debug Resolver Flow
Amazon CloudWatch logs
25. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What about Offline access?
26. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Offline patterns – Application Considerations
• Local Storage (R/W)
• Order of Operations
• Network State Management
• UI Updates
• Conflict Resolution
27. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AppSync – Debug Resolver Flow
Jane
Version : 2 Updated Document
Jane
Version : 2 Updated Document
Version : 3 Updated Document
Version : 1 New Document
Time
John
John
Jane goes offline
Jane comes back online
Version : 4 Updated Document
John
Jane
28. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS AppSync – Conflict Resolution
Conflict resolution in the cloud
1. Server wins
2. Silent reject
3. Custom logic (AWS Lambda)
- Optimistic version check
- Extend with your own checks
Optional
• Client callback for Conflict Resolution is
still available as a fallback
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id" : { "S" : "1" }
},
"condition" : {
"expression" : "attribute_not_exists(id)"
}
}
Example: Check that an ID doesn’t already exist:
"condition" : {
"expression" : "someExpression"
"conditionalCheckFailedHandler" : {
"strategy" : "Custom",
"lambdaArn" : "arn:..."
}
}
Run Lambda if version wrong:
29. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Offline best practices
• Mutations offline – what UIs actually need to be optimistic?
• Use Subscriptions appropriately
• Large payloads/paginated data: Queries
• Frequent updating deltas: Subscriptions
• Be kind to your customer’s battery & CPU!
• Don’t overcomplicate Conflict Resolution
• Data model appropriately, many app actions simply append to a list
• For custom cases, use a AWS Lambda and keep client logic light (race
conditions)
30. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Resources
31. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Additional Resources
aws.amazon.com/appsync/resources
aws-samples/
aws-mobile-appsync-chat-starter-angular
awslabs/
aws-appsync-codegen
dabit3/
vue-graphql-appsync
32. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Demo
33. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Please complete the session survey in the
summit mobile app