Notes and Talks/UJEP/NSQL/01

NoSQL Databases

What are NoSQL databases?

What are SQL databases?

(SQL: Structured Query Language)

A brief history

How did we get SQL databases in the first place?

The 1950s

In the beginning, (raw) data was stored directly in files. The data format had to take the storage medium into account: punched cards, magnetic tapes, disks. The physical layout of data was optimized for efficient writing and reading; records often had a fixed structure and length.

The 1960s

Greater disk capacity and faster writing and reading made it possible to store variable-length records, more complex structures, and indexes. Database systems emerged that offered an abstract view of data and made it possible to search, sort and query data.

The 1970s

Edgar F. Codd🌐 proposed the relational model🌐 of databases: data is represented using relations, usually in the form of tables. Data normalization removes redundancy. Queries are expressed using relational operations: joining (JOIN) and selection (WHERE).

The 1980s

The relational model became established in practice. The SQL language🌐 emerged to standardize work with data (definition, manipulation, and queries).

More: RDBS.

The emergence of NoSQL databases

1998

The term NoSQL was first used (coined) by Carlo Strozzi for his relational database.

2009 →

The term NoSQL began to be used to describe a range of non-relational, distributed databases. (Strozzi: NoREL would be a more appropriate term.)

Its adoption was due to the growth of the web, cloud computing, social networks, mobile applications, and IoT →

Traditional relational databases are not well suited for these tasks.

→ Big Data:

How big is it? The five Vs of big data:

Some sources add further Vs; for example:

Scalability:

It is a property of a system's architecture that makes it possible to increase its performance and capacity by adding resources (hardware, servers, nodes).

In simplified terms:

Vertical scaling

Traditional database systems scale vertically (scaling up).

From the client's perspective, the system appears to be a single server, even if its internal implementation is more complex.

Horizontal scaling

NoSQL systems typically scale horizontally (scaling out).

Servers (nodes) work together in a cluster. Data can be partitioned across nodes (sharding), replicated across multiple nodes (replication), or both. (Replication increases availability and resilience to node failure, but by itself does not increase the capacity to store distinct data.)

vertical one server CPU  RAM  disk horizontal node node node

Vertical scaling enlarges one server. Horizontal scaling adds nodes.

Database consistency / data integrity

Relational (non-distributed) databases: ACID

Strong/strict, transactional consistency: integrity constraints are guaranteed between transactions (a consistent state).

BEGIN → READ/WRITE → COMMIT/ROLLBACK

Atomicity
Consistency
Isolation
Durability

Distributed systems: BASE

We trade strong consistency and isolation for weaker consistency plus better scalability (performance), availability, and resilience to failures (graceful degradation).

NoSQL databases can support ACID transactions; ACID and BASE are therefore not simple, mutually exclusive categories.

CAP

In a distributed data storage system, there are three guarantees:

The CAP (quasi-)theorem

Eric E. Brewer: Towards Robust Distributed Systems:

Only two of C, A, and P can be guaranteed simultaneously.

If a network partition cannot occur, or partitions are not tolerated (there is no need to guarantee P), we can have both C and A at the same time: for example, in a centralized ACID database or with two-phase commit over a network.

In a distributed system with shared data, however, P must be guaranteed; it means that network partitions must be tolerated. What happens when a network partition occurs? The system chooses which operations to restrict:

node node node node partition side A side B

A partition splits the cluster. The two sides cannot exchange the latest write.

Once the network is connected again,

and we have AC(P) again.

It is not a black-and-white choice; there is a gray area CAP twelve years later: how the rules have changed:

Eventual consistency

eventual v1 update v1 ⇒ v2 v1 or v2 v2 strong v1 update v1 ⇒ v2 v2 v2 window

During the inconsistency window, some reads may return an older value. If writes stop, replicas can synchronize and converge; the system's strategy determines which value remains after conflicting writes.

Data distribution

Two distinct, often combined techniques:

sharding A B C replication A A A

Sharding stores different records. Replication stores copies of one record. Lecture 02 develops both.

Sharding

Main goals:

Heterogeneous clusters

We can choose a special strategy when data is "hot" and "cold": distribute

hot current fast, small cold history slow, large

Current data can live on a fast small store, and history on a slow large one.

Replication

Two basic strategies:

Master-slave replication

Advantages:

Disadvantages:

client primary write secondary secondary log log client client read read

Writes go to the primary. The log reaches the secondaries with a lag. Reads may go to a secondary, which can be behind.

Peer-to-peer replication

masterless/leaderless

All nodes are equal, and each can accept both writes and reads. How can the nodes be kept consistent?

Quorum: a minimum number of nodes must confirm an operation for it to be considered successful.

Common configurations:

client coordinator replica replica replica ACK ACK

A write with W = 2. Two replicas have acknowledged, so the coordinator can answer before the third replica finishes.

How a write works

  1. The client sends a write request to one node (the coordinator).

  2. The coordinator forwards the write in parallel to all replicas, including itself.

  3. Each replica that successfully performs the write sends an acknowledgment (ACK) back to the coordinator.

  4. The coordinator counts the ACKs. Once it has W, it reports success to the client.

  5. The remaining replicas complete the write.

  6. If W ACKs have not arrived before the timeout expires, the coordinator reports failure to the client.

How a read works

  1. The client sends a read request to one node (the coordinator).

  2. The coordinator requests the current value in parallel from enough replicas to reach R responses. Each value is tagged with a version or timestamp.

  3. Once the coordinator has received R values, it returns the one with the newest version to the client.

  4. If some replicas have an older value, the coordinator then initiates read repair — an asynchronous update of the stale replicas.