Framework/Springboot

[Springboot] mongodb read preference ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2022. 11. 15. 22:50
๋ฐ˜์‘ํ˜•

Intro

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” spring boot ์—์„œ mongodb read preference๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ œ ๊ฒฝ์šฐ Spring boot ๋กœ ๊ตฌ์„ฑ๋œ ํ”„๋กœ์ ํŠธ์— monogo db read preference ๊ฐ€ ๋”ฐ๋กœ ์ ์šฉ๋˜์–ด ์žˆ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ read preference mode ์ค‘์—์„œ secondaryPreferred ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. 

 

 

Read preference

์ฐธ๊ณ ๋กœ, Read preference๋ž€ MongoDB ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฝ๊ธฐ ์ž‘์—…์„ replica set์˜ members์—๊ฒŒ route ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด 5๊ฐ€์ง€์˜ ๋ฐฉ์‹์ด ์žˆ์œผ๋ฉฐ, primary๊ฐ€ ๊ธฐ๋ณธ default ๊ฐ’์œผ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. 

  • primary (default mode)
  • primaryPreferred
  • secondary
  • secondaryPreferred
  • nearest

 

How to solve the problem

MongoTemplate template = new MongoTemplate(...);
template.setReadPreference(ReadPreference.secondaryPreferred())

๋‹ค์Œ์˜ ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” read preference๋ฅผ ์ ์šฉํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 


https://www.mongodb.com/docs/manual/core/read-preference/

https://stackoverflow.com/questions/19555987/how-to-set-mongodb-readpreference-in-spring-mvcs-contextconfiglocation

 

๋ฐ˜์‘ํ˜•