Database Design & Scaling Flashcards
NoSQL or SQL?
Structured, tabular data with a predefined schema.
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Strong adherence to data types and relationships using foreign keys, constraints, and normalization.
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Best suited for applications where data consistency and relationships are vital, such as transactional systems (e.g., banking).
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Pros: Data integrity, ACID compliance, strong consistency.
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Cons: Limited flexibility if the schema needs frequent changes. You have to alter tables and perform migrations when adding new fields.
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Schema-less or flexible schema, with hierarchical or unstructured data.
NoSQL (MongoDB, Cassandra):
NoSQL or SQL?
Supports key-value pairs, document-based storage, wide-column stores, or graph data structures.
NoSQL (MongoDB, Cassandra):
NoSQL or SQL?
Best for applications where flexibility, scalability, or storing large volumes of unstructured or semi-structured data is more important than strong consistency (e.g., social networks, big data applications).
NoSQL (MongoDB, Cassandra):
NoSQL or SQL?
Pros: Dynamic schema allows you to evolve and scale without altering tables or migrating data.
NoSQL (MongoDB, Cassandra):
NoSQL or SQL?
Cons: Less rigid data integrity, and query complexity increases as you scale.
NoSQL (MongoDB, Cassandra):
NoSQL or SQL?
These databases typically focus on strong consistency (ACID properties: Atomicity, Consistency, Isolation, Durability), ensuring every transaction is executed exactly once and in a correct state.
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Availability can be a concern when scaling horizontally (across servers), as these distributed database systems can introduce complexity and latency to maintain consistency across nodes.
NoSQL ( Cassandra, MongoDB )
NoSQL or SQL?
These systems prioritize availability and partition tolerance over strong consistency (i.e., eventual consistency model). This is crucial for distributed systems where uptime and scaling are more important than immediate consistency.
NoSQL (MongoDB, Cassandra):
Especially Cassandra
NoSQL or SQL?
This DB offers tunable consistency levels (strong or eventual), while a DB of the same type defaults to eventual consistency.
NoSQL, specifically MongoDB
Define ACID compliance
ACID compliance refers to a set of properties that guarantee reliable processing of database transactions in SQL (relational) databases. It ensures that transactions are processed consistently, even in the event of errors, power failures, or other issues.
The A in ACID
Atomicity
Ensures that each transaction is treated as a single, indivisible unit. Either all operations within the transaction are successfully completed, or none of them are. If any part of the transaction fails, the entire transaction is rolled back to its previous state.
Short: All or nothing.
Example: In a banking application, transferring money from Account A to Account B should either debit Account A and credit Account B entirely, or not happen at all.
The C in ACID
Consistency
Guarantees that a transaction brings the database from one valid state to another, maintaining database rules such as constraints, triggers, and referential integrity. After the transaction, the database will always be in a valid state, ensuring data integrity.
Short: Valid States Only
Example: If a transaction violates a foreign key constraint (e.g., referencing a non-existent user), it will fail, preventing invalid data from being inserted.
The I in ACID
Isolation
Ensures that transactions are executed independently of one another. The intermediate states of a transaction should not be visible to other concurrent transactions, preventing issues like dirty reads, lost updates, and phantom reads.
Short: Transactions Don’t Intefere
Example: If two users are buying the last product from an online store, isolation ensures that only one transaction will succeed in updating the inventory, even if both are processed concurrently.
The D in ACID
Durability
Guarantees that once a transaction is committed, it will remain committed, even in the event of a system failure. The changes made by the transaction are permanently recorded to stable storage, typically by using techniques like logging and backups.
Short: Changes are permanent after commit.
Example: After a bank transfer is confirmed, the transaction will persist even if the system crashes immediately after.
NoSQL or SQL?
Scaling relational databases is typically vertical (scaling up by adding more power to a single machine).
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Horizontal scaling (across multiple machines) is possible but more complex due to the inherent need for transactional consistency and managing joins across nodes (sharding is complex).
SQL (MySQL, PostgreSQL):
NoSQL or SQL?
Performance can be limited by this scalability bottleneck for high-traffic applications.
SQL (MySQL, PostgreSQL):