ä»ä»¥ä¸å 个维度ï¼å¯¹ redisãmemcacheãmongoDB åäºå¯¹æ¯ã
1ãæ§è½
é½æ¯è¾é«ï¼æ§è½å¯¹æ们æ¥è¯´åºè¯¥é½ä¸æ¯ç¶é¢ã
æ»ä½æ¥è®²ï¼TPS æ¹é¢ redis å memcache å·®ä¸å¤ï¼è¦å¤§äº mongodbã
2ãæä½ç便å©æ§
memcache
æ°æ®ç»æåä¸ãï¼key-valueï¼
redis 丰å¯ä¸äºï¼æ°æ®æä½æ¹é¢ï¼redis æ´å¥½ä¸äºï¼è¾å°çç½ç» IO 次æ°ï¼åæ¶è¿æä¾ listï¼setï¼
hash çæ°æ®ç»æçåå¨ã
mongodb æ¯æ丰å¯çæ°æ®è¡¨è¾¾ï¼ç´¢å¼ï¼æ类似
å
³ç³»åæ°æ®åºï¼æ¯æçæ¥è¯¢è¯è¨é常丰å¯ã
3ãå
å空é´ç大å°åæ°æ®éç大å°
redis å¨ 2.0 çæ¬åå¢å äºèªå·±ç VM ç¹æ§ï¼çªç ´
ç©çå
åçéå¶ï¼å¯ä»¥å¯¹ key value 设置è¿
ææ¶é´ï¼ç±»ä¼¼ memcacheï¼
memcache å¯ä»¥ä¿®æ¹æ大å¯ç¨å
å,éç¨ LRU ç®æ³ãMemcached 代ç软件 magentï¼æ¯å¦å»ºç«
10 å° 4G ç Memcache é群ï¼å°±ç¸å½äºæäº 40Gã magent -s 10.1.2.1 -s 10.1.2.2:11211 -b
10.1.2.3:14000 mongoDB éå大æ°æ®éçåå¨ï¼ä¾èµæä½ç³»ç» VM åå
å管çï¼åå
åä¹æ¯è¾å害ï¼æå¡
ä¸è¦åå«çæå¡å¨ä¸èµ·ã
4ãå¯ç¨æ§ï¼åç¹é®é¢ï¼
对äºåç¹é®é¢ï¼
redisï¼ä¾èµå®¢æ·ç«¯æ¥å®ç°åå¸å¼è¯»åï¼ä¸»ä»å¤å¶æ¶ï¼æ¯æ¬¡ä»èç¹éæ°è¿æ¥ä¸»èç¹é½è¦ä¾èµæ´
个
å¿«ç
§,æ å¢éå¤å¶ï¼å æ§è½åæçé®é¢ï¼
æ以åç¹é®é¢æ¯è¾å¤æï¼ä¸æ¯æèªå¨ sharding,éè¦ä¾èµç¨åºè®¾å®ä¸è´ hash æºå¶ã
ä¸ç§æ¿ä»£æ¹æ¡æ¯ï¼ä¸ç¨ redis æ¬èº«çå¤å¶æºå¶ï¼éç¨èªå·±å主å¨å¤å¶ï¼å¤ä»½åå¨ï¼ï¼æè
æ¹æ
å¢éå¤å¶çæ¹å¼ï¼éè¦èªå·±å®ç°ï¼ï¼ä¸è´æ§é®é¢åæ§è½çæè¡¡
Memcache æ¬èº«æ²¡æ
æ°æ®åä½æºå¶ï¼ä¹æ²¡å¿
è¦ï¼å¯¹äºæ
éé¢é²ï¼éç¨ä¾èµæçç hash æè
ç¯
ç¶çç®æ³ï¼è§£å³åç¹æ
éå¼èµ·çæå¨é®é¢ã
mongoDB æ¯æ master-slave,replicasetï¼å
é¨éç¨ paxos é举ç®æ³ï¼èªå¨æ
éæ¢å¤ï¼,auto sharding æºå¶ï¼å¯¹å®¢æ·ç«¯å±è½äºæ
é转移åååæºå¶ã
5ãå¯é æ§ï¼æä¹
åï¼
对äºæ°æ®æä¹
ååæ°æ®æ¢å¤ï¼
redis æ¯æï¼å¿«ç
§ãAOFï¼ï¼ä¾èµå¿«ç
§è¿è¡æä¹
åï¼aof å¢å¼ºäºå¯é æ§çåæ¶ï¼å¯¹æ§è½ææå½±
å
memcache ä¸æ¯æï¼é常ç¨å¨åç¼å,æåæ§è½ï¼
MongoDB ä» 1.8 çæ¬å¼å§éç¨ binlog æ¹å¼æ¯ææä¹
åçå¯é æ§
6ãæ°æ®ä¸è´æ§ï¼äºå¡æ¯æï¼
Memcache å¨å¹¶ååºæ¯ä¸ï¼ç¨ cas ä¿è¯ä¸è´æ§redis äºå¡æ¯ææ¯è¾å¼±ï¼åªè½ä¿è¯äºå¡ä¸çæ¯ä¸ªæä½è¿ç»æ§è¡
mongoDB ä¸æ¯æäºå¡
7ãæ°æ®åæ
mongoDB å
ç½®äºæ°æ®åæçåè½(mapreduce),å
¶ä»ä¸æ¯æ
8ãåºç¨åºæ¯
redisï¼æ°æ®éè¾å°çæ´æ§è½æä½åè¿ç®ä¸
memcacheï¼ç¨äºå¨å¨æç³»ç»ä¸åå°æ°æ®åºè´è½½ï¼æåæ§è½;åç¼åï¼æé«æ§è½ï¼éå读å¤å
å°ï¼å¯¹äºæ°æ®éæ¯è¾å¤§ï¼å¯ä»¥éç¨ shardingï¼
MongoDB:主è¦è§£å³
æµ·éæ°æ®ç访é®æçé®é¢ã
è¡¨æ ¼æ¯è¾ï¼
memcache redis ç±»å å
åæ°æ®åº å
åæ°æ®åº
æ°æ®ç±»å å¨å®ä¹ value æ¶å°±è¦åºå®æ°æ®ç±»å ä¸éè¦
æ
å符串ï¼é¾è¡¨ï¼é ååæåºéå
èæå
å ä¸æ¯æ æ¯æ
è¿æçç¥ æ¯æ æ¯æ
åå¸å¼ magent master-slaveï¼ä¸ä¸»ä¸ä»æä¸ä¸»å¤ä»
åå¨
æ°æ®å®å
¨ ä¸æ¯æ ä½¿ç¨ save åå¨å° dump.rdb ä¸
ç¾é¾æ¢å¤ ä¸æ¯æ append only file(aof)ç¨äºæ°æ®æ¢å¤
æ§è½
1ãç±»åââmemcache å redis é½æ¯å°æ°æ®åæ¾å¨å
åï¼æ以æ¯å
åæ°æ®åºãå½ç¶ï¼memcache ä¹å¯ç¨äºç¼åå
¶ä»ä¸è¥¿ï¼ä¾å¦å¾çççã
2ã æ°æ®ç±»åââMemcache å¨æ·»å æ°æ®æ¶å°±è¦æå®æ°æ®çåèé¿åº¦,è redis ä¸éè¦ã
3ã èæå
åââå½ç©çå
åç¨å®æ¶ï¼å¯ä»¥å°ä¸äºå¾ä¹
没ç¨å°ç value 交æ¢å°ç£çã
4ã è¿æçç¥ââmemcache å¨ set æ¶å°±æå®ï¼ä¾å¦ set key1 0 0 8,å³æ°¸ä¸è¿æãRedis å¯ä»¥é
è¿ä¾å¦ expire 设å®ï¼ä¾å¦ expire name 10ã
5ã åå¸å¼ââè®¾å® memcache é群ï¼å©ç¨ magent åä¸ä¸»å¤ä»;redis å¯ä»¥åä¸ä¸»å¤ä»ãé½å¯
以ä¸ä¸»ä¸ä»ã
6ã åå¨æ°æ®å®å
¨ââmemcache æçµå°±æäºï¼æ°æ®æ²¡äºï¼redis å¯ä»¥å®æ save å°ç£çã
7ã ç¾é¾æ¢å¤ââmemcache åä¸ï¼redis 丢äºåå¯ä»¥éè¿ aof æ¢å¤ã
Memecache ç«¯å£ 11211
yum -y install memcached
yum -y install php-pecl-memcache
/etc/init.d/memcached start memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid
-d å¯å¨ä¸ä¸ª
å®æ¤è¿ç¨-p 端å£
-m åé
çå
åæ¯ M
-c æ大è¿è¡å¹¶åæ°-P memcache ç pid
//0 å缩ï¼æ¯å¦ MEMCACHE_COMPRESSEDï¼ 30 ç§å¤±ææ¶é´
//delete 5 æ¯ timeout <?php
$memcache = new Memcache; $memcache -> connect('127.0.0.1', 11211); $memcache -> set('name','yang',0,30);
if(!$memcache->add('name','susan',0, 30)) {
//echo 'susan is exist'; }$memcache -> replace('name', 'lion', 0, 300); echo $memcache -> get('name');
//$memcache -> delete('name', 5);
printf "stats\r\n" | nc 127.0.0.1 11211
telnet localhost 11211 stats quit éåº
Redis ç
é
ç½®æ件 ç«¯å£ 6379
/etc/redis.conf å¯å¨ Redis
redis-server /etc/redis.conf æå
¥ä¸ä¸ªå¼
redis-cli set test "phper.yang" è·åé®å¼
redis-cli get test å
³é Redis
redis-cli shutdown å
³éææ
redis-cli -p 6379 shutdown <?php
$redis=new
Redis(); $redis->connect('127.0.0.1',6379); $redis->set('test',
'Hello World'); echo $redis->get('test'); Mongodb
apt-get install mongo mongo å¯ä»¥è¿å
¥ shell
å½ä»¤è¡pecl install mongo Mongodb 类似 phpmyadmin æä½å¹³å° RockMongo