Querying composed resources










1















I'm currently trying to design a small REST API.



Let's say I have a resource (book) that is composed of multiple other resources (eg. author).



Both resources have separate API's that are deployed in different services (with their own databases).



The author doesn't know a thing about books. However books know their author.



Now I want to support queries like books?author.surname=Poe.



I'm struggling how an implementation could look like that supports this kind of filtering of linked resources.



Since the author doesn't know about books, I can't ask the author api to give me the matching authors and go from here to the related books.
What I could do is ask for every book I have in the DB the author API for that books author and then filter that based on the authors name. But that sounds horribly unperformant.



I guess that nearly every SOA runs into this problem quite early in the design.



I'm asking if there is already a common pattern or best practice that solves this problem?










share|improve this question






















  • when you say books know their author, do you preserve that mapping somewhere?

    – RishikeshDhokare
    Nov 14 '18 at 8:05











  • Yes, inside my book entity

    – Graslandpinguin
    Nov 14 '18 at 18:11















1















I'm currently trying to design a small REST API.



Let's say I have a resource (book) that is composed of multiple other resources (eg. author).



Both resources have separate API's that are deployed in different services (with their own databases).



The author doesn't know a thing about books. However books know their author.



Now I want to support queries like books?author.surname=Poe.



I'm struggling how an implementation could look like that supports this kind of filtering of linked resources.



Since the author doesn't know about books, I can't ask the author api to give me the matching authors and go from here to the related books.
What I could do is ask for every book I have in the DB the author API for that books author and then filter that based on the authors name. But that sounds horribly unperformant.



I guess that nearly every SOA runs into this problem quite early in the design.



I'm asking if there is already a common pattern or best practice that solves this problem?










share|improve this question






















  • when you say books know their author, do you preserve that mapping somewhere?

    – RishikeshDhokare
    Nov 14 '18 at 8:05











  • Yes, inside my book entity

    – Graslandpinguin
    Nov 14 '18 at 18:11













1












1








1








I'm currently trying to design a small REST API.



Let's say I have a resource (book) that is composed of multiple other resources (eg. author).



Both resources have separate API's that are deployed in different services (with their own databases).



The author doesn't know a thing about books. However books know their author.



Now I want to support queries like books?author.surname=Poe.



I'm struggling how an implementation could look like that supports this kind of filtering of linked resources.



Since the author doesn't know about books, I can't ask the author api to give me the matching authors and go from here to the related books.
What I could do is ask for every book I have in the DB the author API for that books author and then filter that based on the authors name. But that sounds horribly unperformant.



I guess that nearly every SOA runs into this problem quite early in the design.



I'm asking if there is already a common pattern or best practice that solves this problem?










share|improve this question














I'm currently trying to design a small REST API.



Let's say I have a resource (book) that is composed of multiple other resources (eg. author).



Both resources have separate API's that are deployed in different services (with their own databases).



The author doesn't know a thing about books. However books know their author.



Now I want to support queries like books?author.surname=Poe.



I'm struggling how an implementation could look like that supports this kind of filtering of linked resources.



Since the author doesn't know about books, I can't ask the author api to give me the matching authors and go from here to the related books.
What I could do is ask for every book I have in the DB the author API for that books author and then filter that based on the authors name. But that sounds horribly unperformant.



I guess that nearly every SOA runs into this problem quite early in the design.



I'm asking if there is already a common pattern or best practice that solves this problem?







rest api design-patterns soa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 7:58









GraslandpinguinGraslandpinguin

1,05951930




1,05951930












  • when you say books know their author, do you preserve that mapping somewhere?

    – RishikeshDhokare
    Nov 14 '18 at 8:05











  • Yes, inside my book entity

    – Graslandpinguin
    Nov 14 '18 at 18:11

















  • when you say books know their author, do you preserve that mapping somewhere?

    – RishikeshDhokare
    Nov 14 '18 at 8:05











  • Yes, inside my book entity

    – Graslandpinguin
    Nov 14 '18 at 18:11
















when you say books know their author, do you preserve that mapping somewhere?

– RishikeshDhokare
Nov 14 '18 at 8:05





when you say books know their author, do you preserve that mapping somewhere?

– RishikeshDhokare
Nov 14 '18 at 8:05













Yes, inside my book entity

– Graslandpinguin
Nov 14 '18 at 18:11





Yes, inside my book entity

– Graslandpinguin
Nov 14 '18 at 18:11












1 Answer
1






active

oldest

votes


















1














Your case clearly requires implementation of reliable relation of one-to-many type between these entities (resources). The pattern you're looking for is... relational databases! They have been invented for this particular use case.



This is what I'd suggest:



First, you need to consider and evaluate if this separation and isolation of data is a sound design decision in your particular case, look at the drawbacks and trade-offs you're setting up yourself for. Is there a really good reason to want this data in separate databases?



Consider the following top-of-mind drawbacks:



  1. You might end up with under-performing queries;

  2. You won't be able to leverage the database indexing and other nice features to your advantage in a future point. This might hinder your ability
    to scale properly in the future

  3. You might end up with inconsistent data. For example an author in Books that's now missing from Authors. So you need a way to have reliable constraints (think
    foreign keys) between databases, which even though it might be
    achievable is unneeded complexity

  4. You're missing out on the inherent transaction context that a database provides. Should you need that in the future, you'd run into problems.

After having considered the above, if you still believe you have a good reason to have this related data in different databases you can try any of the following options that come to mind (minding that each is a kind of workaround that brings its own drawbacks):



  1. Implement a kind of a JOIN between APIs doing as little calls as possible. What I mean is have an Unique ID of Author in Authors resource, Query Authors first with the name and get its unique ID. Then use this id to query Books and get all matching books. Thus you'll only have 2 calls instead of potentially thousands. This solution has constant O(1) instead of O(N) - linear - complexity.


  2. Replication of data (but that would kind of defeat the purpose if what you're aiming at with your design is data isolation). This would solve the issue with the query, but you'll have to deal with replications and more administration together.


  3. Implement connection between separate databases using dblink or similar feature of the databases that you're using in order to query them as a single database.


  4. Use a medium for communication with events between separate micro services but settle for eventual consistency and other drawbacks. See the article here.


Hope this helps!






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295413%2fquerying-composed-resources%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Your case clearly requires implementation of reliable relation of one-to-many type between these entities (resources). The pattern you're looking for is... relational databases! They have been invented for this particular use case.



    This is what I'd suggest:



    First, you need to consider and evaluate if this separation and isolation of data is a sound design decision in your particular case, look at the drawbacks and trade-offs you're setting up yourself for. Is there a really good reason to want this data in separate databases?



    Consider the following top-of-mind drawbacks:



    1. You might end up with under-performing queries;

    2. You won't be able to leverage the database indexing and other nice features to your advantage in a future point. This might hinder your ability
      to scale properly in the future

    3. You might end up with inconsistent data. For example an author in Books that's now missing from Authors. So you need a way to have reliable constraints (think
      foreign keys) between databases, which even though it might be
      achievable is unneeded complexity

    4. You're missing out on the inherent transaction context that a database provides. Should you need that in the future, you'd run into problems.

    After having considered the above, if you still believe you have a good reason to have this related data in different databases you can try any of the following options that come to mind (minding that each is a kind of workaround that brings its own drawbacks):



    1. Implement a kind of a JOIN between APIs doing as little calls as possible. What I mean is have an Unique ID of Author in Authors resource, Query Authors first with the name and get its unique ID. Then use this id to query Books and get all matching books. Thus you'll only have 2 calls instead of potentially thousands. This solution has constant O(1) instead of O(N) - linear - complexity.


    2. Replication of data (but that would kind of defeat the purpose if what you're aiming at with your design is data isolation). This would solve the issue with the query, but you'll have to deal with replications and more administration together.


    3. Implement connection between separate databases using dblink or similar feature of the databases that you're using in order to query them as a single database.


    4. Use a medium for communication with events between separate micro services but settle for eventual consistency and other drawbacks. See the article here.


    Hope this helps!






    share|improve this answer





























      1














      Your case clearly requires implementation of reliable relation of one-to-many type between these entities (resources). The pattern you're looking for is... relational databases! They have been invented for this particular use case.



      This is what I'd suggest:



      First, you need to consider and evaluate if this separation and isolation of data is a sound design decision in your particular case, look at the drawbacks and trade-offs you're setting up yourself for. Is there a really good reason to want this data in separate databases?



      Consider the following top-of-mind drawbacks:



      1. You might end up with under-performing queries;

      2. You won't be able to leverage the database indexing and other nice features to your advantage in a future point. This might hinder your ability
        to scale properly in the future

      3. You might end up with inconsistent data. For example an author in Books that's now missing from Authors. So you need a way to have reliable constraints (think
        foreign keys) between databases, which even though it might be
        achievable is unneeded complexity

      4. You're missing out on the inherent transaction context that a database provides. Should you need that in the future, you'd run into problems.

      After having considered the above, if you still believe you have a good reason to have this related data in different databases you can try any of the following options that come to mind (minding that each is a kind of workaround that brings its own drawbacks):



      1. Implement a kind of a JOIN between APIs doing as little calls as possible. What I mean is have an Unique ID of Author in Authors resource, Query Authors first with the name and get its unique ID. Then use this id to query Books and get all matching books. Thus you'll only have 2 calls instead of potentially thousands. This solution has constant O(1) instead of O(N) - linear - complexity.


      2. Replication of data (but that would kind of defeat the purpose if what you're aiming at with your design is data isolation). This would solve the issue with the query, but you'll have to deal with replications and more administration together.


      3. Implement connection between separate databases using dblink or similar feature of the databases that you're using in order to query them as a single database.


      4. Use a medium for communication with events between separate micro services but settle for eventual consistency and other drawbacks. See the article here.


      Hope this helps!






      share|improve this answer



























        1












        1








        1







        Your case clearly requires implementation of reliable relation of one-to-many type between these entities (resources). The pattern you're looking for is... relational databases! They have been invented for this particular use case.



        This is what I'd suggest:



        First, you need to consider and evaluate if this separation and isolation of data is a sound design decision in your particular case, look at the drawbacks and trade-offs you're setting up yourself for. Is there a really good reason to want this data in separate databases?



        Consider the following top-of-mind drawbacks:



        1. You might end up with under-performing queries;

        2. You won't be able to leverage the database indexing and other nice features to your advantage in a future point. This might hinder your ability
          to scale properly in the future

        3. You might end up with inconsistent data. For example an author in Books that's now missing from Authors. So you need a way to have reliable constraints (think
          foreign keys) between databases, which even though it might be
          achievable is unneeded complexity

        4. You're missing out on the inherent transaction context that a database provides. Should you need that in the future, you'd run into problems.

        After having considered the above, if you still believe you have a good reason to have this related data in different databases you can try any of the following options that come to mind (minding that each is a kind of workaround that brings its own drawbacks):



        1. Implement a kind of a JOIN between APIs doing as little calls as possible. What I mean is have an Unique ID of Author in Authors resource, Query Authors first with the name and get its unique ID. Then use this id to query Books and get all matching books. Thus you'll only have 2 calls instead of potentially thousands. This solution has constant O(1) instead of O(N) - linear - complexity.


        2. Replication of data (but that would kind of defeat the purpose if what you're aiming at with your design is data isolation). This would solve the issue with the query, but you'll have to deal with replications and more administration together.


        3. Implement connection between separate databases using dblink or similar feature of the databases that you're using in order to query them as a single database.


        4. Use a medium for communication with events between separate micro services but settle for eventual consistency and other drawbacks. See the article here.


        Hope this helps!






        share|improve this answer















        Your case clearly requires implementation of reliable relation of one-to-many type between these entities (resources). The pattern you're looking for is... relational databases! They have been invented for this particular use case.



        This is what I'd suggest:



        First, you need to consider and evaluate if this separation and isolation of data is a sound design decision in your particular case, look at the drawbacks and trade-offs you're setting up yourself for. Is there a really good reason to want this data in separate databases?



        Consider the following top-of-mind drawbacks:



        1. You might end up with under-performing queries;

        2. You won't be able to leverage the database indexing and other nice features to your advantage in a future point. This might hinder your ability
          to scale properly in the future

        3. You might end up with inconsistent data. For example an author in Books that's now missing from Authors. So you need a way to have reliable constraints (think
          foreign keys) between databases, which even though it might be
          achievable is unneeded complexity

        4. You're missing out on the inherent transaction context that a database provides. Should you need that in the future, you'd run into problems.

        After having considered the above, if you still believe you have a good reason to have this related data in different databases you can try any of the following options that come to mind (minding that each is a kind of workaround that brings its own drawbacks):



        1. Implement a kind of a JOIN between APIs doing as little calls as possible. What I mean is have an Unique ID of Author in Authors resource, Query Authors first with the name and get its unique ID. Then use this id to query Books and get all matching books. Thus you'll only have 2 calls instead of potentially thousands. This solution has constant O(1) instead of O(N) - linear - complexity.


        2. Replication of data (but that would kind of defeat the purpose if what you're aiming at with your design is data isolation). This would solve the issue with the query, but you'll have to deal with replications and more administration together.


        3. Implement connection between separate databases using dblink or similar feature of the databases that you're using in order to query them as a single database.


        4. Use a medium for communication with events between separate micro services but settle for eventual consistency and other drawbacks. See the article here.


        Hope this helps!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 12:56

























        answered Nov 15 '18 at 15:00









        Plamen PetrovPlamen Petrov

        3,89142337




        3,89142337





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295413%2fquerying-composed-resources%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto