NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence

Author: Martin Fowler, Visit Amazon's Martin Fowler Pagesearch resultsLearn about Author CentralMartin Fowler
This Month Stack Overflow 1


by anonymous   2019-01-13

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