Top Banner
Managing Data in Microservices Randy Shoup @randyshoup linkedin.com/in/randyshoup
59

Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Sep 07, 2018

Download

Documents

lylien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Managing Data in Microservices

Randy Shoup @randyshoup

linkedin.com/in/randyshoup

Page 2: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Background• VP Engineering at Stitch Fix

o Combining “Art and Science” to revolutionize apparel retail

• Consulting “CTO as a service”o Helping companies scale engineering organizations and technology

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

• Chief Engineer / Distinguished Architect at eBayo Multiple generations of eBay’s infrastructure

@randyshoup linkedin.com/in/randyshoup

Page 3: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 4: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 5: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 6: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Stitch Fix

@randyshoup linkedin.com/in/randyshoup

Page 7: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Combining Art and [Data] Science

• 1:1 Ratio of Data Science to Engineeringo Almost 100 software engineerso Almost 100 data scientists and algorithm developerso Unique 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 8: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Styling at Stitch Fix

Personal styling

Inventory

@randyshoup linkedin.com/in/randyshoup

Page 9: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

PersonalizedRecommendations

InventoryAlgorithmic

recommendations

Machine learning

@randyshoup linkedin.com/in/randyshoup

Page 10: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Expert HumanCuration

Human curation

Algorithmic recommendations

@randyshoup linkedin.com/in/randyshoup

Page 11: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

How do we work, and why does it work?

Page 12: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

Practices

CultureTechnology

Organization

Page 13: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 14: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 15: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Small“Service” Teams

• Teams Aligned to Business Domainso Clear, well-defined area of responsibilityo Single service or set of related services

• Cross-functional Teamso All skill sets needed to do the job

• Depend on other teams for supporting services, libraries, and tools

@randyshoup linkedin.com/in/randyshoup

Page 16: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 17: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Test-Driven Development

• Tests help you go fastero Tests “have your back”o Development velocity

• Tests make better codeo Confidence to break thingso Courage to refactor mercilessly

• Tests make better systemso Catch bugs earlier, fail faster

@randyshoup linkedin.com/in/randyshoup

Page 18: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

“Do you have time to do it twice?”

“We don’t have time to do it right!”

Page 19: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Test-Driven Development

• Do it right (enough) the first timeo The more constrained you are on time and resources, the more important

it is to build solid featureso Build one great thing instead of two half-finished things

• Right ≠ Perfect (80 / 20 Rule)

• è Basically no bug tracking system (!)o Bugs are fixed as they come upo Backlog contains features we want to buildo Backlog contains technical debt we want to repay

@randyshoup linkedin.com/in/randyshoup

Page 20: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

ContinuousDelivery

• Most applications deployed multiple times per day

• More solid systemso Release smaller units of worko Smaller changes to roll back or roll forwardo Faster to repair, easier to understand, simpler to diagnose

• Rapid experimentationo Small experiments and rapid iteration are cheap

@randyshoup linkedin.com/in/randyshoup

Page 21: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 22: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

DevOps• End-to-end Ownership

o Team owns service from design to deployment to retirement

• Responsible for o Featureso Qualityo Performanceo Operationso Maintenance

@randyshoup linkedin.com/in/randyshoup

Page 23: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

You Build It, You Run It.-- Werner Vogels

Page 24: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 25: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 26: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

First Law of Distributed Object Design:

Don’t distribute your objects!-- Martin Fowler

Page 27: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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

-- me

Page 28: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservices

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

A

C D E

B

Page 29: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservices are nothing more than SOA done properly.

-- me

Page 30: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservices

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

A

C D E

B

Page 31: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

MicroservicePersistence

• Approach 1: Operate your own data storeo Store to your own instance(s) of {Postgres, MySQL, etc.}, owned and

operated by the service team

• Approach 2: Use a persistence serviceo Store to your own schema in {Dynamo, RDS, Spanner, etc.}, operated as a

service by another team or by a third-party providero Isolated from all other users of the service

• è Only external access to data store is through published service interface

@randyshoup linkedin.com/in/randyshoup

Page 32: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Events asFirst-Class Construct

• “A significant change in state”o Statement that some interesting thing occurredo 0 | 1 | N consumers subscribe to the event, typically asynchronously

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

• Fourth fundamental building blocko State changes è events

@randyshoup linkedin.com/in/randyshoup

Page 33: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Events asFirst-Class Construct

• Events represent how the real world workso Financeo Software development processo Any “workflow”

@randyshoup linkedin.com/in/randyshoup

Page 34: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 35: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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

@randyshoup linkedin.com/in/randyshoup

Page 36: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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

@randyshoup linkedin.com/in/randyshoup

Page 37: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Decouple applications / services from shared DB

Styling app Warehouse app

core_item

core_sku

core_client

@randyshoup linkedin.com/in/randyshoup

Page 38: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 1: Create a service

Styling app Warehouse app

core_item

core_sku

core_client

client-service

@randyshoup linkedin.com/in/randyshoup

Page 39: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 2: Applications use the service

Styling app Warehouse app

core_item

core_sku

core_client

client-service

@randyshoup linkedin.com/in/randyshoup

Page 40: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 3: Move data to private database

Styling app Warehouse app

core_item

core_sku

client-service

core_client

@randyshoup linkedin.com/in/randyshoup

Page 41: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 4: Rinse and Repeat

Styling app Warehouse app

core_sku

client-service

core_client

item-service

core_item

@randyshoup linkedin.com/in/randyshoup

Page 42: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 4: Rinse and Repeat

Styling app Warehouse app

client-service

core_client

item-service

core_item

style-service

core_sku

@randyshoup linkedin.com/in/randyshoup

Page 43: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Extracting Microservices

• Step 4: Rinse and Repeat

Styling app Warehouse app

client-service

core_client

item-service

core_item

style-service

core_sku

@randyshoup linkedin.com/in/randyshoup

Page 44: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Shared Data

• Problemo Monolithic database makes it easy to leverage shared datao Where does shared data go in a microservices world?

@randyshoup linkedin.com/in/randyshoup

Page 45: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 46: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 47: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 consumes event, caches current customer address

fulfillment-servicecustomer-service

@randyshoup linkedin.com/in/randyshoup

Page 48: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Shared Data

• Approach 3: Shared metadata library o Read-only metadata, basically immutableo E.g., size schemas, colors, fabrics, US States, etc.

receiving-serviceitem-service

style-service

@randyshoup linkedin.com/in/randyshoup

Page 49: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Joins

• Problemo Monolithic database makes joins very easyo Splitting the data into separate services makes joins very hard

@randyshoup linkedin.com/in/randyshoup

Page 50: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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

• Best for single A, multiple Bs (1:N join)

AB

order-history-page

customer-service order-service

Page 51: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Joins

• Many common systems do thiso Web application “mashup”

@randyshoup linkedin.com/in/randyshoup

Page 52: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Joins

• Approach 2: “Materialize the View”o Listen to events from item-service and order-feedback-serviceo Maintain denormalized join of items and order feedback in local storage

• Best for high cardinality A and B (M:N join)

Items Order Feedback

item-feedback-serviceorder-feedback-service

item-service

Page 53: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Joins

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

@randyshoup linkedin.com/in/randyshoup

Page 54: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Workflows and Sagas

• Problemo Monolithic database makes transactions across multiple entities easyo Splitting data across services makes transactions very hard

@randyshoup linkedin.com/in/randyshoup

Page 55: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 D

A B C D

@randyshoup linkedin.com/in/randyshoup

Page 56: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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 57: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Microservice Techniques:Workflows and Sagas

• Ideal use for Functions as a Service (“Serverless”)o Very lightweight logico Statelesso Triggered by an event

A B C D

A B C D

ƛ

ƛ

ƛ

ƛ

ƛ

ƛ

ƛ

ƛ

@randyshoup linkedin.com/in/randyshoup

Page 58: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

Modern SoftwareDevelopment

TDD and Continuous

Delivery

DevOpsMicroservices

Small Teams

Page 59: Managing Data in Microservices - QCon New York · • Most applications deployed multiple times per day ... MySQL, etc.}, owned and operated by the service team ... o Analytic systems

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