简体   繁体   中英

Load Pre-Load configuration using java

There are 20 production servers.Whenever the team make a config change and I requested to reload the configuration /restart the services to refresh the cache stored in hashmap..

When the actual transactions are hitting into the server, Will pick the configuration values from map to process the transactions instead of hitting DB every transaction.

I used the following code to connect each server. I am having a couple of questions and suggestions on this approach. 1) Is that logic is fine and stores large data in the memory will create any performance degradation? 2) is there any best approach could suggest on the logic?

        httpurlcon = (HttpURLConnection) url.openConnection();      
        httpurlcon.setDoOutput(true);      
        httpurlcon.setRequestMethod("POST");      
        httpurlcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");      
        httpurlcon.connect();

If you are implementing a cache on the server side using just a HashMap, it might be a better idea to look at using something like a LRU (Least Recently Used) cache, which is really easy to implement in Java using a LinkedHashMap.

Where the HashMap approach could potentially take up too much memory and give you a an out of memory approach, the least recently used cache will get rid of elements to stay at a certain size, prioritising recently used requests. Also, using just a HashMap, you'll want to synchronize all operations on data structure in order to ensure thread safety.

It would also be worth looking at tuning some parameters of the database itself. In MySQL for example, you can tune InnoDB's (the storage engine of MySQL) parameters such as the buffer pool size or the query cache. InnoDB's buffer pool is itself an LRU cache, and if the database is living on some server by itself, you can set the size of the buffer pool to be quite large and increase performance, since the data will be cached in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM