Slide 1

Slide 1 text

Extracting services from a monolith @janogonzalez - October 2017

Slide 2

Slide 2 text

Monoliths

Slide 3

Slide 3 text

$ rails new soundcloud Monoliths How SoundCloud started

Slide 4

Slide 4 text

Monoliths All functionality in the same place User Track Like

Slide 5

Slide 5 text

Monoliths Usually layered Backend DB UI

Slide 6

Slide 6 text

Monoliths More likely to have teams per technology

Slide 7

Slide 7 text

Monoliths We had problems as the team started to grow

Slide 8

Slide 8 text

Monoliths Our delivery was too slow

Slide 9

Slide 9 text

Microservices

Slide 10

Slide 10 text

Microservices Bounded contexts User Track Like

Slide 11

Slide 11 text

Microservices Domain model fits in your head Like Track Likes Count User Likes Count

Slide 12

Slide 12 text

Microservices Each bounded context belongs to a service Likes Service

Slide 13

Slide 13 text

Microservices More likely to have cross-functional teams

Slide 14

Slide 14 text

Microservices Faster delivery

Slide 15

Slide 15 text

The requisites

Slide 16

Slide 16 text

The requisites Easily allocate computing resources

Slide 17

Slide 17 text

The requisites Monitoring

Slide 18

Slide 18 text

The requisites Continuous delivery

Slide 19

Slide 19 text

The problems

Slide 20

Slide 20 text

The problems Cascading failures

Slide 21

Slide 21 text

● Circuit breakers ● Failure accrual The problems Cascading failures

Slide 22

Slide 22 text

The problems Eventual consistency (A: a1,a2; B: b1) (A;B) (A;C) (A: a1, a2) (B: b1, b2)

Slide 23

Slide 23 text

● Define authoritative sources ● Compose at the edge ● Use lifecycle events when necessary The problems Eventual consistency

Slide 24

Slide 24 text

The problems Too many stacks

Slide 25

Slide 25 text

● Standardize RPC and async mechanisms ● Provide support levels for different tech stacks The problems Too many stacks

Slide 26

Slide 26 text

The problems Protecting against API changes

Slide 27

Slide 27 text

● Tolerant readers ● Consumer driven contracts The problems Protecting against API changes

Slide 28

Slide 28 text

The problems Increased chattiness

Slide 29

Slide 29 text

● API Gateway ● BFF The problems Increased chattiness

Slide 30

Slide 30 text

Our architecture

Slide 31

Slide 31 text

Our architecture High level overview

Slide 32

Slide 32 text

Our architecture Layers

Slide 33

Slide 33 text

Extracting services

Slide 34

Slide 34 text

Identify a candidate Does this feature need many changes? Is this feature causing technical risk? Choose an integration approach How to integrate the extracted service? Choose a migration strategy for data How to move the data to its own storage? Extracting services The process 1 2 3

Slide 35

Slide 35 text

Identify a candidate Does this feature need many changes? Is this feature causing technical risk? Choose an integration approach How to integrate the extracted service? Choose a migration strategy for data How to move the data to its own storage? Extracting services The process 1 2 3

Slide 36

Slide 36 text

Choose an integration approach Monolith as gateway BEFO R E Monolith

Slide 37

Slide 37 text

Choose an integration approach Monolith as gateway Monolith Service Service AFTER

Slide 38

Slide 38 text

Introduce a service layer for the feature If you don’t have it already Add an alternate path under a feature flag Beware that in-memory joins than may be needed Switch the traffic Now the code can be cleaned up Monolith as a gateway The process 2.1 2.2 2.3

Slide 39

Slide 39 text

def index likes = user_likes_service.public_track_likes( @user, pagination) respond collection_for(likes) end Monolith as a gateway Introduce a service layer for the feature

Slide 40

Slide 40 text

Introduce a service layer for the feature If you don’t have it already Add an alternate path under a feature flag Beware that in-memory joins than may be needed Switch the traffic Now the code can be cleaned up Monolith as a gateway The process 2.1 2.2 2.3

Slide 41

Slide 41 text

def public_track_likes(user, pagination) if $feature.active?(:use_likes_service, user) … else … end end Monolith as a gateway Add an alternate path under a feature flag

Slide 42

Slide 42 text

Introduce a service layer for the feature If you don’t have it already Add an alternate path under a feature flag Beware that in-memory joins than may be needed Switch the traffic Now the code can be cleaned up Monolith as a gateway The process 2.1 2.2 2.3

Slide 43

Slide 43 text

Choose an integration approach Strangler Monolith BEFO R E

Slide 44

Slide 44 text

Choose an integration approach Strangler Strangler STR AN G LIN G Monolith

Slide 45

Slide 45 text

Choose an integration approach Strangler Strangler Service Monolith AFTER

Slide 46

Slide 46 text

Choose an integration approach Strangler Strangler Service Service SO M E D AY... Service Service

Slide 47

Slide 47 text

Introduce handlers for the paths This allows for intervention Add an alternate path under a feature flag This may imply invoking not only the new service Switch the traffic Now the code can be cleaned up Strangler The process 2.1 2.2 2.3

Slide 48

Slide 48 text

def index handle(request) end Strangler Introduce handlers for the paths

Slide 49

Slide 49 text

Introduce handlers for the paths This allows for intervention Add an alternate path under a feature flag This may imply invoking not only the new service Switch the traffic Now the code can be cleaned up Strangler The process 2.1 2.2 2.3

Slide 50

Slide 50 text

def index if $feature.active?(:use_likes_service, @current_user) … else handle(request) end end Strangler Add an alternate path under a feature flag

Slide 51

Slide 51 text

Introduce handlers for the paths This allows for intervention Add an alternate path under a feature flag This may imply invoking not only the new service Switch the traffic Now the code can be cleaned up Strangler The process 2.1 2.2 2.3

Slide 52

Slide 52 text

Identify a candidate Does this feature need many changes? Is this feature causing technical risk? Choose an integration approach How to integrate the extracted service? Choose a migration strategy for data How to move the data to its own storage? Extracting services The process 1 2 3

Slide 53

Slide 53 text

Choose a migration strategy for data New schema Monolith BEFO R E

Slide 54

Slide 54 text

Choose a migration strategy for data New schema Monolith M IG R ATIO N A Service B

Slide 55

Slide 55 text

Choose a migration strategy for data New schema AFTER Monolith A Service B

Slide 56

Slide 56 text

● Allows changing the storage engine ● Adds risk to the migration project ● I haven’t used it personally, more suitable for services extracted for feature changes. Choose a migration strategy for data New schema

Slide 57

Slide 57 text

Choose a migration strategy for data Same schema Monolith BEFO R E

Slide 58

Slide 58 text

Choose a migration strategy for data Same schema Monolith M IG R ATIO N A Service A’

Slide 59

Slide 59 text

Choose a migration strategy for data Same schema AFTER Monolith A Service A’

Slide 60

Slide 60 text

● Low risk migration ● We usually move the reads slowly and then do a quick write path cut-over ● We’ve chosen this approach for services extracted due to technical risk Choose a migration strategy for data Same schema

Slide 61

Slide 61 text

● Systems that were reading directly from the DB ● How to cut-over writes ● Migrating events from the monolith to the service ● Outages Extracting services Some details I omitted

Slide 62

Slide 62 text

Conclusions

Slide 63

Slide 63 text

Small focused teams Delivering features faster Conclusions Reasons to migrate

Slide 64

Slide 64 text

Fast provisioning Monitoring Deployment pipeline Conclusions Requisites for migrating (Fowler)

Slide 65

Slide 65 text

More moving parts Conclusions The problem you introduce

Slide 66

Slide 66 text

One feature at a time When you need it Don’t stop delivering! Conclusions How to break your monolith

Slide 67

Slide 67 text

It’s all about trade-offs! Conclusions Should you do it?

Slide 68

Slide 68 text

Thanks

Slide 69

Slide 69 text

Q & A