[Redis] Redis Overview

Redis 는 cache 용도로 사용한다. 이는 파레토의 법칙으로 인해 20% 데이터가 80%의 확률로 조회되기 때문에 모든 데이터를 cache에 저장할 필요는 없으며 일부만 저장하더라도 굉장히 효율적일 수 있다는 것을 나타낸다.

cache는 CPU와 DB간의 throughput 차이를 완화시키기 위한 방법으로 빠르게 읽거나, 빠르게 쓰기위해서 사용된다.

(READ) Look Aside cache (lazy loading?)

  • check cache first
  • if data exist in cache, get the data and return it.
  • if no data in cache, fetch the data from database.
  • put the data fetched from database to cache for further request

(WRITE) write back

  • put data without saving into database (super fast, but possible to lose the data when it shut-down) -if data exist in cache, get data from cache
  • periodically flush data bulkly into database and clear cache

Redis provide

  • Collections (List, Set, Hash, Sorted-set ..)
  • operate with single thread which prevent race condition, run atomically
  • provide persistece (redis can load data from static storage, after it started from shutdown)

Redis used for

  • shared data storage for remote multiple servers
  • store authentication token / session
  • implement ranking board using sorted set
  • implement API request limitation per user
  • Job queue

Redis commands get {key} - get value from key
set {key} {value} - set value as key del {key} - delete value from key

lpush {key} {value} - push value on left side with key (append value into index0) rpush {key} {value} - push value on right side with key (append value into last location) lrange {key} {start} {end} - get list between index start and end (start = start index, end = count to get) lpop {key} - pop value from left side rpop {key} - pop value from right side sadd {key} {value} - append value with key srem {key} {value} - remove value from key smember {key} - get set with key scard {key} - get lengh of key set spop {key} - get a value from key randomly zadd {key} {score} {value} - add value with score into key , if there are values with same score, sort with value zcard {key} - get length from key set zrange {key} {start} {end} - get value from range zrangebyscore {key} {min} {max} - get value with score range hset {key} {field} {value} - set filed/value map into key hget {key} {field} - get value from field on input key hdel {key} {field} - remove field from key hlen {key} - get the number of fields hgetAll key - get all fields and values from key hkeys {key} - get all fields hvals {key} - get all values

(fields / value count limits - 40 B)

redis can set TTL for specific key , after set or getset, configuration for TTL will be reset

expire {key} {seconds} - set TTL deleting after input seconds ttl {key} - get secodns for deletion

Redis integration with Java (spring)

@EnableRedisRepositories - declare redis enable on configuration class @RedhHash({value}) - set redis key with {value} + {@id} @Id - set id on specific fields in entity

CrudRepository<{entity}, {keytype}> ####RedisTemplate - opsForValue - opsForList - opsForSet - opsForZSet - opsForHash

AWS ElasticCash (Redis)

Redis Client

  • Jedis
  • Lettuce

Redis Operational Issue?

  • performance issue?
  • connection issue?
  • fallback issue?