Deep Dive into NoSQL: Reading Notes
Original Chinese title: 深入nosql读书笔记
Section 1: NoSQL — Not Only SQL
A broad term for databases and data stores that do not follow classic RDBMS principles and are often associated with large, web-scale datasets.
An RDBMS assumes that data has a well-defined schema, is dense, and is largely consistent.
-
Big data: how much data counts as “big”?
Right now, any dataset larger than a few terabytes can be classified as big data. -
Sources of big data
(1) Sensor data: Sensors distributed across different geographic locations continuously sense their environment and generate data. Even after filtering to keep only useful records, the volume accumulated over time is astonishing. (2) Website clickstream data: For effective marketing and promotion, every click a user makes online—along with its timestamp—is recorded. Using this data, service providers can carefully analyze user access patterns and deliver more targeted services. (3) Mobile device data: Through mobile electronics such as mobile phones, PDAs, and navigation devices, we can obtain information about the location, movement, and behavior of devices and people. Timely analysis of this data enables effective decision-making, such as in traffic monitoring and guidance systems. (4) RFID data: RFID tags can be embedded into products to track physical objects. Once RFID is widely adopted, it will become one of the primary sources of massive data.
Section 4
-
HBase column families
-
Column family: Some keys have highly sparse values while others behave differently; separating them into column families makes indexing easier and speeds up access.
-
Google’s main key themes:
1. Data storage uses a distributed network file system so the system can scale across multiple machines. Files themselves can be very large and are stored across different nodes, each running on a separate machine. 2. The storage structure should support multiple properties: efficiently storing massive sparse data and adapting to evolving schemas without modifying the underlying tables. 3. The data processing model should support running computations on isolated subsets of data and then merging the results. -
Column-oriented databases
LRU algorithm (Least Recently Used) Consistent hashing Naive key assignment strategy: modulo function. E.g., assigning 50 keys to 7 nodes: key 18 goes to node 4 since 18 mod 7 = 4. Drawback: adding or removing a node forces keys to be reshuffled, which is very inefficient. With consistent hashing, adding or removing nodes does not cause a massive impact. Object versioning Vector clocks
Section 9: Transaction and Data Integrity Management
-
9.1 ACID and RDBMS
1. A (Atomicity): A transaction either succeeds completely or fails completely. 2. C (Consistency): If predefined constraints or rules are violated, data will not be persisted. 3. I (Isolation): Data concurrency—if two independent processes or threads operate on the same dataset, they can potentially affect each other. 4. D (Durability): Once a transaction is committed, its persistence is guaranteed. -
The strictness of the isolation level directly affects concurrency.
-
Using higher isolation levels increases the likelihood of starvation and deadlocks.
Starvation occurs when one transaction locks resources needed by other transactions, while a deadlock occurs when two concurrent transactions are each waiting for the other to release resources. -
Three factors affecting distributed systems
1. Consistency (C): Means consistent reads and writes so that concurrent operations see the same valid, consistent state of the data—or at least never stale data. 2. Availability (A): A system that is busy, unreachable, or unresponsive when accessed is considered unavailable. 3. Partition tolerance (P): Parallel processing and horizontal scaling, as opposed to scaling up and building massive supercomputers. -
Vertical scaling vs. horizontal scaling (individual capability vs. team capability)
-
Fallacies of distributed computing (things developers take for granted that often don’t hold)
1. The network is reliable. 2. Latency is zero. 3. Bandwidth is infinite. 4. The network is secure. 5. Topology doesn't change. 6. There is one administrator. 7. Transport cost is zero. 8. The network is homogeneous. -
The three pillars of Brewer’s theorem, which encapsulates the thinking on transaction integrity for this generation of large, scalable distributed systems.
A distributed system cannot simultaneously guarantee all three properties. The system must make trade-offs, sacrificing at least one to achieve the other two. -
Gilbert and Lynch’s proof
-
Trade-offs to make
-
Traditional RDBMSs compromise availability, choosing consistency and partition tolerance
For example, bank transfers. In extreme cases, the entire system may become completely unavailable after a node fails. In many cases, systems support backups, fast replication, and failure recovery. -
Compromising partition tolerance, choosing consistency and availability
Difference between partition tolerance and fault tolerance: If a system cannot serve a partition isolated by the network, but can instantly fail over to other nodes, it is fault-tolerant, though not partition-tolerant. Classic example: Google Bigtable. Primary-replica model: when a failure occurs, ZooKeeper determines which is the primary partition and which is the replica. Based on that decision, all reads and writes are routed to the primary, while the replica switches to read-only until the issue is resolved. Bigtable and HBase store three copies of each dataset, so if one of the three copies fails or falls out of sync, consistency can be maintained through consensus. -
Compromising consistency (weak consistency)
Classic examples: still allowing orders when inventory is zero; Redis not returning 100% of requests.
-
-
R = read quorum, W = write quorum, N = number of nodes in the cluster
-
Three cases
R + W > N: In the extreme case R + W = 2N, consistency is easy to establish because the read and write node sets overlap. In the extreme case the system is strictly consistent and can provide ACID. R = 1, W = N: Reads far outnumber writes. High read concurrency needs to be spread across every node in the cluster. For reads, each node is independent of the others; every write updates all nodes. R = N, W = 1: Writes far outnumber reads; data inconsistency is very high. R = W = Ceiling((N + 1) / 2): Roughly equal reads and writes; requires efficient election. -
BASE (Eventually Consistent)
Section 11
-
Operating on big data: methods and tools should run in parallel with as few interaction points as possible, while minimizing the volume of data transferred (to reduce I/O overhead).
Over
section 1 nosql :not only sql
泛指这一类数据库和数据存储:不遵循经典RDBMS原理,且常与web规模的大型数据集有关。
RDBMS 假定数据的结构已明确定义,数据是致密的,并且很大程度上时一致的。
-
大数据:多大的数据量才算大。
目前来说,任何超过几个TB大小的数据集都可以归为大数据。 -
大数据来源
(1)传感器数据(sensor data):分布在不同地理位置上的传感器,对所处环境进行感知,不断生成数据.即便对这些数据进行过滤,仅保留部分有效数据,长时间累积的数据量也是非常惊人的; (2)网站点击流数据(click stream data):为了进行有效的市场营销和推广,用户在网上的每个点击及其时间都被记录下来;利用这些数据,服务提供商可以对用户存取模式进行仔细的分析,从而提供更加具有针对性的服务; (3)移动设备数据(mobile device data):通过移动电子设备包括移动电话和PDA、导航设备等,我们可以获得设备和人员的位置、移动、用户行为等信息,对这些信息进行及时的分析,可以帮助我们进行有效的决策,比如交通监控和疏导系统; (4)射频ID数据(RFID data):RFID可以嵌入到产品中,实现物体的跟踪.一旦RFID得到广泛的应用。将是大量数据的主要来源之一.
section 4
-
hbase 列族
-
column family 有一些key的value 松散幅度比较大,但是有一些key的value则不同,区分所作的设置,方便索引,加速。
-
google的主要关键主题:
1. 数据存储使用网络文件系统,系统能扩展到多台机器上。文件本身可能非常大,存储在不同节点上,每个节点运行在不同的机器上。 2. 数据存储结构应具备多种特性,高效存储海量稀疏数据,能够适应结构不断调整,同时还不用更改底层表。 3. 数据处理方式应支持在互相隔离的数据子集上执行计算,然后再合并计算结果。 -
面向列的数据库
LRU算法(least recently used) 一致性哈希 简朴的键分配策略:取余函数。e,g 把50个键分配给7个节点。键18去节点4,18mod7 = 4。缺点:一旦添加或者删除节点,键就需要重排,十分低效。 一致性哈希,节点增减不会产生非常大的影响。 对象版本 矢量时钟
section 9 事务和数据完整性的管理
-
9.1ACID 和RDBMS
1. A 原子性 一个事务操作要么完全成功,要么完全失败。 2. C 一致性 如果违反预定义的约束或规则,数据就不会被持久化。 3. I 隔离性 数据的并发 如果两个独立的进程或线程操作统一个数据集,他们有可能会影响到对方 4. D 持久性一旦事务操作被确认了,它就一定会得到保证。 -
隔离级别的严格程度直接影响到并发性。
-
使用高级别的隔离增大饿死和死锁的可能性。
一个事物锁住了其他事物要用的资源的时候会发生饿死,而两个并发事务相互等待对方释放资源时会发生死锁。 -
三个影响分布式系统的因素
1. 一致性C:意味着一致的读写,这样并发操作能看到同样的有效并且一致的数据状态,至少没有过时的数据 2. 可用性A: 一个系统很忙、无法沟通,或者访问时没有响应则被认为是不可用的。 3. 分区容忍性P:并行处理和横向扩展对立的时向上扩展和构建大规模超级计算机 -
垂直扩展和水平扩展(个人能力和团队能力)
-
分布式计算的谬误(开发者认为是理所当然,但又常常不成立的)
1. 网络是可靠的 2. 延迟为0 3. 带宽是无限的。 4. 网络是安全的。 5. 拓扑不会发生变化 6. 只有一个管理员 7. 传输成本为0 8. 网络都是同类型的。 -
Brewer定理 的三大支柱,该定理承载着对这一代大型可扩展分布式系统事务完整性的思索。
分布式系统不可能同时实现所有这三个特性。系统必须作出权衡,至少牺牲一样以成全其他两样。 -
Gilbert 和 Lynch的证明
-
需要做的妥协
-
传统的RDBMS妥协了可用性,选择了一致性和分区容忍性
比如银行转账。 极端情况下,节点失败之后整个系统可能完全不可用。 很多情况下,系统支持备份、快速复制和错误恢复。 -
妥协分区容忍性,选择一致性和可用性
分区容忍性和容错的区别 一个系统无法为受网络隔离的分区提供服务,但是能瞬间切换到其他节点,那时容错的,但是不具备分区容忍性 典型例子 google Bigtable 主从模式 出现错误时,Zookeeper确定哪个是主分区,哪个是从分区,基于判断,所有读写定向到主分区,从分区改为只读,直到问题解决。 Bigtable 和 HBase 对每个数据集分别存储三份,这样三份中的一份出错或无法同步的情况下,课通过协商方式保持一致性。 -
妥协一致性(弱一致性)
典型例子 库存位零仍旧可以下单,redis的非100%返回请求。
-
-
R 读 W 写 N 集群节点数量
-
三种情况
R+W>N 极端情况R+W = 2N 很容易建立一致性,因为读和写的节点存在着重叠。而极端情况下系统绝对一致,可以提供ACID。 R=1 W=N 即系统中读远远大于写。读的高并发需要分散到集群的每一个节点上。就读来说,每一个节点都是独立于其他的节点。一旦写操作即更新所有的节点。 R=N W=1 系统中写远远大于读,数据不一致性非常高。 R=W= Ceiling((N+1)/2) 读写各占一半,需要有效选举。 -
BASE 最终一致性
section 11
-
操作大数据:方法和工具能并行执行,而且之间的交互点要尽可能小。同时并行处理工具要尽可能降低传输的数据量(减少I/O的干扰)