Yesterday I had a session with Ankita Tripathi. It was a really great session, and I learned a lot about SQL databases and NoSQL databases.
What I Understand from SQL vs NoSQL
SQL (Structured Query Language)
SQL is a database that stores data in a vertical way. In SQL databases, you store data in the form of rows and columns. The first row contains fields, and the next rows contain data for those fields. So, it's like adding data vertically.
SQL is mostly used in applications where you already know the structure of the data (i.e., fields) before storing it. For example, if you want to store user data or banking data, you can use a SQL database.
NoSQL (Not Only SQL)
NoSQL is a database that stores data in a horizontal way. In this, data is stored in JSON format, and you can have as many fields as you want. It is generally faster compared to SQL.
When you have dynamic data with multiple fields, you use a NoSQL database. For example, when storing product data, you use NoSQL. It stores data in the form of JSON and keeps adding new fields as needed, which makes it scalable.
Examples
Flipkart
Most of the data in Flipkart is stored using NoSQL. That's because it has many products with different categories and different data structures. If SQL were used here, it would get very complicated. For example, in the mobile category, you might see fields like screen size and accessories, whereas in the clothing category, you see size and color. NoSQL handles this easily.
Netflix
Netflix uses a hybrid model, which means it uses both SQL and NoSQL. When it comes to data like recommendations or watch-later lists, NoSQL is used because it's faster when dynamic tasks are happening. But for things like user data or payment data, SQL is used since it provides better insights and structure.
Uber (Ride Sharing)
Uber also uses a NoSQL database because there are frequent changes in fields with different categories. For example, if Uber introduces a new feature like pool sharing or bus rides, with SQL they would have to create new fields manually, which can make the app unscalable. But with NoSQL, it's easier to add new features with different fields. Also, for real-time ride locations, faster data access is needed, which NoSQL provides.
Why We Use SQL
SQL stores data in rows and columns, which makes it easier to generate insights like charts and reports. It is great for structured data like banking systems where all fields are required. If one field is null, it throws an error — which is actually useful when you want to make sure all information is filled in.
Why We Use NoSQL
NoSQL stores data in JSON format, and you can add fields anytime. It's flexible and works well when you're adding new features or when speed is important. Operations in NoSQL are generally faster, and it allows you to update or insert new fields easily without changing the overall structure.
CAP Theorem
CAP Theorem is a fundamental concept used in distributed systems. It gives three guarantees:
- Consistency: Returns the most recent data or an error.
- Availability: Every request receives a response.
- Partition Tolerance: The system keeps working even if there are network issues.
According to the Theorem
A distributed system must tolerate partitions. It can only choose one of the other two — consistency or availability.
- CP: System prefers consistency over availability (like NoSQL databases).
- AP: System prefers availability over consistency (like SQL databases).
Top comments (0)