Home [Development] Redis
Post
Cancel

[Development] Redis

Redis

  • an in-memory key-value store that can be used as a database, cache or message broker.
  • Redis began as a caching database, but it has since evolved into a primary database. Many applications built today use Redis as a primary database. However, most Redis service providers support Redis as a cache but not as a primary database.
  • you can use Redis as both an in-memory cache and a primary database in a single system, thus eliminating the complexity and latency of two separate systems

Redis as a Cache vs Redis as a Primary Database

Redis VS Memcached

  • Memcached์— ๋น„ํ•ด ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜์—ฌ ๊ฐ€๋ณ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

Spring Data Redis (What I used)

https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
  cloud:
    cache:
      type: redis
      cache-names:
        - ***
        - ***
        - ***
    redis:
      host: ***
      lettuce:
        pool:
          max-active: **
          min-idle: **

lettuce

Jedis VS Lettuce

  • commonality : Redis์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” Spring Data Redis Library (์„ฑ๋Šฅ๊ณผ ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ํด๋ผ์ด์–ธํŠธ)
  • Jedis : deprecated
    • ๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•˜์—ฌ Blocking ์ด์Šˆ๊ฐ€ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • Lettuce
    • Lettuce ์—ญ์‹œ ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋ฉฐ ๋™๊ธฐ, ๋น„๋™๊ธฐ ๋ฐฉ์‹์„ ๋‘˜ ๋‹ค ์ง€์›ํ•˜์—ฌ non-blockingํ•˜๊ฒŒ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ  ํ™•์žฅ์„ฑ์ด ๋›ฐ์–ด๋‚˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์„ฑ์ด Jedis์— ๋น„ํ•ด ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

** ๋น ๋ฅด๊ฒŒ ์‰ฝ๊ฒŒ ๊ฐœ๋ฐœํ•  ํ•„์š”์„ฑ๋ณด๋‹ค ์ถ”ํ›„ ๋ณ€๊ฒฝ ์‚ฌํ•ญ ๋ฐ ํŠœ๋‹ ๋“ฑ์„ ๋Œ€๋น„ํ•˜์—ฌ ํ™•์žฅ์„ฑ์ด ๋†’์œผ๋ฉฐ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด ์•ˆ์ •์ ์œผ๋กœ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” Lettuce๊ฐ€ ๋” ์ ํ•ฉ

Screen Shot 2023-04-21 at 6 26 10 PM


max-active VS max-idle

how to use Redis-cli

1
2
3
4
5
brew install redis ( default port : 6379)
redis-cli --raw -h ... -p ... (--raw : ํ•œ๊ธ€ ์•ˆ๊นจ์ ธ์„œ ๋‚˜์˜ด)
>> ping (์ ‘์† ํ…Œ์ŠคํŠธ)
>> keys * 
>> get (key name)

Caching

  • ํ•œ๋ฒˆ ์ฒ˜๋ฆฌํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ๋กœ ์ €์žฅ์†Œ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ด ์ž„์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ผํ•˜๊ฑฐ๋‚˜ ์œ ์‚ฌ ์š”์ฒญ์ด ์™”์„ ๊ฒฝ์šฐ ์ €์žฅ์†Œ์—์„œ ๋ฐ”๋กœ ์ฝ์–ด์™€์„œ ์‘๋‹ต์„ ํ•˜์—ฌ ์„ฑ๋Šฅ ๋ฐ ์‘๋‹ต์†๋„ ํ–ฅ์ƒ์„ ์œ„ํ•œ ๊ธฐ์ˆ 

@Cachable vs @CacheEvict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Cacheable(cacheNames = CacheNames.DISCLOSURE)

@Cacheable(cacheNames = CacheNames.ADD_ATTR, key = โ€œ#root.methodNameโ€, condition = โ€œ#result != nullโ€)

@Cacheable(cacheNames = CacheNames.SUPPLIER, key = โ€œ#supplierCodeโ€)

@Cacheable(value = โ€œownerCodeโ€, key = โ€œ#result  == nullโ€)
public String getOwnerCode() {...}

@Cacheable(cacheNames = CacheNames.PROMO, key = โ€œ#root.methodNameโ€, unless =  โ€œ#result.isEmpty()โ€)

@CacheEvict(cacheNames = CacheNames.DISCLOSURE)

@CacheEvict(cacheNames = โ€supplierโ€, key = โ€œ#request.supplierCodeโ€)
public void changeSupplier(@NotNull SupplierModifyRequest request) {...}

Redis Sharding

Redis Redis ร‡luster

This post is licensed under CC BY 4.0 by the author.

[Development] ElasticSearch - UpdateByQuery

[AWS] Cloudfront

Comments powered by Disqus.