๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

Spring Data Redis์˜ @Indexed ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์ 

by dkswnkk 2023. 8. 7.

@Indexed๋ž€?

@Indexed๋Š” Spring Data Redis ๋ชจ๋“ˆ์˜ ์ฃผ์š” ์–ด๋…ธํ…Œ์ด์…˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. ์ฃผ ๋ชฉ์ ์œผ๋กœ๋Š” Redis์˜ ๋ณด์กฐ ์ธ๋ฑ์Šค(Secondary Index) ์ƒ์„ฑ์— ์‚ฌ์šฉ๋˜๋ฉฐ,  @Id๊ฐ€ ๋ถ™์—ฌ์ง„ ๊ฐ์ฒด ์™ธ์—๋„ @Indexed๊ฐ€ ๋ถ™์—ฌ์ง„ ๊ฐ์ฒด๋กœ๋„ ๊ฐ’์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ๋Š” id๋ฟ๋งŒ ์•„๋‹ˆ๋ผ name์œผ๋กœ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@RedisHash("Person")
public class Person {

    @Id
    private String id;
    
    @Indexed
    private String name;
    
    private int age;
}

 

์˜ˆ์‹œ

ํ•œ๋ฒˆ Person("dkswnkk", "JuHyeong", 26)์„ ์ €์žฅํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

@Indexed ์—†์ด ์ €์žฅํ•œ ๊ฒฝ์šฐ

@RedisHash("Person")
public class Person {

    @Id
    private String id;
    
    private String name;
    
    private int age;
}

Redis ๋กœ๊ทธ

"HMSET" "Person:dkswnkk" "_class" "com.example.test.Person" "age" "26" "id" "dkswnkk" "name" "JuHyeong"

 

@Indexed๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•œ ๊ฒฝ์šฐ

@RedisHash("Person")
public class Person {

    @Id
    private String id;
    
    @Indexed
    private String name;
    
    private int age;
}

Redis ๋กœ๊ทธ

"HMSET" "Person:dkswnkk" "_class" "com.example.test.Person" "age" "26" "id" "dkswnkk" "name" "JuHyeong"
"SADD" "Person:name:JuHyeong" "dkswnkk"
"SADD" "Person:dkswnkk:idx" "Person:name:JuHyeong"

๋กœ๊ทธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ, "Person:name:JuHyeong" "dkswnkk"๋ฅผ ๋ณด๋ฉด ๋ณด์กฐ ์ธ๋ฑ์Šค๊ฐ€ ์‹ค์ œ๋กœ ์ €์žฅ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ , Redis์—์„œ ๊ฐ์ฒด์˜ CRUD ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ด€๋ จ๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค๋„ ํ•จ๊ป˜ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•˜๊ธฐ์— ์ถ”๊ฐ€์ ์ธ Redis ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
 

์ฃผ์˜์ 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ Time to Live(TTL) ์„ค์ • ์‹œ์ž…๋‹ˆ๋‹ค. Redis์—์„œ TTL์€ ํŠน์ • ํ‚ค์— ๋Œ€ํ•œ ๋งŒ๋ฃŒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ์„ค์ •๋œ ์‹œ๊ฐ„์ด ์ง€๋‚˜๋ฉด ํ•ด๋‹น ํ‚ค์™€ ์—ฐ๊ด€๋œ ๋ฐ์ดํ„ฐ๋Š” ์ž๋™์œผ๋กœ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ @Indexed๋กœ ์ƒ์„ฑ๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ TTL์ด ๋งŒ๋ฃŒ๋˜๋”๋ผ๋„ ์ž๋™์œผ๋กœ ์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋•Œ๋ฌธ์— ์ด๋ฅผ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์‚ญ์ œํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ๋ถˆํ•„์š”ํ•œ ๋ณด์กฐ ์ธ๋ฑ์Šค ๋ฐ์ดํ„ฐ๊ฐ€ Redis์— ๊ณ„์† ๋‚จ๊ฒŒ ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„์™€ ์ž˜๋ชป๋œ ์กฐํšŒ ๊ฒฐ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

[Person ๊ฐ์ฒด ์ €์žฅ]

"HMSET" "Person:dkswnkk" "_class" "com.example.test.Person" "age" "26" "id" "dkswnkk" "name" "JuHyeong"
"SADD" "Person:name:JuHyeong" "dkswnkk"
"SADD" "Person:dkswnkk:idx" "Person:name:JuHyeong"

 
[TTL 10์ดˆ๋กœ ์„ค์ •]

"EXPIRE" "Person:dkswnkk" "10"

 
[10์ดˆ ํ›„ ํ™•์ธ]

"Person:name:JuHyeong"
"Person:dkswnkk:idx"

๋กœ๊ทธ๋ฅผ ๋ณด๋ฉด 10์ดˆ ํ›„, "Person:dkswnkk" ๋ฐ์ดํ„ฐ๋Š” ์ •์ƒ์ ์œผ๋กœ ์‚ญ์ œ๋˜์—ˆ์ง€๋งŒ, "Person:name:JuHyeong"์™€ "Person:dkswnkk:idx"์— ๊ด€๋ จ๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค ๊ฐ’๋“ค์€ ์—ฌ์ „ํžˆ ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์›์ธ์€ @Indexed ๊ธฐ๋Šฅ์ด Redis์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์•„๋‹ˆ๋ผ, Spring Data Redis์—์„œ ์ œ๊ณตํ•˜๋Š” ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Redis์˜ TTL์ด ๋งŒ๋ฃŒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์—, Spring Data Redis์˜ @Indexed๋กœ ์ƒ์„ฑ๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค๊นŒ์ง€ ์‚ญ์ œํ•˜๋Š” ๊ณผ์ •์ด ํฌํ•จ๋˜์–ด์žˆ์ง€ ์•Š์•„ ์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
 

ํ•ด๊ฒฐ๋ฐฉ์•ˆ

Redis์˜ Key Space Notifications ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ฉด, TTL์ด ๋งŒ๋ฃŒ๋˜๋Š” ์‹œ์ ์— ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ , ๋ณด์กฐ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class RedisConfig {
     //... Configuration details
}

์œ„์™€ ๊ฐ™์ด @EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)๋ฅผ Redis Config ๋ถ€๋ถ„์— ๋ช…์‹œํ•ด ์ฃผ๋ฉด Redis์˜ Key Space Notifications ๊ธฐ๋Šฅ์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.

Spring Data Redis๋Š” ์ด Key Space Notifications ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ TTL ๋งŒ๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ , ์ด๋ฅผ ํ†ตํ•ด @Indexed๋กœ ์ง€์ •๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค๋ฅผ ํ•จ๊ป˜ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. 

๋™์ž‘ ์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. enableKeyspaceEvents ์˜ต์…˜์ด ON_STARTUP์œผ๋กœ ์„ค์ •๋˜๋ฉด, Spring Data Redis๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ Redis์˜ Key Space Notifications ๊ธฐ๋Šฅ์„ ํ™œ์„ฑํ™”์‹œํ‚จ๋‹ค.
  2. ์ดํ›„, Redis์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ TTL์ด ๋งŒ๋ฃŒ๋˜๋ฉด, Redis๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ด์— ๊ด€ํ•œ ์•Œ๋ฆผ์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
  3. Spring Data Redis๋Š” ์ด ์•Œ๋ฆผ์„ ๊ฐ์ง€ํ•˜๊ณ , ํ•ด๋‹น ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ๋ณด์กฐ ์ธ๋ฑ์Šค(์ฆ‰, @Indexed๊ฐ€ ๋ถ™์€ ๊ฐ์ฒด)๋ฅผ ์ž๋™์œผ๋กœ ์‚ญ์ œํ•œ๋‹ค.

 

์ฐธ๊ณ 

๋Œ“๊ธ€