• Redis는 대표적인 Key/Value DB 중의 하나이다.

  • Key/Value DB의 논리구조

    • Table: 하나의 DB에서 데이터를 저장하는 논리구조(관계형 DB에서 표현하는 논리적 개념인 테이블과 동일하다)
    • Data Sets: 테이블을 구성하는 논리적 단위이다.(관계형 DB의 Row와 동일) 하나의 데이터 셋은 하나의 Key와 한개 이상의 Field/Element로 구성된다.
    • Key: 하나의 Key는 하나 이상의 조합된 값으로 표현 가능하다. ex) 주문번호 또는 주문번호+순번, 년월일+순번 등
    • Values: 해당 Key에 대한 구체적인 데이터 값을 표현한다. Value는 하나 이상의 Field 또는 Element로 구성된다.
  • 데이터 입력/수정/삭제/조회

    • set: 데이터를 저장할 때(key, value)
    • get: 저장된 데이터를 검색할 때
    • rename: 저장된 데이터 값을 변경할 때
    • randomkey: 저장된 key 중에 하나의 key를 랜덤하게 검색할 때
    • keys: 저장된 모든 key를 검색할 때
    • exists: 검색 대상 key가 존재하는지 여부를 확인할 때
    • mset/mget: 여러 개의 key와 value를 한번 저장하고 검색할 때
    set 1111 "WS RYU" //key:1111, value:WS RYU
    set 1112 "STEVE JOBS" //key:1112, value:STEVE JOBS
    set 1113 "KO HONG"
    set 1114 "KH KIM"
    
    get 1111
    "WS RYU"
    get 1112
    "STEVE JOBS"
    keys *
    "1111"
    "1112"
    "1113"
    "1114"
    
    keys *2 //현재 저장되어 있는 key중에 2로 끝나는 key만 검색
    1) 1112
    del 1112 //key:1112 삭제
    (integer) 1
    
    keys *
    "1111"
    "1113"
    "1114"
    
    rename 1113 1116
    OK
    keys*
    "1111"
    "1116"
    "1114"
    
    randomkey // 현재 key중에 랜덤으로 key 검색
    "1111"
    exists 1112 //key:1112의 존재여부 검색
    (integer) 0
    strlen 1111 //key:1111의 value의 길이
    (integer) 6
    
    flushall //현재 저장되어 있는 모든 key 삭제
    keys *
    (empty list or set)
    
    setex 1111 30 "WS RYU" //ex는 expire(일정시간 동안만 저장), ttl(time to live) 30초
    ttl 1111
    (integer) 29 //29초 남음
    
    mset 1113 "NoSQL User Group" 1115 "PIT" //여러개 필드를 한번에 저장
    mget 1113 1115 //여러개 필드를 한번에 조회
    1) "NoSQL User Group"
    2) "PIT"
    
    set seq_no 201809001 //sequence 발행을 위한 key/value 저장
    incr seq_no
    201809002
    incr seq_no
    201809003
    decr seq_no
    201809002
    incrby seq_no 10
    201809012
    decrby seq_no 10
    201809002
    
    append 1115 " co." //1115의 value에 co. append
    get 1115
    "PIT co."
    
    save //현재 입력한 key/value값을 파일로 저장
    clear //화면 clear
    time 
    1) 1685365775 //데이터 저장시간(unix time in seconds)
    2) 804925 //microseconds
    
    info
    # Server
    redis_version:6.2.6
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:11725cf13da3738b
    redis_mode:standalone
    os:Linux 5.10.124-linuxkit aarch64
    arch_bits:64
    multiplexing_api:epoll
    atomicvar_api:atomic-builtin
    gcc_version:10.2.1
    process_id:1
    process_supervised:no
    run_id:f907b8741c7338196394f0136aa785c7b0b77576
    tcp_port:6379
    server_time_usec:1685365812385527
    uptime_in_seconds:1380
    uptime_in_days:0
    hz:10
    configured_hz:10
    lru_clock:7644212
    executable:/data/redis-server
    config_file:
    io_threads_active:0
    
    # Clients
    connected_clients:1
    cluster_connections:0
    maxclients:10000
    client_recent_max_input_buffer:16
    client_recent_max_output_buffer:0
    blocked_clients:0
    tracking_clients:0
    clients_in_timeout_table:0
    
    # Memory
    used_memory:906768
    used_memory_human:885.52K
    used_memory_rss:7168000
    used_memory_rss_human:6.84M
    used_memory_peak:926640
    used_memory_peak_human:904.92K
    used_memory_peak_perc:97.86%
    used_memory_overhead:863328
    used_memory_startup:842648
    used_memory_dataset:43440
    used_memory_dataset_perc:67.75%
    allocator_allocated:4424456
    allocator_active:10223616
    allocator_resident:11272192
    total_system_memory:8337526784
    total_system_memory_human:7.76G
    used_memory_lua:37888
    used_memory_lua_human:37.00K
    used_memory_scripts:0
    used_memory_scripts_human:0B
    number_of_cached_scripts:0
    maxmemory:0
    maxmemory_human:0B
    maxmemory_policy:noeviction
    allocator_frag_ratio:2.31
    allocator_frag_bytes:5799160
    allocator_rss_ratio:1.10
    allocator_rss_bytes:1048576
    rss_overhead_ratio:0.64
    rss_overhead_bytes:-4104192
    mem_fragmentation_ratio:8.30
    mem_fragmentation_bytes:6304000
    mem_not_counted_for_evict:0
    mem_replication_backlog:0
    mem_clients_slaves:0
    mem_clients_normal:20496
    mem_aof_buffer:0
    mem_allocator:jemalloc-5.1.0
    active_defrag_running:0
    lazyfree_pending_objects:0
    lazyfreed_objects:0
    
    # Persistence
    loading:0
    current_cow_size:0
    current_cow_size_age:0
    current_fork_perc:0.00
    current_save_keys_processed:0
    current_save_keys_total:0
    rdb_changes_since_last_save:0
    rdb_bgsave_in_progress:0
    rdb_last_save_time:1685365773
    rdb_last_bgsave_status:ok
    rdb_last_bgsave_time_sec:-1
    rdb_current_bgsave_time_sec:-1
    rdb_last_cow_size:0
    aof_enabled:0
    aof_rewrite_in_progress:0
    aof_rewrite_scheduled:0
    aof_last_rewrite_time_sec:-1
    aof_current_rewrite_time_sec:-1
    aof_last_bgrewrite_status:ok
    aof_last_write_status:ok
    aof_last_cow_size:0
    module_fork_in_progress:0
    module_fork_last_cow_size:0
    
    # Stats
    total_connections_received:1
    total_commands_processed:129
    instantaneous_ops_per_sec:0
    total_net_input_bytes:3963
    total_net_output_bytes:3614
    instantaneous_input_kbps:0.01
    instantaneous_output_kbps:0.00
    rejected_connections:0
    sync_full:0
    sync_partial_ok:0
    sync_partial_err:0
    expired_keys:1
    expired_stale_perc:0.00
    expired_time_cap_reached_count:0
    expire_cycle_cpu_milliseconds:72
    evicted_keys:0
    keyspace_hits:20
    keyspace_misses:1
    pubsub_channels:0
    pubsub_patterns:0
    latest_fork_usec:0
    total_forks:0
    migrate_cached_sockets:0
    slave_expires_tracked_keys:0
    active_defrag_hits:0
    active_defrag_misses:0
    active_defrag_key_hits:0
    active_defrag_key_misses:0
    tracking_total_keys:0
    tracking_total_items:0
    tracking_total_prefixes:0
    unexpected_error_replies:0
    total_error_replies:8
    dump_payload_sanitizations:0
    total_reads_processed:126
    total_writes_processed:125
    io_threaded_reads_processed:0
    io_threaded_writes_processed:0
    
    # Replication
    role:master
    connected_slaves:0
    master_failover_state:no-failover
    master_replid:046ba3c1b8e43014fc96dfe7e05e62dac2f77b72
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:0
    second_repl_offset:-1
    repl_backlog_active:0
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:0
    repl_backlog_histlen:0
    
    # CPU
    used_cpu_sys:3.892965
    used_cpu_user:2.411504
    used_cpu_sys_children:0.005058
    used_cpu_user_children:0.000762
    used_cpu_sys_main_thread:3.887938
    used_cpu_user_main_thread:2.410495
    
    # Modules
    
    # Errorstats
    errorstat_ERR:count=7
    errorstat_WRONGTYPE:count=1
    
    # Cluster
    cluster_enabled:0
    
    # Keyspace
    db0:keys=3,expires=0,avg_ttl=0