Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Moduliths

Dimitri
October 21, 2022

 Moduliths

Tech talk about Spring Boot Moduliths, given for Optis

Dimitri

October 21, 2022
Tweet

More Decks by Dimitri

Other Decks in Programming

Transcript

  1. Spring Boot Moduliths

  2. 🧚 The fairy tale of a growing application

  3. Medimind Once upon a time, there was a small application…

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

  5. Medimind And grew… User Inventory Medication Schedule

  6. Medimind And grew even more… User Inventory Medication Schedule Prescription

  7. Medimind Eventually nobody understood the application… User Inventory Medication Schedule

    Prescription Notification
  8. 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
  9. 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 ❌ ❌
  10. 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 ❌ ❌ ❌ ❌ ❌ ❌ ❌ ❌
  11. 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
  12. 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
  13. The end … ?

  14. What did we learn? • High coupling is evil •

    Microservices can help • Extra resources • Extra code • Less focus on business code
  15. 🤔 Is there an alternative?

  16. Medimind Modular monoliths User Inventory Medication Schedule Prescription Notification Spring

    Data Events
  17. Spring Data Events? @Entity // Extend from AbstractAggregateRoot public class

    Schedule extends AbstractAggregateRoot<Schedule> { @OneToMany private List<CompletedEvent> completedEvents; public void addCompletedEvent(LocalDate eventDate) { this.completedEvents.add(CompletedEvent.of(this, eventDate)); // Call registerEvent() registerEvent(MedicationTakenEvent.of(this, eventDate)); } }
  18. 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()); } }
  19. How to prevent high coupling? • Java access modifiers •

    Java modules • Moduliths
  20. Meet Moduliths /ˈmɑːd ͡ ʒəlɪθ/ Speciaal voor Kenneth in het

    fonetische schrift
  21. Add some dependencies… <dependency> <groupId>org.moduliths</groupId> <artifactId>moduliths-core</artifactId> </dependency> <dependency> <groupId>org.moduliths</groupId> <artifactId>moduliths-test</artifactId>

    <scope>test</scope> </dependency>
  22. Structure your code… 1. Create a new package for each

    module
  23. Structure your code… 2. Put exposed classes in a module

    package 1. Create a new package for each module
  24. 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
  25. … And write a test @ModuleTest public class InventoryModuleTest {

    @Test void moduleLoads() { } }
  26. … And write a test

  27. 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
  28. Resources • https://moduliths.org/ • https://www.youtube.com/watch?v=bVaiTPYlHFE • https://github.com/g00glen00b/medication-assistant • https://speakerdeck.com/g00glen00b/tech-talk-moduliths

  29. 🙏 Thank you for listening!