The introduction of caching at the data tier has several benefits:
- Increase data Read speed
- Increase system scalability by extending the cache to increase system load-carrying capacity
- Reduce storage costs, cache+db can assume the original need for more than a few db to bear the volume of requests, saving machine costs
Depending on the business scenario, there are usually several ways to use the cache
- Lazy (trigger on Read): First query the data in DB and then write the relevant data to the cache
- Hunger (Trigger on write): writes to DB and then writes the relevant data to the cache
- Periodic refresh: Suitable for periodic running data tasks, or list-based data, and does not require absolute real-time
1 What is a distributed cache
Distributed caching can handle a large amount of dynamic data, so it is more suitable for scenarios that require user-generated content such as social networking sites in the Web 2.0 era. After extending from the local cache to the distributed cache, the focus is on the difference in the speed of data transfer between the CPU, memory, and cache, and the speed of data transfer between the business system, the database, and the distributed cache.
2 Features of distributed cache
Distributed cache is managed and controlled by a server, with multiple client nodes storing data, which can further improve the data read rate. So which node should we choose when we want to read some data? If one node is found, then the efficiency is too low. Therefore, the data storage and reading nodes need to be determined based on the consistent hashing algorithm. Based on the data d, the total number of nodes n, the hash value corresponding to the data d is computed by the consistent hash algorithm (equivalent to the house numbers), and the corresponding node can be found based on the hash value. The advantage of a consistent hashing algorithm is that the number of nodes changes (decrease or increase) without recalculating the hash value, ensuring that the corresponding node can be found correctly and quickly when the data is stored or read.
Distributed cache can read data in high performance, can dynamically expand cache nodes, can automatically discover and switch fault nodes, can automatically balance data partition, and can provide users with a graphical management interface, deployment and maintenance is very convenient.
3 Where to use the cache
Distributed cache has been widely used in the field of distributed and cloud computing.
4 What is Memcache
Memory-based Key-value distributed cache server
5 Why use Memcache distributed cache, Memcache features
(1). In case of high concurrency, distributed caching can reduce the pressure on the application server to access the database, and can reduce the interaction with the database by accessing the distributed cache (link, data disk IO)
(2). The distributed cache can provide sufficient links, memory
(3). Based on Key-value
(4). No Data persistence required
(5). A memory-based cluster that is assembled by more than one machine. Based on k-v storage data structures.
6 The difference between a Redis database cache and a memcache cache
Memcache:1.memcache Memory for disk storage
2.key-value--Similar map
3. Data has a limitation period of . (Redis no requirement)
4.key stores up to 255 characters value:1MB Object
Redis:string:key-value key Max storage 512MB, List/set/map/zset maximum support 2^32-1 (approx. more than 4 billion)
Data Hold (RDB, AOF), bgrewriteaof (AOF file rewrite)
7 Memcache Usage Scenarios
1. Reducing database interaction silence as a data persistence layer cache Mybatis/hibernate
2. Session Pool as Application server in distributed environment
Memcached and tomcat Integration
Reference:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
The combination of Tomcat and memcache is divided into two categories: non-sticky (non-stick), sticky (adhesion)
Non-sticky (non-stick): Tomcat fully managed session (Tomcat is stateless), memcache acts as the storage master server for the session.
Each memcache master server will have a backup server for real-time synchronization of the master server's data (MSM software encoding implementation, which is similar to the Redis session management).
Sticky (adhesion): Tomcat still manages the session. Memcahce from when you are backing up a node, in general, there is one failover node for each of the backup nodes.
Nginx and Tomcat Integration: Non-adhesive (polling, least_conn), adhesion (ip_hash, sticky)
Best Practices:
Tomcat and Memcache Non-sticky | Nginx and Tomcat non-adhesive (polling, Least_conn)
Tomcat and memcache Sticky | Nginx and Tomcat adhesion (ip_hash, sticky)
Tomcat and memcache Sticky | Nginx and Tomcat non-adhesive (polling, least_conn)//error
Tomcat and Memcache Non-sticky | Nginx and Tomcat adhesion (ip_hash, sticky)//poor
Take TOMCAT7 as an example:
1 Importing Jars
Memcached-session-manager-${version}.jar Memcache Management Session implementation
Memcached-session-manager-tc[6|7|8]-${version}.jar--Fit Tomcat version
Spymemcached-${version}.jar Link Memcache Driver
Serialization Package
Configure Sticky mode
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.31.128:11211,n2:192.168.31.128:11211"
failovernodes= "N1"
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
Configure Non-sticky
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.31.128:11211,n2:192.168.31.128:11212"
Sticky= "false"
Sessionbackupasync= "false"
Lockingmode= "Uripattern:/path1|/path2"
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
Why not use Redis storage to reply to a session?
By default, the Redissessionmanager currently available in the market does not support clustering and cannot achieve massive data storage. Therefore not suitable for production environments (consider overriding Redissessionmanager).
Information
Distributed Cache-memacache (http://www.tutorialspoint.com/)
Caching-why you need to use distributed cache (http://blog.csdn.net/xcw931924821/article/details/52335495)
Big Data Summary record-memcache