rest vs. messaging for microservices

40
REST VS. Messaging Integration Approaches for Microservices Eberhard Wolff / ewolff / olivergierke Oliver Gierke

Upload: eberhard-wolff

Post on 16-Apr-2017

34.836 views

Category:

Software


4 download

TRANSCRIPT

Page 1: REST vs. Messaging For Microservices

REST VS. MessagingIntegration Approaches for Microservices

Eberhard Wolff !/" ewolff !/" olivergierke

Oliver Gierke

Page 2: REST vs. Messaging For Microservices

Microservices

2

ECommerce

Order Customer

Delivery

Order Customer

Delivery

Package WAR

Page 3: REST vs. Messaging For Microservices

Integration

3

UI

Logic

Data

Links Modular UI

REST Messaging

ReplicationNo Common

Schema!

Page 4: REST vs. Messaging For Microservices

Overview

4

Page 5: REST vs. Messaging For Microservices

REST

• Architectural style

• Constraints in architecture result in traits of the system

• Identifiable resources, uniform interface, representations, hypermedia

• Synchronous by default

5

? $

Microservice

Microservice

Page 6: REST vs. Messaging For Microservices

REST

• Service discovery • DNS or registry & hypermedia

• Load balancing • Dedicated infrastructure • So!ware load-balancer (Ribbon)

6

? "

Microservice

Microservice

Page 7: REST vs. Messaging For Microservices

Messaging

• Microservices send message

• Asynchronously

7

Microservice

Microservice

!!!

Page 8: REST vs. Messaging For Microservices

Load Balancing

• Can have any numbers of instances of a receiver

• Load Balancing very easy

8

Microservice

%%%%%

MicroserviceMicroserviceMicroservice

Page 9: REST vs. Messaging For Microservices

Service Discovery

• Microservices send messages to queues / topics

• Receiver(s) read messages

• Decoupled by queues & messages

• No need for service discovery

9

Microservice

%%%%%

MicroserviceMicroserviceMicroservice

Page 10: REST vs. Messaging For Microservices

Fire & Forget

10

Page 11: REST vs. Messaging For Microservices

Example

11

Order

Payment

Credit card booking 200€

Page 12: REST vs. Messaging For Microservices

REST

• F&F doesn’t fit naturally

• Which HTTP method to use?

• Requests to create side effects • DELETE, PUT, POST

12

? $

Microservice

Microservice

Page 13: REST vs. Messaging For Microservices

Safety / Idempotency

13

HTTP Method Safe Itempotent

GET & &

PUT ' &

DELETE ' &

POST ' '

PATCH ' '

Page 14: REST vs. Messaging For Microservices

REST: Failure

• Remote system unavailable • Can’t easily retry because

of non-itempotency

• Status codes to communicating semantic problems

14

? $

Microservice

Microservice

(

Page 15: REST vs. Messaging For Microservices

Messaging

• Hand message over to messaging system

• Messaging system guarantees delivery

• Stores message

• Acknowledgement

• Might have duplicated messages

15

Microservice

%%%%%

Microservice

Page 16: REST vs. Messaging For Microservices

Messaging: Failures

• Message doesn’t make it into the message broker

• e.g. Timeout / TCP problem

• Retry

• Rely on re-transmission of incoming message

16

%%

Microservice

%%%%%(

%%%%%

Microservice

Page 17: REST vs. Messaging For Microservices

Request & Reply

17

Page 18: REST vs. Messaging For Microservices

Example

18

Order

Payment

Validate credit card # OK / not OK

Page 19: REST vs. Messaging For Microservices

REST

• Natural model

• GET request

• Support for caching built in • ETags, Last-Modified,

conditional GET / PUT

• Still needs care • Timeouts, resilience

19

? $

Microservice

Microservice

Page 20: REST vs. Messaging For Microservices

Messaging

• Send request

• Expect response

• Correlation• …or temporary queue

• Asynchronous by design

20

Microservice

Microservice

! ! ! !

Page 21: REST vs. Messaging For Microservices

Resilience

• Messaging can guarantee delivery

• Failure just increases latency

• System must deal with latency anyway

21

Microservice

Microservice

% % % %

(

Page 22: REST vs. Messaging For Microservices

Events

22

Page 23: REST vs. Messaging For Microservices

Event Driven Architecture

• Order sends events

• Decoupled: no call but events

• Receiver handle events as they please

23

Order

New Order Event

Payment: Books credit card

Delivery:Ship products

Page 24: REST vs. Messaging For Microservices

Event Driven Architecture• System are built around publishing domain events

• Multiple event listeners

• Event listener decides what to do

• Can easily add new event listener with additional business logic

• Challenges• Delivery hard to guarantee• What about old events?

24

Page 25: REST vs. Messaging For Microservices

Events + REST = Feed• System stores domain events and publishes feed (e.g. Atom)• Strong consistency within the service• No additional infrastructure required• Getting closer to Event Sourcing

• Clients subscribe to feed• Clients in charge of polling frequency

• Server side optimizations: caching, ETags, pagination, links

• Client side optimizations: conditional requests

25

Page 26: REST vs. Messaging For Microservices

Messaging• Publish / Subscribe e.g. JMS Topics

• History of events limited

• Guaranteed delivery somewhat harder

26

Page 27: REST vs. Messaging For Microservices

More Decoupling

• Enterprise Integration Patterns (Hohpe, Woolf)

• www.eaipatterns.com

• Contains patterns like Router, Translator or Adapter

• Create flexible messaging architectures

27

Page 28: REST vs. Messaging For Microservices

Code

28

@Inject OrderRepository repository;

@Transactionalpublic void order(Order order) { repository.save(order.deliver()); doCreditCardBooking(order.getCcNumber());}

Page 29: REST vs. Messaging For Microservices

Transactions

29

Page 30: REST vs. Messaging For Microservices

Messaging & Transactions (Commit)• Database commit

• Incoming messages acknowledged

• Commit success: outgoing messages sent

• Outgoing messages hopefully handled successfully.

• Inconsistencies: Outgoing messages not yet processed

30

Microservice %%%%

Page 31: REST vs. Messaging For Microservices

Microservice %%%%

Messaging & Transactions (Rollback)

• Database rollback

• Outgoing message not sent

• Incoming message retransmitted

31

(

Page 32: REST vs. Messaging For Microservices

REST & Transactions• No implicit infrastructure support

• But can be built manually

32

Page 33: REST vs. Messaging For Microservices

REST & Transactions

33

@Inject OrderRepository repository;@Inject ApplicationEventPublisher publisher;

@Transactionalpublic void order(Order order) { repository.save(order.deliver()); publisher.publish(new OrderDeliveredEvent(order));}

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)public void onOrder(Order order) { doCreditCardBooking(order.getCcNumber());}

Page 34: REST vs. Messaging For Microservices

Evolvability

34

Page 35: REST vs. Messaging For Microservices

Evolvability• Core aspect of Microservices: independent deployability

• Means: decoupling

• Change in one system must not break downstream systems

35

Page 36: REST vs. Messaging For Microservices

REST• Core concepts built into the protocol

• Representations • Content negotiation • Media types

• Hypermedia • Discoverability

36

Page 37: REST vs. Messaging For Microservices

Messaging• Data format: Your choice• i.e. easy to evolve if changes backwards-compatible

• But: no support for content negotation

37

Page 38: REST vs. Messaging For Microservices

Summary

38

REST Messaging

Communication style synchronous asynchronous

Service DiscoveryDNS, Service RegistryResource Discovery

Message BrokerQueues / Topics

StrengthsContent negotiation, Hypermedia

More control over direct interactionMessages in

Re-submission of messages

Page 39: REST vs. Messaging For Microservices

Your next project: Messaging or

REST?39

Page 40: REST vs. Messaging For Microservices

You’ll probably use both :-)

40