Distributed System JVM Local cache synchronization Implementation Dlcache

Source: Internet
Author: User

Out-of-the-box distributed k/v caches have many implementations, most notably redis,memcached. So why do we have to do it ourselves, after we have solved the high latency caused by the large number of RPC calls under the distributed system, we find that many services require a lot of access to the distributed cache, because the distributed cache is usually deployed on a separate server, in the LAN, usually a single network also needs 1ms, A small request may require one or two of cache access, complex services such as delegation, deposit, financing, etc. will be accessed ten or twenty times, even if the program has been optimized, but only the network latency of access to the distributed cache takes up a large proportion of the overall response time, and these require extensive access to the data is usually not large amount of data itself, Therefore, we optimize it as a local cache, while using the local cache needs to solve the synchronization problem between the application servers, and the problem of automatic node joining in the distributed system.

Ehcache is one of the most widely used local caches, although it supports clustering, but in a distributed environment is not flexible, using Ehcache as a distributed system cache under the relevant shortcomings can be referenced Http://blog.sina.com.cn/s/blog_ 6151984a0101816j.html, how to synchronize the new node after joining is also a problem, it is likely similar to the Galera SST mechanism.

Based on the above considerations, we have to implement a distributed system to solve the above problems in the JVM cache synchronization implementation.

User manual
0, the overall design and requirements can be seen in the "RABBITMQ-based distributed JVM Synchronization scheme." In the detailed description of pptx, see Http://pan.baidu.com/s/1bpmfHoF.
System dependent
1, install RABBITMQ 3.5.x.
2, based on JDK 1.7.
3, based on spring 3.2.x.

1. Introduction of JAR Package Com.medsoft.dlcache Http://pan.baidu.com/s/1c2sjruo
2. Ensure that the following three properties are injected through spring propertyplaceholderconfigurer
Localcache.isdist,true/false, default true, optional
Localcache.publishexchanges, the target Exchange list to which this node will publish messages, separated by commas
Localcache.subscribeexchanges, the target Exchange list to which this node will subscribe messages, comma-delimited
LOCALCACHE.HOST,RABBITMQ Host Address
LOCALCACHE.PORT,RABBITMQ Host Port
LOCALCACHE.USERNAME,RABBITMQ User Name
LOCALCACHE.PASSWORD,RABBITMQ Password

Spring needs to define a com.rabbitmq.client.ConnectionFactory implementation, as follows:
<bean id= "Rabbitconnectionfactory" class= "Com.rabbitmq.client.ConnectionFactory" >
<property name= "host" value= "${rabbitmq.host}" ></property>
<property name= "Port" value= "${rabbitmq.port}" ></property>
<property name= "username" value= "${rabbitmq.username}" ></property>
<property name= "Password" value= "${rabbitmq.password}" ></property>
</bean>

3, the cache name for the Pojo to be cached fully qualified name such as Com.hundsun.hitop.base.meta.pojo.param,pojo must implement the CacheEntry interface

4, for the audience range of different caches, divided into different rabbitmq exchange, see 5.

5. Cache API Description
5.1 Cache read-write interface for Localcacheservice
cacheentry getcacheentry (String cachename, String key): Get cache entry
boolean savecacheentry (String cachename, CacheEntry cacheentry): Add or modify cache entries
boolean removecacheentry (String cachename, string key): Delete cache entry
map<string, cacheentry> getcache (String cachename): Getting the entire cache
boolean Replacecache (String cachename,concurrenthashmap<string, cacheentry> cache): Replace entire cache
boolean Replacecache (String cachename,list<cacheentry> caches): Replace entire cache

5.2 Cache Initialization Interface Abstractlocalcacheinitializer, need to implement the List<cacheentry> LoadData () interface, because the design is to consider the presentation layer and the service layer is distributed deployment, At the same time the presentation layer does not have access to the DB, so the Applicationlistener interface is implemented in order to ensure that the RPC connection is established. Therefore, for the implementation of the LoadData () interface, there are two scenarios:
• If it is a presentation layer, the interface of the service layer is called through some kind of RPC protocol to load the data, and for Hang Seng T2, it is the T2 service that invokes the service layer to load the data.
• If you are a service layer, you can implement DAO directly.

5.3 Presentation Layer--Service layer application launch sequence under Distributed system
Start the service layer and start the presentation layer at a time, otherwise it will cause failure.

Distributed System JVM Local cache synchronization Implementation Dlcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.