There are many existing libraries and components. You can google about cache coherence.
There are various ready-to-use solutions, Jboss cache, Oracle consistency, GridGain, etc.
If you want to write handwritten code, there are several ways you can do this. Each node in your cluster should know about other nodes (in the simplest case). Data can be replicated in different ways.
Synchronous Replication Recording is performed in the local cache only when records are sent to other nodes.
Asynchronous replication. Records are found in the local node and are later replicated to other nodes.
Invalid cache. When the data changes, all nodes receive a signal, cancel their data and re-read it from the database.
This can be difficult to implement, you need a special protocol (you can use JGroups
) that your nodes will use to exchange data. I suggest you find a ready-made solution
Anton source share