import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; private Cache<String, User> userCache; userCache = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(60, TimeUnit.MINUTES).build();.
SQL Server is used for this a lot.
Because to fetch the user details for a userId there will be network call to a different machine.
Whereas in the current code there will be no network call. At most what can happen is request reaches M1 and there is computation and chaching.. next time it reaches M2 it computes and caches it.. And now both machines have it..
And so further calls will be very fast. compare to making a network call each time from M1 and M2 to the shared volume..
Althought theoritically the original code posted have a flaw that each machine caches its own data and load balancer could send it to any machine.. But i think the current approach will be faster..
Please comment i may be missing something here
This is why it's likely best to use an existing solution, like MariaDB, because this code is already optimized to run with multiple clients or to run in multi master (many database instances) runtime mode.
Trying to duplicate the performance of code with decades of development by 1000s of developers (as the code is open source)... highly unlikely you as a single human will be able to code in the same level of performance.
1000s of people are usually smarter than 1 person.