In the same method, how can redis, database and api, ensure the transactionality or ultimate consistency of the method?

there are three actions in a Service method

doSomething {

Database write operations

redis write

call an api

}

how do I achieve the transactionality or ultimate consistency of this method?

if there are only redis and database operations

@ Transactional
doSomething {


redis    

}
redis throws an exception that causes database rollback to implement transactional
is there a problem?


in this case, you can refer to the ideas of TCC (Try, Confirm, Cancel).

it is impossible for multiple data stores to control through a single transaction. To control it manually


you define what consistency is first. If that API has huge side effects, such as launching a nuclear bomb, success or failure is unlikely to roll back


implementing transaction solutions across mysql and redis must be very complex. final consistency scheme is recommended.

final consistency can be achieved by message queue decoupling. Basic idea:

@Transactional
doSomething{
    
      
}

then start another service to receive messages

{
    redis    
}

points to pay attention to:

  1. Database write operations and send messages are transactional, and kafka has the impression that there are transactional messages.
  2. sequence of messages: make database modifications to the same data to ensure that the order of the redis write operation service is also the original modification order.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e97d16-1684.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e97d16-1684.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?