plataforma distribuída de microserviços ou, como a olist funciona

68
OLIST ARCHITECTURE

Upload: osvaldo-santana

Post on 21-Jan-2018

835 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Plataforma distribuída de Microserviços ou, como a Olist funciona

OLIST ARCHITECTURE

Page 2: Plataforma distribuída de Microserviços ou, como a Olist funciona

MerchantsOnline & Offline

Page 3: Plataforma distribuída de Microserviços ou, como a Olist funciona

BEFORE…

Page 4: Plataforma distribuída de Microserviços ou, como a Olist funciona

OLIST VERSION ONE (AKA V1)

AngularJS

Page 5: Plataforma distribuída de Microserviços ou, como a Olist funciona

OLIST VERSION ONE (AKA V1)

AngularJS

COMPLEX!

Page 6: Plataforma distribuída de Microserviços ou, como a Olist funciona

MONOLITHIC

Page 7: Plataforma distribuída de Microserviços ou, como a Olist funciona

MONOLITHICNo Scalability

Page 8: Plataforma distribuída de Microserviços ou, como a Olist funciona

MONOLITHICNo Scalability

No Reliability

Page 9: Plataforma distribuída de Microserviços ou, como a Olist funciona

MONOLITHICNo Scalability

No Reliability No Safety

Page 10: Plataforma distribuída de Microserviços ou, como a Olist funciona

THEN…

Page 11: Plataforma distribuída de Microserviços ou, como a Olist funciona

LET'S WRITE A NEW VERSION

Page 12: Plataforma distribuída de Microserviços ou, como a Olist funciona

REQUIREMENTS

Page 13: Plataforma distribuída de Microserviços ou, como a Olist funciona

SIMPLICITY

Page 14: Plataforma distribuída de Microserviços ou, como a Olist funciona

SCALABILITY

Page 15: Plataforma distribuída de Microserviços ou, como a Olist funciona

RESILIENCE

Page 16: Plataforma distribuída de Microserviços ou, como a Olist funciona

MODULARITY

Page 17: Plataforma distribuída de Microserviços ou, como a Olist funciona

SAFETY

Page 18: Plataforma distribuída de Microserviços ou, como a Olist funciona

PREMISSE

• No matter if a system is internal or external, it eventually…

• … goes offline…

• … crashes…

• … or change their behaviour without notice.

Page 19: Plataforma distribuída de Microserviços ou, como a Olist funciona

A NEW ARCHITECTURE

Page 20: Plataforma distribuída de Microserviços ou, como a Olist funciona

MICROSERVICES

Page 21: Plataforma distribuída de Microserviços ou, como a Olist funciona

MICROSERVICES or SOA?

Page 22: Plataforma distribuída de Microserviços ou, como a Olist funciona

MICROSERVICES or SOA?

Page 23: Plataforma distribuída de Microserviços ou, como a Olist funciona

IMPLEMENTATION MODELS

Page 24: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 25: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

• RESTful communication between services

• Synchronous Service Calls

• Strict dependency between services

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 26: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

• RESTful communication between services

• Synchronous Service Calls

• Strict dependency between services

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 27: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

• RESTful communication between services

• Synchronous Service Calls

• Strict dependency between services

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 28: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

• RESTful communication between services

• Synchronous Service Calls

• Strict dependency between services

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 29: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION VIA API

• RESTful communication between services

• Synchronous Service Calls

• Strict dependency between services

• No resilience

• No safety* (eg. data loss on request failures)

A P I

A P I

G a t e w a y

A P I

A P IHTTP Requests

Page 30: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Page 31: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Page 32: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Page 33: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Page 34: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Page 35: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Task #4

call task #4

Page 36: Plataforma distribuída de Microserviços ou, como a Olist funciona

Workers

ASYNC TASK EXECUTION• Asynchronous Procedure Calls

• Queue based task execution

• Client must knows about their dependents (I've to change API every time I need to add a new task)

• No decoupling ➡ No modularity

• No safety* (eg. data loss on deployment failures)

A P I

Task #1

Task #2

Task #3

call task #1call task #2call task #3

Task #4

call task #4

Page 37: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGE EVENT TRIGGERING

Event

A P I

Service Consumer

Service Consumer

Service Consumer

queues

Page 38: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGE EVENT TRIGGERING

• Action Event triggering

• Queue based message event handling

• Event as messages

Event

A P I

Service Consumer

Service Consumer

Service Consumer

queues

Page 39: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGE EVENT TRIGGERING

• Action Event triggering

• Queue based message event handling

• Event as messages

Event

A P I

Service Consumer

Service Consumer

Service Consumer

queues

Page 40: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGE EVENT TRIGGERING

• Action Event triggering

• Queue based message event handling

• Event as messages

Event

A P I

Service Consumer

Service Consumer

Service Consumer

queues

Page 41: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGE EVENT TRIGGERING

• Action Event triggering

• Queue based message event handling

• Event as messages

• It works!

Event

A P I

Service Consumer

Service Consumer

Service Consumer

queues

Page 42: Plataforma distribuída de Microserviços ou, como a Olist funciona

BASIC BUILDING BLOCKS

Page 43: Plataforma distribuída de Microserviços ou, como a Olist funciona

MESSAGES

• Also known as Resource in REST context

• Follow a contract (schema)

• Can be enveloped with metadata (eg. SNS/SQS metadata)

Page 44: Plataforma distribuída de Microserviços ou, como a Olist funciona

GLOBAL TOPICS• Publisher in PubSub Pattern

• Global topics for message publication

• Topics belong to the system (or architecture) and not to a (micro)service

• We use AWS SNS

• Kafka was not available on our hosting provider (Heroku) at the time we choose SNS

Page 45: Plataforma distribuída de Microserviços ou, como a Olist funciona

SERVICE QUEUES

• Subscribers in PubSub Pattern

• Queues subscribe topics

• One queue belong exclusively to one (micro)service

• We use AWS SQS

• SQS can be used as a SNS subscriber

Page 46: Plataforma distribuída de Microserviços ou, como a Olist funciona

API• Main Data Entry Point

• Data Validation

• Data Persistence

• Workflow Management (eg. status and state machine)

• Event Triggering

• Idempotency handling (eg. discard duplicated requests returning a HTTP 304 Not Modified)

• Python 3, Django REST Framework

A P I

Page 47: Plataforma distribuída de Microserviços ou, como a Olist funciona

WEBHOOK

• Data Entry Point

• No Data Persistence

• Proxy HTTP ➡ SNS

• Event Triggering

• Python 3, Django REST Framework

W e b h o o k

Page 48: Plataforma distribuída de Microserviços ou, como a Olist funciona

SERVICE• Event Handling / Message Processing

• Business Logic

• Some service could trigger some events but it's not so usual

• Service Types:

• Dequeuer - process messages from one queue

• Broker - process messages from multiple queues

• Python 3, Loafer / Asyncio

API #1 Event #1

Service

Page 49: Plataforma distribuída de Microserviços ou, como a Olist funciona

JOB

• Scheduled Job

• No Persistence

• Event Triggering

• Python 3, etc.

Job⏲

Page 50: Plataforma distribuída de Microserviços ou, como a Olist funciona

CLIENT

• Web or Mobile Applications

• No Persistence (or basic persistence)

• Web presentation of APIs' resources

• Python 3, Django

W e b A p p

A P I

Page 51: Plataforma distribuída de Microserviços ou, como a Olist funciona

LIBRARIES

• Common Libraries — common utilities for APIs and Services (eg. event triggering/topic publishing)

• Client Libraries — libraries to connect our APIs

• Auth Library — library to handle authentication basics

• Open Source Libraries — useful libraries for community (eg. correios)

Page 52: Plataforma distribuída de Microserviços ou, como a Olist funciona

TOOLS

• Legacy Data Migrator — tool that connects in all databases and provides a small framework for data migration. It uses Kenneth Reiz's records library. It was initially used to migrate data from the old version of our application.

• Toolbelt — tool that provide basic management commands to interact with our APIs, partner APIs and to make ease to manage SQS queues or trigger some events in SNS Topics.

Page 53: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 54: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 55: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 56: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 57: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 58: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 59: Plataforma distribuída de Microserviços ou, como a Olist funciona

WORKING SAMPLE posting list and shipping label generation

Webapp FulfillmentAPI

httprequesttopic

triggerevent

queueplp

generatingservice

InternalCarrier

APItopic

queue

plp-closing-service

External Service

success

queue• generate pdf• upload pdf to s3• set tracking code and

pdf url at fulfillment-api

Page 60: Plataforma distribuída de Microserviços ou, como a Olist funciona

DEVELOPMENT

Page 61: Plataforma distribuída de Microserviços ou, como a Olist funciona

REMOTE TEAM

Page 62: Plataforma distribuída de Microserviços ou, como a Olist funciona

DEVELOPMENT TOOLS

• Github - code management

• CircleCI - continuous integration

• vim / PyCharm / SublimeText / etc - development

Page 63: Plataforma distribuída de Microserviços ou, como a Olist funciona

COMMUNICATION TOOLS• JIRA - project management

• Confluence - documentation

• Google Apps - Mail, Calendar, Docs

• Slack - chat and monitoring integrations

• Mumble - voice conference

• tmux, ngrok, vim (and mumble) - pair programming

Page 64: Plataforma distribuída de Microserviços ou, como a Olist funciona

DEPLOYMENT

Page 65: Plataforma distribuída de Microserviços ou, como a Olist funciona
Page 66: Plataforma distribuída de Microserviços ou, como a Olist funciona

• Hosted on Heroku PaaS (our secret weapon!)

• Easy deployment, configuration management, log handling, etc

• Heroku PostgreSQL Database

• Easy deployment, easy configuration and setup, easy backup, replica and foreign data wrappers

• Other services and tools

• Logentries, Sentry and New Relic

Page 67: Plataforma distribuída de Microserviços ou, como a Olist funciona

CHALLENGES

Page 68: Plataforma distribuída de Microserviços ou, como a Olist funciona

• It's hard to make evolution of message/resource contracts between services

• All sequential process must be splitted over multiple (small) services

• Denormalization of data can easily lead to problems of data consistency if we do not take certain precautions

• Information needed for one API must be replicated through services and stored locally

• Data migration or refactoring in several services requires the development of an specific application