From 0c4c4239ec58ec6b4bc84d3efb0aa4f2d00f2de5 Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Mon, 9 Nov 2026 16:30:12 +0900 Subject: [PATCH] fix(redis): share Garnet endpoint and update database versions --- README.md | 9 +- .../auction/redis/auction-redis.module.ts | 58 +---------- .../modules/global/redis/redis.config.spec.ts | 54 +++++----- .../src/modules/global/redis/redis.config.ts | 57 ++++------- .../modules/global/redis/redis.module.spec.ts | 99 +++++++++++++++++++ .../src/modules/global/redis/redis.module.ts | 37 +------ .../src/scripts/auction-redis-rehearsal.ts | 74 +------------- 7 files changed, 157 insertions(+), 231 deletions(-) create mode 100644 apps/api/src/modules/global/redis/redis.module.spec.ts diff --git a/README.md b/README.md index 83acefd7..29f59f7e 100644 --- a/README.md +++ b/README.md @@ -94,9 +94,8 @@ LOLYMPUS는 이 반복 작업을 줄이는 데 초점을 맞췄습니다. 운영 - TypeScript - TanStack Query - NestJS -- MongoDB -- Redis -- Redis 8.4 (경매 실시간 상태 전용) +- MongoDB 8.3 +- Redis 8.10 (일반 캐시/스트림과 경매 실시간 상태 공유) - NATS - Riot API - pnpm workspace @@ -118,9 +117,9 @@ scripts/ deployment and maintenance scripts - Node.js 24 이상 - pnpm 10.4.1 (`packageManager` 기준, `corepack enable`로 활성화 권장) -- MongoDB 7.0, 일반 캐시/스트림용 Redis, 경매 실시간 상태 전용 Redis 8.4, NATS 실행 환경 +- MongoDB 8.3, Redis 8.10, NATS 실행 환경 -경매 Redis는 라이브 경매 상태의 source of truth로 사용되므로 일반 캐시/스트림 Redis와 물리적으로 분리해야 합니다. API는 `REDIS_URL`/`REDIS_ENDPOINT`와 별도로 `AUCTION_REDIS_URL`/`AUCTION_REDIS_ENDPOINT`를 읽고, 두 값이 같은 호스트/포트를 가리키면 부팅을 거부합니다. +일반 캐시/스트림, 경매 실시간 상태, WebSocket Pub/Sub는 `GARNET_ENDPOINT`로 지정한 하나의 Redis 인스턴스를 공유합니다. 값은 `host:port`, `redis://...`, `rediss://...` 형식을 지원합니다. 인증이 필요하면 기존 `GARNET_USERID`(또는 `GARNET_USERNAME`)와 `GARNET_PASSWORD`를 설정합니다. `REDIS_*`, `AUCTION_REDIS_*`, `GARNET_URL` 연결 설정은 사용하지 않습니다. 경매 점검 스크립트도 같은 설정을 사용합니다. ## 실행 방법 diff --git a/apps/api/src/modules/auction/redis/auction-redis.module.ts b/apps/api/src/modules/auction/redis/auction-redis.module.ts index 5623f363..44a77b08 100644 --- a/apps/api/src/modules/auction/redis/auction-redis.module.ts +++ b/apps/api/src/modules/auction/redis/auction-redis.module.ts @@ -2,10 +2,7 @@ import { Logger, Module } from '@nestjs/common'; import { createClient } from 'redis'; import { SecretModule } from '../../global/secret/secret.module'; import { SecretService } from '../../global/secret/secret.service'; -import { - assertDistinctRedisInstances, - resolveRedisClientOptions, -} from '../../global/redis/redis.config'; +import { loadRedisClientOptions } from '../../global/redis/redis.config'; import { AUCTION_REDIS_CLIENT } from './auction-redis.constants'; import { AuctionRedisService, @@ -14,18 +11,6 @@ import { import { AuctionRedisCommandService } from './auction-redis-command.service'; import { AuctionRedisStateReaderService } from './auction-redis-state-reader.service'; -async function readFirstSecret( - secretService: SecretService, - keys: string[], -): Promise { - for (const key of keys) { - const value = await secretService.get(key, ''); - const trimmed = value.trim(); - if (trimmed) return trimmed; - } - return undefined; -} - @Module({ imports: [SecretModule], providers: [ @@ -35,45 +20,10 @@ async function readFirstSecret( useFactory: async ( secretService: SecretService, ): Promise => { - await secretService.ready(); - - const [ - redisUrl, - redisEndpoint, - auctionRedisUrl, - auctionRedisEndpoint, - auctionRedisUsername, - auctionRedisPassword, - ] = await Promise.all([ - readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']), - readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']), - readFirstSecret(secretService, ['AUCTION_REDIS_URL']), - readFirstSecret(secretService, ['AUCTION_REDIS_ENDPOINT']), - readFirstSecret(secretService, [ - 'AUCTION_REDIS_USERNAME', - 'AUCTION_REDIS_USERID', - ]), - readFirstSecret(secretService, ['AUCTION_REDIS_PASSWORD']), - ]); - - const redisOptions = resolveRedisClientOptions({ - endpoint: redisEndpoint, - endpointEnvName: 'REDIS_ENDPOINT', - url: redisUrl, - urlEnvName: 'REDIS_URL', - }); - const auctionRedisOptions = resolveRedisClientOptions({ - endpoint: auctionRedisEndpoint, - endpointEnvName: 'AUCTION_REDIS_ENDPOINT', - password: auctionRedisPassword, - url: auctionRedisUrl, - urlEnvName: 'AUCTION_REDIS_URL', - username: auctionRedisUsername, - }); - assertDistinctRedisInstances(redisOptions.url, auctionRedisOptions.url); - const logger = new Logger('AuctionRedisClient'); - const client = createClient(auctionRedisOptions); + const client = createClient( + await loadRedisClientOptions(secretService), + ); client.on('error', (error: unknown) => { logger.error( `Auction Redis client error: ${ diff --git a/apps/api/src/modules/global/redis/redis.config.spec.ts b/apps/api/src/modules/global/redis/redis.config.spec.ts index a3e50e66..441e356b 100644 --- a/apps/api/src/modules/global/redis/redis.config.spec.ts +++ b/apps/api/src/modules/global/redis/redis.config.spec.ts @@ -1,12 +1,8 @@ -import { - assertDistinctRedisInstances, - normalizeRedisUrl, - resolveRedisClientOptions, -} from './redis.config'; +import { normalizeRedisUrl, resolveRedisClientOptions } from './redis.config'; describe('redis.config', () => { it('normalizes host:port endpoints into redis URLs', () => { - expect(normalizeRedisUrl('127.0.0.1:6379', 'REDIS_ENDPOINT')).toBe( + expect(normalizeRedisUrl(' 127.0.0.1:6379 ')).toBe( 'redis://127.0.0.1:6379', ); expect(normalizeRedisUrl('rediss://cache.example.com:6380')).toBe( @@ -15,8 +11,8 @@ describe('redis.config', () => { }); it('throws a clear error for empty endpoints', () => { - expect(() => normalizeRedisUrl(' ', 'REDIS_ENDPOINT')).toThrow( - 'REDIS_ENDPOINT cannot be empty', + expect(() => normalizeRedisUrl(' ')).toThrow( + 'GARNET_ENDPOINT cannot be empty', ); }); @@ -26,34 +22,32 @@ describe('redis.config', () => { endpoint: 'redis.internal:6379', username: '', password: undefined, - endpointEnvName: 'REDIS_ENDPOINT', }), ).toEqual({ url: 'redis://redis.internal:6379', }); }); - it('rejects auction Redis when it points at the same physical Redis instance', () => { - expect(() => - assertDistinctRedisInstances( - 'redis://cache.internal:6379/0', - 'redis://cache.internal:6379/1', - ), - ).toThrow('AUCTION_REDIS must use a separate Redis instance'); - }); + it.each([undefined, '', ' '])( + 'rejects a missing or blank endpoint: %p', + (endpoint) => { + expect(() => resolveRedisClientOptions({ endpoint })).toThrow( + 'GARNET_ENDPOINT cannot be empty', + ); + }, + ); - it('allows different Redis hosts or ports', () => { - expect(() => - assertDistinctRedisInstances( - 'redis://cache.internal:6379', - 'redis://auction.internal:6379', - ), - ).not.toThrow(); - expect(() => - assertDistinctRedisInstances( - 'redis://cache.internal:6379', - 'redis://cache.internal:6380', - ), - ).not.toThrow(); + it('preserves TLS, database selection, and credentials', () => { + expect( + resolveRedisClientOptions({ + endpoint: ' rediss://garnet.internal:6380/1 ', + username: ' garnet-user ', + password: ' garnet-password ', + }), + ).toEqual({ + url: 'rediss://garnet.internal:6380/1', + username: 'garnet-user', + password: 'garnet-password', + }); }); }); diff --git a/apps/api/src/modules/global/redis/redis.config.ts b/apps/api/src/modules/global/redis/redis.config.ts index e98a9c8c..81c329de 100644 --- a/apps/api/src/modules/global/redis/redis.config.ts +++ b/apps/api/src/modules/global/redis/redis.config.ts @@ -1,3 +1,5 @@ +import type { SecretService } from '../secret/secret.service'; + export interface RedisClientOptions { password?: string; url: string; @@ -6,10 +8,7 @@ export interface RedisClientOptions { export interface ResolveRedisClientOptionsParams { endpoint?: string; - endpointEnvName?: string; password?: string; - url?: string; - urlEnvName?: string; username?: string; } @@ -18,13 +17,10 @@ function trimOptional(value: string | undefined): string | undefined { return trimmed ? trimmed : undefined; } -export function normalizeRedisUrl( - endpointOrUrl: string, - envName = 'REDIS_URL', -): string { +export function normalizeRedisUrl(endpointOrUrl: string): string { const trimmed = endpointOrUrl.trim(); if (!trimmed) { - throw new Error(`${envName} cannot be empty`); + throw new Error('GARNET_ENDPOINT cannot be empty'); } if (/^redis(s)?:\/\//iu.test(trimmed)) { return trimmed; @@ -35,19 +31,8 @@ export function normalizeRedisUrl( export function resolveRedisClientOptions( params: ResolveRedisClientOptionsParams, ): RedisClientOptions { - const url = trimOptional(params.url); - const endpoint = trimOptional(params.endpoint); - const selectedEndpoint = url ?? endpoint; - const selectedEnvName = url - ? (params.urlEnvName ?? 'REDIS_URL') - : (params.endpointEnvName ?? 'REDIS_ENDPOINT'); - - if (!selectedEndpoint) { - throw new Error(`${selectedEnvName} cannot be empty`); - } - const options: RedisClientOptions = { - url: normalizeRedisUrl(selectedEndpoint, selectedEnvName), + url: normalizeRedisUrl(params.endpoint ?? ''), }; const username = trimOptional(params.username); const password = trimOptional(params.password); @@ -56,22 +41,20 @@ export function resolveRedisClientOptions( return options; } -function parseRedisInstanceIdentity(redisUrl: string): string { - const url = new URL(normalizeRedisUrl(redisUrl)); - const hostname = url.hostname.toLowerCase(); - const port = url.port || '6379'; - return `${hostname}:${port}`; -} +export async function loadRedisClientOptions( + secretService: Pick, +): Promise { + await secretService.ready(); + const [endpoint, username, userId, password] = await Promise.all([ + secretService.get('GARNET_ENDPOINT', ''), + secretService.get('GARNET_USERNAME', ''), + secretService.get('GARNET_USERID', ''), + secretService.get('GARNET_PASSWORD', ''), + ]); -export function assertDistinctRedisInstances( - redisUrl: string, - auctionRedisUrl: string, -): void { - const redisIdentity = parseRedisInstanceIdentity(redisUrl); - const auctionRedisIdentity = parseRedisInstanceIdentity(auctionRedisUrl); - if (redisIdentity === auctionRedisIdentity) { - throw new Error( - 'AUCTION_REDIS must use a separate Redis instance from REDIS because auction Redis is a live state source of truth.', - ); - } + return resolveRedisClientOptions({ + endpoint, + password, + username: trimOptional(username) ?? userId, + }); } diff --git a/apps/api/src/modules/global/redis/redis.module.spec.ts b/apps/api/src/modules/global/redis/redis.module.spec.ts new file mode 100644 index 00000000..35b4fd43 --- /dev/null +++ b/apps/api/src/modules/global/redis/redis.module.spec.ts @@ -0,0 +1,99 @@ +import { Test } from '@nestjs/testing'; +import { createClient } from 'redis'; +import { AuctionRedisModule } from '../../auction/redis/auction-redis.module'; +import { AuctionRedisService } from '../../auction/redis/auction-redis.service'; +import { SecretService } from '../secret/secret.service'; +import { RedisModule } from './redis.module'; +import { RedisService, type RedisClient } from './redis.service'; + +jest.mock('redis', () => ({ createClient: jest.fn() })); + +describe('shared Garnet instance', () => { + it.each([ + { + name: 'only GARNET_ENDPOINT is configured', + secrets: { GARNET_ENDPOINT: 'garnet.internal:6379' }, + options: { url: 'redis://garnet.internal:6379' }, + }, + { + name: 'existing Garnet credentials are configured', + secrets: { + GARNET_ENDPOINT: ' rediss://garnet.internal:6380/1 ', + GARNET_USERID: 'garnet-user', + GARNET_PASSWORD: 'garnet-password', + }, + options: { + url: 'rediss://garnet.internal:6380/1', + username: 'garnet-user', + password: 'garnet-password', + }, + }, + { + name: 'obsolete Redis settings are also configured', + secrets: { + GARNET_ENDPOINT: 'garnet.internal:6379', + GARNET_USERNAME: 'garnet-user', + GARNET_PASSWORD: 'garnet-password', + GARNET_URL: 'redis://obsolete-garnet.internal:6379', + REDIS_URL: 'redis://obsolete-common.internal:6379', + REDIS_ENDPOINT: 'obsolete-common.internal:6379', + REDIS_USERNAME: 'obsolete-common-user', + REDIS_PASSWORD: 'obsolete-common-password', + AUCTION_REDIS_URL: 'redis://obsolete-common.internal:6379', + AUCTION_REDIS_ENDPOINT: 'obsolete-common.internal:6379', + AUCTION_REDIS_USERNAME: 'obsolete-auction-user', + AUCTION_REDIS_PASSWORD: 'obsolete-auction-password', + }, + options: { + url: 'redis://garnet.internal:6379', + username: 'garnet-user', + password: 'garnet-password', + }, + }, + ])('boots both Redis modules when $name', async ({ secrets, options }) => { + const values: Record = secrets; + const secretService = { + ready: jest.fn().mockResolvedValue(undefined), + get: jest.fn((key: string, fallback: string) => + Promise.resolve(values[key] ?? fallback), + ), + }; + const clients = Array.from({ length: 2 }, () => ({ + connect: jest.fn().mockResolvedValue(undefined), + on: jest.fn(), + quit: jest.fn().mockResolvedValue(undefined), + destroy: jest.fn(), + })); + const createClientMock = jest.mocked(createClient); + createClientMock.mockReset(); + for (const client of clients) { + createClientMock.mockReturnValueOnce(client as unknown as RedisClient); + } + + const module = await Test.createTestingModule({ + imports: [RedisModule, AuctionRedisModule], + }) + .overrideProvider(SecretService) + .useValue(secretService) + .compile(); + + try { + await module.init(); + expect(createClientMock).toHaveBeenCalledTimes(2); + for (let index = 0; index < clients.length; index++) { + expect(createClientMock).toHaveBeenNthCalledWith(index + 1, options); + expect(clients[index].connect).toHaveBeenCalledTimes(1); + } + expect(clients).toContain(module.get(RedisService).raw()); + expect(clients).toContain(module.get(AuctionRedisService).raw()); + expect(module.get(RedisService).raw()).not.toBe( + module.get(AuctionRedisService).raw(), + ); + } finally { + await module.close(); + } + for (const client of clients) { + expect(client.quit).toHaveBeenCalledTimes(1); + } + }); +}); diff --git a/apps/api/src/modules/global/redis/redis.module.ts b/apps/api/src/modules/global/redis/redis.module.ts index 805985f8..99c5b863 100644 --- a/apps/api/src/modules/global/redis/redis.module.ts +++ b/apps/api/src/modules/global/redis/redis.module.ts @@ -3,22 +3,10 @@ import { createClient } from 'redis'; import { SecretModule } from '../secret/secret.module'; import { SecretService } from '../secret/secret.service'; import { REDIS_CLIENT } from './redis.constants'; -import { resolveRedisClientOptions } from './redis.config'; +import { loadRedisClientOptions } from './redis.config'; import { DistributedLockService } from './distributed-lock.service'; import { RedisService, type RedisClient } from './redis.service'; -async function readFirstSecret( - secretService: SecretService, - keys: string[], -): Promise { - for (const key of keys) { - const value = await secretService.get(key, ''); - const trimmed = value.trim(); - if (trimmed) return trimmed; - } - return undefined; -} - @Module({ imports: [SecretModule], providers: [ @@ -28,30 +16,9 @@ async function readFirstSecret( useFactory: async ( secretService: SecretService, ): Promise => { - await secretService.ready(); - - const [url, endpoint, username, password] = await Promise.all([ - readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']), - readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']), - readFirstSecret(secretService, [ - 'REDIS_USERNAME', - 'REDIS_USERID', - 'GARNET_USERNAME', - 'GARNET_USERID', - ]), - readFirstSecret(secretService, ['REDIS_PASSWORD', 'GARNET_PASSWORD']), - ]); - const logger = new Logger('RedisClient'); const client = createClient( - resolveRedisClientOptions({ - endpoint, - endpointEnvName: 'REDIS_ENDPOINT', - password, - url, - urlEnvName: 'REDIS_URL', - username, - }), + await loadRedisClientOptions(secretService), ); client.on('error', (error: unknown) => { diff --git a/apps/api/src/scripts/auction-redis-rehearsal.ts b/apps/api/src/scripts/auction-redis-rehearsal.ts index 61f2edab..026b6f15 100644 --- a/apps/api/src/scripts/auction-redis-rehearsal.ts +++ b/apps/api/src/scripts/auction-redis-rehearsal.ts @@ -15,10 +15,7 @@ import { ConnectionService, resolveMongoDbName, } from '../modules/global/database/connection.service'; -import { - assertDistinctRedisInstances, - resolveRedisClientOptions, -} from '../modules/global/redis/redis.config'; +import { loadRedisClientOptions } from '../modules/global/redis/redis.config'; import { SecretService } from '../modules/global/secret/secret.service'; type RedisClient = ReturnType; @@ -59,18 +56,6 @@ function loadProjectEnv(): void { } } -async function readFirstSecret( - secretService: SecretService, - keys: string[], -): Promise { - for (const key of keys) { - const value = await secretService.get(key, ''); - const trimmed = value.trim(); - if (trimmed) return trimmed; - } - return undefined; -} - function requireSafeNodeEnv(): void { if ( process.env.NODE_ENV === 'prod' && @@ -90,37 +75,7 @@ async function createRedisClientFromSecrets( secretService: SecretService, kind: 'common' | 'auction', ): Promise { - const prefix = kind === 'auction' ? 'AUCTION_REDIS' : 'REDIS'; - const urlKeys = - kind === 'auction' ? [`${prefix}_URL`] : ['REDIS_URL', 'GARNET_URL']; - const endpointKeys = - kind === 'auction' - ? [`${prefix}_ENDPOINT`] - : ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']; - const usernameKeys = - kind === 'auction' - ? [`${prefix}_USERNAME`, `${prefix}_USERID`] - : ['REDIS_USERNAME', 'REDIS_USERID', 'GARNET_USERNAME', 'GARNET_USERID']; - const passwordKeys = - kind === 'auction' - ? [`${prefix}_PASSWORD`] - : ['REDIS_PASSWORD', 'GARNET_PASSWORD']; - const [url, endpoint, username, password] = await Promise.all([ - readFirstSecret(secretService, urlKeys), - readFirstSecret(secretService, endpointKeys), - readFirstSecret(secretService, usernameKeys), - readFirstSecret(secretService, passwordKeys), - ]); - const client = createClient( - resolveRedisClientOptions({ - endpoint, - endpointEnvName: `${prefix}_ENDPOINT`, - password, - url, - urlEnvName: `${prefix}_URL`, - username, - }), - ); + const client = createClient(await loadRedisClientOptions(secretService)); client.on('error', (error) => { console.error(`[auction-rehearsal] ${kind} Redis error:`, error); }); @@ -128,31 +83,10 @@ async function createRedisClientFromSecrets( return client; } -async function assertRedisTopology(secretService: SecretService): Promise<{ +async function connectRedisClients(secretService: SecretService): Promise<{ auctionRedisClient: RedisClient; commonRedisClient: RedisClient; }> { - const [redisUrl, redisEndpoint, auctionRedisUrl, auctionRedisEndpoint] = - await Promise.all([ - readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']), - readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']), - readFirstSecret(secretService, ['AUCTION_REDIS_URL']), - readFirstSecret(secretService, ['AUCTION_REDIS_ENDPOINT']), - ]); - const redisOptions = resolveRedisClientOptions({ - endpoint: redisEndpoint, - endpointEnvName: 'REDIS_ENDPOINT', - url: redisUrl, - urlEnvName: 'REDIS_URL', - }); - const auctionRedisOptions = resolveRedisClientOptions({ - endpoint: auctionRedisEndpoint, - endpointEnvName: 'AUCTION_REDIS_ENDPOINT', - url: auctionRedisUrl, - urlEnvName: 'AUCTION_REDIS_URL', - }); - assertDistinctRedisInstances(redisOptions.url, auctionRedisOptions.url); - const [commonRedisClient, auctionRedisClient] = await Promise.all([ createRedisClientFromSecrets(secretService, 'common'), createRedisClientFromSecrets(secretService, 'auction'), @@ -409,7 +343,7 @@ async function main(): Promise { ); const { auctionRedisClient, commonRedisClient } = - await assertRedisTopology(secretService); + await connectRedisClients(secretService); const connectionService = new ConnectionService(secretService); const auctionRedisService = new AuctionRedisService(auctionRedisClient); const auctionRedisCommandService = new AuctionRedisCommandService(