Slide 1

Slide 1 text

Spring Boot Moduliths

Slide 2

Slide 2 text

🧚 The fairy tale of a growing application

Slide 3

Slide 3 text

Medimind Once upon a time, there was a small application… User Inventory Medication

Slide 4

Slide 4 text

Medimind Each sprint, the application grew… User Inventory Medication Shedule

Slide 5

Slide 5 text

Medimind And grew… User Inventory Medication Schedule

Slide 6

Slide 6 text

Medimind And grew even more… User Inventory Medication Schedule Prescription

Slide 7

Slide 7 text

Medimind Eventually nobody understood the application… User Inventory Medication Schedule Prescription Notification

Slide 8

Slide 8 text

Notification Microservice Prescription Microservice Schedule Microservice Medication Microservice User Microservice Inventory Microservice Until one day, the Architect Wizard™ arrived, and transformed it into microservices…. User Inventory Medication Schedule Notification Prescription Mediminder gateway Event bus

Slide 9

Slide 9 text

Notification Microservice Prescription Microservice Schedule Microservice Medication Microservice User Microservice Inventory Microservice Peace returned, until one day a microservice caught fire… User Inventory Medication Schedule Notification Prescription Mediminder gateway Event bus ❌ ❌

Slide 10

Slide 10 text

Notification Microservice Prescription Microservice Schedule Microservice Medication Microservice User Microservice Inventory Microservice The fire spread… User Inventory Medication Schedule Notification Prescription Mediminder gateway Event bus ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌

Slide 11

Slide 11 text

Notification Microservice Prescription Microservice Schedule Microservice Medication Microservice User Microservice Inventory Microservice The DevOps Heroes™ quickly restarted the microservice… User Inventory Medication Schedule Notification Prescription Mediminder gateway Event bus

Slide 12

Slide 12 text

Notification Microservice Prescription Microservice Schedule Microservice Medication Microservice User Microservice Inventory Microservice And added extra infrastructure to prevent future problems User Inventory Medication Schedule Notification Prescription Mediminder gateway Event bus

Slide 13

Slide 13 text

The end … ?

Slide 14

Slide 14 text

What did we learn? ● High coupling is evil ● Microservices can help ● Extra resources ● Extra code ● Less focus on business code

Slide 15

Slide 15 text

🤔 Is there an alternative?

Slide 16

Slide 16 text

Medimind Modular monoliths User Inventory Medication Schedule Prescription Notification Spring Data Events

Slide 17

Slide 17 text

Spring Data Events? @Entity // Extend from AbstractAggregateRoot public class Schedule extends AbstractAggregateRoot { @OneToMany private List completedEvents; public void addCompletedEvent(LocalDate eventDate) { this.completedEvents.add(CompletedEvent.of(this, eventDate)); // Call registerEvent() registerEvent(MedicationTakenEvent.of(this, eventDate)); } }

Slide 18

Slide 18 text

Spring Data Events? @Component @RequiredArgsConstructor public class MedicationTakenEventListener { private final InventoryFacade facade; @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) public void subtractFromInventory(MedicationTakenEvent event) { facade.removeQuantity(event.userId(), event.medicationId(), event.quantity()); } }

Slide 19

Slide 19 text

How to prevent high coupling? ● Java access modifiers ● Java modules ● Moduliths

Slide 20

Slide 20 text

Meet Moduliths /ˈmɑːd ͡ ʒəlɪθ/ Speciaal voor Kenneth in het fonetische schrift

Slide 21

Slide 21 text

Add some dependencies… org.moduliths moduliths-core org.moduliths moduliths-test test

Slide 22

Slide 22 text

Structure your code… 1. Create a new package for each module

Slide 23

Slide 23 text

Structure your code… 2. Put exposed classes in a module package 1. Create a new package for each module

Slide 24

Slide 24 text

Structure your code… 3. Put implementation details in a subpackage 2. Put exposed classes in a module package 1. Create a new package for each module

Slide 25

Slide 25 text

… And write a test @ModuleTest public class InventoryModuleTest { @Test void moduleLoads() { } }

Slide 26

Slide 26 text

… And write a test

Slide 27

Slide 27 text

Pro’s & Con’s ● Developed by Spring Data lead ● Favours conventions over configuration ● Easy integration with Spring Boot ● Only bootstraps module during tests ● Small community ● Does not replace microservices

Slide 28

Slide 28 text

Resources ● https://moduliths.org/ ● https://www.youtube.com/watch?v=bVaiTPYlHFE ● https://github.com/g00glen00b/medication-assistant ● https://speakerdeck.com/g00glen00b/tech-talk-moduliths

Slide 29

Slide 29 text

🙏 Thank you for listening!