I am developing a simple Spring Boot microservice with Cassandra db.
The main entity in my server in Subscription class below:
public final class Subscription {
private String id;
private String firstUser;
private String secondUser;
private String firstUserStatus;
private String secondUserStatus;
}
My repository consumes Subscription object and persists it.
But before persisting, I should check for some constraints and the main is that there mustn't be another subscription with the same firstUser and secondUser.
The case when it can fail is pretty simple:
- one subscription is going to be persisted
- table is being verified for the existence of another subscription with the same users
- another subscription with the same users could be persisted now and make the previous verification stale
- subscription is being persisted.
What is the proper way to resolve it?
Worth mentioning: the order of users doesn't matter. If subscription with users "1" and "2" is going to be persisted no other subscription with users "1" and "2" or "2" and "1" must exist.
UPD: considering @bhspencer answer, I could propose another case (I need also force statuses to be consistent):
- one subscription is going to be persisted with
firstUserStatus="ISSUED"andsecondUserStatus="WAITING_ACTION" - table is being verified for the existence of another subscription with the same users
- another subscription with the same id (users) and
firstUserStatus="WAITING_ACTION"andsecondUserStatus="ISSUED"could be persisted now and make the previous verification stale - initial subscription is being persisted and overrides the already existed one ->
secondUserStatus="ISSUED"is lost