Database Concurrency Handling
This section explains the Database Concurrency Handling functionality, which helps you to maintain data consistency in the database and update the data properly across all platforms and engines. This capability is useful when two threads try to access a same record from the database, framework throws a concurrent modification exception in both api and ingester components, whereas in api will abort the transaction and throw the response back to the end-user, while in ingester it will re-try for three times and add the message to the error-topic.
This feature describes handling the concurrency control problem in database, basically concurrency problem occurs when two or more threads access a same record, the database throws concurrency exception, So the application must be intelligent enough to catch the exception and process it, We have two components in the microservice basically
- API - When concurrency problem occurs, the exception goes back to the user with response stating the concurrency problem has occurred.
- Ingester - When concurrency problem occurs, the ingester catches the exception and retry the record for three times to try for successful ingestion, even if after three retries, data is not ingested, you should add that particular event to the error-stream, such that there is no message loss.
Enable Concurrency Control in Microservices
To enable this feature in your microservices you have to do the following:
- For MongoDb, you don't need to do any change in your entity files, as mongo db handles versioning internally
- For Dynamodb, by default hereafter a version attribute is added in your entity while generation, which automatically gets incremented and that will be taken care by dynamodb itself
- For Sql db, as sql entities files are not generated and it is managed by respective microservices, they need to add @version attribute in your entity files, so that JPA takes care of the concurrency problem.
Sample API Response
[
{
"message": "Concurrent Modification Exception has occured, Please retry",
"code": "MSF-003"
}
]
In this topic