$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. 🧚 The fairy tale of a growing application

    View Slide

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

    View Slide

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

    View Slide

  5. Medimind
    And grew…
    User Inventory
    Medication
    Schedule

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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



    View Slide

  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



    ❌ ❌ ❌
    ❌ ❌


    View Slide

  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

    View Slide

  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

    View Slide

  13. The end … ?

    View Slide

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

    View Slide

  15. 🤔 Is there an alternative?

    View Slide

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

    View Slide

  17. 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));
    }
    }

    View Slide

  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());
    }
    }

    View Slide

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

    View Slide

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

    View Slide

  21. Add some dependencies…

    org.moduliths
    moduliths-core


    org.moduliths
    moduliths-test
    test

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  26. … And write a test

    View Slide

  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

    View Slide

  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

    View Slide

  29. 🙏 Thank you for listening!

    View Slide