๐Ÿ“š Redis๋ž€? + @Cacheable ์จ๋ณด๊ธฐ

temprmnยท2023๋…„ 7์›” 31์ผ
0
post-thumbnail

Redis๋ž€? + @Cacheable ์จ๋ณด๊ธฐ

๐Ÿ’ก ๊ฐ™์ด ๋ณด๋ฉด ์ข‹์€ ๊ธ€/์˜์ƒ

1. ๊ฐœ์š”


๐Ÿ”ป Remote Dictionary Server

  • Remote: ์›๊ฒฉ/์™ธ๋ถ€
  • Dictionary: <key-value> ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” Dictionary ์ž๋ฃŒํ˜•(Java์—์„œ๋Š” HashMap).
  • Server: ์„œ๋ฒ„

๐Ÿ’ฝ In-memory Data Structure Store

: ์ธ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ

๐Ÿค” Q. ์ธ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ญ”๊ฐ€์š”?

A. ์ธ๋ฉ”๋ชจ๋ฆฌ๋ž€, ์ปดํ“จํ„ฐ์˜ RAM(๋ฉ”๋ชจ๋ฆฌ)์— ๋ฐ์ดํ„ฐ๋ฅผ ์˜ฌ๋ ค์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋น ๋ฅธ ์†๋„๊ฐ€ ์žฅ์ ์ž…๋‹ˆ๋‹ค. SSD๋‚˜ HDD์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋ณด๋‹ค, RAM์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ์ˆ˜์‹ญ์—์„œ ์ˆ˜๋ฐฑ๋ฐฐ๋Š” ๋น ๋ฅด๋‹ค๊ณ  ํ•˜๋„ค์š”~

ํ•˜์ง€๋งŒ, RAM(๋ฉ”๋ชจ๋ฆฌ)์˜ ์šฉ๋Ÿ‰์€ SSD๋‚˜ HDD๋ณด๋‹ค ํ›จ์”ฌ ์ ๋‹ค๋Š” ์น˜๋ช…์ ์ธ ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿคทโ€โ™€๏ธ ์‚ฌ์šฉ์ฒ˜

  1. ์ธ์ฆ ํ† ํฐ ๋“ฑ์„ ์ €์žฅ (์ฃผ๋กœ String ๋˜๋Š” Hash)
  2. ๋žญํ‚น ๋ณด๋“œ๋กœ ์‚ฌ์šฉ (์ฃผ๋กœ Sorted Set)
  3. ์œ ์ € API ์ œํ•œ
  4. JobQueue (์ฃผ๋กœ List)

2. ๋ฐ์ดํ„ฐํƒ€์ž…(์ž๋ฃŒ๊ตฌ์กฐ)


๋ฐ์ดํ„ฐํƒ€์ž…์ €์žฅ ํ˜•ํƒœ
Stringkey-value
Listkey-value1, value2, โ€ฆ
Setkey-value1, value2, โ€ฆ
Sorted Setkeymembervalue
Hashkeyfieldvalue
(๊ทธ ์™ธ์— ๋” ์žˆ์Œ!)

๊ทธ ์™ธ: Bitmap, HyperLogLog, Streamโ€ฆ

3. ์บ์‹œ(Cache)


์บ์‹œ๋ž€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋‚˜ ๊ฐ’์„ ๋ฏธ๋ฆฌ ๋ณต์‚ฌํ•ด ๋†“๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

  • Redis์— ์บ์‹ฑํ•œ๋‹ค = Redis์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•ด๋‘”๋‹ค = RAM์— ์บ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ๋‹ค
  • TTL(Time To Live)์„ ์„ค์ •ํ•ด์„œ ์บ์‹œ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ช…์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

4. ์ฃผ์˜ํ•  ์ 


  1. Redis ์„œ๋ฒ„๋Š” ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ(singleย thread)๋กœ ์ˆ˜ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ช…๋ น์„ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!

    โ†’ Queue์ฒ˜๋Ÿผ ๋ช…๋ น1-๋ช…๋ น2-๋ช…๋ น3-โ€ฆ ํ˜•ํƒœ๋กœ ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•˜๋„ค์š”

    โ†’ ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N)์ธ ๋ช…๋ น์–ด ์‚ฌ์šฉ์„ ์ตœ๋Œ€ํ•œ ์ง€์–‘ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค

    • ์˜ˆ๋ฅผ ๋“ค๋ฉดโ€ฆ ๋ฐ์ดํ„ฐ ์ „์ฒด ์กฐํšŒ > keys *, ๋ฐ์ดํ„ฐ ์ „์ฒด ์‚ญ์ œ > flushall ๋“ฑ
    • ๋‹ค๋Ÿ‰์œผ๋กœ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์€ ๋˜๋„๋ก ํ”ผํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค
  • ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ์ธ๊ฒŒ ๋ฌด์กฐ๊ฑด ๋‹จ์ ์€ ์•„๋‹ˆ๊ณ โ€ฆ

    • ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๊ฐœ์˜ PC์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ Redis ์„œ๋ฒ„๋ฅผ ๋„์šฐ๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค (์žฅ์ )
      Q. ๊ทธ๋Ÿผ A, B, C. 3๊ฐœ์˜ ์„œ๋ฒ„๊ฐ€ ๋„์›Œ์ ธ์žˆ์„ ๋•Œ, ๊ฐ ์„œ๋ฒ„์—์„œ ์‹คํ–‰ํ•œ ๋ช…๋ น์ด ๋™์‹œ์— ์‹คํ–‰๋˜๋‚˜์š”?
      A. ๊ทธ๋ ‡์ง€๋Š” ์•Š์€ ๊ฑฐ ๊ฐ™์•„์š”โ€ฆ
  1. ํฌ๊ธฐ๊ฐ€ ํฐ ๋ฐ์ดํ„ฐ๋Š” ์ €์žฅํ•˜์ง€ ์•Š๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค๋ฉดโ€ฆ ์ €ํฌ๋Š” ์—”ํ‹ฐํ‹ฐ ์ž์ฒด๋ฅผ ์ €์žฅํ–ˆ์„ ๋•Œ stackoverflow ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค
    • ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅํ•˜๋„๋ก ํ•ด์š”~

5. ์บ์‹ฑํ•ด๋ณด๊ธฐ (์‹ค์Šต?)


1) ๋กœ์ปฌ์— Redis ์„ค์น˜

  1. ์„ค์น˜
    1. .msi ํŒŒ์ผ ๋‹ค์šด
    2. ์ฐธ๊ณ  โ†’ [REDIS] ๐Ÿ“š Window10 ํ™˜๊ฒฝ์— Redis ์„ค์น˜ & ์„ค์ •
  2. ๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ • ๋ฐฉ๋ฒ•
    • Redis ์„ค์น˜ ๊ฒฝ๋กœ๋กœ ๊ฐ€์„œ redis-cli ์‹คํ–‰
      redis 127.0.0.1:6379> auth {๋น„๋ฐ€๋ฒˆํ˜ธ}
      (error) ERR Client sent AUTH, but no password is set
      
      redis 127.0.0.1:6379> **CONFIG SET requirepass {๋น„๋ฐ€๋ฒˆํ˜ธ}**
      OK
      
      redis 127.0.0.1:6379> auth {๋น„๋ฐ€๋ฒˆํ˜ธ}
      OK

2) ์˜ˆ์ œ ํŒŒ์ผ

  1. ์˜ˆ์ œ ๋ ˆํฌ์ง€ํ† ๋ฆฌ
  2. ์˜ˆ์ œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ(DownGit ์‚ฌ์šฉ)
  • application.properties ์„ค์ •
    #mysql
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.properties.hibernate.show_sql=true
    spring.jpa.properties.hibernate.format_sql=true
    spring.jpa.properties.hibernate.use_sql_comments=true
    
    **spring.datasource.url=jdbc:mysql://localhost:3306/{๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค}?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
    spring.datasource.username=root**
    **spring.datasource.password={๋น„๋ฐ€๋ฒˆํ˜ธ}**
    spring.jpa.database=mysql
    
    #redis
    spring.data.redis.lettuce.pool.max-active=10
    spring.data.redis.lettuce.pool.max-idle=10
    spring.data.redis.lettuce.pool.min-idle=2
    **spring.data.redis.host={host}
    spring.data.redis.port={port}
    spring.data.redis.password={๋น„๋ฐ€๋ฒˆํ˜ธ}**
    • redis ์˜ต์…˜๋“ค (์ฐธ๊ณ ์šฉ)
      ๋ณ€์ˆ˜๊ธฐ๋ณธ๊ฐ’์„ค๋ช…
      spring.data.redis.port6379์„œ๋ฒ„ ํฌํŠธ
      spring.data.redis.hostlocalhost์„œ๋ฒ„ ํ˜ธ์ŠคํŠธ
      spring.data.redis.password์„œ๋ฒ„ ๋กœ๊ทธ์ธ ํŒจ์Šค์›Œ๋“œ
      spring.data.redis.pool.max-active8pool์— ํ• ๋‹น๋  ์ˆ˜ ์žˆ๋Š” ์ปค๋„ฅ์…˜ ์ตœ๋Œ€์ˆ˜ (์Œ์ˆ˜๋กœ ํ•˜๋ฉด ๋ฌด์ œํ•œ)
      spring.data.redis.pool.max-idle8pool์˜ "idle" ์ปค๋„ฅ์…˜ ์ตœ๋Œ€์ˆ˜ (์Œ์ˆ˜๋กœ ํ•˜๋ฉด ๋ฌด์ œํ•œ)
      spring.data.redis.pool.min-idle0ํ’€์—์„œ ๊ด€๋ฆฌํ•˜๋Š” idle ์ปค๋„ฅ์…˜์˜ ์ต€์†Œ์ˆ˜ ๋Œ€์ƒ (์–‘์ˆ˜์ผ ๋•Œ๋งŒ ์œ ํšจ)

      ์ด๊ฑฐ ๋ง๊ณ ๋„ ๋” ์žˆ๋‹ค๊ณ  ํ•˜๋„ค์š”~

3) ๋ฌด์—‡์ด ์ถ”๊ฐ€๋˜์—ˆ๋Š”๊ฐ€

  1. build.gradle
  2. Config
    • RedisConfig
    • RedisCacheConfig
  3. @Cacheable annotation ์ถ”๊ฐ€

4) ํ…Œ์ŠคํŠธ ํ™”๋ฉด

  • Postman
  • redis-cli ๋ฐ Redis Insight
profile
`ISFJ` T 49% F 51% /

0๊ฐœ์˜ ๋Œ“๊ธ€