50
Managing Data at Scale: Microservices and Events Randy Shoup @randyshoup linkedin.com/in/randyshoup

Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Managing Data at Scale: Microservices and Events

Randy Shoup @randyshoup

linkedin.com/in/randyshoup

Page 2: Managing Data at Scale: Microservices and Events · Microservices 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  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 Engine

o  World’s largest Platform-as-a-Service

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

Page 3: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

Page 4: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

Page 5: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

Page 6: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Stitch Fix

Page 7: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Combining Art and [Data] Science

•  1:1 Ratio of Data Science to Engineering o  >75 software engineers o  >75 data scientists and algorithm developers o  Unique in our industry

•  Apply intelligence to *every* part of the business o  Buying o  Inventory management o  Logistics optimization o  Styling recommendations o  Demand prediction

•  Humans and machines augmenting each other

Page 8: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Styling at Stitch Fix

Personal styling

Inventory

Page 9: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Personalized Recommendations

Inventory Algorithmic recommendations

Machine learning

Page 10: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Expert Human Curation

Human curation

Algorithmic recommendations

Page 11: Managing Data at Scale: Microservices and Events · Microservices 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 12: Managing Data at Scale: Microservices and Events · Microservices 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 13: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

First Law of Distributed Object Design: Don’t distribute your objects!

-- Martin Fowler

Page 14: Managing Data at Scale: Microservices and Events · Microservices 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 15: Managing Data at Scale: Microservices and Events · Microservices 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 16: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservices are nothing more than SOA done properly.

-- me

Page 17: Managing Data at Scale: Microservices and Events · Microservices 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 18: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Persistence

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

operated by the service team

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

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

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

Page 19: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservices

•  Each unit is simple

•  Independent scaling and performance

•  Independent testing and deployment

•  “Optimal” technology stack

•  Security boundary

•  Multiple cooperating units

•  Exchange in-process for network latencies

•  More sophisticated deployment and monitoring tools

•  Overall system complexity

Pros Cons

Page 20: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Why Rearchitect?

•  Velocity o  Time to market is constrained by coupling and lack of isolation in the

monolith o  Teams step on each others’ toes, and can no longer develop

independently o  Difficult for new engineers to be productive

•  Scaling o  Vertical scaling of the monolith no longer works

o  Parts of the system need to scale independently of others

Page 21: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Why Rearchitect?

•  Deployment o  Parts of the system need to deploy independently of others o  Monolithic release is too slow, too complicated, too risky

Page 22: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

“The only thing a Big Bang migration guarantees is a big *Bang*.”

-- Martin Fowler

Page 23: Managing Data at Scale: Microservices and Events · Microservices 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 24: Managing Data at Scale: Microservices and Events · Microservices 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 25: Managing Data at Scale: Microservices and Events · Microservices 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 26: Managing Data at Scale: Microservices and Events · Microservices 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 27: Managing Data at Scale: Microservices and Events · Microservices 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 28: Managing Data at Scale: Microservices and Events · Microservices 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 29: Managing Data at Scale: Microservices and Events · Microservices 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 30: Managing Data at Scale: Microservices and Events · Microservices 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 31: Managing Data at Scale: Microservices and Events · Microservices 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 32: Managing Data at Scale: Microservices and Events · Microservices 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 33: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Events as First-Class Construct

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

•  Traditional 3-tier system o  Presentation è interface / interaction

o  Application è stateless business logic o  Persistence è database

•  Fourth fundamental building block o  State changes è events o  0 | 1 | N consumers subscribe to the event, typically asynchronously

@randyshoup linkedin.com/in/randyshoup

Page 34: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservices and Events

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

•  A service interface includes o  Synchronous request-response (REST, gRPC, etc) o  Events the service produces o  Events the service consumes o  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 at Scale: Microservices and Events · Microservices 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 microservices world?

@randyshoup linkedin.com/in/randyshoup

Page 36: Managing Data at Scale: Microservices and Events · Microservices 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 Record o  Every piece of data is owned by a single service o  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-service styling-service

customer-search

billing-service

Page 37: Managing Data at Scale: Microservices and Events · Microservices 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 Lookup o  Customer service owns customer data o  Fulfillment service calls customer service in real time

fulfillment-service

customer-service

@randyshoup linkedin.com/in/randyshoup

Page 38: Managing Data at Scale: Microservices and Events · Microservices 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 cache o  Customer service owns customer data o  Customer service sends address-updated event when customer address

changes o  Fulfillment service caches current customer address

fulfillment-service customer-service

@randyshoup linkedin.com/in/randyshoup

Page 39: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques: Shared Data

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

receiving-service item-service

style-service

Page 40: Managing Data at Scale: Microservices and Events · Microservices 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 41: Managing Data at Scale: Microservices and Events · Microservices 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 Application o  Get a single customer from customer-service o  Query matching orders for that customer from order-service

Customers

Orders

order-history-page

customer-service order-service

Page 42: Managing Data at Scale: Microservices and Events · Microservices 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-service o  Maintain denormalized join of items and orders together in local storage

Items Order Feedback

item-feedback-service item-service

order-feedback-service

Page 43: Managing Data at Scale: Microservices and Events · Microservices 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 this o  “Materialized view” in database systems o  Most NoSQL systems o  Search engines o  Analytic systems

@randyshoup linkedin.com/in/randyshoup

Page 44: Managing Data at Scale: Microservices and Events · Microservices 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 45: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques: Workflows and Sagas

•  Transaction è Saga o  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 46: Managing Data at Scale: Microservices and Events · Microservices 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 this o  Payment processing o  Expense approval o  Travel o  Any multi-step workflow

@randyshoup linkedin.com/in/randyshoup

Page 47: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Microservice Techniques: Workflows and Sagas

•  Simple event-driven processing

o  Very lightweight logic o  Stateless o  Triggered by an event

•  è Consider Function-as-a-Service (“Serverless”)

A B C

A B C

@randyshoup linkedin.com/in/randyshoup

ƛ ƛ ƛ

ƛ ƛ ƛ

Page 48: Managing Data at Scale: Microservices and Events · Microservices 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 49: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Events!

Page 50: Managing Data at Scale: Microservices and Events · Microservices and Events • Events are a first-class part of a service interface • A service interface includes o Synchronous

Thank You!•  @randyshoup

•  linkedin.com/in/randyshoup