Sdílet prostřednictvím


Fundamentals of Data Synchronization

Microsoft Sync Framework synchronizes data stores, or replicas, by using three basic components: a synchronization session and two synchronization providers. To synchronize data, an application creates a synchronization session and passes it a source provider and a destination provider. The session uses the source provider to get new changes that have occurred on the source replica, and uses the destination provider to apply these changes to the destination replica.

Creating a provider requires the most development work. A provider maintains metadata and knowledge for the replica, and metadata for each item that is to be synchronized. A provider also transfers the actual item data to and from its data store. A provider uses Sync Framework components to help enumerate changes when the provider is acting as a source, and to detect conflicts and apply changes when acting as a destination.

Synchronization Algorithm

The algorithm for a one-way synchronization includes the following steps:

  1. The session gets the current knowledge of the destination replica and sends it to the source provider.

  2. The source provider enumerates changes that are not contained in the destination knowledge.

  3. The source provider sends changes to the session.

  4. The session detects conflicts and applies changes to the destination replica through the destination provider.

Two-way synchronization between two replicas is achieved by two one-way synchronizations.

Managed code The SyncOrchestrator object performs two one-way synchronizations when the Direction property is set to DownloadAndUpload or UploadAndDownload.

Unmanaged code The application must create an ISyncSession object for each one-way synchronization.

Metadata Management

Each replica must provide a set of metadata that describes itself and its knowledge to the synchronization community. (A synchronization community is a set of replicas that can synchronize with one another.) The required metadata for each replica is: the replica ID, the current tick count, the replica key map, the current knowledge, the forgotten knowledge, a conflict log, and a tombstone log. A replica must also maintain metadata for each item that is to be synchronized. The required metadata for each item is: the global ID, the current version, and the creation version. Typically, the synchronization provider manages the metadata that is required for synchronization. However, depending on the implementation of the replica, it may be more useful for some parts of the metadata management to be handled by a separate component, such as a service that cleans up tombstones at scheduled times instead of during synchronization.

For more information about metadata, see Metadata Requirements.

Synchronization Providers

The provider manages the metadata for a replica and works with Sync Framework to enumerate changes and detect conflicts. The provider also works with the item store of a replica to send item data when the provider is acting as the source provider, and to apply changes when the provider is acting as the destination provider.

Managed code A provider implements the abstract methods and properties from KnowledgeSyncProvider, IChangeDataRetriever, and INotifyingChangeApplierTarget.

Unmanaged code A provider implements the IKnowledgeSyncProvider, ISyncProvider, ISynchronousDataRetriever, and ISynchronousNotifyingChangeApplierTarget interfaces (or for asynchronous providers, the asynchronous versions).

For more information about synchronization providers and how to implement and use these interfaces, see Synchronization Providers.

Synchronization Applications

The application is the component that creates the synchronization session object, connects it with the providers, and hosts the synchronization runtime.

Implementing a Synchronization Application by Using Managed Code

To start synchronization, create a SyncOrchestrator object. Then, set the LocalProvider and RemoteProvider properties to the SyncProvider interfaces of the two providers that want to synchronize. The application obtains or creates the provider interfaces by using whatever method is appropriate. After the SyncOrchestrator object is created, call Synchronize to start synchronization. At this point, Sync Framework makes all the necessary calls into the two providers to perform synchronization.

Both the SyncOrchestrator object and the KnowledgeSyncProvider object raise events during synchronization. To receive an event, implement an event handler method and register to receive the event.

Implementing a Synchronization Application by Using Unmanaged Code

To start synchronization, create an an ISyncSession object by passing CLSID_SyncServices and IID_IApplicationSyncServices to the CoCreateInstance function, and then call IApplicationSyncServices::CreateSyncSession on the IApplicationSyncServices object that is returned.

The CreateSyncSession method requires the ISyncProvider interfaces of the two providers that want to synchronize. The application obtains or creates the provider interfaces by using whatever method is appropriate. After the ISyncSession object is created, call ISyncSession::Start to start synchronization. At this point, Sync Framework makes all the necessary calls into the two providers to perform synchronization.

The ISyncSession object raises events during synchronization. To receive events, implement the ISyncCallback interface and register it by using ISyncSession::RegisterCallback.

See Also

Reference

ISyncSession Interface
ISyncProvider Interface
IKnowledgeSyncProvider Interface
ISynchronousDataRetriever Interface
ISynchronousNotifyingChangeApplierTarget Interface
IApplicationSyncServices Interface
IProviderSyncServices Interface
ISyncCallback Interface
SyncOrchestrator
KnowledgeSyncProvider
IChangeDataRetriever
INotifyingChangeApplierTarget
SyncCallbacks

Concepts

Metadata Requirements
Synchronization Providers
Synchronization Applications

Other Resources

Microsoft Sync Framework