[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
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
- Jedis
- Lettuce
- performance issue?
- connection issue?
- fallback issue?
(fields / value count limits - 40 B)
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
Redis Operational Issue?