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

Fulfilling Data Needs between Microservices

Fulfilling Data Needs between Microservices

Sometimes an application is split into multiple smaller services, each responsible for a certain aspect of the application. Doing this correctly mandates that each service has its own storage. But what if one service needs data from another service? Let's find out some ways of achieving this!

Tom Van Herreweghe

May 17, 2023
Tweet

More Decks by Tom Van Herreweghe

Other Decks in Technology

Transcript

  1. What is a "microservice"? Autonomous Specialized Develop, Build, Deploy independently

    Data ownership Communicate through well-​ defined API's Knowledge isolation https://aws.amazon.com/microservices/
  2. What are "data needs"? When a service needs Data it

    doesn't own To perform a task from another service
  3. Concrete example Service Projects is requested to return the details

    of a specific project, together with the name of the customer
  4. Concrete example Projects Customers Customer ID Name Address Project ID

    Title Customer 443f16e3-​ e398-4e57-88ea-9fa4972be6a0 443f16e3-​ e398-4e57-88ea-9fa4972b Bezos Company Parkway Drive 666, 90210 Bvrly Hills 17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0 Install Solar Panels
  5. Solution Projects Customers API Gateway one of many solutions actually

    Routes the request Consolidates responses Protocol translation {   ​ "id" : "17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0",   ​ "title" : "Install Solar Panels",   ​ "customer" : {   ​   ​ "type": "customer",   ​   ​ "id" : "443f16e3-​ e398-4e57-88ea-9fa4972be6a0"   ​ } } {   ​ "id" : "443f16e3-​ e398-4e57-88ea-9fa4972be6a0",   ​ "Name" : "Bezos Company",   ​ "address" : "Parkway Drive 666, 90210 Bvrly Hills" } {   ​ "id" : "17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0",   ​ "title" : "Install Solar Panels",   ​ "customer" : {   ​   ​ "id" : "443f16e3-​ e398-4e57-88ea-9fa4972be6a0",   ​   ​ "Name" : "Bezos Company",   ​   ​ "address" : "Parkway Drive 666, 90210 Bvrly Hills"   ​ } } API Gateway
  6. Task 1 Return data you don't own Request Solution: introduce

    a separate component AKA not a microservice's task
  7. What is a "microservice"? Autonomous Specialized Develop, Build, Deploy independently

    Data ownership Communicate through well-​ defined API's Knowledge isolation https://aws.amazon.com/microservices/
  8. Solution #2API Calls Pro's Cons Loose "implementation" coupling High "temporal"

    coupling aka "latency" But... seems acceptable Very microservice'y! API Contract
  9. Solution #2API Calls Pro's Cons Loose "implementation" coupling High "temporal"

    coupling But... seems acceptable Very microservice'y!
  10. API Contracts Make your API a 1st Class Citizen it

    was implied in the definition of a Microservice JSON Schema
  11. Solution #3Event-​ Carried State Transfer Customers Service Bus T r

    a n s l a t e Domain Event Customer Activated Customer Updated Produce Domain events 1. 3. Send out integration event 2. Translate into 1 integration event with all entity data {   ​ "action" : "UPDATE",   ​ "id" : "17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0",   ​ "data" : {   ​   ​ "first_name" : "Josh",   ​   ​ "last_name" : "Holme",   ​   ​ // ...   ​ } } Producing Events
  12. Solution #3Event-​ Carried State Transfer Fat Event https://verraes.net/2019/05/patterns-​ for-​ decoupling-​

    distsys-​ segregated-​ event-​ layers/ https://verraes.net/2019/05/patterns-​ for-​ decoupling-​ distsys-​ fat-​ event/ Segregated Event Layers Rich
  13. Projects Solution #3Event-​ Carried State Transfer Service Bus {  

    ​ "action" : "UPDATE",   ​ "id" : "17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0",   ​ "data" : {   ​   ​ "first_name" : "Josh",   ​   ​ "last_name" : "Holme",   ​   ​ // ...   ​ } } Customer Updated A C L Ingest integration event 1. 2. Pick interesting data 3. Store in a read model ID | NAME 17a4b5ac-38f0-4945-​ b3a5-5aed968ce0b0 | Josh Holme Consuming Events
  14. Solution #3Event-​ Carried State Transfer Pro's Cons Loose "implementation" coupling

    Loose "temporal" coupling Data duplication Is this really a problem these days? Eventual Consistency
  15. Guard the boundaries C o m p o n e

    n t O t h e r c o d e exposed hidden Cannot depend on each other https://qossmic.github.io/deptrac/ same codebase
  16. e.g. Github Actions Mirror to Read-​ Only repository C o

    m p o n e n t O t h e r c o d e C o m p o n e n t same repository separate repository mirror
  17. Re-​ use s e r v i c e C

    o m p o n e n t composer require // composer.json {   ​ "repositories": [   ​   ​ {   ​   ​   ​ "type": "vcs", "no-​ api": true, "url": "<read-​ only repository>"   ​   ​ } } (the Read-​ only repository)
  18. Decouple s e r v i c e Component Interfaces

    depends on & uses for typehinting depends on implements Dependency-​ inversion principle (separate repository)
  19. To avoid cyclic dependencies Customers C L i b r

    a r y depends on depends on v1 depends on v2 conflict!
  20. Solution #4Service Composition Component Should not do writes to your

    database Service no longer retains data ownership