GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations...

31
Building Microservices with gRPC and NATS Shiju Varghese Consulting Solutions Architect April 28, 2017

Transcript of GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations...

Page 1: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Building Microservices with gRPC and NATSShiju Varghese Consulting Solutions Architect

April 28, 2017

Page 2: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

About Me

• Consulting Solutions Architect and Trainer• Focused on Golang, Microservices and Cloud-Native

distributed systems architectures• Published Author: “Web Development with Go” and “Go

Recipes”• Honoured with Microsoft MVP award seven times• Blog: https://medium.com/@shijuvar

Page 3: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Agenda

• Inter-Process communications in Microservicesarchitecture

• Building high performance APIs with gRPC and ProtocolBuffers

• Building Microservices with event-driven architecturesusing NATS

Page 4: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

MICROSERVICES ARCHITECTURE

Page 5: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Monolithic Architecture

Catalog Management

Customer Accounts

Orders Management

Payment

Monolithic Database

Monolithic E-Commerce App

eCom Store Web

Shipment

Page 6: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Moving to Microservices

Catalog Management

Customer Accounts

Orders Management

Payment

eCom Store Web

Shipment

Catalog DB

Accounts DB

Orders DB

Payment DB

Store DB

Shipment DB

Page 7: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Microservices

• Software broken up into functional components • Componentization via Services in which each service is

packaged as one unit of execution • Independent, autonomous process with no dependency

on other Microservices • Autonomous services around Bounded Context • Decentralization of data management • Independently replaceable and upgradeable

Page 8: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Challenges

• A business transaction may span into multiple services • Decentralization of data management • Communications between Microservices without having

any performance bottleneck

Page 9: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Inter-Process Communications between Microservices

• Communications over high performance APIs • Event-Driven architecture using messaging systems

Page 10: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Design Considerations for Building APIs

• Scaling APIs into millions (even billions) of APIs calls • Wire format; Serialisation and deserialisation of

messages • Building streaming APIs • RESTful Vs RPC? • Text encoding Vs binary encoding?

Page 11: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Why not REST

• Uses HTTP/1.x; Separate TCP Connection per request • Text on the wire; Not performance efficient • Harder API evolution • Not Domain-Specific • Not strongly-typed • Lack of streaming capabilities

Page 12: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

GRPC AND PROTOCOL BUFFERS

Page 13: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

What is gRPC

• High performance, open-source universal RPC framework

• A Cloud Native Computing Foundation(CNCF) project • Open source version of Google’s internal framework

Stubby • Uses Protocol Buffers as the IDL • HTTP/2 for transport • Bi-Directional streaming • RPC is efficient, domain-specific and strongly-typed • Works across languages and platforms

Page 14: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Protocol Buffers

• Google's language-neutral, platform-neutral, extensible mechanism for serialising structured data

• IDL - Describe once and generate interfaces for multiple languages

• Structure of the Request and Response • Binary format for network transmission • Supports multiple languages

Page 15: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Communication between gRPC Server and Client app

Page 16: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Types of RPC Methods

• Simple RPC • Server-side streaming RPC • Client-side streaming RPC • Bi-directional streaming RPC

Page 17: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

gRPC Workflow

ProtoBuf Definitions

protoc Compiler Go

Ruby

Java gRPC Server

gRPC Client Define 1 Compile 2

Implement 3Generate Code

protoc --go_out=plugins=grpc

Page 18: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

DEMO

Page 19: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

grpc-gateway - gRPC to JSON Proxy Generator

Page 20: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

BUILDING MICROSERVICES WITH EVENT-DRIVEN

ARCHITECTURE USING NATS

Page 21: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Inter-Process Communication Using an Event-Driven Architecture

Page 22: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Event-Sourcing

Aggregate ID Aggregate Type Event ID Event Type Event Data

301 Order 1001 OrderCreated …

301 Order 1002 OrderApproved …

301 Order 1003 OrderShipped …

301 Order 1004 OrderDelivered …

Event Table

Page 23: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

• Open source, lightweight, high-performance cloud native messaging system

• Highly performant, extremely light-weight; Capable of sending 11-12 million messages per second

• Publish-Subscribe messaging system • Available in two interoperable modules: • NATS Server • NATS Streaming Server (with persistent messaging)

Page 24: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Chart source: bravenewgeek.com/dissecting-message-queues

Page 25: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Messaging Patterns

• Publish-Subscribe • Queueing • Request-Replay

Page 26: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Components of Messaging Architecture

• Message: Messages are the unit of data exchange. A payload, which is used for exchanging the data between applications.

• Subject: Subject specifies the destination of messages. • Producer: Producers send messages to the NATS server. • Consumer: Consumers receive messages from the NATS

server. • Messaging Server: NATS Server distributes the messages

from producers to consumers.

Page 27: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Publish-Subscribe

Page 28: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

DEMO

Page 29: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Demo - Workflow

OrderService - gRPC Server

gRPC client

Subscriber

OrderCreated Event

Subscriber

SubscriberQueue Group

NATS publisher, which publishes events when new orders are created

RPC call for creating a new order

Example Source: https://github.com/shijuvar/gokit/tree/master/examples/grpc-nats

Page 30: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

Thank you

Page 31: GIDS2017 Microservices gRPC NATS - DeveloperMarch€¢ Focused on Golang, ... Design Considerations for Building APIs ... NATS Server distributes the messages from producers to consumers.

www.modsummit.com

www.developersummit.com