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
- scalability (Scaling)
fast performanceย ยป real-time analytics, chat/messaging, media streaming, and pub/sub apps.
- https://redis.com/blog/redis-cache-vs-redis-primary-database-in-90-seconds/#:~:text=Redis%20began%20as%20a%20caching,not%20as%20a%20primary%20database.
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
- a Redis client of Java that is fully non-blocking.
- It supports both synchronous and asynchronous communication.
- scalable
- thread-safe
- https://www.baeldung.com/java-redis-lettuce
Jedis VS Lettuce
- commonality : Redis์ ์ ๊ทผํ๊ธฐ ์ํด์ ์ฌ์ฉํ๋ Spring Data Redis Library (์ฑ๋ฅ๊ณผ ์ฌ์ฉ์ฑ์ ์ํด ๋ง๋ค์ด์ง ํด๋ผ์ด์ธํธ)
- Jedis : deprecated
- ๋๊ธฐ ๋ฐฉ์์ผ๋ก ์๋ํ์ฌ Blocking ์ด์๊ฐ ๋ฐ์ ๊ฐ๋ฅํ๋ค๋ ๋จ์ ์ด ์์ต๋๋ค.
- Lettuce
- Lettuce ์ญ์ ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ฉฐ ๋๊ธฐ, ๋น๋๊ธฐ ๋ฐฉ์์ ๋ ๋ค ์ง์ํ์ฌ non-blockingํ๊ฒ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๊ณ ํ์ฅ์ฑ์ด ๋ฐ์ด๋๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค. ํ์ง๋ง ์ฌ์ฉ์ฑ์ด Jedis์ ๋นํด ์ด๋ ต๋ค๋ ๋จ์ ์ด ์์ต๋๋ค.
** ๋น ๋ฅด๊ฒ ์ฝ๊ฒ ๊ฐ๋ฐํ ํ์์ฑ๋ณด๋ค ์ถํ ๋ณ๊ฒฝ ์ฌํญ ๋ฐ ํ๋ ๋ฑ์ ๋๋นํ์ฌ ํ์ฅ์ฑ์ด ๋์ผ๋ฉฐ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํตํด ์์ ์ ์ผ๋ก ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๋ Lettuce๊ฐ ๋ ์ ํฉ
- https://abbo.tistory.com/107
- https://velog.io/@gale4739/Spring-Boot-Redis-%EC%A0%81%EC%9A%A9%EA%B8%B0-With-lettuce
- https://redis.com/blog/jedis-vs-lettuce-an-exploration/#:~:text=What%20is%20Lettuce%3F,Sentinel%2C%20Pipelining%2C%20and%20codecs.
- non-blocking VS blocking
- pooling
- max-acitve : Maximum active connections to Redis instance
- maxidle : the max number of connections that can be idle in the pool without being quickly closed (the default value is 8)
- minIdle : This is the number of connections that are ready for immediate use.
- https://medium.com/geekculture/the-pooling-of-connections-in-redis-e8188335bf64#:~:text=maxIdle%3A%20This%20is%20the%20max,a%20short%20period%20of%20time.
max-active VS max-idle
- max-active : the maximum number of active connections that can be allocated from this pool at the same time.
- max-idle : The maximum number of connections that should be kept in the pool at all times.
- https://stackoverflow.com/questions/9451818/what-is-the-difference-between-maxactive-vs-maxidle-for-tomcat-connection-pools
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
- @Cacheable
- ์บ์๊ฐ ์์ผ๋ฉด ์บ์์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์์ผ๋ฉด ๋ฑ๋กํ๋ค.
- @CacheEvict
- ์บ์์ญ์
- ๋จผ์ Evict ์ ๋ํด์๋ Cache ์์ฒด๋ฅผ ์ง์ด๋ค๋ ์๋ฏธ๋ณด๋ค๋, ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํด์ ๋ ์ด์ ์บ์ํ ์ ์์ ๋, ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ๋ณดํ๊ธฐ ์ํด์, ๊ธฐ์กด ์บ์๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ฐ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
- https://charsyam.wordpress.com/2022/04/18/%EC%9E%85-%EA%B0%9C%EB%B0%9C-spring-%EC%9D%98-cacheevict-%EC%97%90%EC%84%9C-allentriestrue-%EB%8A%94-redis%EC%97%90%EC%84%9C-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%8F%99%EC%9E%91%ED%95%98%EA%B2%8C/
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
- Sharding, also known as partitioning, is splitting the data up by key; While replication, also known as mirroring, is to copy all data. Sharding is useful to increase performance, reducing the hit and memory load on any one resource. Replication is useful for getting a high availability of reads.
- Sharding is used to scale Writes while Replication is used to scale Reads.
Redis Redis รluster
- Redis scales horizontally with a deployment topology called Redis Cluster.
- Advantages
- High Performance
- High Availability
- Horizontal & Vertical Scalability
- Native Solution
- https://scalegrid.io/blog/intro-to-redis-cluster-sharding-advantages-limitations-deploying-and-client-connections/
Comments powered by Disqus.