Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
-
Upload
boris-kravtsov -
Category
Technology
-
view
90 -
download
0
Transcript of Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup
![Page 1: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/1.jpg)
Microservice Architecture with Event Sourcing
Shahid Zaman Boris Kravtsov
![Page 2: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/2.jpg)
![Page 3: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/3.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/4.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/5.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/6.jpg)
![Page 7: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/7.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/8.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/9.jpg)
![Page 10: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/11.jpg)
Bounded Context
![Page 12: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/12.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/13.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/14.jpg)
![Page 15: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/15.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/16.jpg)
Managing complexity - testing
![Page 17: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/17.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/18.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/19.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/20.jpg)
▪ Added complexity
▪ Weird
Drawbacks of CQRS
![Page 21: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/21.jpg)
![Page 22: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/22.jpg)
![Page 23: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/23.jpg)
![Page 24: Microservice Architecuture with Event Sourcing @ Sydney JVM Meetup](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/24.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/25.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/26.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/27.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/28.jpg)
▪ 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](https://reader031.fdocuments.in/reader031/viewer/2022022412/58f2c8521a28abd50c8b45a1/html5/thumbnails/29.jpg)
▪ 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