in Microservices using DDD, Event Sourcing & CQRS Augusto Branquinho Mike Shigueru Matsumoto
in Microservices using DDD, Event Sourcing & CQRS
Augusto BranquinhoMike Shigueru Matsumoto
Technical stack
1. Real Wave
Modules
Information
Frontend
Integration
Backend
Marketing
Sales
Care
Payments
Insights
Integration
2. Kotlin
Designed by Jetbrains
Kotlin in production: Google, Pivotal, Uber, Atlassian, Coursera, etc
Statically typed
Modern
Drastically reduce the boilerplate
Java, Android
Javascript
Native
2. Kotlin
Data Class
toString
equals
hashCode
copy
properties - get/set
2. Kotlin
🤢 Java
😎 Kotlin
Lambda
2. Kotlin
Higher Order Functions
2. Kotlin
Extension method
Reified & Inline
2. Kotlin
Smart cast
Pattern matching
String template
Reified
Operator overloading
Unit and Nothing
High order function
Infix
Internal
Properties
Extension methods/properties
Lazy initialization, Delegation
Object (singleton)
Deprecated
Default value
Tacit programming: let, also, apply, run, with
Tailrec
…
3. Event Sourcing & CQRS
Event Sourcing (ES)
100% reliable logging (aka: version control)
all operations on the system are stream event-oriented: nothing happens before the event being committed
it is possible to rebuild all the views since the beginning of time
you can build new views using the same principle
events are immutable
3. Event Sourcing & CQRS
CQRS
"It's a pattern" (Greg Young)
uses a different model to update (Command) and another to read (Query)
Command (accept/reject)Change stateEvents
Query (views)Are free of side effects
3. Event Sourcing & CQRS
MotivationES, CQRS
Clients: Bank, TelecomAudit Log (query the past)InsightsMultiple views (Multi-Tenant)
E.g.: Catalog managerReliable logsOffers are immutableHigh requests rate (millions)Decoupled views
3. Event Sourcing & CQRS
Real Wave Microservices
Command Api
Catalog Manager
Query Api
Catalog Search
Join Use Pay
query response
Catalog Processor
accept/reject
Offers
3. Event Sourcing & CQRS
Catalog Search
Catalog Processor
Low write latency
High read latency
Catalog Manager
3. Event Sourcing & CQRS
Code
3. Event Sourcing & CQRS
Command Bus
3. Event Sourcing & CQRS
DDD
Command Bus
3. Event Sourcing & CQRS
❌✔Bounded Context
3. Event Sourcing & CQRS
Command Bus
First Command
Nothing was replayed yet
3. Event Sourcing & CQRS
DDD
3. Event Sourcing & CQRS
3. Event Sourcing & CQRS
Happens in the past
Just accept, no logic here
3. Event Sourcing & CQRS
Command Bus ❌❌Not here yet!!!
3. Event Sourcing & CQRS
Command Bus
Replay for all past events will start for this ID before this Command executes⚠
3. Event Sourcing & CQRS
1 - Replay
3. Event Sourcing & CQRS
2 - Accepts/Rejects
1 - Replay
3. Event Sourcing & CQRS
3 - Accepted
1 - Replay
2 - Accepts/Rejects
3. Event Sourcing & CQRS
Catalog Processor
Another team maybe?🤔
3. Event Sourcing & CQRS
3. Event Sourcing & CQRS
3. Event Sourcing & CQRS
3. Event Sourcing & CQRS
Catalog Search
Micro Services
3. Event Sourcing & CQRS
A
B
CD
E
F Catalog Search
Catalog 1Catalog 2
Catalog 3Catalog n
3. Event Sourcing & CQRS
😌Conclusion
3. Event Sourcing & CQRS
🔥Burn down
Conclusion😌
3. Event Sourcing & CQRS
🏗Replay
TODOsnapshotssagas
recoveryfix inconsistencybuild a new view
😌Conclusion
Motivation
KotlinEvents (immutable) (Axon)
Catalog v1Data class (boilerplate)Event Stream
lambdaModernEasy to learn…
4. Kotlin, DDD, ES & CQRS
Why should you try using Kotlin?
4. Kotlin, DDD, ES & CQRS
4. Kotlin, DDD, ES & CQRS
Events (Event Store Database)Smart Casts
Pattern Matching
Final
Sealed Class
4. Kotlin, DDD, ES & CQRS
Events (Event Store Database)
4. Kotlin, DDD, ES & CQRS
Events (Event Store Database)
4. Kotlin, DDD, ES & CQRS
Value Objects - data classTypesafe
Primitive ObsessionInvalid ObjectsImmutability
4. Kotlin, DDD, ES & CQRS
Anemic Domain Model - kotlin extensionsOnly Getters / SettersServices Layers are OVER ABUSED!!!
😎
😖
5. Kotlin 102
Tests
5. Kotlin 102
Coroutines
asynchronous programming
we put the complications into libraries
experimental != unsecurity
5. Kotlin 102
Coroutines
What's the result?
5. Kotlin 102
Coroutines
What's the result?
5. Kotlin 102
Coroutines are light weight! 😱
5. Kotlin 102
Coroutines are light weight!
😱
😱
Coroutines - async/await
5. Kotlin 102
5. Kotlin 102
Kotlin is the path to Functional Programming. Functional Programming leads to Pure Functions, Pure Functions leads to Monad and Asynchronous Programming. Asynchronous Programming leads to Reactive Programming.
Master Yoda - Return of the Jedi
5. Kotlin 102
Functional ProgrammingPurity (no-side effect) Purity vs I/O
Exceptions propagationThread limits
Referential Transparency
5. Kotlin 102Monad/Either - zkotlinhttps://github.com/ZupIT/zkotlin
5. Kotlin 102Monad - Either -> Either -> Either
FOLD HELL
5. Kotlin 102
Either foldCompose
5. Kotlin 102
Either foldCompose ping-pong effectE.g. Payments Retry
😞
5. Kotlin 102
Either foldCompose ping-pong effect
😇
5. Kotlin 102
Either foldCompose ping-pong effect
😎
5. Kotlin 102
Either, Coroutines, Extensions, zkotlin all together
5. Kotlin 102
Wraps I/O problems with Either<L,R>
5. Kotlin 102
Wraps I/O problems with Either<L,R>
✔
❌
❌
Remote I/O
5. Kotlin 102
sync {}
async {}
5. Kotlin 102runBlocking
5. Kotlin 102zkotlin
Couroutines/Ext. Methods
5. Kotlin 102Conclusion
Kotlin & Functional ProgrammingPurity, Referential Transparency, Immutability
Higher-order functionsSugar syntax (it, ƛ as last parameter)
Less boilerplatea nicer syntax to compose Either pattern
Coroutines is easy to use
Ready for Spring Reactor
Catalog Reindex Process before Coroutines: +/- 30 m, after < 4 m
😌
Everything is final on Kotlin
6. Kotlin, Java & Legacy
Spock Mockito
SpringBoot - allopen
6. Kotlin, Java & Legacy
final
6. Kotlin, Java & Legacy
JPA
6. Kotlin, Java & Legacy
Spring Data QueryDSL - Plugins (modules)
Default Interface Java 8 (Kotlin 1.2.40)
Lambdas return - Unit
Mike Shigueru MatsumotoEmail: [email protected]
Augusto BranquinhoEmail: [email protected]
Conclusion
540+ Zuppers.Uberlândia MGUI / UX / R&D / DEV / SUPPORT
São José do Rio Preto SPDEV
Ribeirão Preto SPUI/UX/DESIGN
São Paulo SPSALES / CONSULTING / PMO / REQUIREMENTSUI/UXJoinville SC
R&D
Lisboa, PortugalSALES / CONSULTING / R&D
BH MGUI / UX / R&D / DEV
We’re a fast growing company (2011), but just starting.Kevin EfrusyAccel Ventures
Romero RodriguesBuscapé Company
Martin ScobariGeneral Atlantic
Silvio GenesiniSilvigen Consultoria
Zup