The system is divided into three main layers, each responsible for a specific aspect of the synchronization process:
- Client-Side Database Layer: Manages local storage using IndexedDB, facilitating offline data storage and operations.
- Synchronization Layer: Acts as the mediator between the client and server databases, managing data synchronization, conflict resolution, and change tracking.
- Server-Side Logic: Handles interactions with the MongoDB database, processes synchronization requests, and maintains a global state of data across all clients.
LoggerService- Handles logging across the application.DatabaseService- Manages database connections and operations.DataPreparationService- Prepares and validates data before synchronization.DataSyncService- Responsible for the synchronization logic, including conflict resolution.ConflictResolver- Defines strategies for resolving data conflicts during synchronization.SyncManager- Coordinates the entire synchronization process, utilizing all other services.IndexedDBUtil- Manages local IndexedDB operations (if this is relevant to the overall architecture).
SyncManagerorchestrates the synchronization process, relying onDatabaseServicefor database interactions,DataPreparationServicefor preparing data, andDataSyncServicefor handling the synchronization logic.DataSyncServiceusesConflictResolverto handle conflicts andLoggerServiceto log activities.DataPreparationServicealso utilizesLoggerServiceto log its activities.DatabaseServiceis used directly bySyncManagerfor database operations.LoggerServiceis used across various services for logging different events and errors.
Here's a textual layout of the architecture:
+-----------------+
| SyncManager |
+-----------------+
| |
+----------+ +-----------+
| |
+-------v--------+ +------v------+
| DatabaseService| | DataPrepService|
+----------------+ +--------------+
|
|
+----------------+ +------v------+
| IndexedDBUtil | | DataSyncService|
+----------------+ +--------------+
|
+-------v-------+
| ConflictResolver |
+-----------------+
+-----------------+
| LoggerService |
+-----------------+
+------------------------------------+
| <<service>> |
| LoggerService |
|------------------------------------|
| -logger: winston.Logger |
|------------------------------------|
| +log(message: string, level: string)|
+---------------^--------------------+
| uses
+------------------------------------+ +------------------------------------+
| <<service>> | | <<service>> |
| SyncManager | | DatabaseService |
|------------------------------------| |------------------------------------|
| -databaseService: DatabaseService |------->| +connect(): Promise<void> |
| -dataPreparationService: | | +disconnect(): Promise<void> |
| DataPreparationService | +------------------------------------+
| -dataSyncService: DataSyncService | | <<service>> |
| -loggerService: LoggerService | | DataPreparationService |
|------------------------------------| |------------------------------------|
| +performSync(): Promise<void> | | +prepareData(): { data: string } |
+-----------------^------------------+ +---------------^--------------------+
| injects uses |
+------------------------------------+ +------------------------------------+
| <<service>> | | <<service>> |
| DataSyncService | | DataSyncService |
|------------------------------------| |------------------------------------|
| -logger: LoggerService |<-------| -logger: LoggerService |
|------------------------------------| |------------------------------------|
| +sync(data: any): void | | +sync(data: any): void |
+------------------------------------+ +------------------------------------+
Step-by-Step Process:
-
Start Synchronization:
- The user or system triggers the synchronization process in the
SyncManager.
- The user or system triggers the synchronization process in the
-
Connect to Database:
- The
SyncManagersends a message toDatabaseServiceto establish a database connection. DatabaseServiceacknowledges the connection.
- The
-
Fetch Data:
SyncManagerrequests raw data fromDatabaseService.DatabaseServiceretrieves and returns the data.
-
Prepare Data:
SyncManagersends the raw data toDataPreparationServicefor processing.DataPreparationServiceprocesses the data and returns the prepared data.
-
Synchronize Data:
SyncManagersends the prepared data toDataSyncService.DataSyncServiceinitiates the synchronization.- If conflicts are detected,
DataSyncServicerequests resolution fromConflictResolver. ConflictResolverresolves the conflicts and returns the resolved data.DataSyncServicecompletes the synchronization by sending the resolved data to the server or database.
-
Complete Synchronization:
SyncManagerlogs the completion and possibly returns a status to the user or system.
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
| User/System | | SyncManager | | DatabaseService | | DataPreparationService| | DataSyncService |
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
| | | | |
|---------------------->| | | |
| Start Sync | | | |
|---------------------->| | | |
| Connect Database |------------------------>| | |
| | Connect | | |
| |<------------------------| | |
| | | | |
| | Fetch xData |-----------------------------| |
| | | Return Data | |
| |<------------------------| | |
| | | | |
| | Prepare Data |-----------------------------| |
| | | Return Prepared Data | |
| |<------------------------------------------------------| |
| | | | Synchronize Data |
| |------------------------------------------------------>| |
| | | | Resolve Conflicts |
| | | |---------------------------->
| | | | Return Resolved Data |
| | | |<----------------------------|
| | | | |
| | Complete Sync |-----------------------------| |
| |<------------------------------------------------------| |
+------------------+ +------------------+ +---------------------+ +----------------------+ +------------------+
+--------------+ +--------------+ +-----------------------+ +----------------+ +---------------+
| SyncManager | | DatabaseSvc | | DataPreparationSvc | | DataSyncSvc | | LoggerService |
+------+-------+ +------+-------+ +----------+-----------+ +--------+-------+ +-------+-------+
| | | | |
| [1] connect() | | | |
|-------------------->| | | |
| | | | |
| | [2] <Promise resolved> | | |
|<--------------------| | | |
| | | | |
| [3] prepareData() | | | |
|----------------------------------------------->| | |
| | | [4] <Data preparation> | |
| | |----------------------------| |
| | | | |
| | |<---------------------------| |
| | | | |
| [5] sync(data) | | | |
|----------------------------------------------------------+ | |
| | | | [6] <Sync start> | |
| | | |----------------->| |
| | | | | [7] log(info) |
| | | | |--------------------->|
| | | | | |
| | | | | [8] <Log recorded> |
| | | | |<---------------------|
| | | | | |
| | | | [9] <Sync done> | |
| | |<-------|-----------------| |
| | | | |
| | | [10] Return sync result | |
|<----------------------------------------------------------+ | |
| | | | |
| [11] log(error) if any | | |
|---------------------+------------------------->| | |
| | | | |
| [12] <Error logged> | | | |
|<--------------------| | | |
| | | | |
+------+-------+ +------+-------+ +----------+-----------+ +--------+-------+ +-------+-------+
| SyncManager | | DatabaseSvc | | DataPreparationSvc | | DataSyncSvc | | LoggerService |
+--------------+ +--------------+ +-----------------------+ +----------------+ +---------------+