The company that I work for has a web system, some modules will be used in the works, which do not always have connection to the internet. What the best solution to deploy web modules of the system in construction? The system cant read/write in the database.
-
similar to [this](http://stackoverflow.com/questions/271610/strategy-for-offline-online-data-synchronization?rq=1) – Dan May 21 '13 at 07:02
-
Don't forget to accept an answer if you found one of them useful! – Dan Jun 20 '13 at 07:36
2 Answers
You can have a local database that runs on your webserver. Every 15 min you check if there is a connection and transfer the changes from your local database onto your "real" database.

- 5,689
- 1
- 24
- 43
-
I thought this solution, but since the web application can change data in the database, as well as in construction. And there are several constructions with the system accessing the same database at the same time. There may be inconsistency in transactions this 15 minute interval. – Dr. jrtNeto Apr 25 '13 at 19:00
There is no perfect solution to this problem due to the CAP theorem. If there was, distributed systems would be a whole lot easier, though :)
The way I see it, you have a couple of options. All options require a copy of the data client-side so that the client can at least read the data while it's disconnected.
Don't allow inconsistent writes, just inconsistent reads.
Either the client or the server would take write-ownership of the data whenever the client is off the network. Whichever side owns the data is allowed to write, and the other side must read a potentially-stale local copy of the data (or it could generate errors if that's preferable, or at least tell the user that the data is stale). This is more difficult if you have multiple clients running at the same time.
A similar approach is to run all server-side write operations when you know no clients will be at work (i.e. run all your jobs at midnight). It's simple, but it works for many applications.
Allow inconsistencies and then deal with them later. When you have to be able to write to both sides of the disconnected network concurrently, this is the only way to go. There are a couple of ways to mitigate the inconsistency issues, however, depending on your design:
If you can make all transformations commutative (you can change the order of the transformations and the outcome will be the same), you could store the transformations performed on the client and the server and then apply any cached ones from the client when the it reconnects to the server. This makes dealing with inconsistencies very simple. This is what ATMs do when they are disconnected from the banking network - their commutative transformations are similar to, "Deduct $50 from acct #12345." Note that this can still cause invalid state - the user could deduct more than they had in their account by visiting multiple ATMs which are disconnected from the banking network. However, the bank makes up for this by charging overdraft fees when the ATMs reconnect to the network, and so they don't typically lose any money as a result.
If conflicts are rare, you could just tell a user of the client, "Hey, you wrote this while you were offline, but the value changed on the server - which copy should I keep?" This has the issue that you might have to ask it multiple times, since the user has to perform an atomic CAS operation on the value being changed and it's possible that multiple clients might be reconnecting at the same time. This approach can also suffer from the ABA problem if you're not careful (but that depends what you're doing).

- 7,155
- 2
- 29
- 54