Take the consistency of redis and database cache as an example
Redis cache and database consistency
Briefly introduce the solution
1. Real-time synchronization
If the requirement for strong consistency is relatively high, a real-time synchronization scheme should be adopted, that is, the query cache query cannot be retrieved from the DB and saved to the cache; when updating the cache, first update the database and then expire the cache settings (it is recommended not to update the cache content , Directly set cache expiration).
@Cacheable: used when querying, note that Long type needs to be converted to Sting type, otherwise an exception will be thrown
@CachePut: used when updating, using this annotation, data will be queried from the DB
@CacheEvict: used when deleted;
@Caching: combined usage
Two, asynchronous queue
For those with a high degree of concurrency, asynchronous queues can be used for synchronization, and message middleware such as kafka can be used to handle message production and consumption.
Third, use Ali's synchronization tool canal
The canal implementation method is to simulate the synchronization mechanism of mysql slave and master, monitor the DB bitlog log update to trigger the update of the cache, this method can liberate the hands of the programmer and reduce the workload, but there are some limitations when using it.
One lesson deciphers Alibaba Canal
- The master will record the changes to the binary log (binary log) (these records are called binary log events, binary log events, which can be viewed through show binlog events);
- The slave copies the master's binary log events to its relay log;
- The slave redoes the events in the relay log, which will change to reflect its own data.
- canal simulates the interactive protocol of mysql slave, pretends to be mysql slave, and sends a dump protocol to mysql master
- The mysql master receives the dump request and starts to push the binary log to the slave (that is, canal)
- Canal parses binary log object (originally byte stream)
Fourth, the use of UDF custom function
Faced with the API of mysql for programming and the use of triggers for cache synchronization, UDF is mainly implemented in c / c ++ language, and the learning cost is high.
The UDF custom function is the trigger of the database. It is a special stored procedure related to table events. When the database table has an event, it will trigger the operation of certain functions.