Redisのキー有効期限とTTL
Redisでキーに有効期間を設定して自動的に期限切れにする方法、TTLを制御するコマンド、期限切れのキーを削除するタイミングをRedisが決める仕組みを学びます。
「Redisのキー有効期限とTTL」はCoddyKit上の無料Redis Caching & Messaging (Pub/Sub, Streams)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 3600Set 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 300Checking 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:42Millisecond 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:99Expiring 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 1893456000Removing 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:42How 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__:expiredQuick 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時間対応のAIチューター)、Redis Caching & Messaging (Pub/Sub, Streams)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Redis Caching & Messaging (Pub/Sub, Streams)コースには全4レッスンが含まれています。
「Redisのキー有効期限とTTL」で何を学びますか?
Redisでキーに有効期間を設定して自動的に期限切れにする方法、TTLを制御するコマンド、期限切れのキーを削除するタイミングをRedisが決める仕組みを学びます。 ブラウザで直接実行するハンズオンコードでRedis Caching & Messaging (Pub/Sub, Streams)を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Redis 入門
- Redis CLI の基礎
- Redis の主要なデータ構造
- Redisのキー有効期限とTTL