Memory -- In-memory DB(H2)
SSD (Flash)
Diskhtml
IOPS
Latency
Throughput
Capacity -- persistencenode
Solved: Scalability
Not Solved:sql
Query Language Secondary Index: Primary Index is global, and Secondary Index in local(need to be created); ACID transaction Trust and Confidence
Impl: persistent HashMap
Aggregate: value数据库
JSON format Document → Document ID → Transparent
Aggregate: document并发
Row-key → column family
Column family = key + value
Aggregate: column familyapp
You can easily pull individual column/document/value back and forth about the case
Store the whole thing -- Atomicity
You can distribute the data by placing different aggregates on different nodes
Design Trade-offnosql
Consistency: Single Threading + Async
Availability: Every request must be processed
Partition
What we want: shortest latency and highest consistency分布式
Only one search type: AODB (same aggregate)
Solution: Map-reduce
More searches: RDB ide
http://www.cnblogs.com/fxjwin...post
-- | IOPS | Latency | Throughput | Capacity |
Memory | 10M | 100ns | 10GB/s | 100GB |
Flash | 100K | 10us | 1GB/s | 1TB |
Hard Drive | 100 | 10ms | 100MB/s | 1TB |
Flash - lifetime IO limitation
Hard Drive - Mechanical seeking
延时: Latency = 1 second / IOPS
频率: IOPS
IOPS和Throughput并无直接的关系,通常而言,Throughput不会限制IOPS。
IOPS是操做次数的频率,Throughput是吞吐量。
ACID,是指数据库管理系统(DBMS)在寫入/更新資料的過程中,為保證事务(transaction)是正確可靠的,所必須具備的四个特性:原子性(atomicity,或稱不可分割性)、一致性(consistency)、隔离性(isolation,又称独立性)、持久性(durability)。
原子性:一個事务(transaction)中的全部操做,要么所有完成,要么所有不完成,不会结束在中间某个环节。事务在执行过程当中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务历来没有执行过同样。
一致性:在事务开始以前和事务结束之后,数据库的完整性没有被破坏。这表示写入的资料必须彻底符合全部的预设规则,这包含资料的精确度、串联性以及后续数据库能够自发性地完成预约的工做。
隔离性:数据库容许多个并发事务同时对齐数据进行读写和修改的能力,隔离性能够防止多个事务并发执行时因为交叉执行而致使数据的不一致。事务隔离分为不一样级别,包括读未提交(Read uncommitted)、读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)。
持久性:事务处理结束后,对数据的修改就是永久的,即使系统故障也不会丢失。
读写频率:
DAU = 1M, 每秒平均登陆数 = 1M * 2 queries per day / 86400seconds = 25 QPS
Peak = 25 * 2 = 50 QPS
数据量:
1M * 10KB = 10GB
Tweet Table | ||
---|---|---|
post_id | content | user_id |
谁赞了个人贴?
Add user list column in post table?(幼稚的作法)
Implementation:
Select users.user_id, users.name From likes Join users On likes.user_id = users.user_id Where likes.photo_id = 9
我赞了哪些贴?在user table也加post list column?
Scalability: 1M用户赞了一个贴以后,每一个新赞都得把整个list读出再写回去。
实现快速查找。例如,用users.user_id或者users.email查找用户。
常看法法:
B+ Tree
Block based & Sequential Read -- 适合硬盘 -- Disk的sequential读写很快
例如1M个nodes,用二叉树存,须要21层;若是用1024叉树,须要3层;
check memory的话,搜索一个node,二叉树要搜索21 1 = 21次,1024叉树须要3 1023 = 3K次
check Hard Drive的话,二叉树须要21次,1024叉树层数少,因此只须要3次(延时远大于check memory);其次,B+ Tree内容都在叶子结点,一大块一大块连续存储,硬盘读取更快。(使用sequential read,利于在同一个level的顺序查找)
Hash: 更费空间一点
Begin Transaction; change userA.money; change userB.money; Commit Transaction;
Write Ahead Log:
Begin Transaction; //LOG: change userA.money from 10 to 0 change userA.money; //LOG: change userB.money from 5 to 15 change userB.money; /LOG: Success Commit Transaction;
if you cannot find the log, roll back.
Trick: Save Logs on SSD(faster).
Will transaction and logs have race condition?
待续。
例如,100M用户,100TB数据。
拆数据库
根据应用把不一样类型的数据放在不一样的Database机器上
Tradeoffs
Relations再也不存在
Transaction没法保证
瓶颈:一张表都放不下了、或者Latency要求很高(须要存在Memory中)
Database sharding 拆表 (sharding key = primary key)
Transaction怎么办
Range Query或non-sharding key query须要查询多台机器
Latency ^
Reliability v
数据继续增大,怎么办
方案一:基于重建数据库的data migration(Function: % N)
开N+1台新机器
Double Write(既写入老机器,也写入新机器)
写一个小脚本
从以前的机器读出全部数据
依次写入新机器
Verification 看一致性
kill老机器
方案二:Consistent Hashing
把数据map到一个圆上
N台机器按360/N的比例分配数据段
问题:Web server怎么知道谁负责哪一段?从相交数据段的两台机器上migrate数据
High Volume Read
Data size imbalance 不必定要分配的数据量均匀
Consistent Hashing的缺点:和%N的Hashing相比,function比较复杂,维护更难一些
方案三:现实系统中通常采用Micro Shards (Virtual Nodes)
例如网站每秒几万次的访问量
方案:多开几台机器,存储相同的data,这样在不少read的时候,分担压力
Tradeoff:带来replicate多台机器的write问题
方案:先写入Master Server,再由Master机器写入其它的Slave机器,可是,会有延迟,带来data consistency的问题
问题:Master crashed? 解决:replace by a slave, or double master.
方案:Zookeeper,quorum write,保证全部机器同时获得更新,见Paxos,raft的介绍
Everywhere
Client, Edge/CDN, Datacenter, L1/L2/L3
Problem: Hot Post(明星发推)
Tool: Memcache 在Memory里作的Cache 减小对DB的queries
Memcache Simple API: get(key)-->value, set(key, value, time-to-live)
Million Level QPS per box
k-v pair data --> | Memtable(skiplist) --> | SS Table(key-sorted --> merge) |
write to | batching | key-sorted |
also write to Write Ahead Log |
数据量增大
拆数据库
Sharding
读操做增多
Replication
Caching
写操做增多
LSM Tree Based Index
Scalability
Query Language
Secondary Index
ACID Transactions
Trust and confidence