How do you track record relations in NoSQL?

Technology CommunityCategory: NoSQLHow do you track record relations in NoSQL?
VietMX Staff asked 3 years ago

All the answers for how to store many-to-many associations in the “NoSQL way” reduce to the same thing: storing data redundantly.

In NoSQL, you don’t design your database based on the relationships between data entities. You design your database based on the queries you will run against it. Use the same criteria you would use to denormalize a relational database: if it’s more important for data to have cohesion (think of values in a comma-separated list instead of a normalized table), then do it that way.

But this inevitably optimizes for one type of query (e.g. comments by any user for a given article) at the expense of other types of queries (comments for any article by a given user). If your application has the need for both types of queries to be equally optimized, you should not denormalize. And likewise, you should not use a NoSQL solution if you need to use the data in a relational way.

There is a risk with denormalization and redundancy that redundant sets of data will get out of sync with one another. This is called an anomaly. When you use a normalized relational database, the RDBMS can prevent anomalies. In a denormalized database or in NoSQL, it becomes your responsibility to write application code to prevent anomalies.

One might think that it’d be great for a NoSQL database to do the hard work of preventing anomalies for you. There is a paradigm that can do this – the relational paradigm.