# Multi-Master database replication

iCure Database is built on Apache CouchDB a web enabled database optimized for use over a network with multiple users, oriented at massively distributed systems with heavy usage that handle "Big Data".

### iCure Online/Offline update mode

CouchDB does not store data in a table along with relationships, but uses a B-Tree instead.  Each database in every user device is a collection of independent documents that maintain their own data and self-contained schemas. Document metadata contains revision information, so that differences happening while databases were disconnected, may be merged. CouchDB impelements its own version of a Multiversion Concurency Controll system, that supports [Master-Master replication](https://docs.couchdb.org/en/stable/replication/intro.html#master-master-replication).&#x20;

This enables the iCure to implement a fully [Hybrid Cloud Storage](../icure-data-stack/hybrid-cloud-storage). Files are not locked during update and they can be updated both online and offline, allowing the iCure to implement a conflict resolution method.&#x20;

![Locking vs "No-Locking" database files during transactions. Source: CouchDb.org](https://1169539330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-ME3IzcBZh-0wwmYnl0o%2F-MfI7v_GeIELrU9PazT2%2F-MfIDypf-s_yFbgpT0sI%2Fimage.png?alt=media\&token=58c872b9-0ba1-4f73-aab4-23d509187598)

### Data Synchronization in iCure&#x20;

Under normal conditions database synchronization between connected databases take place in **less that 300ms.**&#x20;

### Data integrity

Other features include document-level [ACID](https://en.wikipedia.org/wiki/ACID) semantics with [eventual consistency](https://en.wikipedia.org/wiki/Eventual_consistency), (incremental) MapReduce, and (incremental) replication. \


![CouchDB vs the CAP Theorem. Source: CouchDb.org](https://1169539330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-ME3IzcBZh-0wwmYnl0o%2F-MfIEHIutu4LFpAIuS8n%2F-MfIEtCO0kZtlCn6df4D%2Fimage.png?alt=media\&token=1e46bd25-cc73-4ff6-a76a-fa75498aacd9)

CAP Theorem dictates that Databases can be optimized for two of the three most requested qualities: Consistency, Availability and Partition Tolerance. While CouchDB has been optimized for Partition Tolerance and Availability, it has the ability to provide local consistency at local databases and through incremental replication eventual consistency in the iCure database.&#x20;

![Incremental Replication leads to consistency between Db Nodes Source: CouchDb.org](https://1169539330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-ME3IzcBZh-0wwmYnl0o%2F-MfIEHIutu4LFpAIuS8n%2F-MfIFfA1AFc8y7Fm9IR6%2Fimage.png?alt=media\&token=7694737f-df6c-4e2e-896d-176eae1a4f66)

The [iCure Data Model](../icure-data-stack/icure-data-model) has been designed so that it is agnostic to higher level Medical Data conceptual Data Models such as Open EHR and FHIR.  It does however make some basic assumptions over the nature of the data, that are to be utilized for. That is in line with the selection of CouchDb. In a healthcare setup one might expect that there are several encryption perimeters and distributed systems, that are to some extend experiencing aberrant disconnections.
