To SQL or NoSQL
Tags: database
Published: 2020-06-06 01:22:05

When working on personal projects or large-scale applications, developers will often have to set up a database layer to persist the data. With all of the different databases out there, there is not a one-size-fits-all database solution and what you choose will depend on your project needs and any tradeoffs considered.

Below are the two main types of databases.
  * **SQL / relational databases** - data is structured and database is accessed with SQL (Structured Query Language)
  * **NoSQL / non-relational databases** - data is unstructured and database is developed to scale and handle many queries

Here’s a side-by-side breakdown of the two types:

<table>
  <tr>
    <th></th>
    <th>SQL</th>
    <th>NoSQL</th>
  </tr>

  <tr>
    <td>STORAGE</td>
    <td>Data is structured and stored in tables
<ul>
<li>Each <strong>row</strong> contains information about one entity (record)</li>
<li>Each <strong>column</strong> contains separate data points (attributes)</li>
</ul>
</td>
    <td>Data is unstructured, and there are different types of NoSQL databases with different storage models
<ul>
<li><strong>Document Databases</strong>: Data is stored in documents and the documents are grouped together in collections. Each document can have a different structure.</li>
<li><strong>Key-Value Stores</strong>: Data is stored in an array of key-value pairs, with the key acting as an attribute name that is linked to a value</li>
<li><strong>Wide-Column Databases</strong>: Instead of "tables", columnar databases have column families, which are containers for rows. These databases are best used for analyzing large datasets.</li>
<li><strong>Graph Databases</strong>: Data is saved in graph structures with nodes (representing entities), properties (information about the entities), and lines (connections between the entities). These are best used to store data whose relations are best represented in a graph.</li>
</ul>
</td>
 </tr>

<tr>
    <td>DATABASES</td>
    <td>
<ul>
<li><strong>Open Source</strong>: PostgreSQL, MySQL, SQLite, MariaDB</li>
<li><strong>Other</strong>: Oracle DB, MS SQL Server</li>
</ul> 
</td>

    <td>
<ul>
<li><strong>Document Databases</strong>: MongoDB</li>
<li><strong>Key-Value Stores</strong>: Redis, Dynamo, Voldemort</li>
<li><strong>Wide-Column Databases</strong>: Cassandra, HBase</li>
<li><strong>Graph Databases</strong>: Neo4J, InfiniteGraph</li>
</ul>
</td>
  </tr>

<tr>
    <td>SCHEMA</td>
    <td><strong>Fixed schema</strong>
<ul>
<li>Columns must be decided on and set up before you add any entries/rows</li>
<li>Each row must have data for each column</li>
<li>Schema can be altered later, but it will modify the entire database and will go offline to update</li>
<ul>
</td>
    <td><strong>Dynamic schema</strong>
<ul>
<li>New columns can be added on the fly</li>
</li>Each entry/row equivalent doesn't have to contain data for each column equivalent</li>
</ul>
</td>
  </tr>

<tr>
    <td> QUERYING </td>
    <td>Uses SQL to define and manipulate the data</td>
    <td>Queries are focused on a collection of documents, sometimes called UnQL (Unstructured Query Language)</td>
  </tr>

<tr>
    <td>SCALABILITY</td>
    <td><strong>Database can scale vertically</strong>
<ul>
<li>Vertical scaling is more costly as it needs more horsepower (CPU, RAM) of the hardware to increase capacity</li>
<li>Horizontal scaling across multiple servers is possible but it's challenging and time consuming</li>
</ul>
</td>
    <td><strong>Database can scale horizontally</strong>
<ul>
<li>Horizontal scaling needs more machines (servers) in the NoSQL database infrastructure to increase capacity, which is cheaper because servers/hardware/cloud hosting is cheap</li>
</ul>
</td>
  </tr>

<tr>
    <td>RELIABILITY / ACID COMPLIANCY (Atomicity, Consistency, Isolation, Durability)
</td>
    <td>
<ul>
<li>Majority of SQL databases are ACID compliant</li>
<li>Strong data reliability and safe guarantee of performing transactions</li>
</ul>
</td>
    <td>Majority of NoSQL databases sacrifice ACID compliance for performance and scalability </td>
  </tr>

<tr>
    <td>WHEN TO USE</td>
    <td>
<ul>
<li>When you need to ensure ACID compliance (e-commerce and financial apps, etc)</li>
<li>The data is structured, unchanging, consistent</li>
<li>Business is not facing hyper-fast growth that requires more servers</li>
</ul>
</td>
    <td>
<ul>
<li>When you don’t want data to become a bottleneck</li>
<li>Working with big data</li>
<li>Storing large volumes of data that often have little structure (and can store different types of data together and on the fly)</li>
<li>Taking advantage of cloud computing and storage because it’s cheaper and can spread out across multiple servers for scalability</li>
<li>When rapid development is important</li>

</ul>
</td>
  </tr>

</table>


*Resources*
[Grokking the System Design Interview - SQL vs. NoSQL *(paid resource)*](https://www.educative.io/courses/grokking-the-system-design-interview/YQlK1mDPgpK)
[mongoDB - NoSQL vs SQL Databases](https://www.mongodb.com/nosql-explained/nosql-vs-sql)