153c3577eSopenharmony_ci# distributeddatamgr\_distributeddatamgr<a name="EN-US_TOPIC_0000001124232845"></a>
253c3577eSopenharmony_ci
353c3577eSopenharmony_ci-   [Introduction](#section11660541593)
453c3577eSopenharmony_ci-   [Directory Structure](#section161941989596)
553c3577eSopenharmony_ci-   [Constraints](#section119744591305)
653c3577eSopenharmony_ci-   [Description](#section1312121216216)
753c3577eSopenharmony_ci-   [Repositories Involved](#section1371113476307)
853c3577eSopenharmony_ci
953c3577eSopenharmony_ci## Introduction<a name="section11660541593"></a>
1053c3577eSopenharmony_ci
1153c3577eSopenharmony_ciDistributed Data Service \(DDS\) provides the capability to store data in the databases of different devices. DDS isolates data based on a triplet of the account, app, and database. DDS synchronizes data between trusted devices to provide users with consistent data access experience on different devices.
1253c3577eSopenharmony_ci
1353c3577eSopenharmony_ciDDS manages data of OpenHarmony in a distributed manner. It consists of the following parts:
1453c3577eSopenharmony_ci
1553c3577eSopenharmony_ci-   **Service interfaces**
1653c3577eSopenharmony_ci
1753c3577eSopenharmony_ci    DDS provides APIs for other modules to create databases, access data, and subscribe to data. Supporting the KV data model and common data types, the APIs are highly compatible and easy to use, and can be released.
1853c3577eSopenharmony_ci
1953c3577eSopenharmony_ci-   **Service component**
2053c3577eSopenharmony_ci
2153c3577eSopenharmony_ci    The service component manages metadata, permissions, backup and restoration, and multiple users. It also initializes the storage and synchronization components and communication adaptation layer of the distributed database.
2253c3577eSopenharmony_ci
2353c3577eSopenharmony_ci-   **Storage component**
2453c3577eSopenharmony_ci
2553c3577eSopenharmony_ci    The storage component provides data access, data reduction, transactions, snapshots, data combination, and conflict resolution.
2653c3577eSopenharmony_ci
2753c3577eSopenharmony_ci-   **Synchronization component**
2853c3577eSopenharmony_ci
2953c3577eSopenharmony_ci    The synchronization component connects the storage and communication components. It maintains data consistency between online devices by synchronizing data generated on the local device to other devices and merging data received from other devices into the local device.
3053c3577eSopenharmony_ci
3153c3577eSopenharmony_ci-   **Communication adaptation layer**
3253c3577eSopenharmony_ci
3353c3577eSopenharmony_ci    The communication adaptation layer invokes interfaces of the public communication layer to create and connect to communication channels, receive device online and offline messages, maintain metadata of the connected and disconnected devices, send device online and offline messages to the synchronization component, synchronize the list of devices connected to and maintained by components, and invoke interfaces to encapsulate and send data to the connected devices.
3453c3577eSopenharmony_ci
3553c3577eSopenharmony_ci
3653c3577eSopenharmony_ciYou call APIs of DDS to create, access, and subscribe to distributed databases. The service interfaces store data to the storage component based on the capabilities provided by the service component. The storage component invokes the synchronization component to synchronize data. The synchronization component uses the communication adaptation layer to synchronize data to remote devices, which update the data in the storage component.
3753c3577eSopenharmony_ci
3853c3577eSopenharmony_ci**Figure  1**  How DDS works<a name="fig371116145419"></a>  
3953c3577eSopenharmony_ci
4053c3577eSopenharmony_ci
4153c3577eSopenharmony_ci![](figures/distributed-datamgr-subsystem-architecture.png)
4253c3577eSopenharmony_ci
4353c3577eSopenharmony_ci## Directory Structure<a name="section161941989596"></a>
4453c3577eSopenharmony_ci
4553c3577eSopenharmony_ci```
4653c3577eSopenharmony_ci/foundation/distributeddatamgr/datamgr_service
4753c3577eSopenharmony_ci├── interfaces                    # APIs
4853c3577eSopenharmony_ci│   └── innerkits                 # Native APIs
4953c3577eSopenharmony_ci│   └── jskits                    # JavaScript APIs
5053c3577eSopenharmony_ci├── services                      # Service code
5153c3577eSopenharmony_ci│   └── distributeddataservice    # DDS implementation
5253c3577eSopenharmony_ci└── test                          # Test case resources
5353c3577eSopenharmony_ci```
5453c3577eSopenharmony_ci
5553c3577eSopenharmony_ci## Constraints<a name="section119744591305"></a>
5653c3577eSopenharmony_ci
5753c3577eSopenharmony_ci-   To use all functions of DDS, you need to obtain the  **ohos.permission.DISTRIBUTED\_DATASYNC**  permission.
5853c3577eSopenharmony_ci-   DDS supports the KV data model, but not foreign keys or triggers of the relational database.
5953c3577eSopenharmony_ci-   DDS supports the following KV data model specifications:
6053c3577eSopenharmony_ci    -   For the device KV store, the maximum size of a key is 896 bytes, and that of a value is 4 MB.
6153c3577eSopenharmony_ci    -   For the single KV store, the maximum size of a key is 1 KB, and that of a value is 4 MB. 
6253c3577eSopenharmony_ci    -   Each app can open a maximum of 16 databases simultaneously.
6353c3577eSopenharmony_ci
6453c3577eSopenharmony_ci-   DDS cannot completely replace the database in the service sandbox for storing data, because the storage types supported by them are not completely the same. You need to determine the data to be synchronized in distributed mode and store the data in DDS.
6553c3577eSopenharmony_ci-   Currently, DDS does not allow customization of conflict resolution policies.
6653c3577eSopenharmony_ci-   DDS supports a maximum of 1000  **KvStore**  API calls per second, and 10,000 per minute. It supports a maximum of 50  **KvManager**  API calls per second, and 500 per minute.
6753c3577eSopenharmony_ci
6853c3577eSopenharmony_ci## Description<a name="section1312121216216"></a>
6953c3577eSopenharmony_ci
7053c3577eSopenharmony_ciSome basic concepts related to the DDS are as follows:
7153c3577eSopenharmony_ci
7253c3577eSopenharmony_ci-   **KV data model**
7353c3577eSopenharmony_ci
7453c3577eSopenharmony_ci    KV is short for key-value. The KV database is a type of NoSQL database. Data in this type of database is organized, indexed, and stored in the form of key-value pairs.
7553c3577eSopenharmony_ci
7653c3577eSopenharmony_ci    The KV data model is suitable for storing service data that does not involve too many data or service relationships. It provides better read and write performance than the SQL database. The KV data model is widely used in distributed scenarios because it handles conflict more easily in database version compatibility and data synchronization. The distributed database is based on the KV data model and provides KV-based access interfaces.
7753c3577eSopenharmony_ci
7853c3577eSopenharmony_ci-   **Distributed database transactions**
7953c3577eSopenharmony_ci
8053c3577eSopenharmony_ci    Distributed database transactions include local transactions \(same as the transactions of traditional databases\) and synchronization transactions. Synchronization transactions refer to data synchronization between devices in the unit of local transaction. Synchronization of a local transaction modification either succeeds or fails on multiple devices.
8153c3577eSopenharmony_ci
8253c3577eSopenharmony_ci-   **Distributed database consistency**
8353c3577eSopenharmony_ci
8453c3577eSopenharmony_ci    In a distributed scenario where multiple devices are used in the same network, distributed database consistency means that data is consistent on these devices. This consistency can be classified into strong, weak, and eventual consistency.
8553c3577eSopenharmony_ci
8653c3577eSopenharmony_ci    -   **Strong consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network will obtain the updated data.
8753c3577eSopenharmony_ci    -   **Weak consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may or may not obtain the updated data. The time when all devices have the same data is uncertain.
8853c3577eSopenharmony_ci    -   **Eventual consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may not obtain the updated data immediately. However, data on all the devices will become consistent after some time.
8953c3577eSopenharmony_ci
9053c3577eSopenharmony_ci    Strong consistency has high requirements on distributed data management and may be used in distributed server scenarios. DDS supports only ultimate consistency because mobile devices are not always online and there is no center.
9153c3577eSopenharmony_ci
9253c3577eSopenharmony_ci-   **Distributed database synchronization**
9353c3577eSopenharmony_ci
9453c3577eSopenharmony_ci    After discovering and authenticating a device, the bottom-layer communication component notifies the DDS that the device goes online. After receiving the notification, DDS establishes an encrypted transmission channel to synchronize data between the two devices.
9553c3577eSopenharmony_ci
9653c3577eSopenharmony_ci    DDS provides both manual and automatic synchronization. In manual synchronization, you can specify the list of target devices and the synchronization mode \(PULL, PUSH, or PULL\_PUSH\). In automatic synchronization, the distributed database synchronizes data \(when devices go online or data is modified\), and you are unaware of the synchronization.
9753c3577eSopenharmony_ci
9853c3577eSopenharmony_ci-   **Single KV store**
9953c3577eSopenharmony_ci
10053c3577eSopenharmony_ci    Data is saved locally in the unit of a single KV entry. Only one entry is saved for each key. When users modify data locally, the corresponding KV entry is modified, regardless of whether the data has been synchronized. As for synchronization, the latest modification is synchronized to remote devices.
10153c3577eSopenharmony_ci
10253c3577eSopenharmony_ci-   **Device KV store**
10353c3577eSopenharmony_ci
10453c3577eSopenharmony_ci    The device KV store is based on the single KV store and adds device IDs before the keys of key-value pairs. This ensures that the data generated by each device is isolated from each other and is centrally managed by the system. With the device KV store, you can query data of a specified device, but cannot modify the data synchronized from remote devices.
10553c3577eSopenharmony_ci
10653c3577eSopenharmony_ci-   **Conflict resolution policy for the distributed database**
10753c3577eSopenharmony_ci
10853c3577eSopenharmony_ci    A data conflict occurs when multiple devices modify the same data and commit the modification to the database. In this case, the default conflict resolution policy is used, that is, the data modification committed later takes effect. Currently, the conflict resolution policy cannot be customized.
10953c3577eSopenharmony_ci
11053c3577eSopenharmony_ci-   **Schema-based database management and data query based on predicates**
11153c3577eSopenharmony_ci
11253c3577eSopenharmony_ci    When creating or opening a single KV store, you can specify a schema. The database detects the value format of key-value pairs based on the schema and checks the value structure. In addition, the database provides index creation and predicate-based query functions based on the fields in the values.
11353c3577eSopenharmony_ci
11453c3577eSopenharmony_ci-   **Distributed database backup capability**
11553c3577eSopenharmony_ci
11653c3577eSopenharmony_ci    DDS provides the database backup capability. By setting  **backup**  to  **true**, you can trigger daily database backup. If a distributed database is damaged, DDS deletes the database and restores the most recent data from the backup database. If no backup database is available, DDS creates one. DDS can also back up encrypted databases.
11753c3577eSopenharmony_ci
11853c3577eSopenharmony_ci
11953c3577eSopenharmony_ci## Repositories Involved<a name="section1371113476307"></a>
12053c3577eSopenharmony_ci
12153c3577eSopenharmony_ciDistributed Data Management subsystem
12253c3577eSopenharmony_ci
12353c3577eSopenharmony_cidistributeddatamgr\_datamgr\_service
12453c3577eSopenharmony_ci
12553c3577eSopenharmony_cithird\_party\_sqlite
12653c3577eSopenharmony_ci
12753c3577eSopenharmony_ci
128