Scaling out

Past one server, data must be split, and joins get expensive

Scale up: one bigger server16 cores32 cores64 cores128 coresjoins and transactions stay easy; there is a largest machineScale out: more servers, data split by keykeys A…keys B…keys C…keys D…keys E…keys F…keys G…keys H…no ceiling; a join or transaction across servers is costlyNoSQL databases scale out by splitting data across many servers, and give up what that splitting makes expensive.A relational database keeps joins and multi-row transactions cheap because all the data is on one machine (plus copies). Spread rows across ahundred machines and a join must gather from all of them. So NoSQL stores make each request touch one server: fetch by key, write one item.Many now offer limited transactions and SQL-like queries, but the design rule holds: one request, one partition.

Scaling up has a ceiling; scaling out has none, but spreads data so requests should touch one partition.

Data models

The same order, four ways, each cheap for one kind of question

Key-valuekeyorder:101value: opaque to the database0x7b2263…(any bytes)get, put, delete by keyRedis · DynamoDBDocument{ "_id": 101, "customer": { "name": "Ben" }, "items": [ {"sku": "PEN"}, {"sku": "INK"} ]}query by any field, nestedMongoDB · Cosmos DBWide-columnrow key: customer#22025-10-01#101: 1.20…2025-10-02#103: 1.20…2025-10-05#105: 1.20…rows sorted, columns varyCassandra · BigtableGraphBenorder 101PenPLACEDCONTAINSfollow relationshipsNeo4j · Neptune · GremlinThe same order stored four ways: each model makes one kind of question cheap and the others harder.Key-value: the fastest, dumbest lookup. Document: the whole order as one self-describing record, so one read returns it all. Wide-column: manysorted entries under one row key, built for huge write volumes and time series. Graph: things and the links between them, built for traversal.In lesson 2 this order took four normalised tables.

Key-value, document, wide-column and graph databases each make one kind of question cheap.

Partition keys

A key with many evenly used values spreads the load; a busy one does not

Key: user idhash("user#1000") … hash("user#2999")p1265p2245p3252p4253p5257p6244p7216p8268Key: today’s datehash("2025-11-15") for every writep10p20p30p42,000p50p60p70p80The partition key decides which server stores each item: many evenly used values spread the load; one busy value cannot.2,000 writes hashed across 8 partitions: by user id each partition takes 216 to 268; by date, one partition takes all 2,000 and the restsit idle. Each partition has a throughput limit (on DynamoDB 1,000 writes and 3,000 reads a second), so a hot partition throttles the whole tablehowever much capacity it has. Choose a key with many values that requests touch evenly: user, device, order. Counts computed by hashing.

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.

Access patterns

Design keys from the questions, so each answer is one read

Query: PK = 'CUST#42' AND begins_with(SK, 'ORDER#') → Ben’s orders, newest last, from one partitionPK (partition key)SK (sort key)attributesCUST#42PROFILEname=Ben · city=LeedsCUST#42ORDER#2025-10-01#101total=1.20CUST#42ORDER#2025-10-02#103total=3.00CUST#42ORDER#2025-10-05#105total=1.20CUST#43PROFILEname=Asha · city=PuneCUST#43ORDER#2025-10-01#102total=6.50one item collection:a customer and their orders,stored together, sorted by SKA key-value design starts from the questions the application will ask, and shapes keys so each answer is one read.This is single-table design: a customer’s profile and orders share a partition key and sort by date inside it, so “Ben’s orders this month” is oneQuery. Write the access patterns down first; a question you did not plan for needs a secondary index (a second copy of the data underanother key) or a scan of everything. Items are limited in size (400 KB on DynamoDB, 2 MB on Cosmos DB), so unbounded lists do notbelong inside one item.

In key-value design, a partition key groups items and a sort key orders them, so a planned question is one Query.

DynamoDB capacity · Going deeper

DynamoDB counts reads in 4 KB units and writes in 1 KB units

Reading one 6 KB item1 read unit = one strongly consistent read a second, up to 4 KBstrongly consistent2 RCU: ⌈6 ÷ 4⌉eventually consistent1 RCU: halfin a transaction4 RCU: doubleWriting one 2.5 KB item1 write unit = one write a second, up to 1 KBstandard write3 WCU: ⌈2.5 ÷ 1⌉in a transaction6 WCU: doubleLimits worth knowingItem400 KB, attribute names includedQuery or Scan1 MB per call, then LastEvaluatedKeyBatchGetItem100 items, 16 MBBatchWriteItem25 puts or deletes, 16 MBTransaction100 actions, 4 MBLocal secondary index5; same partition key; set at creationGlobal secondary index20 by default; any key; eventual readsStreamschanges kept 24 hoursPartition · sort key2,048 · 1,024 bytesDynamoDB bills and throttles by size: reads in 4 KB units, writes in 1 KB units, rounded up per item.A Scan reads, and charges for, every item it passes; a filter only trims what comes back. A Query stays inside one partitionkey. On-demand mode bills per request; provisioned mode sets RCU and WCU with auto scaling and throttles beyond them. Aglobal secondary index has its own capacity and can be added at any time; a local one shares the table’s and caps eachpartition key’s items at 10 GB. TTL deletes expired items at no charge, and DAX is an in-memory cache in front of the table.

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

Client writesprice = 12Replica 1 · 12Replica 2 · 12Replica 3 · still 10acknowledged after 2 of 3Client readsasks 2 replicas: 2 and 3gets 12 and 10; newest wins: 12R + W > N: 2 + 2 > 3every read set overlapsevery write setWith N copies, a write confirmed by W and a read that asks R see the latest value whenever R + W > N.Here N = 3: writing to 2 and reading from 2 must share a replica. Lower R or W for speed (R = 1 answers from the nearest copy) andaccept stale reads. When the network splits, a system must refuse requests to stay consistent, or answer and risk staleness (the CAPtheorem); even with no failure, stronger consistency costs latency (PACELC). Cassandra lets each request choose its levels.

With N copies, a write confirmed by W and a read from R see the newest value when R + W > N: 2 + 2 > 3.

Replication layouts · Going deeper

One leader can lag, many leaders can conflict, no leader needs quorums

Single leaderleaderfollowerfollowerWrites go to one node, which streams itslog to the rest. Reads from a follower may lag.PostgreSQL, MySQL, SQL Server, MongoDB,RDS read replicas, AuroraMulti-leaderleaderleaderRegion ARegion BEach region accepts writes and sends them tothe others. Two edits of one item conflict.DynamoDB global tables, Cosmos DB multi-region writes: by default the last writer winsLeaderlessnodenodenodeAny node takes a write and passes it on; aread asks several and keeps the newest.Cassandra, ScyllaDB, Riak: quorums,R + W > N, chosen per requestWho may accept a write decides what can go wrong when the copies fall out of step.Synchronous replication makes COMMIT wait for a copy, so failover loses nothing (an RDS Multi-AZ standby); asynchronousreplication is faster, but a failover can lose the last writes, and a read just after your own write may miss it (readreplicas). In a network split, CAP forces a choice: a CP system stops writes on the side without a majority (MongoDB, etcd,Spanner); an AP system keeps answering on both sides and reconciles afterwards (Cassandra, DynamoDB global tables).

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

Azure Cosmos DB: five consistency levelsStrongreads always see thelatest committed writeslowestBounded stalenessat most K versions or Tseconds behindSessionsee your own writes, inorderdefaultConsistent prefixnever out of order; maylagEventualany order; convergeseventuallyfasteststronger guaranteeslower latency, higher availability, lower costDynamoDB: eventually consistent reads by default; strongly consistent on request, at twice the read cost. MongoDB: read and write concerns.Consistency is a dial, not a switch: between “always latest” and “eventually the same” sit useful middle levels.Session is the default because it matches what users notice: after I post a comment, I see it, even if a reader across the world sees it a momentlater. Pick per workload: strong for balances and stock counts, session for user-facing apps, eventual for counts of likes and views.

Cosmos DB offers strong, bounded staleness, session (default), consistent prefix and eventual consistency.

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.

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.

Graph databases

Stored links make each hop cheap, however large the graph

AnaBoCyDiEdFiFriends of Ana’s friendsMATCH (a:Person {name:"Ana"}) -[:KNOWS]->()-[:KNOWS]->(f)RETURN DISTINCT f.name→ Di, Ed, Fi: two hops, touching only linked nodesIn SQL: one self-join per hopFROM knows k1 JOIN knows k2 ON k2.a = k1.b …each join searches the whole tableA graph database stores relationships as links, so following them costs the same however large the graph grows.Each hop moves from a node to its neighbours directly, instead of searching a table of pairs. Fraud rings, recommendations, network topologyand access paths are graph questions. Cypher (Neo4j, Neptune) and Gremlin (Neptune, Cosmos DB for Apache Gremlin) are the query languages.

A graph database stores nodes and edges; a traversal follows links directly instead of joining tables.

NoSQL services

Every model is a managed service on every cloud

NeedAzureAWSGoogle CloudKey-valueCosmos DB, Table storageDynamoDBFirestore, BigtableDocumentCosmos DB for NoSQL / MongoDBDocumentDB, DynamoDBFirestoreWide-columnCosmos DB for Apache CassandraKeyspacesBigtableGraphCosmos DB for Apache GremlinNeptune(partners)In-memoryAzure Managed RedisElastiCache, MemoryDBMemorystoreObjects (files, blobs)Blob StorageS3Cloud StorageFile sharesAzure Files (SMB, NFS)EFS, FSxFilestoreEach cloud offers every model as a managed service; Azure Cosmos DB is one service that speaks several of them.Cosmos DB’s APIs are NoSQL (its native JSON API), MongoDB, Apache Cassandra, Apache Gremlin and Table. Azure Table storage is the simpler,cheaper key-value store inside a storage account, keyed by PartitionKey and RowKey. Blob Storage and Azure Files hold unstructured data: objectsover HTTPS, and shared folders a server can mount.

Azure Cosmos DB is multi-model; AWS has DynamoDB, DocumentDB, Keyspaces and Neptune; Google has Firestore and Bigtable.

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.

1 RUa point read of a 1 KB item by its id and partition key. Writes cost several times more; a query costs what it has to read.
2 MBthe largest item.
20 GBthe most data one partition key value (a logical partition) can hold.
10,000 RU/sthe most one physical partition serves, with up to 50 GB. Cosmos DB splits partitions as data grows.

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.

Lesson complete

You finished “NoSQL databases”

Nicely done.

Open in a new tab