0Pricing
Redis Caching & Messaging (Pub/Sub, Streams) · 강의

Redis의 키 만료와 TTL

Redis에서 키에 TTL 값을 연결해 자동으로 만료시키는 방식과 TTL을 제어하는 명령어, 만료된 키를 삭제할 시점을 Redis가 결정하는 방식을 배워 보세요.

Redis의 키 만료와 TTL은(는) CoddyKit의 무료 Redis Caching & Messaging (Pub/Sub, Streams) 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Redis Caching & Messaging (Pub/Sub, Streams) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Keys Need to Expire

Redis often holds temporary data — sessions, one-time codes, cached responses. Expiration lets a key delete itself after a set time so memory stays lean.

Setting a TTL with EXPIRE

EXPIRE attaches a time-to-live in seconds to an existing key. Once the clock runs out, the key vanishes. See the example below.

SET session:42 "active"
EXPIRE session:42 3600

Set and Expire in One Step

Set value and TTL together with SET ... EX — safer than two separate commands, since there's no window where the key has no expiry.

SET otp:99 "123456" EX 300

Checking Remaining Time

TTL returns the seconds left on a key. It returns -1 if the key has no expiry, and -2 if the key doesn't exist at all.

TTL session:42

Millisecond Precision

For finer control, PEXPIRE and PTTL work in milliseconds instead of seconds. The code below sets and reads a millisecond TTL.

PEXPIRE otp:99 1500
PTTL otp:99

Expiring at a Specific Time

EXPIREAT takes a Unix timestamp, so a key can die at an exact wall-clock moment — like precisely at midnight.

EXPIREAT daily:report 1893456000

Removing an Expiry

PERSIST strips a key's TTL, making it permanent again — handy when temporary data gets promoted to durable.

PERSIST session:42
TTL session:42

How Redis Deletes Expired Keys

Redis removes expired keys two ways: lazy deletion checks on access, and active deletion samples keys in a background task and purges expired ones.

Writes Can Clear TTL

Watch out: a plain SET (no EX) overwrites a value and clears its existing TTL. Modifying commands like APPEND or INCR keep the TTL intact.

Common Use Cases

TTL powers real patterns: auto-expiring sessions, rate-limit counters, one-time passwords and verification tokens, and caches that refresh after N seconds.

Keyspace Notifications on Expiry

Redis can publish an event when a key expires via keyspace notifications, so your app can react. Enable expired-key events, then subscribe. See below.

CONFIG SET notify-keyspace-events Ex
SUBSCRIBE __keyevent@0__:expired

Quick Check

Test your expiration knowledge.

Recap

Recap: EXPIRE and SET ... EX set a TTL, TTL reads it (-1 none, -2 missing), PEXPIRE/EXPIREAT give finer control, PERSIST removes it, plain overwrites clear it.

자주 묻는 질문

“Redis의 키 만료와 TTL” 강의는 무료인가요?

네 — “Redis의 키 만료와 TTL” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Redis Caching & Messaging (Pub/Sub, Streams) 강의 전체를 잠금 해제할 수 있습니다. Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 총 4개의 강의가 포함되어 있습니다.

“Redis의 키 만료와 TTL”에서 뭘 배우나요?

Redis에서 키에 TTL 값을 연결해 자동으로 만료시키는 방식과 TTL을 제어하는 명령어, 만료된 키를 삭제할 시점을 Redis가 결정하는 방식을 배워 보세요. 브라우저에서 직접 실행하는 실습 코드로 Redis Caching & Messaging (Pub/Sub, Streams)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Redis Caching & Messaging (Pub/Sub, Streams)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Redis Caching & Messaging (Pub/Sub, Streams)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“Redis의 키 만료와 TTL” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Redis Caching & Messaging (Pub/Sub, Streams) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Redis Caching & Messaging (Pub/Sub, Streams) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Redis 소개
  2. Redis CLI 기초
  3. Redis 핵심 데이터 구조
  4. Redis의 키 만료와 TTL
← Redis Caching & Messaging (Pub/Sub, Streams)(으)로 돌아가기