Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve the authors names of books in microservice world?

So I'm starting a journey down the road of microservices. I've spent some hours online trying immerse myself into this topic.

One concept I'm not quite grasping yet is the idea of not using SQL joins and therefore having a small independent database for authors and the same for books.

So I understand the following SQL:

BooksTable - id, name, authorid
AuthorsTable - id, name

select book.name, author.name from book 
join author on book.authorId = author.id

In Node.js world

index.js

app.get('/api/books' bookDomain.get());

bookDomain.js

exports.get = () => {
  const books = bookService.get();

  const authors = authorService.get();

  /*
    This is where I'm lost: how do you achieve the simple SQL 
    above? I'm assuming in the domain is where this information is 
    "joined"? am I correct?
  */
};

Services

Database1
**bookService.js**
database context

Database2
**authorService.js**
database context

expected data (something like it, basically i'm saying JSON should be the return type)

[{
  book {
    "name": "Book 1",
    "author": "Author Name 1"
  }
},
{
  book {
    "name": "Book 2",
    "author": "Author Name 2"
  }
}]
like image 406
Rod Avatar asked Jul 26 '18 14:07

Rod


1 Answers

There are at least three ways that I can think of to solve this problem.

  1. Document Model NoSQL DB: Instead of using a SQL DB, consider using a document model NoSQL DB like Mongo DB. So instead of a relational DB with the tables like Books and Authors and an AuthorBooks table that are all joined on a pair of foreign keys - you can use a document NoSQL DB like Mongo where your books are stored as document type, in a BSON format that looks almost identical to the JSON in your question. A nice feature of document DBs like Mongo is that you can set up an index on JSON inside the Book document on Author, thus improving your query time. This is discussed quite well in NoSQL Distilled by Martin Fowler (Sec. 2.2 and Chp. 9).
  2. Break the foreign key relationship so that referential integrity is maintained by the service instead of the DB: Instead of relying on the relational DB to enforce referential integrity for you (via the maintenance of foreign keys) restrict access of the DB to just your microservice and maintain the integrity of foreign keys by your service itself. This tactic is discussed by Sam Newman in Building Microservices on pp. 84-85.
  3. Denormalize the DB: Instead of having two tables each for books and authors, just combine them into a denormalized table. So, create a new table where the info for your books is duplicated and author info is unique to each book per row. It's ugly. Searches now have a larger search space, but it's simple too. For example, something like the following format:

    book_id | book_name              | book_author
    =====================================================
          1 | NoSQL Distilled        | Pramod J. Sadalage
    -----------------------------------------------------
          1 | NoSQL Distilled        | Martin Fowler
    -----------------------------------------------------
          2 | Building Microservices | Sam Newman
like image 147
entpnerd Avatar answered Oct 24 '22 05:10

entpnerd