Redis의 Lua 런타임은 애초에 싱글스레드로, 동시적인 요청으로 인한 동시성 문제는 일어나지 않는다고 봐도 된다.
다만, 기본적인 Lua 사용법으로 redis.call('GET'
, redis.call('SET'
을 써서 좌석 배열을 수정하려고 하면, 배열의 길이가 너무 긴데 항상 배열 전체를 수정하고 반영하니 Redis 자체의 처리 속도가 늦어지고 응답이 밀릴 수도 있다는 우려가 생긴다.
예시:
local function updateSeat(eventId, row, col, value)
local seats = redis.call('GET', 'event:' .. eventId)
seats[row][col] = value
return redis.call('SET', 'event:' .. eventId, seats)
end
이에 대해 redis.call('SETBIT'
메서드를 사용하여 언제나 비트 하나만을 수정하고 반영하도록 변경할 수 있다. 이를 위해선 Redis 내부에서 좌석 배열을 저장하는 방식이 비트맵으로 바뀌어야 한다.
예시:
local function bookSeat(eventId, row, col)
local key = 'event:' .. eventId .. ':row:' .. row
-- 이미 예약된 좌석인지 확인
local current = redis.call('GETBIT', key, col)
if current == 1 then
return { status = 'ERROR', message = 'Already booked' }
end
-- 좌석 예약
redis.call('SETBIT', key, col, 1)
return { status = 'OK' }
end