오늘은 메모리 캐시 서버의 강자로 떠오르고 있는 redis에 대해 알아보도록 하겠습니다.
memory cache서버의 종류는 많고 그중 대표적인게 memcached였지만 대량의 캐시데이터를 처리하는데는 memcached보다
redis성능이 월등히 뛰어나기 때문에 요즘 같은 대용량 데이터를 다루는 시대에 대용량 서비스에 적합하다는 생각을 합니다.
이제부터 redis 설치에 대해 알아보도록 하겠습니다.
작업환경
OS : RHEL 5.6
Redis : 2.4
step 1. source download
http://redis.io/download
step 2. build
$ tar xzvf redis-2.4.16.tar.gz
$ cd redis-2.4.16
$ make
step 3. 실행
$ src/redis-server ./redis.conf &
$ tail -f /home/alm/log/dev.redis.log
위와같이 준비가 다 되었다면 java를 이용하여 client Program Test Code를 짜서 확인해 보도록 하겠습니다.
step 4. java source code 작성
20byte의 string 1000000건 입력 Test
import redis.clients.jedis.Jedis;
public class RedisTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Redis client library test...");
Jedis client = new Jedis("10.20.252.69", 6379);
/*
System.out.println("set(\"mykey\") : " + client.set("mykey","ROCKSEA!!!"));
System.out.println("get(\"mykey\") : " + client.get("mykey"));
System.out.println("mget(\"mykey\") : " + client.mget("mykey", "mykey1"));
System.out.println("echo(\"mykey\") : " + client.echo("mykey"));
System.out.println("exists(\"mykey\") : " + client.exists("mykey"));
System.out.println("ttl(\"mykey\") : " + client.ttl("mykey"));
System.out.println("ping() : " + client.ping());
*/
String str = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
for(int i=0; i < 1000000; i++){
System.out.println("set(\"mykey\""+i+") : " + client.set("mykey"+i,str));
}
}
}
redis server 정보 확인
$ ./src/redis-cli
redis 127.0.0.1:6379> INFO
redis_version:2.4.16
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.7.1
process_id:5262
uptime_in_seconds:55857
uptime_in_days:0
lru_clock:471398
used_cpu_sys:28.12
used_cpu_user:15.16
used_cpu_sys_children:0.87
used_cpu_user_children:6.55
connected_clients:1
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
used_memory:313126280
used_memory_human:298.62M
used_memory_rss:81117184
used_memory_peak:342514688
used_memory_peak_human:326.65M
mem_fragmentation_ratio:0.26
mem_allocator:jemalloc-3.0.0
loading:0
aof_enabled:0
changes_since_last_save:0
bgsave_in_progress:0
last_save_time:1346837319
bgrewriteaof_in_progress:0
total_connections_received:7
total_commands_processed:1000018
expired_keys:0
evicted_keys:0
keyspace_hits:6
keyspace_misses:4
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:12982
vm_enabled:0
role:master
db0:keys=1000000,expires=0
redis 127.0.0.1:6379>
벤치마크 결과
세션수 |
사용메모리 |
0 |
4m |
100000 |
31m |
1000000 |
298m |
기타 redis client 명령어
APPEND key value Append a value to a key
AUTH password Authenticate to the server
BGREWRITEAOF Asynchronously rewrite the append-only file
BGSAVE Asynchronously save the dataset to disk
BITCOUNT key [start] [end] Count set bits in a string
BITOP operation destkey key [key ...] Perform bitwise operations between strings
BLPOP key [key ...] timeout Remove and get the first element in a list, or block until one is available
BRPOP key [key ...] timeout Remove and get the last element in a list, or block until one is available
BRPOPLPUSH source destination timeout Pop a value from a list, push it to another list and return it; or block until one is available
CLIENT KILL ip:port Kill the connection of a client
CLIENT LIST Get the list of client connections
CONFIG GET parameter Get the value of a configuration parameter
CONFIG SET parameter value Set a configuration parameter to the given value
CONFIG RESETSTAT Reset the stats returned by INFO
DBSIZE Return the number of keys in the selected database
DEBUG OBJECT key Get debugging information about a key
DEBUG SEGFAULT Make the server crash
DECR key Decrement the integer value of a key by one
DECRBY key decrement Decrement the integer value of a key by the given number
DEL key [key ...] Delete a key
DISCARD Discard all commands issued after MULTI
DUMP key Return a serialized version of the value stored at the specified key.
ECHO message Echo the given string
EVAL script numkeys key [key ...] arg [arg ...] Execute a Lua script server side
EVALSHA sha1 numkeys key [key ...] arg [arg ...] Execute a Lua script server side
EXEC Execute all commands issued after MULTI
EXISTS key Determine if a key exists
EXPIRE key seconds Set a key's time to live in seconds
EXPIREAT key timestamp Set the expiration for a key as a UNIX timestamp
FLUSHALL Remove all keys from all databases
FLUSHDB Remove all keys from the current database
GET key Get the value of a key
GETBIT key offset Returns the bit value at offset in the string value stored at key
GETRANGE key start end Get a substring of the string stored at a key
GETSET key value Set the string value of a key and return its old value
HDEL key field [field ...] Delete one or more hash fields
HEXISTS key field Determine if a hash field exists
HGET key field Get the value of a hash field
HGETALL key Get all the fields and values in a hash
HINCRBY key field increment Increment the integer value of a hash field by the given number
HINCRBYFLOAT key field increment Increment the float value of a hash field by the given amount
HKEYS key Get all the fields in a hash
HLEN key Get the number of fields in a hash
HMGET key field [field ...] Get the values of all the given hash fields
HMSET key field value [field value ...] Set multiple hash fields to multiple values
HSET key field value Set the string value of a hash field
HSETNX key field value Set the value of a hash field, only if the field does not exist
HVALS key Get all the values in a hash
INCR key Increment the integer value of a key by one
INCRBY key increment Increment the integer value of a key by the given amount
INCRBYFLOAT key increment Increment the float value of a key by the given amount
INFO Get information and statistics about the server
KEYS pattern Find all keys matching the given pattern
LASTSAVE Get the UNIX time stamp of the last successful save to disk
LINDEX key index Get an element from a list by its index
LINSERT key BEFORE|AFTER pivot value Insert an element before or after another element in a list
LLEN key Get the length of a list
LPOP key Remove and get the first element in a list
LPUSH key value [value ...] Prepend one or multiple values to a list
LPUSHX key value Prepend a value to a list, only if the list exists
LRANGE key start stop Get a range of elements from a list
LREM key count value Remove elements from a list
LSET key index value Set the value of an element in a list by its index
LTRIM key start stop Trim a list to the specified range
MGET key [key ...] Get the values of all the given keys
MIGRATE host port key destination-db timeout Atomically transfer a key from a Redis instance to another one.
MONITOR Listen for all requests received by the server in real time
MOVE key db Move a key to another database
MSET key value [key value ...] Set multiple keys to multiple values
MSETNX key value [key value ...] Set multiple keys to multiple values, only if none of the keys exist
MULTI Mark the start of a transaction block
OBJECT subcommand [arguments [arguments ...]] Inspect the internals of Redis objects
PERSIST key Remove the expiration from a key
PEXPIRE key milliseconds Set a key's time to live in milliseconds
PEXPIREAT key milliseconds-timestamp Set the expiration for a key as a UNIX timestamp specified in milliseconds
PING Ping the server
PSETEX key milliseconds value Set the value and expiration in milliseconds of a key
PSUBSCRIBE pattern [pattern ...] Listen for messages published to channels matching the given patterns
PTTL key Get the time to live for a key in milliseconds
PUBLISH channel message Post a message to a channel
PUNSUBSCRIBE [pattern [pattern ...]] Stop listening for messages posted to channels matching the given patterns
QUIT Close the connection
RANDOMKEY Return a random key from the keyspace
RENAME key newkey Rename a key
RENAMENX key newkey Rename a key, only if the new key does not exist
RESTORE key ttl serialized-value Create a key using the provided serialized value, previously obtained using DUMP.
RPOP key Remove and get the last element in a list
RPOPLPUSH source destination Remove the last element in a list, append it to another list and return it
RPUSH key value [value ...] Append one or multiple values to a list
RPUSHX key value Append a value to a list, only if the list exists
SADD key member [member ...] Add one or more members to a set
SAVE Synchronously save the dataset to disk
SCARD key Get the number of members in a set
SCRIPT EXISTS script [script ...] Check existence of scripts in the script cache.
SCRIPT FLUSH Remove all the scripts from the script cache.
SCRIPT KILL Kill the script currently in execution.
SCRIPT LOAD script Load the specified Lua script into the script cache.
SDIFF key [key ...] Subtract multiple sets
SDIFFSTORE destination key [key ...] Subtract multiple sets and store the resulting set in a key
SELECT index Change the selected database for the current connection
SET key value Set the string value of a key
SETBIT key offset value Sets or clears the bit at offset in the string value stored at key
SETEX key seconds value Set the value and expiration of a key
SETNX key value Set the value of a key, only if the key does not exist
SETRANGE key offset value Overwrite part of a string at key starting at the specified offset
SHUTDOWN [NOSAVE] [SAVE] Synchronously save the dataset to disk and then shut down the server
SINTER key [key ...] Intersect multiple sets
SINTERSTORE destination key [key ...] Intersect multiple sets and store the resulting set in a key
SISMEMBER key member Determine if a given value is a member of a set
SLAVEOF host port Make the server a slave of another instance, or promote it as master
SLOWLOG subcommand [argument] Manages the Redis slow queries log
SMEMBERS key Get all the members in a set
SMOVE source destination member Move a member from one set to another
SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination] Sort the elements in a list, set or sorted set
SPOP key Remove and return a random member from a set
SRANDMEMBER key Get a random member from a set
SREM key member [member ...] Remove one or more members from a set
STRLEN key Get the length of the value stored in a key
SUBSCRIBE channel [channel ...] Listen for messages published to the given channels
SUNION key [key ...] Add multiple sets
SUNIONSTORE destination key [key ...] Add multiple sets and store the resulting set in a key
SYNC Internal command used for replication
TIME Return the current server time
TTL key Get the time to live for a key
TYPE key Determine the type stored at key
UNSUBSCRIBE [channel [channel ...]] Stop listening for messages posted to the given channels
UNWATCH Forget about all watched keys
WATCH key [key ...] Watch the given keys to determine execution of the MULTI/EXEC block
ZADD key score member [score] [member] Add one or more members to a sorted set, or update its score if it already exists
ZCARD key Get the number of members in a sorted set
ZCOUNT key min max Count the members in a sorted set with scores within the given values
ZINCRBY key increment member Increment the score of a member in a sorted set
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] Intersect multiple sorted sets and store the resulting sorted set in a new key
ZRANGE key start stop [WITHSCORES] Return a range of members in a sorted set, by index
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] Return a range of members in a sorted set, by score
ZRANK key member Determine the index of a member in a sorted set
ZREM key member [member ...] Remove one or more members from a sorted set
ZREMRANGEBYRANK key start stop Remove all members in a sorted set within the given indexes
ZREMRANGEBYSCORE key min max Remove all members in a sorted set within the given scores
ZREVRANGE key start stop [WITHSCORES] Return a range of members in a sorted set, by index, with scores ordered from high to low
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] Return a range of members in a sorted set, by score, with scores ordered from high to low
ZREVRANK key member Determine the index of a member in a sorted set, with scores ordered from high to low
ZSCORE key member Get the score associated with the given member in a sorted set
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] Add multiple sorted sets and store the resulting sorted set in a new key
이상으로 posting을 마치도록 하겠습니다.
.by rocksea