Distributed Databases: One Logical Database Spread Across Many Machines
A distributed database stores its data across multiple physical locations, which may be servers in different buildings, different countries, or different cloud regions.
To the user and to the application it behaves as one single database, and a query does not name the site the data sits on.
Everything difficult about a distributed database follows from that gap: the system must keep a single, consistent picture out of parts that fail and communicate over a slow, unreliable network.
Queries and updates behave identically at every site, so the system is far easier to manage.
Heterogeneous
Sites may run different DBMSs, different schemas and different operating systems, often because separate organisations merged.
A translation layer must convert between them, which adds cost and is a common source of errors.
Fragmentation: Splitting the Database Between Sites
Fragmentation divides the database into parts, and each site stores and manages one fragment.
Horizontal fragmentation splits by row, for example each country's branch holding its own customers.
Vertical fragmentation splits by column, for example payroll data held separately from contact details.
Fragmentation pays off when local data is the data accessed most often, because most queries are then answered without touching the network.
The other method of spreading data is replication, which copies data to several sites instead of dividing it, and is covered as a feature below.
Why Consistency Is Hard Once Data Is Spread Out
Data consistency means every user sees the same data whichever site they reach, and it is the hardest guarantee to keep in a distributed system.
Network latency: an update takes measurable time to travel to the other sites, so there is always a window in which sites disagree.
Concurrent access: two users at different sites can update the same item at the same moment, and neither site knows about the other's change yet.
Partial failure: one site can go down while the rest keep running, which no single-machine database ever has to handle.
Example
A hotel chain holds room availability at each hotel and at head office.
Without consistency, a customer books the last room through the website while a walk-in guest books the same room at reception.
Both bookings succeed, and the error only surfaces when the guests arrive.
ACID Across Sites: The Same Four Guarantees, a Harder Job
ACID stands for Atomicity, Consistency, Isolation, and Durability.
The four properties are defined and worked through in A3.3.6, so what matters here is what each one costs once the transaction spans several machines.
Atomicity across sites is enforced by the two-phase commit protocol.
In the prepare phase, a coordinator asks every participating site whether it can commit, and each site replies with a vote and locks the data it would change.
In the commit phase, the coordinator sends commit only if every site voted yes, and sends abort if a single site voted no or failed to reply.
The cost is that each site holds its locks while it waits, so a coordinator that crashes between the phases leaves those sites blocked.
Consistency across sites means a constraint may depend on data the local site does not hold, so checking it requires a round trip before the transaction can complete.
Isolation across sites requires locks to be coordinated between machines, which creates the possibility of a distributed deadlock that no single site can detect on its own.
Durability across sites means a committed transaction must survive the loss of a whole site, so the commit is written to more than one site before it is acknowledged.
Exam technique
An answer that only restates what ACID means will not earn the marks on a distributed-database question.
The mark is for the distributed difficulty: the extra network round trips, the locks held during two-phase commit, and the failure of a site part-way through.
Concurrency control lets many transactions run at once without leaving the data inconsistent.
Pessimistic concurrency control (PCC) locks a resource before touching it, which prevents conflicts but makes other transactions wait.
Optimistic concurrency control (OCC) assumes conflicts are rare, allows the work to proceed, and checks for a conflict at commit time, aborting if one is found.
Tip
Choose PCC where conflicts are frequent, such as seat booking for one popular event.
Choose OCC where conflicts are rare, such as users editing their own profiles.
Naming the scenario is what turns a definition into a justified choice in an exam answer.
Consistency Models: How Quickly Sites Must Agree
Strong consistency: an update is visible at every site before the transaction is reported as complete.
Eventual consistency: an update spreads over time, so sites may disagree for a period but converge if no further updates arrive.
Causal consistency: updates that depend on one another are seen in the same order everywhere, while unrelated updates may arrive in any order.
Note
Strong consistency suits financial transactions, where a stale balance is unacceptable.
Eventual consistency suits a social media feed, where a post appearing a second late for some users costs nothing.
The choice trades correctness against speed and availability, so it depends on what the data is for.
Data Partitioning: Dividing Data for Performance
Data partitioning divides a large table into smaller sections that can be stored and queried separately.
Range partitioning splits on value ranges, such as one partition per year.
Hash partitioning applies a hash function to a key to spread rows evenly across partitions.
List partitioning assigns rows by a specific value, such as one partition per region.
Round-robin partitioning places each successive row in the next partition, giving an even spread with no relationship to the data.
Note
Partitioning improves performance because a query restricted to one range reads only that partition.
A poorly chosen partition key does the opposite: if most queries span every partition, the system pays the coordination cost without the benefit.
Data Security: More Sites Means More Ways In
A distributed database has multiple access points, so an attacker only has to find the weakest site.
Authentication verifies who is making a request, and must be applied consistently at every site.
Encryption protects data both in transit between sites and at rest on each site's storage.
Data crossing between sites travels over networks the organisation may not control.
Encrypting that traffic is what stops an intercepted message being readable, and it is the point most often missed in security answers.
Distribution Transparency: Hiding the Distribution from the User
Distribution transparency means the system behaves as one database, so users write queries without knowing how the data is spread.
Access transparency: data is requested in the same way whether it is local or remote.
Location transparency: a user can reach data without knowing which site physically holds it, so data can be moved between sites without rewriting any query.
Replication transparency: users are unaware that copies exist, and never choose which copy to read.
Failure transparency: the system keeps working when a site fails, serving requests from the sites that remain.
Concurrency transparency: many users work at once without seeing each other's partial results.
Analogy
Distribution transparency works like a cloud storage service on a phone.
A file opens the same way whether it is cached on the device or fetched from a server in another country.
The analogy has a limit: cloud storage can make you wait for a download, while a distributed database is expected to hide the delay as well as the location.
Fault Tolerance: Staying Up When a Site Goes Down
Fault tolerance keeps the system operational despite the failure of a site, a disk or a network link.
Redundancy stores multiple copies of the data, so no single loss destroys it.
Automatic failover switches requests to a working site when a failure is detected, without the user acting.
Fault tolerance is why distributed databases are chosen for banking, healthcare and air traffic control, where downtime is not acceptable.
Note
Fault tolerance is not the same as a backup.
A backup restores data after an outage has already interrupted service, while fault tolerance is what stops the outage being visible at all.
Global Query Processing: Planning a Query Across Sites
Global query processing works out how to answer a query that needs data from several sites.
Local optimization decides how each site finds its own part of the answer.
Global optimization decides which site does which work and in what order, so that as little data as possible is sent across the network.
Moving a large table across the network to join it with a small one is the classic mistake, since sending the small table the other way is far cheaper.
Tip
Design queries to minimise data transfer between sites.
Filtering and aggregating at the local site before sending results is what keeps a distributed query fast.
Replication: Keeping Copies at Several Sites
Replication stores copies of data at more than one site, which improves both availability and read performance.
Full replication copies the entire database to every site, giving the fastest reads and the highest cost to keep consistent.
Partial replication copies only the most critical or most read data.
No replication gives each site unique data, which is fragmentation on its own.
Master-slave: one site accepts all writes and the others copy from it, which keeps consistency simple.
Multi-master: several sites accept writes and reconcile them, which improves availability but creates conflicts to resolve.
Peer-to-Peer: All nodes can act as both master and slave.
Note
Every copy added by replication is another copy that must be kept up to date.
Full replication therefore buys availability at the cost of consistency work on every single write.
Scalability: Growing by Adding Sites
Scalability is the ability to handle more data and more users by adding nodes rather than by replacing hardware with something bigger.
This is the main reason organisations choose a distributed design, and it is why NoSQL and distributed systems are often discussed together.
Advantages of Distributed Databases
Faster performance where queries are answered by local access rather than over the network.
Growth by adding sites, without replacing existing hardware.
Improved fault tolerance and reliability, since one site failing does not stop the system.
Local autonomy, letting each site manage its own data while still sharing it.
Disadvantages of Distributed Databases
More complex to design and manage than a single-site database.
Consistency and concurrency control require extra network round trips, which cost time on every transaction.
Harder to secure, because there are more access points to defend.
Translation problems between systems in a heterogeneous setup.
Note
There is no best set of features to build in, only the set that fits the use case.
A banking system prioritises strong consistency and accepts the latency.
A social platform prioritises scalability and eventual consistency, and accepts brief disagreement between sites.
Real-World Applications
Hotel chains keeping availability at each property while head office reports across all of them.
Air traffic control and military systems, where fault tolerance matters more than cost.
Corporate systems with departments in different countries needing local speed and global reporting.
Pharmaceutical companies tracking treatments and trial data across many national sites.
Active recall
Explain the difference between fragmentation and replication.
Describe the two phases of the two-phase commit protocol.
Why can a coordinator crash during two-phase commit block the participating sites?
Distinguish between strong, eventual and causal consistency.
Name three types of distribution transparency and say what each hides from the user.
Give one advantage and one disadvantage of full replication.