Transactions
You must be aware that Kii Cloud data management does not support the transaction processing.
The transaction processing is the feature for ensuring the data consistency and mainly adopted by relational databases (RDB). Kii Cloud, like other NoSQL databases, does not support this so as to achieve high scalability and availability.
Since the current Kii Cloud does not support the transaction processing, we do not recommend building applications that require strict data consistency with Kii Cloud (e.g. online banking application).
Transaction processing in a nutshell
Transaction processing is the feature for treating a set of processes as individual and indivisible operation. Here, atomically executing the processes is important for ensuring the data consistency; at the end of the execution, either all processes must be succeeded or failed.
Here is an example. In this example, we are handling an online bank transfer between two bank accounts. This action requires subtracting the balance on the account A and adding the balance on the account B. These two actions must be atomic; either both actions must succeed or fail. If only one action is succeeded, we will have the balance mismatch.
When you are using RDB, you could use the transaction processing. All operations performed inside a transaction can be either committed or rolled back at the end. If the transaction is committed, all operations are fixed. If the transaction is rolled back, all operations are canceled.
Kii Cloud fixes the update per request. If we implement the bank transfer example shown above with Kii Cloud, we have a risk of losing the data consistency if the overall processes are interrupted for some reasons (e.g. network error and application down). You will need to design your application so as to deal with these situations.
Tips for ensuring data consistency
There are some techniques you can use to ensure the data consistency with Kii Cloud. While all of these techniques are not perfect (i.e. not suitable for supporting strict data consistency), you will be able to maintain the consistency in most use cases.
Using the server extension
Kii Cloud allows you to deploy your custom JavaScript code (server code) in Kii Cloud. These codes will be executed on the server.
Since the main causes of losing the data consistency are network error and application down, you can alleviate the situation by implementing the critical operations in server code.
The following figure illustrates how you can use the Server Extension feature to improve the data consistency. In this flow, single function call initiates the execution of the server code. Once the server code is executed, all operations will be executed on the server side and they will not be affected by the client.
This method will solve the data consistency issue in most cases. Some possible cases in which the method fails are as follows:
Server code execution took too much time and it exceeded the time limit.
Server code execution was unexpectedly terminated for some reasons (e.g. server maintenance).
An unexpected error occurred on the server.
Updating in bigger data chunk
One KiiObject update is atomically processed in Kii Cloud. If your update can be made in one KiiObject update, we recommend you to make all necessary update in one request so as to ensure the data consistency.
When using RDB, the best practice was to normalize the items and try to hold only one relationship per record. This practice, however, will increase the number of required updates and thus not suitable when using Kii Cloud. When you are developing with Kii Cloud, the best practice is to reduce the number of updates by properly designing what to be stored in objects.
For example, let us assume that we are putting a game player's information in a KiiObject. In this game, one user (KiiUser) will have three characters (HERO, WARRIOR, and MAGE). As shown below, there are two possible data designs. To ensure the data consistency in Kii Cloud, the Design #1 is better.
Design #1: Stores three characters in one KiiObject
Design #2: Stores one character in one KiiObject
Implementing auto recovery
If applicable, you can also try to detect the data inconsistency and recover it automatically.
For example, suppose that your application is trying to create a bi-directional link between two KiiObjects. In this case, you can implement your application so as to check the link and recover it when only a uni-directional link exists.
Auto-recovering the data inconsistency instantly, however, is hard because the situation can occur anytime. Rather than trying to detect the inconsistency in real-time, you might want to check for the inconsistency at regular time intervals.