티스토리 뷰

Developer

[ memcached ] installation guide

rocksea 2012. 9. 10. 11:27

memcached란  이름 그대로 memory cache 서버를 말합니다. 세션이라든지 자주쓰이는 데이터를 메모리에 올려두어 I/O속도를 최대한 빠르게 하기위한 System에 사용됩니다.

현 서비스에서의 memcached를 적용한 기업

( YouTube, Reddit, Zynga, Facebook, Orange, Twitter, Wikipedia, Heroku )

 

설치환경

OS : Ubuntu 12.04

 

Download memcached java client

https://github.com/gwhalin/Memcached-Java-Client/downloads

 

step 1. memcached 설치

$ sudo apt-get install memcached

 

step 2. memcached 설정

$ vim /etc/memcached.conf

# memcached default config file
# 2003 - Jay Bonci <jaybonci@debian.org>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
#-l 127.0.0.1
-l 0.0.0.0

 

step 3. memcached 시작

$ service memcached start

 

step 4. telnet client 간단한 조작

$ telnet localhost 11211

stats


STAT pid 14343
STAT uptime 5581
STAT time 1347343326
STAT version 1.4.13
STAT libevent 2.0.16-stable
STAT pointer_size 64
STAT rusage_user 14.336896
STAT rusage_system 19.557222
STAT curr_connections 5
STAT total_connections 7
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 10
STAT cmd_set 1000000
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 4
STAT get_misses 6
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 232777862
STAT bytes_written 8000929
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 20
STAT hash_bytes 8388608
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 279777780
STAT curr_items 1000000
STAT total_items 1000000
STAT evictions 0
STAT reclaimed 0
END

 

Command Description Example
get Reads a value get mykey
set Set a key unconditionally set mykey 0 60 5
add Add a new key add newkey 0 60 5
replace Overwrite existing key replace key 0 60 5
append Append data to existing key append key 0 60 15
prepend Prepend data to existing key prepend key 0 60 15
incr Increments numerical key value by given number incr mykey 2
decr Decrements numerical key value by given number decr mykey 5
delete Deletes an existing key delete mykey
flush_all Invalidate specific items immediately flush_all
Invalidate all items in n seconds flush_all 900
stats Prints general statistics stats
Prints memory statistics stats slabs
Prints memory statistics stats malloc
Print higher level allocation statistics stats items
stats detail
stats sizes
Resets statistics stats reset
version Prints server version. version
verbosity Increases log level verbosity
quit Terminate telnet session quit

 

step 5. java test code

import java.util.HashMap;

 

import com.danga.MemCached.*;

public class MemCachedTest {
 private static MemCachedClient mcc;    

 public static void main(String[] args) {
  //initialize the SockIOPool that maintains the Memcached Server Connection Pool
  String[] servers = {"192.198.0.200:11211"};
  Integer[] weights = {1};
  SockIOPool pool = SockIOPool.getInstance("Test1");
  pool.setServers( servers );
  pool.setWeights( weights );       
  pool.setFailover( true );
  pool.setInitConn( 10 );
  pool.setMinConn( 5 );
  pool.setMaxConn( 250 );
  pool.setMaintSleep( 30 );
  pool.setNagle( false );
  pool.setSocketTO( 3000 );
  pool.setAliveCheck( true );
  pool.initialize();
  /*
   *
   *
  //Get the Memcached Client from SockIOPool named Test1
  //MemCachedClient mcc = new MemCachedClient("Test1");
  mcc = new MemCachedClient("Test1");
  //add some value in cache
  System.out.println("add status:"+mcc.add("1", "Original"));
  //Get value from cache
  System.out.println("Get from Cache:"+mcc.get("1"));

  System.out.println("add status:"+mcc.add("1", "Modified"));
  System.out.println("Get from Cache:"+mcc.get("1"));

  //use set function to add/update value, use replace to update and not add
  System.out.println("set status:"+mcc.set("1","Modified"));
  System.out.println("Get from Cache after set:"+mcc.get("1"));

  //use delete function to delete key from cache
  System.out.println("remove status:"+mcc.delete("1"));
  System.out.println("Get from Cache after delete:"+mcc.get("1"));

  //Use getMulti function to retrieve multiple keys values in one function
  // Its helpful in reducing network calls to 1
  mcc.set("2", "2");
  mcc.set("3", "3");
  mcc.set("4", "4");
  mcc.set("5", "5");
  String [] keys = {"1", "2","3","INVALID","5"};
  HashMap<String,Object> hm = (HashMap<String, Object>) mcc.getMulti(keys);

  for(String key : hm.keySet()){
   System.out.println("KEY:"+key+" VALUE:"+hm.get(key));
  }
  
  
  */
  
  mcc = new MemCachedClient("Test1");
  String str = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
  for(int i=0; i < 100000; i++){
   mcc.add(Integer.toString(i), str+i);
   System.out.println("key : "+ i);
  }
 }

}

 

성능테스트

200byte String 100000개 add case

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
15377 memcache  20   0  345m  31m  944 S    0  0.2   0:03.31 memcached

사용메모리 : 31m ( 27777780 byte )

200byte String 1000000개 add case

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
14343 memcache  20   0  573m 299m  940 S    0  1.9   0:33.88 memcached

사용메모리 : 299m ( 279777780 byte )

 

이상으로 posting을 마치도록 하겠습니다.

.by rocksea

 

 

'Developer' 카테고리의 다른 글

Heuristic  (0) 2012.09.25
[ redis ] master , slave 설정  (0) 2012.09.14
[redis] redis 설치 및 벤치마크 테스트  (0) 2012.09.06
[ nodejs ] installation guide  (0) 2012.09.05
[ ldconfig ] apt-get 사용시 ldconfig 오류  (0) 2012.09.03
댓글