managing data in microservices - qcon san franciscomicroservices and events • events are a...

42
Managing Data in Microservices Randy Shoup @randyshoup linkedin.com/in/randyshoup

Upload: others

Post on 25-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Managing Data in Microservices

Randy Shoup @randyshoup

linkedin.com/in/randyshoup

Page 2: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Background• VP Engineering at Stitch Fix

o Using technology and data science to revolutionize clothing retail

• Consulting “CTO as a service”o Helping companies move fast at scale J

• Director of Engineering for Google App Engineo World’s largest Platform-as-a-Service

• Chief Engineer at eBayo Evolving multiple generations of eBay’s infrastructure

Page 3: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 4: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 5: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 6: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 7: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

PersonalizedRecommendations

InventoryAlgorithmic

recommendations

Machine learning

@randyshoup linkedin.com/in/randyshoup

Page 8: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Expert HumanCuration

Human curation

Algorithmic recommendations

@randyshoup linkedin.com/in/randyshoup

Page 9: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Data at the Center• 1:1 Ratio of Data Science to Engineering

o More than 100 software engineerso ~80 data scientists and algorithm developerso Unique ratio in our industry

• Apply intelligence to *every* part of the businesso Buyingo Inventory managemento Logistics optimizationo Styling recommendationso Demand prediction

• Humans and machines augmenting each other

@randyshoup linkedin.com/in/randyshoup

Page 10: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Design Goals• Feature Velocity

o Teams can move rapidly and independently

• Scalabilityo Components can be scaled independently depending on load

• Resilienceo Component failures are isolated, do not cascade

@randyshoup linkedin.com/in/randyshoup

Page 11: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

High-Performing Organizations

• Multiple deploys per day vs. one per month

• Commit to deploy in less than 1 hour vs. one week

• Recover from failure in less than 1 hour vs. one day

• Change failure rate of 0-15% vs. 31-45%

@randyshoup linkedin.com/in/randyshoup

https://puppet.com/resources/whitepaper/state-of-devops-report

Page 12: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

High-Performing Organizations

è2.5x more likely to exceed business goalso ProfitabilityoMarket shareo Productivity

@randyshoup linkedin.com/in/randyshoup

https://puppet.com/resources/whitepaper/state-of-devops-report

Page 13: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

“Tell us how you did things at Google and eBay.”…“Sure, I will tell you, but you have to promise not to do them![… yet]”

Page 14: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Evolution to Microservices

• eBay • 5th generation today• Monolithic Perl à Monolithic C++ à Java à microservices

• Twitter• 3rd generation today• Monolithic Rails à JS / Rails / Scala à microservices

• Amazon• Nth generation today• Monolithic Perl / C++ à Java / Scala à microservices

@randyshoup linkedin.com/in/randyshoup

Page 15: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

No one starts with microservices…

Past a certain scale, everyone ends up with microservices

Page 16: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

If you don’t end up regretting your early technology decisions, you probably over-engineered.

Page 17: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservices

• Single-purpose• Simple, well-defined interface• Modular and independent

A

C D E

B

Page 18: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservices

• Single-purpose• Simple, well-defined interface• Modular and independent• Isolated persistence (!)

A

C D E

B

Page 19: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Problem: Monolithic shared DB

• Clients• Shipments• Items• Styles, SKUs• Warehouses• etc.

stitchfix.com Styling app Warehouse app Merch app

CS app Logistics app Payments service Profile service

Page 20: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Decouple applications / services from shared DB

• Clients• Shipments• Items• Styles, SKUs• Warehouses• etc.

stitchfix.com Styling app Warehouse app Merch app

CS app Logistics app Payments service Profile service

Page 21: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Decouple applications / services from shared DB

Styling app Warehouse app

core_item

core_sku

core_client

Page 22: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Step 1: Create a service

Styling app Warehouse app

core_item

core_sku

core_client

client-service

Page 23: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Step 2: Applications use the service

Styling app Warehouse app

core_item

core_sku

core_client

client-service

Page 24: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Step 3: Move data to private database

Styling app Warehouse app

core_item

core_sku

client-service

core_client

Page 25: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Step 4: Rinse and Repeat

Styling app Warehouse app

core_sku

client-service

core_client

item-service

core_item

Page 26: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Extracting Microservices

• Step 4: Rinse and Repeat

Styling app Warehouse app

client-service

core_client

item-service

core_item

style-service

core_sku

Page 27: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

With Microservices, how do we do• Shared Data• Joins• Transactions

Page 28: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Events asFirst-Class Construct

• “A significant change in state”o Statement that some interesting thing occurred

• Traditional 3-tier systemo Presentation è interface / interactiono Application è stateless business logico Persistence è database

• Fourth fundamental building blocko State changes è eventso 0 | 1 | N consumers subscribe to the event, typically asynchronously

@randyshoup linkedin.com/in/randyshoup

Page 29: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservicesand Events

• Events are a first-class part of a service interface

• A service interface includeso Synchronous request-response (REST, gRPC, etc)o Events the service produceso Events the service consumeso Bulk reads and writes (ETL)

• The interface includes any mechanism for getting data in or out of the service (!)

@randyshoup linkedin.com/in/randyshoup

Page 30: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Shared Data

• Monolithic database makes it easy to leverage shared data

• Where does shared data go in a microservicesworld?

@randyshoup linkedin.com/in/randyshoup

Page 31: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Shared Data

• Principle: Single System of Recordo Every piece of data is owned by a single serviceo That service is the canonical system of record for that data

• Every other copy is a read-only, non-authoritative cache

@randyshoup linkedin.com/in/randyshoup

customer-servicestyling-service

customer-search

billing-service

Page 32: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Shared Data

• Approach 1: Synchronous Lookupo Customer service owns customer datao Fulfillment service calls customer service in real time

fulfillment-service

customer-service

@randyshoup linkedin.com/in/randyshoup

Page 33: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Shared Data

• Approach 2: Async event + local cacheo Customer service owns customer datao Customer service sends address-updated event when customer address

changeso Fulfillment service caches current customer address

fulfillment-servicecustomer-service

@randyshoup linkedin.com/in/randyshoup

Page 34: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Joins

• Monolithic database makes it easy to join tables

• Splitting the data across microservices makes joins very hard

@randyshoup linkedin.com/in/randyshoup

SELECT FROM A INNER JOIN B ON …

Page 35: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Joins

• Approach 1: Join in Client Applicationo Get a single customer from customer-serviceo Query matching orders for that customer from order-service

Customers

Orders

order-history-page

customer-service order-service

Page 36: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Joins

• Approach 2: Service that “Materializes the View”o Listen to events from item-service, events from order-serviceo Maintain denormalized join of items and orders together in local storage

Items Order Feedback

item-feedback-serviceitem-serviceorder-feedback-service

Page 37: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Joins

• Many common systems do thiso “Materialized view” in database systemso Most NoSQL systemso Search engineso Analytic systems

@randyshoup linkedin.com/in/randyshoup

Page 38: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Workflows and Sagas

• Monolithic database makes transactions across multiple entities easy

• Splitting data across services makes transactions very hard

@randyshoup linkedin.com/in/randyshoup

BEGIN; INSERT INTO A …; UPDATE B...; COMMIT;

Page 39: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Workflows and Sagas

• Transaction è Sagao Model the transaction as a state machine of atomic events

• Reimplement as a workflow

• Roll back by applying compensating operations in reverse

A B C

A B C

@randyshoup linkedin.com/in/randyshoup

Page 40: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques:Workflows and Sagas

• Many common systems do thiso Payment processingo Expense approval o Any multi-step workflow

@randyshoup linkedin.com/in/randyshoup

Page 41: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

With Microservices, how do we do• Shared Data• Joins• Transactions

Page 42: Managing Data in Microservices - QCon San FranciscoMicroservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Thanks!• Stitch Fix is hiring!

o www.stitchfix.com/careerso Based in San Franciscoo Hiring everywhere!o More than half remote, all across USo Application development, Platform engineering,

Data Science

• Please contact meo @randyshoupo linkedin.com/in/randyshoup