# DBMS

## Database

Almost always more than one database are used, but it is import to understand which database is best to perform what particular task. There are different functionalities and features involved while designing a system, and our focus remains to use the best suited database for a particular use case

**Database** is a collection of related data which represents some aspect of the real world

## DBMS, Database Management System

* A software for storing and retrieving users' data
* DBMS Provides security and removes redundancy
* End-Users, Application Programmers, and Database Administrators are users who access a DBMS
* DBMS serves an efficient handler to balance the needs of multiple applications using the same data

{% hint style="info" %}
**Type of DBMS:**

**Hierarchical DBMS**

* In a Hierarchical database, model data is organised in a tree-like structure
* Data is Stored Hierarchically (top down or bottom up) format&#x20;
* Data is represented using a parent-child relationship
* In Hierarchical DBMS parent may have many children, but children have only one parent

**Network Model**

* The network database model allows each child to have multiple parents
* It helps you to address the need to model more complex relationships like as the orders/parts many-to-many relationship
* In this model, entities are organised in a graph which can be accessed through several paths.

**Relational model**

* Relational DBMS is the most widely used DBMS model because it is one of the easiest
* This model is based on normali**s**ing data in the rows and columns of the tables
* Relational model stored in fixed structures and manipulated using SQL

**Object-Oriented Model**

* In Object-oriented Model data stored in the form of objects
* The structure which is called classes which display data within it
* It defines a database as a collection of objects which stores both data members values and operations
  {% endhint %}

## SQL vs NoSQL Database

SQL stands for Structured Query Language, also know as relational database, while NoSQL is known as non-relational database.

> **Relational databases** enforces [ACID](https://ankit-apdc.gitbook.io/system-design/building-blocks/sql#sql-database-properties),  hence we have schema based transaction oriented data stores. It's proven and suitable for 99% of the real world applications. We can practically do anything with relational databases.
>
> However, there are limitations on speed and scaling when it comes to massive high availability data stores. So we bargain availability for speed and scaling purpose and therefore end-up using NoSQL (non relational database)

For example, Google and Amazon have terabytes of data stored in big data centres. Querying and inserting is not performance efficient in these scenarios because of the blocking/schema/transaction nature of the RDBMs. That's the reason they have implemented their own databases (actually, **key-value stores**) for massive performance gain and scalability.

## Database Selection: Questions to be Answered

* How much data do you expect to store when the application is mature?
* How many users do you expect to handle simultaneously at peak load?
* What availability, scalability, latency, throughput, and data consistency does your application need?

{% hint style="success" %}

* **Latency**: Response time for single request
* **Throughput**: Number of transactions per sec
  {% endhint %}

- How often will your database schema change?

{% hint style="info" %}

> The term "[schema](https://en.wiktionary.org/wiki/schema)" refers to the organi**s**ation of data as a blueprint of how the database is constructed (divided into database tables in the case of [relational databases](https://en.wikipedia.org/wiki/Relational_databases))
> {% endhint %}

* What is the geographic distribution of your user population?
* What is the natural “shape” of your data?
* Does your application need online transaction processing (OLTP), analytic queries (OLAP), or both?
* What ratio of reads to writes do you expect in production?
* Do you need geographic queries and/or full-text queries?
* What are your preferred programming languages?
* Do you have a budget? If so, will it cover licenses and support contracts?
* Are there legal restrictions on your data storage?

## Choosing a Database

No database is perfect, serving all the purposes; there are multiple types of database, and every database comes with its own advantages and disadvantages.&#x20;

In general, CAP (Consistency, Availability and Partition-Tolerance) theorem is used as a deciding factor for choosing a database.&#x20;

{% hint style="success" %}
**CAP**

* **Consistency**: Every read receives most recent write or an error
* **Availability**: Every request receives a (non-error) response, without guarantee that it contains the most recent write
* **Partition-Tolerance**: The system continues to operate an arbitrary number of messages being dropped (or delayed) by the network between the nodes
  {% endhint %}

No database at present satisfies all the CAP properties; every database satisfies just two of the three properties. Therefore decision to chose a database comes from the requirements and use-case.

<div align="center"><img src="https://679135566-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ma8GKQU92FWew4_578V%2F-MaDUZVrb8kjeLMR99Zo%2F-MaE0Wy2j7Bk22w-bj37%2FDatabase-Systems-according-to-the-CAP-Theorem.png?alt=media&#x26;token=ef42b77b-5ca9-4ed3-a030-a98085ced627" alt=""></div>

If everything written is complicated for you, here is another way to look at it:

{% hint style="success" %}

* **Atomicity**: Operations are classified into atoms, and that whole atom execute or none of it, not possible to execute a part of it (for example bank transaction), relational database to be used for the purpose
* **Scaling (Vertical or Horizontal)**: NoSQL is preferred for horizontal scaling, for vertical scaling SQL is fine
* **Speed:** if speed is a priority over ACID, need to use NoSQL (document-based is preferred)
  {% endhint %}

This flow diagram will further help in the understanding, which database to chose:

<div align="left"><img src="https://679135566-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ma8GKQU92FWew4_578V%2F-MaDUZVrb8kjeLMR99Zo%2F-MaE2e6B0jxrr2afbYhl%2Fnosql-database-decision-tree-wopt.jpg?alt=media&#x26;token=b39446f7-9c5b-48b8-b9b3-bad607c1c6cc" alt=""></div>

## Reference for Further Reading

{% embed url="<https://www.innoarchitech.com/blog/how-choose-right-database-system-relational-rdbms-vs-nosql-vs-newsql>" %}
