Open In App

Transactions in NoSQL

Last Updated : 04 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A NoSQL originally referring to non SQL or nonrelational is a database that provides a mechanism for storage and retrieval of data. In this article, we will see NoSQL transactions. There are some features of NoSQL:

  • It has the feature of Horizontal Scaling.
  • The main advantage of using NoSQL is that it is easy to use for developers.
  • NoSQL has very flexible schemas.

Types of NoSQL Database:

  • Key-Value Databases: They contain only keys and values which is why they are called simpler types of databases.
  • Document Database: In these databases, they contain fields and values and they always store the document similar to JSON.
  • Wide Column Stores: These are the special NoSQL databases because they store data in form of tables, rows, and columns.
  • Graph Databases: Here are Nodes and Edges present where nodes are used for storing information about places, things, and people also. while edges are used for storing information regarding the relationship between the nodes.

Transactions in NoSQL:

Here transactional semantics are described in terms of ACID properties:

  • Atomicity:  It means the transaction can either be completed or fails completely. There is not a partial completion of the transaction in it.
  • Consistency: The database always is in a consistent state while the transactions are at the beginning and end. The data should be consistent when the transaction begins and ends.
  • Isolation: Isolation is the main property as it means when two transactions are being held at the same time we get the same result when the transaction is executed in sequence or the transaction is executed in parallel.
  • Durability: When the transaction is being ended it cannot be reversed as stated by this property of transaction. It also states that any changes in transactions should be saved.

NoSQL database maintains all these properties, Here in Consistency database provides various different consistency policies. At one end application can specify the absolute consistency which actually guarantees that all reads return the most recently written value for a given designated key. wherever at the other end application has the capability to tolerate the inconsistent data and can specify weak consistency.

Isolation’s main aim is to ensure that an operation is independent of other concurrent operations and even has the capability of optimistic/pessimistic locking.

Durability mainly tells us that data is stored in a case when the system failure occurs. In durability the data is durable enough if the disk fails the data can be stored in any other case memory even the memory sometimes can be crashed but if data is durable the data can be saved anywhere and can be fetched when it is to be used.

Importance of ACID Transactions:

These all acid transactions we read above have to ensure the highest possibility for reliability and integrity of data. These acid transactions ensure that our data never falls into an inconsistent state because the operation or task is only partially completed or cannot be completed. ACID properties provide correctness and consistency to our database.

Working of ACID Transactions:

We discuss the working of ACID transactions by taking the example :

Example of ACID transactions

 

We have to send 50 from S1 to S2 so we have to know here how the ACID transaction works in such an operation:

1. Atomicity: The atomicity ensures that the full operation takes place or is aborted back. It ensures that both operations take place or neither even one cannot take place.

  • First operation  S1 : A – 50;
  • Second operation  S2 : B + 50;

2. Consistency: Consistency property ensures that the integrity of transactions is maintained properly. Such as:

  • Before transaction S1 : A + B = 150. ( A = 100, B = 50).
  • After transaction : S2 : A + B = 150. ( A = 50, B = 100).

3. Isolation: Isolation ensures that if the two transactions are going on no other transaction can be accessed between them. It means two operations can be done at one time and no other operation or task can interfere with them.

Such as if two operations such as A – 50 and B + 50 are working no third transaction can interfere with them.

4. Durability: As we discussed above one time it completes the operation cannot be reversed after it. If all transactions are completed it cannot go back to its previous steps.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads