Memcached learning experience of distributed cache system

Source: Internet
Author: User
Tags hash memcached socket jboss
Distributed Cache System memcached learning experience--Javaeye technology websiteDistributed Cache system Memcached learning experience--Javaeye technology website (EXT)


Origin: In data-driven web development, it is often repeated to remove the same data from the database, which greatly increases the database load. Caching is a good way to solve this problem.
What is memcached?
Memcached is a high-performance, distributed memory object caching system developed by Danga Interactive for reducing database load and increasing access speed in dynamic applications.

what memcached can cache.
By maintaining a unified, huge hash table in memory, memcached can be used to store data in a variety of formats, including images, videos, files, and the results of database retrieval.

memcached fast.
Very fast. Memcached uses libevent (using Epoll under Linux if possible) to equalize any number of open links, use non-blocking network I/O, and implement reference counting for internal objects (so objects can be in various states for multiple clients). Use your own page block allocator and hash table, so virtual memory is not fragmented and the time complexity of virtual memory allocation is guaranteed to be O (1).
Danga Interactive developed memcached for the speed of Danga Interactive. Currently, LiveJournal.com has provided up to 20 million page visits per day to 1 million of users. These, however, are done by a cluster of Web servers and database servers. Memcached almost completely abandons the way any data is read from the database, and it also shortens the speed at which the user can view the page, better resource allocation, and access to the database when the memcache fails.

Features of Memcached
The memcached cache is distributed and can be accessed simultaneously by multiple users on different hosts, thus solving the limitations of shared memory only for single-machine applications, and less disk overhead and blocking when using databases to do similar things.

Use of memcached
One, memcached server-side installation (it is installed as a system service here)
Download file: memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 Extracting files to c:\memcached
2 command line input ' c:\memcached\memcached.exe-d install '
3 command line input ' c:\memcached\memcached.exe-d start ', the command starts memcached, the default listener port is 11211
You can view its help through memcached.exe-h

Second, the client uses
Download memcached java client:http://www.whalin.com/memcached/#download
1 Add the Java_memcached-release_2.0.1.jar jar package to the classpath of the project after decompression
2 using memcached Java Client for a simple application
package com.danga.memcached.test;             import  java.util.Date;             import  com.danga.memcached.memcachedclient;       import  com.danga.memcached.sockiopool;                    publicclass Test {            Protectedstatic memcachedclient mcc = new memcachedclient ();                              string[] servers ={"192.168.40.4:12000"};                              integer[] weights =&nbsp { 3 };                             SockIOPool pool =  Sockiopool.getinstance ();                     // set the servers and the weights              pool.setservers ( servers );                   pool.setweights (  weights );                     // set some basic pool settings        // 5 initial, 5 min, and 250 max conns        // and set the max idle time for a conn                 pool.setinitconn ( 5 );                   pool.setminconn ( 5  );                   Pool.setmaxconn ( 250 );                   pool.setmaxidle ( 1000 * 60 * 60 * 6 );                     //  set the sleep for the maint thread        // it will wake up every x seconds and        //&NBsp;maintain the pool size                 pool.setmaintsleep ( 30 );                     //  Confirmation of the last sent packet arrives; This method closes the cache for the socket,                         pool.setnagle ( false );                              Pool.setsocketto ( 3000 );                          pool.setsocketconnectto ( 0 ) ;                    / / initialize the connection pool                           pool.initialize ();                                    // lets set some  compression on for the client       // compress  anything larger than 64k                 mcc.setcompressenable ( true );                   mcc.setcompressthreshold ( 64 *  1024 );              }                         publicstaticvoid  Bulidcache () {          //set (key,value,date)  ,date is an expiration time, If you want this expiration time to take effect, the New date (long date)   parameter Date passed here needs to be a value greater than or equal to 1000.     //Because the java client implementation of the source code is so implemented  expiry.gettime ()  / 1000    Values less than 1000, divided by 1000, are 0, i.e. never expire               Mcc.set (  "test",  "this is a test string"  ,new date (11211));                            }                    publicstaticvoid output ()  {                  string value =  (String)  mcc.get (  "test"  );                   system.out.println (value);                }                          Publicstaticvoid main (String[] args) {                   bulidcache ();                  output ();                   }                   }          comments have a question, Feeling memcache just solves the problem of large data volume query, there is no update.
In another case, I need to frequently query the contents of the cache, update it, and set it to Memcahe.
At the same time, the updated data is also written back to the database, otherwise the data in the database is not up-to-date.
To ask, is the performance cost of writing back to the DB from the cache a lot. That is, memcache can not meet the needs of this application.
Also, in the query, update, Memcache is not able to ensure that thread security. Bottom wrote

memcached very fast, but I didn't say it was faster than the ehcache of this machine, but with the local cache there are two points of discomfort:
1. The cache is placed in memory or placed on disk. Applying a restart causes the memory cache to be lost and not fast enough on the disk. How much memory is appropriate. If it is a large-volume application, the cache object set-out may cause the server load to fluctuate sharply (we have suffered a loss and now open Ehcache, but the upper limit of the cache object is very small)
2. How the cache objects are shared in the cluster application. JBoss Cache uses a broadcast, when the traffic is large, you can drag your service to death, this we have eaten a loss

Of course, someone talked about the Sohu example, once the traffic is large, all kinds of caches are used, currently we are squid + memcached + ehcache. Squid is also a good thing, but the cache content deletion is not very flexible, more suitable for web1.0, such as Sina Sohu News

In addition, the Java memcached client with 1.6 good, no need to upgrade to 2.1,2.x seems to be not stable. Forums, SNS and other applications, will be cached using a variety of technologies.

Take Sohu BBS to say, PV is 5000w, peak 8000w
Which posts, comments read and write frequently, other parts read frequently.
Post list, comment list using C development (where the sorting algorithm is ingenious), socket call
Post, comment content using squid cache

Other frequently read sections use memcached, squid, and timed static pages to generate a variety of techniques.

Database with MySQL, sub-table.

Personally, the JVM-level cache can be used in small-scale applications, and memcached is not available
Large-scale Web site application, is certainly the system of horizontal segmentation, a variety of cache combination. Javaeyename wrote Woodless wrote Caoyangx wrote Robbin wrote Xtcn wrote

The Cache server is more reliable than DB server, and this assumption is not true. Or you should assume that:

So you can't rely too much on the database,
Your design to ensure that without a database, the site can provide services as usual,
Otherwise, in case of large-scale database server problems, the consequences will be very serious:)

Reducing the IO for the database is correct.
But without a database, the site will also provide services as usual and too bull X.



Portal-level Web site, database as its name: database, just responsible for saving the data
Pages using CMS to publish to static pages, either put squid, or put CDN

The update of the portal is relatively slow, such as SNS, the forum to update in time, full static I am afraid not.

That depends on the granularity, if you understand the cache is always page level then there is nothing to say. Woodless wrote Caoyangx wrote Robbin wrote Xtcn wrote

The Cache server is more reliable than DB server, and this assumption is not true. Or you should assume that:

So you can't rely too much on the database,
Your design to ensure that without a database, the site can provide services as usual,
Otherwise, in case of large-scale database server problems, the consequences will be very serious:)

Reducing the IO for the database is correct.
But without a database, the site will also provide services as usual and too bull X.



Portal-level Web site, database as its name: database, just responsible for saving the data
Pages using CMS to publish to static pages, either put squid, or put CDN

The update of the portal is relatively slow, such as SNS, the forum to update in time, full static I am afraid not. Caoyangx wrote Robbin wrote Xtcn wrote

The Cache server is more reliable than DB server, and this assumption is not true. Or you should assume that:

So you can't rely too much on the database,
Your design to ensure that without a database, the site can provide services as usual,
Otherwise, in case of large-scale database server problems, the consequences will be very serious:)

Reducing the IO for the database is correct.
But without a database, the site will also provide services as usual and too bull X.



Portal-level Web site, database as its name: database, just responsible for saving the data
Pages use CMS to publish to static pages, either put squid or put Cdnrobbin wrote Xtcn wrote

The Cache server is more reliable than DB server, and this assumption is not true. Or you should assume that:

So you can't rely too much on the database,
Your design to ensure that without a database, the site can provide services as usual,
Otherwise, in case of large-scale database server problems, the consequences will be very serious:)

Reducing the IO for the database is correct.
But without a database, the site will also provide services as usual and too bull X. Wangzy wrote
Hi, when you use spymemcached, it seems that his binary protocol support is not available, in the latest stable version of the memcached, or I used the wrong. Bottom wrote

It doesn't have to be that fast. Our app reads memcached with 2MB of traffic per second, about 5 times times the amount of network traffic that reads the database. But you need to know that now random PC is Gigabit network card, so memcached get/set operation Delay is very little, not much slower than Echache get/set. In a complete Web application, my stress test showed that the performance difference <= 5% landlord does not seem to know very well memcached, our company has been used for a long time, very stable, very fast, concurrent connection can go up to 1w, my application is often kept in 3-5k It's fast not only because of the use of libevent, but also because it took the "Memory redundancy for access speed" memory management strategy, the article on the Internet specifically analyze its memory allocation management of the source code, speak very clearly, in this above JBoss cache, Ehcache, Oscache and it can not compare, memcached cluster is also very good, I heard that there are 200+ memcached cluster abroad, we also have this aspect of the attempt, the effect is very good, a down will not cause other machines down, but this data lost, need to slowly accumulate back , and support multi-client, Java, PHP, Python, Ruby can share the data, it is used as a database.
My advice is: Your application access volume is large, the response speed is very high, the data consistency requirements in general, with it, block in front of the database, very cool (memcached is the internet company developed, just meet these three conditions); If the app is not busy, use Ehcache. Just found the problem:
If someone encounters an object written to memcached, it appears:
(Client_error bad data chunk)
When wrong, please check your key. See if there are any spaces inside.
If there is a space, the above error will occur. Xtcn wrote

The Cache server is more reliable than DB server, and this assumption is not true. Or you should assume that:

So you can't rely too much on the database,
Your design to ensure that without a database, the site can provide services as usual,
Otherwise, in case of large-scale database server problems, the consequences will be very serious: Nowonder wrote before the survey memcached biggest problem is that it is a single point, is a centralized distribution, the N-machine composed of it as long as there is a down, the whole hanging ...
1, and will not be the entire hanging off, will only access some cache content cannot hit
2, even if all hung up, the cache is to reduce access to the database, so it is nothing more than the pressure on the database a little bit, can have any impact.
3. You can use Memcachedb if you want the cache to persist, or if you have failover capability.


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.