Consumer Driven Contracts
Say what you mean and mean what you say
@robdcrowley
robdcrowley
Slide 2
Slide 2 text
Session Goals
Learn how consumer driven contracts facilitate a low friction
approach to service evolution
Explore how Pact enables service providers to know if a change
would break consumers before deploying
Consumer driven contracts as a catalyst for cultural change
Empower you with innovative ideas to bring back to your
organisation!
Slide 3
Slide 3 text
Digital disruption is driving change across established
industries
Slide 4
Slide 4 text
The agility to respond to rapidly changing customer
needs is vital for remaining competitive
Slide 5
Slide 5 text
Software architecture must also become more agile
Slide 6
Slide 6 text
Modern Architectures are Complex
Service
Service
Service
Service Service
Service
Service Service
Service
Service
Service
Client
Client
Client
Service
Service
Service
Service
Slide 7
Slide 7 text
Some people, when confronted with a problem, think
“I know, I'll use system integration tests.” Now their
problems scale combinatorially.
Slide 8
Slide 8 text
Combinatorial Scaling
2 code paths: 26 = 64
4 code paths: 46 = 4096
6 code paths: 66 = 46656
8 code paths: 86 = 262144
Service
Service
Service
Service Service
Service
Service
Service
Service
Service
Service
Service
Client
Client
Client
Slide 9
Slide 9 text
“Reliability problems largely occur at
module boundaries, and most result
from inconsistencies in both sides
expectations”
- Bertrand Meyer, Object Success
Slide 10
Slide 10 text
Implicit Interfaces
No explicit interface between
collaborators enforced by type
system or compiler checks
Service
Service
Service
Service Service
Service
Service
Service
Service
Service
Service
Service
Client
Client
Client
Slide 11
Slide 11 text
Implicit Interfaces
No explicit interface between
collaborators enforced by type
system or compiler checks
Let’s change and deploy a
service
Service
Service
Service
Service Service
Service
Service
Service
Service
Service
Service
Service
Client
Client
Client
Slide 12
Slide 12 text
Implicit Interfaces
No explicit interface between
collaborators enforced by type
system or compiler checks
Let’s change and deploy a
service
Hope we don’t break existing
collaborators
Service
Service
Service
Service Service
Service
Service
Service
Service
Service
Service
Service
Client
Client
Client
Slide 13
Slide 13 text
Integration Tests
Slow (longer feedback cycle)
Brittle
Difficult to manage
Finding the actual issue requires triage
Scale combinatorially
Written after the code rather than before
Slide 14
Slide 14 text
So if integration tests are not the solution
then what is?
Slide 15
Slide 15 text
So if integration tests are not the solution
then what is?
Test Symmetry
Slide 16
Slide 16 text
symmetry
s m tri/
ˈ ɪ ɪ
noun
The correspondence of the form and
arrangement of elements or parts on opposite
sides of a dividing line.
Slide 17
Slide 17 text
Test Symmetry
A B
Slide 18
Slide 18 text
A B
Mock
Test Symmetry
Slide 19
Slide 19 text
A B
Mock
Trivial
Test Symmetry
Slide 20
Slide 20 text
A B
Mock
Trivial Tricky
Test Symmetry
Slide 21
Slide 21 text
Symmetrical tests have the exact opposite properties
Slide 22
Slide 22 text
Pact
www.pact.io
Slide 23
Slide 23 text
Know when you break a consumer before deploying
the change
Fast (rapid feedback cycle)
Reliable
Low ramp up cost
Straightforward to debug
Scale linearly
Contract Tests
Slide 28
Slide 28 text
Given ”products exists”
Upon receiving ”a GET request for product catalogue”
With “an Accept header of application/hal+json”
Will respond with ”200 OK”
And “a Content-Type header of application/hal+json”
And ”the product catalogue details in the entity body”
Slide 29
Slide 29 text
Given ”products exists”
Upon receiving ”a GET request for product catalogue”
With “an Accept header of text/plain”
Will respond with ”406 Not Acceptable”
Slide 30
Slide 30 text
Given ”Widget product does not exist”
Upon receiving ”a GET request for Widget product”
Will respond with ”404 Not Found”
Slide 31
Slide 31 text
Examples
Slide 32
Slide 32 text
Joining the dots...
Product
Sales
Service
Product
Catalogue
Service
Product
Stock
Service
Pact
Broker
git clone https://github.com/robcrowley/microservices-pact-demo.git
JUnit for pact generation
Gradle pact plugin for provider
verification
Pact Broker for pact repository
Docker integration
Slide 33
Slide 33 text
Start with the consumer
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
No content
Slide 36
Slide 36 text
No content
Slide 37
Slide 37 text
No content
Slide 38
Slide 38 text
Then move onto the provider
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
No content
Slide 43
Slide 43 text
“Be liberal in what you accept, and
conservative in what you send”
- Jon Postel, The Robustness Principle
Slide 44
Slide 44 text
Implications of Postel’s Law
An expectation may not be made for a field to be absent from a
response
Exception: Request headers that change the behaviour of the
provider must be specified in expectations
Slide 45
Slide 45 text
Sharing Pacts
Slide 46
Slide 46 text
Pact Broker
Consumer Provider
Pact Broker
Slide 47
Slide 47 text
Pact Broker
Consumer Provider
Pact Broker
Publish
PUT /pacts/provider/{provider}/consumer/{consumer}/versions/{version} HTTP/1.1
Slide 48
Slide 48 text
Pact Broker
Consumer Provider
Pact Broker
Publish
PUT /pacts/provider/{provider}/consumer/{consumer}/versions/{version} HTTP/1.1
Pull
GET /pacts/provider/{provider}/consumer/{consumer}/latest HTTP/1.1
Slide 49
Slide 49 text
Facilitates the sharing of pacts between consumer
and provider projects
Enables service release cycles to be decoupled
Example driven API documentation
Visualisations of relationships between services
Web Hooks to provide integration with CI Builds
Pact Broker
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
Backwards Compatibility Testing
Use Tags to ensure provider is compatible with latest and released versions of consumers
PUT /participants/{consumer}/versions/{version}/tags/{tag} HTTP/1.1
GET /pacts/provider/{provider}/consumer/{consumer}/latest/{tag} HTTP/1.1
Consumer Head
(Pact Broker)
Consumer Production
(Pact Broker)
Provider Head
(Source Control)
Contract Tests
Contract Tests
Producer backwards
compatibility
Provider Production
(Source Control)
Contract Tests
Consumer backwards
compatibility
Already Tested
Slide 53
Slide 53 text
So once contract tests are in place I can throw all
of my integration tests away?
Slide 54
Slide 54 text
It depends on the criticality of the business capability
the service supports and your risk appetite
Slide 55
Slide 55 text
No content
Slide 56
Slide 56 text
Make production monitoring and alerting a priority
Slide 57
Slide 57 text
Other Benefits
Slide 58
Slide 58 text
Higher quality services
Slide 59
Slide 59 text
“Quality comes from deliberate thought”
- Michael Feathers, Testing Patience
Slide 60
Slide 60 text
Reduced waste in delivery process
Slide 61
Slide 61 text
The business value realized by a service is defined
by the parts of it’s provider contract being leveraged
by consumers
Slide 62
Slide 62 text
“The biggest cause of failure in software-
intensive systems is not technical failure; it’s
building the wrong thing.”
- Mary Poppendieck
Slide 63
Slide 63 text
Focusing on delivering the capabilities as defined in
consumer contracts drives building the right thing
Slide 64
Slide 64 text
Takeaways
Consumer Driven Contracts enable services to evolve gracefully
Pact combines isolated unit testing with contract testing
Pact enables delivery at speed by providing rapid feedback on
potential breakages
Pact allows test driven development principles to be applied to
services
Consumer Driven Contracts can be a catalyst for cultural change by
fostering collaboration between delivery teams and reducing waste
Slide 65
Slide 65 text
“Deploying to production quickly is easy;
doing so quickly and responsibly
however is non trivial”
- Rob Crowley
Slide 66
Slide 66 text
Adopting a consumer driven contract testing
approach can help
Slide 67
Slide 67 text
Questions
Slide 68
Slide 68 text
More Info
Gitbook: docs.pact.io
Github: realestate-com-au/pact and DiUS/pact-jvm
User group: https://groups.google.com/forum/#!forum/pact-support
Twitter: pact_up
Pact to the Rescue!: http://www.slideshare.net/bethesque/pact-44565612
Deploy with Confidence using Pact Go!: https://www.youtube.com/watch?v=SMadH_ALLII
Consumer Driven Contracts – A Service Evolution Pattern: http://martinfowler.com/articles/consumerDrivenContracts.html