[Database] Simple SQL vs NoSQL

1. Schema

  • SQL stores data in tables.
  • NoSQL stores in different methods (key/value, document, columnar and graph)

2. Querying

  • SQL databases uses regular SQL statements.
  • NoSQL uses unQL (Unstructured Query Language)

3. Scalability

  • SQL databases are vertically scalable (beef the same server hardware).
  • NoSQL databases are horizontally scalable (add more servers)

4. Reliability

SQL databases are ACID friendly, NoSQL sacrifices ACID property for performance reasons.

4.1) What is ACID?

Set of properties of database transactions to guarantee validity even in the event of errors, power failures.etc.

Atomicity Consistency Isolation Durability

4.1.1) Atomicity

  • Transactions are often composed of multiple statements. 
  • Atomicity guarantees each transaction is treated a single 'unit', which either succeeds completely or fails completely. 
  • If any of the statements in a transaction fails to complete, the entire transaction fails and database is left unchanged. 
  • An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes.
  • Eg. money transfer from account A to B. It consists of two operations: withdraw from A and deposit to B. These 2 operations are atomic and ensures that money is neither lost or created if either of those two operations fail.

4.1.2) Consistency

  • Ensures transaction can only bring the database from one valid state to another.
  • Any data written must be valid according to defined rule.

4.1.3) Isolation

  • Transactions are often executed concurrently. 
  • Isolation ensures that concurrent execution of transactions leaves the database in the same state that would have been obtained if transactions were executed sequentially. 
  • DBMS usually acquires locks on data which may result in a loss of concurrency or implements multiversion concurrency control. This requires adding logic for the application to function correctly.
  • Most DBMSs offer a number of transaction isolation levels, which control the degree of locking that occurs when selecting data. (Serializable, Repeatable reads, read committed, read uncommited)

4.1.4) Durability


  • Once a transaction has been committed, it will remain committed even in system failure.
  • This means that transactions are recorded in non-volatile memory (memory that can be restored after a power-failure - not temporary)
  • Eg. if flight booking reports that a seat is successfully booked, the seat remains booked even if the system crashes.
  • Many DBMSs implement durability by writing transactions into a transaction log that can be reprocessed to recreate the system state right before any later failure. A transaction is deemed committed only after it is entered in the log.

5) When to use SQL?

  • Data has table and defined structure.
  • ACID compilance
  • Not looking for insanely fast data retrieval (reading from dish vs memory)


6) When to use NoSQL?

  • Storing big amount of data that has no defined structure.
  • Using cloud storage and hosting (for scaling)
  • Quick software development and prototyping.



Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS