microservice architecuture with event sourcing @ sydney jvm meetup

30
Microservice Architecture with Event Sourcing Shahid Zaman Boris Kravtsov

Upload: boris-kravtsov

Post on 16-Apr-2017

91 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Microservice Architecture with Event Sourcing

Shahid Zaman Boris Kravtsov

Page 2: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 3: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Microservice architecture is a method of developing software applications as a suite of independently deployable, small, modular services in which each service runs a unique process and communicates through a well-defined, lightweight mechanism to serve a business goal

Microservice Architecture

Page 4: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Build and ship code faster and cheaper

▪ Reduction of complexity?

▪ Horizontal vs vertical scaling

▪ Web scale

▪ Breaking down the monolith

▪ Organisational changes

Why Microservices?

Page 5: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ REST with light weight JSON vs WS-* spec

▪ Global canonical model vs bounded context per service

▪ Shared database with tight coupling vs loosely coupled database per service

SOA vs Microservices

Page 6: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 7: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Smaller services where code can fit in one developer’s head

▪ Independently deployed and scaled

▪ Improved fault isolation

▪ Polyglot technical architecture - ‘best’ tool for the job

▪ Fail fast - easily experiment

Benefits of Microservices

Page 8: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Drawbacks of Microservices

▪ Additional complexity in distributed system

▪ Partial failures

▪ Polyglot persistence and distributed transactions?

▪ Testing is hard

▪ Operational complexity

▪ Requires CI/CD

▪ Changes in business processes and organisational structure

Page 9: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 10: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Domain Driven Design - DDD

▪ Place the project's primary focus on the core domain and domain logic

▪ Base complex designs on a model of the domain

Page 11: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Bounded Context

Page 12: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

REST with CRUD

▪ Antipattern

▪ Database as a service thinking

▪ Exposing internal database structure instead of a well thought out contract

▪ Simple client-server architecture

Page 13: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Event Sourcing

▪ Storing state vs storing a sequence of store changing events

▪ State is recorded as a complete series of events from actions taken within a domain

▪ Replaying of events to restore state

▪ In contrast with CRUD where only current state of an entity is stored

▪ Append only / immutable store

▪ Event history is replayed to retrieve the current state of an entity

▪ Snapshots can help in situations where there is too much history

Page 14: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 15: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Solves data concurrency issues

▪ Solves Object-relational impedance mismatch problem

▪ Retains state across time. Temporal queries are possible

▪ Reduces complexity in testing

▪ Allows for multiple views of data

Benefits of Event Sourcing

Page 16: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Managing complexity - testing

Page 17: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ New apps only - requires a rewrite

▪ More complex to implement and requires skilled developers

▪ Querying event store is challenging - state needs to be reconstructed

▪ Its not a top level architecture and should be applied selectively

Drawbacks of Event Sourcing

Page 18: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Stands for: Command Query Responsibility Segregation

▪ Inspired by Event Sourcing, but only a first stepping stone and not an architecture

▪ Write and Read are separate

CQRS

Page 19: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Query and write scalability

▪ Business focus

▪ Reduction of complexity

▪ Solves ORIM problem

Benefits of CQRS

Page 20: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Added complexity

▪ Weird

Drawbacks of CQRS

Page 21: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 22: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 23: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
Page 24: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

GemFire (Apache Geode)

GemFire is a distributed, in-memory database with strong data consistency, built to support transactional applications with low latency

and high concurrency needs

Page 25: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

GemFire Server GemFire Server

GemFire with CQRS

GemFire Server

Partitioned Data

Partitioned Data

Client

query

Partitioned Query Data

write

Partitioned Query Data

Partitioned Query Data

automatic cache invalidationcontinuous query

Page 26: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ What is the scenario again?

▪ Microservice architecture ▪ Event sourced data model ▪ CQRS patterned data access

Where does Spring fit in this scenario?

• Spring Boot • Spring Cloud/Netflix projects • Spring Cloud Stream • Project Reactor

https://github.com/benwilcock/microservice-sampler

Page 27: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

Spring Cloud/Netflix projects

• Spring Cloud Config provides server and client-side support for externalised configuration in a distributed system.

• Spring Cloud Netflix - Eureka for service discovery

• Spring Cloud Netflix - Zuul for Gateway service

• Spring Cloud Netflix - Ribbon for software load balancing

https://github.com/benwilcock/microservice-sampler

Page 28: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Spring Cloud Stream is a framework for building message-driven microservices.

▪ Built on top of Spring boot. ▪ Based on the model of Spring Integration

@SpringBootApplication @EnableBinding(Source.class) public class StreamdemoApplication {

public static void main(String[] args) { SpringApplication.run(StreamdemoApplication.class, args); }

@Bean @InboundChannelAdapter(value = Source.OUTPUT) public MessageSource<String> timerMessageSource() { return () -> new GenericMessage<>(new SimpleDateFormat().format(new Date())); } }

Spring Cloud Stream

Page 29: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

▪ Lots of microservices and coordination benefits from reactive programming model

▪ Avoid latency and increase better throughput

▪ Project reactor is an implementation of Reactive Streams spec for JVM

▪ Process unbounded number of elements in sequence

▪ Asynchronously call and pass elements between components

▪ With backpressure implementation

Project Reactor

Page 30: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup

start.spring.io