mchev
mchev

Reputation: 735

Sync multiple local databases to one remotely

I need to create a system with local webservers on Raspberry Pi 4 running laravel for API calls, websockets, etc. Each RPI will be installed in multiple customers places.

For this project i want to have the abality to save/sync the database to a remote server (when the local system is connected to internet).

Multiple local databases => One remote database cutomers based

The question is, how to synchronize databases and identify properly each customers data and render them in a mutualised remote dashboard.

My first thought was to set a customer_id or a team_id on each tables but it seems dirty.

The other way is to create multiple databases on the remote server for the synchronization and one extra database to set customers ids and database connection informations...

Someone has already experimented something like that? Is there a sure and clean way to do this?

Upvotes: 0

Views: 1487

Answers (1)

Rob Lambden
Rob Lambden

Reputation: 2293

You refer to locale but I am assuming you mean local.

From what you have said you have two options at the central site. The central database can either store information from the remote databases into a single table with an additional column that indicates which remote site it's from, or you can setup a separate table (or database) for each remote site.

How do you want to use the data?

If you only ever want to work with the data from one remote site at a time it doesn't really matter - in both scenarios you need to identify what data you want to work with and build your SQL statement to either filter by the appropriate column, or you need to direct it to the appropriate table(s).

If you want to work on data from multiple remote sites at the same time, then using different tables requires tyhat you use UNION queries to extract the data and this is unlikely to scale well. In that case you would be better off using a column to mark each record with the remote site it references.

I recommend that you consider using Uuids as primary keys - it may be that key collision will not be an issue in your scenario but if it becomes one trying to alter the design retrospectively is likely to be quite a bit of work.

You also asked about how to synchronize the databases. That will depend on what type of connection you have between the sites and the capabilities of your software, but typically you would have the local system periodically talk to a webservice at the central site. Assuming you are collecting sensor data or some such the dialogue would be something like:

Client - Hello Server, my last sensor reading is timestamped xxxx Server - Hello Client, [ send me sensor readings from yyyy | I don't need any data ]

You can include things like a signature check (for example an MD5 sum of the records within a time period) if you want to but that may be overkill.

Upvotes: 2

Related Questions