1 of 23

High Availability,

Cost Effective Messenger Bot with AWS Lambda Serverless Infrastructure

Rizki Romadhoni - 22.02.2017

[ , ].concat([ ])

===

2 of 23

High Availability

Refers to a system or component that is continuously operational for a desirably long length of time.

Availability can be measured relative to "100% operational" or "never failing."

3 of 23

Cost Effective

Producing good results without costing a lot of money.

Based on the traffic model of your app.

4 of 23

Chatbot

Example

  • Mark Zuckerberg: After a year of coding, here's Jarvis.

Chatbot Advantages

  • Single interface to do everything, (R.A.N)
  • No need to install apps
  • Feels natural

Becoming hype

Messaging app installs doesn't seem to decrease. Meanwhile, app uninstall rate stays high

5 of 23

Chatbot - Messenger Platform

Inbound - Messenger Webhook

Your entrypoint to receive messages from Messenger Webhook.

Responder - AI, dumb bot, human, IoT, etc.

Listens incoming messages in order to reply it with the desired response according to the message itself.

Outbound - Messenger Send API

Send responses to Messenger Send API.

6 of 23

Event-Driven Architecture

A software architecture pattern promoting the production, detection, consumption of, and reaction to events.

  1. Loosely coupled.
  2. Highly distributed.
  3. Flexible component layout.

7 of 23

Chatbot - Event-Driven Architecture

Messenger Webhook

Inbound

Outbound

Messenger Send API

Responder 1

Responder 2

Responder n

Message in

Message out

Inbound Message Queue

Outbound Message Queue

8 of 23

Serverless,

why?

  • Reduced operational cost - effective computing output.
  • Horizontal scaling is completely automatic, elastic, and managed by the provider.
  • That’s it.

AWS Lambda Pricing:

$0.0000002 / 100ms execution

Means $0.2 / 1M 100ms execution

9 of 23

Serverless Architecture

Overlapping Definition

There’s no one clear view of what ‘Serverless’ is, coming from two different but overlapping areas:

  • (Mobile) Backend as a service (BaaS)
  • Function as a Service (FaaS)

10 of 23

(Mobile) Backend as a Service

  • Everything is fully managed by provider.
  • Requires less or zero knowledge about system’s logic and state.
  • Less or zero control over system’s state.

11 of 23

Function as a Service

  • Run code without thinking about servers.
  • Pay for only the compute time you consume.
  • Distributed compute service to execute application logic in response to events
  • Scale as it happens.
  • Flexible component layout.
  • Optimization directly affects operational cost.
  • Available programming languages: JavaScript, Python, Java ( any JVM language).

12 of 23

How does this FaaS thingy work?

Client

Amazon API Gateway*

AWS

Lambda

HTTP Req

Trigger a Function

Node.js App

Container Cache

Exec Function

Result

Result

Spawned

Spawn App

Callback

HTTP Resp

13 of 23

Serverless Infrastructure

14 of 23

Serverless Frameworks

15 of 23

Events

Functions in FaaS are triggered by event types defined by the provider (AWS in this case:).

  • API Gateway (HTTP)
  • SNS (Event Notification)
  • DynamoDB / Kinesis Streams
  • S3 (File System)
  • Schedule
  • Alexa Skill

16 of 23

Functions

Independent functions that respond to your events.

Available configurations:

  • Memory
  • Timeout
  • Runtime

17 of 23

Chatbot - Serverless Infrastructure

Messenger Webhook

Messenger Send API

Message in

Message out

Inbound Function

Inbound Topic

Outbound Topic

Outbound Function

Responder n Function

Responder 1 Function

Responder 2 Function

18 of 23

CLI Cheatsheet

# Install serverless globally

$ npm install serverless -g

# Create an AWS Lambda function in Node.js

$ serverless create --template aws-nodejs

# Deploy to live AWS account

$ serverless deploy

# Function deployed!

$ http://api.amazon.com/users/update

19 of 23

Operational Cost - AWS

Assumption:

  • ~100K inbound and/or outbound messages per day, ~3M per month.

20 of 23

Operational Cost - AWS

Monthly operational cost:

  1. 3M AWS Lambda execution for inbound and/or outbound layer.
    1. (4.5 x 10^6) x (2 X 10^-7) = $0.9 Lambda
      1. 1.5 x 10^6 Inbound execution
      2. 1.5 x 10^6 Responder execution
      3. 1.5 x 10^6 Outbound execution
    2. (1.5 x 10^6) x (4.25 x 10^-6) = $6.37 API Gateway
      • 1.5 x 10^6 Inbound http event
    3. (3 x 10^6) x (5 x 10^7) = $1.5 SNS Publish (Deliveries to Lambda are free)
      • 1.5 x 10^6 Inbound message queue
      • 1.5 x 10^6 Outbound message queue

21 of 23

Operational Cost - AWS

Total per month:

Total: $0.9 + $6.37 + $1.5 = $8.77 per month for ~3M in/out messages.

These pricing are based on singapore region.

https://aws.amazon.com/sns/pricing/

https://aws.amazon.com/lambda/pricing/

22 of 23

References

23 of 23

Serverless allows us to build a highly available messenger bot without worrying about operational cost and scale out.