Scaling out
Past one server, data must be split, and joins get expensive
Scaling up has a ceiling; scaling out has none, but spreads data so requests should touch one partition.
NoSQL exists because of a limit. A relational database usually lives on one server, plus copies. When it needs more power you buy a bigger machine, and eventually there is no bigger machine. The alternative is many machines, each holding a slice of the data, chosen by a key. There is no ceiling. But a question that needs rows from every machine, like a join, now has to gather from all of them. So NoSQL databases are designed around one rule: each request should touch one partition.
Data models
The same order, four ways, each cheap for one kind of question
Key-value, document, wide-column and graph databases each make one kind of question cheap.
Here is the order from lesson one, stored four ways. Key-value: a key, and a value the database never looks inside. The fastest lookup there is. Document: the whole order as one JSON record, customer and items nested inside, so one read returns everything, and you can still query any field. Wide-column: under one row key, many sorted entries, ideal for time series and enormous write rates. Graph: the customer, the order and the product as nodes, and the relationships between them stored as links.
Partition keys
A key with many evenly used values spreads the load; a busy one does not
The partition key is hashed to choose a partition. By user id: 216 to 268 writes each. By date: one hot partition takes all 2,000.
In a partitioned store, the key you choose is the most important design decision. Its hash decides which partition holds each item. Key the table by user id, and 2,000 writes spread almost evenly across eight partitions. Key it by today's date, and every write today hashes to the same partition. One partition does all the work and hits its limit while seven sit idle. That is a hot partition, and no amount of extra capacity on the table fixes it.
Access patterns
Design keys from the questions, so each answer is one read
In key-value design, a partition key groups items and a sort key orders them, so a planned question is one Query.
With no joins, the table must be shaped around the questions. List them first: show a customer, show their orders by date. Then give the customer profile and every order the same partition key, CUST forty-two, and a sort key that starts with ORDER and the date. They are stored together, in order. Ben's orders this month is one query to one partition. A question you did not plan for needs a secondary index or a full scan.
DynamoDB capacity · Going deeper
DynamoDB counts reads in 4 KB units and writes in 1 KB units
A 6 KB strongly consistent read costs 2 RCU; a 2.5 KB write costs 3 WCU. An item is at most 400 KB; a Query returns at most 1 MB.
Replication
Reads see the latest write when read and write sets must overlap
With N copies, a write confirmed by W and a read from R see the newest value when R + W > N: 2 + 2 > 3.
Every NoSQL store keeps several copies of each item, often three. A write is acknowledged once 2 of the 3 replicas have it. The third catches up a moment later. A read asks 2 replicas and takes the newest answer. Because 2 plus 2 is more than 3, the replicas it asks must include at least one that has the write. Read from just one, and you are faster, but may see the old value.
Replication layouts · Going deeper
One leader can lag, many leaders can conflict, no leader needs quorums
Single-leader replication sends all writes to one node; multi-leader accepts them in every region; leaderless uses quorums.
Consistency levels
Consistency is a dial with useful stops between the ends
Cosmos DB offers strong, bounded staleness, session (default), consistent prefix and eventual consistency.
Real systems offer more than two choices. Cosmos DB has five. Strong: every read sees the latest write. Bounded staleness: reads may lag, but by no more than a set number of versions or seconds. Session, the default: within your own session you always see your own writes. Consistent prefix: you may be behind, but never see things out of order. Eventual: fastest and cheapest, and everything agrees in time.
Consistency levels · Going deeper
Azure storage for DP-900: Blob, Files and Table
Blob Storage holds objects in containers with access tiers; Azure Files is a mountable share; Table storage is key-value.
Blob Storage holds objects in containers with access tiers; Azure Files is a mountable share; Table storage is key-value. A storage account holds four services. Blob Storage objects in containers. Block blobs for files and data lakes; append blobs for logs; page blobs for virtual machine disks. Access tiers Hot, Cool, Cold and Archive trade storage price against access price; Archive must be rehydrated (hours) before reading. Data Lake Storage Gen2 adds a hierarchical namespace (real folders) for analytics. Azure Files fully managed file shares over SMB and NFS, mounted by Windows, Linux and macOS like a network drive. Used to lift and shift applications that expect a shared folder. Table storage a NoSQL key-value store: each entity has a PartitionKey and a RowKey that together are unique, plus up to 252 properties. Cheap and simple; Cosmos DB for Table offers the same API with global distribution. Queue storage simple message queues between components. Cosmos DB in one paragraph. A globally distributed, multi-model database. Data is stored in containers partitioned by a partition key you choose; throughput is bought in Request Units per second (RU/s), provisioned, autoscaled or serverless. Any number of regions can be added, with single-digit-millisecond reads and five consistency levels. Choose it when you need global scale, low latency and flexible schemas.
A storage account holds four services
- Blob Storage objects in containers. Block blobs for files and data lakes; append blobs for logs; page blobs for virtual machine disks. Access tiers Hot, Cool, Cold and Archive trade storage price against access price; Archive must be rehydrated (hours) before reading. Data Lake Storage Gen2 adds a hierarchical namespace (real folders) for analytics.
- Azure Files fully managed file shares over SMB and NFS, mounted by Windows, Linux and macOS like a network drive. Used to lift and shift applications that expect a shared folder.
- Table storage a NoSQL key-value store: each entity has a PartitionKey and a RowKey that together are unique, plus up to 252 properties. Cheap and simple; Cosmos DB for Table offers the same API with global distribution.
- Queue storage simple message queues between components.
Cosmos DB in one paragraph
Graph databases
Stored links make each hop cheap, however large the graph
A graph database stores nodes and edges; a traversal follows links directly instead of joining tables.
Some questions are about connections. Six people and who knows whom. Ask for friends of Ana's friends. A graph database starts at Ana, follows her links to Bo and Cy, and follows theirs: Di, Ed, Fi. It only ever touches nodes on the path. In SQL, each hop is another self-join over the whole table of pairs, and four hops is four joins.
NoSQL services
Every model is a managed service on every cloud
Azure Cosmos DB is multi-model; AWS has DynamoDB, DocumentDB, Keyspaces and Neptune; Google has Firestore and Bigtable.
Every cloud offers each of these models as a managed service. Azure Cosmos DB is unusual: one service with several interfaces, its native NoSQL API for JSON, plus MongoDB, Cassandra, Gremlin and Table. AWS splits them into DynamoDB, DocumentDB, Keyspaces and Neptune. Google has Firestore and Bigtable. Beside them sit object storage and file shares for unstructured data.
Cosmos DB request units · Going deeper
Cosmos DB charges every operation in request units
A request unit is the cost of reading one 1 KB item by id and partition key; everything else costs more.
A request unit is the cost of reading one 1 KB item by id and partition key; everything else costs more. Three ways to pay for throughput. Provisioned, manual you set RU/s on a container (from 400) or share them across a database. Requests beyond it get HTTP 429 with a retry-after time. Provisioned, autoscale you set a maximum (from 1,000 RU/s); it scales between 10% of that and the maximum, billed by the hour’s peak. Serverless no setting; you pay per RU used. For spiky or small workloads. What drives the RU charge. Cross-partition queries a query without the partition key fans out to every physical partition. Supply the key and it reads one. Indexing every property is indexed by default, so writes pay for it. Exclude paths you never filter on. Consistency reads at strong or bounded staleness cost about twice the RUs of session, consistent prefix or eventual. The partition key is chosen when the container is created. Each response carries its cost in the x-ms-request-charge header: measure real operations before sizing. Strong consistency is not available on an account with multi-region writes. The change feed lists every insert and update in order, per partition key.
Three ways to pay for throughput
- Provisioned, manual you set RU/s on a container (from 400) or share them across a database. Requests beyond it get HTTP 429 with a retry-after time.
- Provisioned, autoscale you set a maximum (from 1,000 RU/s); it scales between 10% of that and the maximum, billed by the hour’s peak.
- Serverless no setting; you pay per RU used. For spiky or small workloads.
What drives the RU charge
- Cross-partition queries a query without the partition key fans out to every physical partition. Supply the key and it reads one.
- Indexing every property is indexed by default, so writes pay for it. Exclude paths you never filter on.
- Consistency reads at strong or bounded staleness cost about twice the RUs of session, consistent prefix or eventual.
Lesson complete
You finished “NoSQL databases”
Nicely done.