AWS AppSync

Natraj T
hubbleconnected
Published in
4 min readAug 24, 2018

Introduction

Hubble Connected is an IOT company. We have 1.2 million registered users. The users have connected devices such as cameras and other sensors. These devices capture and push snaps, clips to server(cloud), to the respective users’ accounts. We had traffic like 16k notification per minute. Whenever devices uploads the clips and snaps to server, we need to update the user that new Motion/Sound is detected. But we had a lot of loopholes in our system. So this blog will explain you what was fault in our current design and how we overcome it using AppSync.

Old Design and problems it caused:

GET/POST Events WorkFlow

Below is the flow for POST events

  1. Device pushes the events to server.
  2. Server validates and persists in DynamoDB.
  3. Server send notification to all the logged in apps.

Algorithm for GET/DELETE events

1) App calls GET Events, server returns the latest 100/50 based on page size.

2)App calls delete events. Server deletes the data in DynamoDB.

The Problems with the above flow:

1) Whenever new event is pushed by device, app doesn’t get real time update without user refreshing the page.

2) When the app calls the GET events it loads all events rather than only the latest events

Ex: There are total 40 events. When app call get events for first time server returns 40 events.New event is pushed by device. Now when the app call get events , only the new event should be sent to app. By currently we are returning 41 events.

3) Wastage of IOPS (Point 2) and bandwidth.

4) High latency.

5) We need to develop a new version of endpoint , if app needs more attributes in response from server.

AWS AppSync

The AWS AppSync GraphQL service offers real-time updates and built-in offline support along with first class integration with ElasticSearch, DynamoDB, and AWS Lambda.

Design using AppSync

POST events using AppSync
  1. Device pushes the event to server. Server persist the event using AppSync in DynamoDB.
  2. AppSync pushes the new event to all the subscribed apps.
GET/Delete Events using AppSync

1) App call the GET events . AppSync api returns the paginated results.

2) App calls the delete AppSync api for deleting events. Record is removed from all the subscribed clients cache once deleted in server successfully.

Challenges with using AppSync:

  1. Authentication

Appsync sync supports :

We had our own authentication server .So we used AWS_IAM with AWS STS.The AWS Security Token Service (STS) is a web service that enables you to request temporary, limited-privilege credentials for AWS Identity and Access Management (IAM) users or for users that you authenticate.

GetSessionToken

Returns a set of temporary credentials for an AWS account or IAM user. The credentials consist of an access key ID, a secret access key, and a security token.The GetSessionToken operation must be called by using the long-term AWS security credentials of the AWS account or an IAM user.

Authentication Flow

1) App calls the login server Api with user name and password.

2) Server authenticates and returns the access key ID, a secret access key, and a security token.

3) App uses these key to access AWS resources(AppSync).

2. Fetching records from parent-child table. From the event type declaration, we can see the comments(limit: Int, nextToken: String). We need to configure the resolver for this . Because each and every field in Appsync is a function. Configure the resolver for comments otherwise it fetches only the parent data and returns null for child records. Whenever the child records are returned null, it indicates resolver is not configured.

3. Testing the subscription

This problem will be faced by beginners. Once we configure the subscription resolver, we need to test it if its working fine. This can be done using AppSync console.

Ex:

subscription {
subscribeToDeleteEvent{
id
}}

Conclusion:

AppSync is good for rapid prototyping and development with GraphQL.Also building real time apps, automatically managing offline user’s data. But one must take into account pricing of AppSync also. Appsync charges for Data modification,Data transfer, Real-time update and Connectivity charges.So it should only be used , if you have less data modification requests and Subscriptions.

AppSync Pricing:

Refer this page for pricing: https://aws.amazon.com/appsync/pricing/

For schema refer this link https://docs.aws.amazon.com/appsync/latest/devguide/quickstart-launch-a-sample-schema.html

Links:

--

--