Start data synchronization

However, for all social apps or apps with some social attributes, an inevitable routine work is “maintenance of friend data”. Common friend data includes but is not limited to:

  • Friend Profile update
  • New Friend Application
  • Friend changes (buddy/block/delete, etc.)
  • .

When friend data changes such as these occur, it is often required that the changes be synchronized to the client in a timely manner. Otherwise, once the information of multiple users on the same friend chain is inconsistent, there may be a lot of puzzling phenomena.

When online, users can directly send control messages (such as friend information update notification or friend relationship change notification) over the long-link channel to actively inform clients of data update operations. In this way, fine-grained updates can be made in a more timely manner and the amount of data required for the next data synchronization can be reduced.

When the user is offline, the user needs to actively request the friend synchronization interface after the next startup of the App to synchronize the changes of the friend data during the offline period.

Full update VS incremental update

One of the rougher approaches to data synchronization is “full update,” which pulls the full list of friends each time, empties the local cache of friends data, and reinserts it.

This can be done, but the consequence is that unless there is a product-level limit on the number of friends, a large number of friends can result in a large amount of data per request, which not only slows requests down, but also increases the chance that requests will fail. Although it can be optimized with paging pull, the overall experience is still not very good.

Another sensible approach is “incremental updates”, where you update only the part of your friend’s data that should have changed, returning as much as the interface changes, and not returning as much as it hasn’t changed. Compared with full update, it can significantly reduce the amount of requested data and improve the speed of request response.

Implementation of incremental updates

To implement incremental updates to data synchronization, simply add a “version” parameter to the “Synchronize friends Interface” request.

This parameter indicates the version number of the friend data maintained by the server. The client needs to carry this parameter each time it requests the interface. The default value can be 0, indicating that it is the first request and full update is performed.

When the friend data of a specified user changes, the version number maintained by the server is also updated.

Each time, the server compares the data difference between the two versions and returns the incremental friend data as well as the latest version number. When the client obtains the friend data and updates it, it needs to save the version number locally and transfer the version number as it is the next time it synchronizes the friend.

Synchronize friends interface

/v1/friends/sync/{version}

Request parameters:

The parameter name Parameters that Whether must The data type
version Version number of the friend data saved locally on the client true number

Response parameters:

The parameter name Parameters that The data type
version The version of the friend data maintained by the server needs to be saved locally on the client and then transmitted as is the next time the friend is synchronized number
friends If the local version number of the client is the same as that of the server, this list has no data. Null indicates that the friend list of the client is the latest. An empty array if there are no friends array