Skip to main content

Databases, Replication and Sharding

SQL vs NoSQL​

Relational (SQL)NoSQL
ModelTables, joins, schemaDocument, key-value, wide-column, graph
ConsistencyACID transactionsOften tunable / eventual
ScalingVertical first, then replicas and shardingDesigned to scale out
Use whenStrong relationships, transactionsHuge scale, flexible schema, simple access patterns

Replication​

ModeBehaviour
Leader-followerWrites go to the leader; followers serve reads
Multi-leaderSeveral writers, needs conflict resolution
LeaderlessQuorum reads and writes (Dynamo style)

Synchronous replication gives stronger durability with higher latency; asynchronous is faster but followers can lag (replication lag), so a user may read stale data after writing.

Sharding (partitioning)​

Split data across machines.

SchemeDescriptionTrade-off
RangeKey ranges per shardEasy range queries, hot spots
Hashhash(key) mod N or consistent hashingEven load, hard range queries
DirectoryLookup service maps keys to shardsFlexible, extra hop

Challenges: cross-shard joins and transactions, resharding, choosing a shard key that avoids hot spots.

CAP theorem​

During a network partition a distributed store must choose between consistency (every read sees the latest write) and availability (every request gets a response).

TypeBehaviour in a partitionExamples
CPRefuse some requests to stay consistentHBase, etcd, ZooKeeper
APKeep serving, reconcile laterCassandra, DynamoDB (default)

The PACELC extension adds that, even without a partition, you trade latency against consistency.

Consistency models​

Strong, eventual, causal, read-your-writes, monotonic reads.

Indexing and storage engines​

B-trees favour reads and in-place updates (typical relational DBs); LSM trees (Cassandra, RocksDB) favour high write throughput with background compaction.