HIDING THE LEAD
COUPLING, COHESION AND MICROSERVICES
Sam Newman
https://www.flickr.com/photos/spongebabyalwaysfull/8793058944/
@samnewman
Sam Newman
Building
Microservices
DESIGNING FINE-GRAINED SYSTEMS
@samnewman
NEW BOOK!
https://samnewman.io/books/monolith-to-microservices/
@samnewman
INDEPENDENT DEPLOYABILITY
Inventory
Returns
Invoicing
Accounts
v1
Customer
Service
Shipping
@samnewman
INDEPENDENT DEPLOYABILITY
Inventory
Returns
Invoicing
Customer
Service
Shipping
Accounts
v2
@samnewman
INDEPENDENT DEPLOYABILITY
Inventory
Returns
Invoicing
Customer
Service
Shipping
Accounts
v2
@samnewman
Customer
Service
Accounts
v1
Independent deployability
requires interface stability
Maintaining backwards
compatibility is key
@samnewman
Customer
Service
Accounts
v2
Independent deployability
requires interface stability
Maintaining backwards
compatibility is key
@samnewman
Customer
Service
Accounts
v2
Independent deployability
requires interface stability
Maintaining backwards
compatibility is key
@samnewman
https://www.flickr.com/photos/photographingtravis/16939917735/
@samnewman
MODULES
@samnewman
MODULES
Module A
Module D
Module B
Module C
Module E
@samnewman
MODULES
Module A
Module D
Module B
Module C
Module E
Allow for
independent
working
@samnewman
MODULES
Module A
Module D
Module B
Module C
Module E
Allow for
independent
working
And reuse
@samnewman
MODULES
Module A
Module D
Module B
Module C
Module E
Allow for
independent
working
And reuse
Namespaces,
packages
@samnewman
MODULES
Module A
Module D
Module B
Module C
Module E
Allow for
independent
working
And reuse
Namespaces,
packages
NPMs, Gems,
JARs etc
@samnewman
AND MICROSERVICES?
Module A
Module DModule B
Module C
Module E
@samnewman
AND MICROSERVICES?
Module A
Module DModule B
Module C
Module E
“Modules” run on different
computers
@samnewman
AND MICROSERVICES?
Module A
Module DModule B
Module C
Module E
Communication
between
modules is done
via network calls
“Modules” run on different
computers
@samnewman
AND MICROSERVICES?
Module A
Module DModule B
Module C
Module E
Communication
between
modules is done
via network calls
“Modules” run on different
computers
Allows for easier
independent deployability
@samnewman
Microservices, when done right, are a form of modular
architecture
@samnewmanhttps://www.flickr.com/photos/ibm_media/33838065805/
@samnewman
INFORMATION HIDING
http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci
@samnewman
INFORMATION HIDING
http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci
Published in 1971
@samnewman
INFORMATION HIDING
http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci
Published in 1971
Looked at how best to define
module boundaries
@samnewman
INFORMATION HIDING
http://repository.cmu.edu/cgi/viewcontent.cgi?article=2979&context=compsci
Published in 1971
Looked at how best to define
module boundaries
Found that “information hiding”
worked best
@samnewman
INFORMATION HIDING AND MICROSERVICES
https://blog.acolyer.org/2016/09/05/on-the-criteria-to-be-used-in-decomposing-systems-into-modules/
@samnewman
Accounts
NOT HIDING?
@samnewman
Accounts
NOT HIDING?
Shipping
@samnewman
Accounts
NOT HIDING?
Shipping
If an upstream consumer
can reach into your internal
implementation..
@samnewman
Accounts
NOT HIDING?
Shipping
If an upstream consumer
can reach into your internal
implementation..
…then you can’t change
this implementation without
breaking the consumer
@samnewman
Accounts
NOT HIDING?
Shipping
If an upstream consumer
can reach into your internal
implementation..
…then you can’t change
this implementation without
breaking the consumer
@samnewman
Accounts
NOT HIDING?
Shipping
If an upstream consumer
can reach into your internal
implementation..
…then you can’t change
this implementation without
breaking the consumer
@samnewman
HIDING!
Accounts
Shipping
@samnewman
HIDING!
Accounts
Shipping
Hide your secrets!
Hidden
@samnewman
HIDING!
Accounts
Shipping
Hide your secrets!
Be explicit about what is
shared, and what is
hidden
Hidden
@samnewman
HIDING!
Accounts
Shipping
Hide your secrets!
Be explicit about what is
shared, and what is
hidden
Shared
Hidden
@samnewman
HIDING!
Accounts
Shipping
Hide your secrets!
Be explicit about what is
shared, and what is
hidden
Shared
Hidden
@samnewman
HIDING!
Accounts
Shipping
Hide your secrets!
Be explicit about what is
shared, and what is
hidden
Shared
Hidden
Hidden things can
change, shared things
can’t
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
Code
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
Code
@samnewman
ACCIDENTAL BREAKAGE
public class Customer {
private int id;
private String name;
private int age;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
Code
{
“id” : 123,
“name” : “sam”,
“dob” : 15/02/1974
}
JSON
@samnewman
DATA TRANSFER OBJECT
https://martinfowler.com/eaaCatalog/dataTransferObject.html
Customer
@samnewman
DATA TRANSFER OBJECT
https://martinfowler.com/eaaCatalog/dataTransferObject.html
Customer
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
@samnewman
DATA TRANSFER OBJECT
https://martinfowler.com/eaaCatalog/dataTransferObject.html
Customer
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
public class CustomerDTO {
private int id;
private String name;
private int age;
…
}
@samnewman
DATA TRANSFER OBJECT
https://martinfowler.com/eaaCatalog/dataTransferObject.html
Customer
public class Customer {
private int id;
private String name;
private LocalDate dob;
…
}
public class CustomerDTO {
private int id;
private String name;
private int age;
…
}
{
“id” : 123,
“name” : “sam”,
“age” : 15
}
JSON
@samnewman
AND CONNECTIONS BETWEEN THEM?
@samnewman
AND CONNECTIONS BETWEEN THEM?
“The connections between
modules are the assumptions
which the modules make
about each other.”
- David Parnas
@samnewman
AND CONNECTIONS BETWEEN THEM?
“The connections between
modules are the assumptions
which the modules make
about each other.”
- David Parnas
Information hiding is about
reducing the assumptions
one module has for another
@samnewman
EXPLICIT CONTRACTS?
Module DModule B
@samnewman
EXPLICIT CONTRACTS?
Module DModule B
@samnewman
EXPLICIT CONTRACTS?
Module DModule B
Need an explicit understanding of
the assumptions of consumers
@samnewman
EXPLICIT CONTRACTS?
Module DModule B
Need an explicit understanding of
the assumptions of consumers
To help us understand what can
change easily, and what cannot
@samnewman
EXPLICIT CONTRACTS?
Module DModule B
Need an explicit understanding of
the assumptions of consumers
To help us understand what can
change easily, and what cannot
Schemas can help!
@samnewman
Schemas FTW!
@samnewman
SCHEMAS
https://json-schema.org/ https://developers.google.com/protocol-buffers/
@samnewman
CHECKING SCHEMA COMPATIBILITY
https://protolock.dev/
@samnewman
And of course testing…
@samnewman
STRUCTURED PROGRAMMING
@samnewman
COUPLING AND COHESION
@samnewman
COUPLING AND COHESION
Coupling
@samnewman
COUPLING AND COHESION
Coupling Cohesion
@samnewman
COUPLING AND COHESION
Coupling Cohesion
Low
@samnewman
COUPLING AND COHESION
Coupling Cohesion
Low Strong
@samnewman
COHESION
The code that changes together, stays together
@samnewman
WEAK COHESION
@samnewman
WEAK COHESION
@samnewman
COUPLING
The degree to which changing one module requires a
change in another
@samnewman
CONSTANTINE’S LAW
“A structure is stable if cohesion is strong and coupling is low.”
- Albert Endres and Dieter Rombach
@samnewman
TYPES OF COUPLING
@samnewman
TYPES OF COUPLING
Domain
@samnewman
TYPES OF COUPLING
Domain Tramp
@samnewman
TYPES OF COUPLING
Domain CommonTramp
@samnewman
TYPES OF COUPLING
Domain Common ContentTramp
@samnewman
TYPES OF COUPLING
Domain Common ContentTramp
@samnewman
TYPES OF COUPLING
Domain Common Content
Increasing coupling
Tramp
@samnewman
DOMAIN COUPLING
Order Processor
Warehouse
Reserve Stock
Payment
Take Payment
@samnewman
DOMAIN COUPLING
Order Processor
Warehouse
Reserve Stock
Payment
Take Payment
Domain Coupling
@samnewman
TRAMP COUPLING
@samnewman
Order Processor
TRAMP COUPLING
@samnewman
Order Processor Warehouse
TRAMP COUPLING
@samnewman
Order Processor Warehouse
Shipping Manifest
Send Order Request
# Item ID
3 2231
2 134
TRAMP COUPLING
@samnewman
Order Processor
Shipping
Warehouse
Shipping Manifest
Send Order Request
# Item ID
3 2231
2 134
TRAMP COUPLING
@samnewman
Order Processor
Shipping
Warehouse
Shipping Manifest
Send Order Request
# Item ID
3 2231
2 134
TRAMP COUPLING
Shipping Manifest
Dispatch Package
Request
@samnewman
AVOIDING TRAMP COUPLING
@samnewman
AVOIDING TRAMP COUPLING
Order Processor
@samnewman
AVOIDING TRAMP COUPLING
Order Processor Warehouse
@samnewman
AVOIDING TRAMP COUPLING
Order Processor Warehouse
Send Order Request
# Item ID
3 2231
2 134
@samnewman
AVOIDING TRAMP COUPLING
Order Processor
Shipping
Warehouse
Send Order Request
# Item ID
3 2231
2 134
@samnewman
AVOIDING TRAMP COUPLING
Order Processor
Shipping
Warehouse
Shipping Manifest
Send Order Request
# Item ID
3 2231
2 134
@samnewman
AVOIDING TRAMP COUPLING (CONT)
Order Processor Warehouse
Shipping
1342
22313
ItemID#
Send Order Request
Shipping Manifest
Dispatch Package Request
123 The Place, UK
Express Shipping
@samnewman
COMMON COUPLING
Warehouse
@samnewman
COMMON COUPLING
Warehouse
Stock Levels
@samnewman
COMMON COUPLING
Warehouse
Stock Levels
@samnewman
COMMON COUPLING
Warehouse
Stock Levels Order Processor
@samnewman
COMMON COUPLING
Warehouse
Stock Levels Order ProcessorForecasting
@samnewman
COMMON COUPLING
Warehouse
Stock Levels Order ProcessorForecasting
Change to the common data
impacts multiple microservices
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
Order Processor
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
WarehouseOrder Processor
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
WarehouseOrder Processor
Information hiding bypassed
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
WarehouseOrder Processor
Information hiding bypassed
Unclear what can safely be
changed in the Order service
@samnewman
CONTENT COUPLING
Order ID Status
123 PLACED
456 SHIPPED
WarehouseOrder Processor
Information hiding bypassed
Unclear what can safely be
changed in the Order service
Aka Pathological Coupling
@samnewman
TYPES OF COUPLING
Domain Common ContentTramp
@samnewman
TYPES OF COUPLING
Domain Common Content
Increasing coupling
Tramp
@samnewman
IN SUMMARY
@samnewman
IN SUMMARY
Information hiding is super important
@samnewman
IN SUMMARY
Information hiding is super important
Some coupling is worse than others
@samnewman
IN SUMMARY
Information hiding is super important
Some coupling is worse than others
Information hiding, low coupling, strong
cohesion = Independent Deployability
@samnewman
THANKS!
https://samnewman.io/

Hiding The Lead: Coupling, cohesion and microservices