Keys, Strings, Expiration, and TTL Basics
Learn the Redis fundamentals that power most caching patterns: key naming, simple values, and automatic expiration.
Inside this chapter
- Keys as Identifiers
- Strings
- Expiration and TTL
- Why TTL Is So Powerful
- Business Example
Series navigation
Study the chapters in order for the clearest path from Redis basics to advanced cache architecture, operations, and distributed-system design. Use the navigation at the bottom to move smoothly through the full tutorial series.
Keys as Identifiers
Everything in Redis is accessed by key. A good key naming convention makes Redis usage far easier to understand and operate. Teams often namespace keys by feature or domain.
user:42:profile
product:998:details
session:abc123 Strings
Strings are the most commonly used Redis type. They can store text, numbers, JSON blobs, tokens, or serialized application objects depending on the design.
SET user:42:name "Riya"
GET user:42:name Expiration and TTL
SET otp:login:42 "834921" EX 300
TTL otp:login:42
Expiration is one of the most important Redis features. It lets temporary data disappear automatically after a defined time. This is ideal for tokens, short-lived cache entries, and session-like state.
Why TTL Is So Powerful
Many application values are only useful for a short time. Instead of writing custom cleanup jobs for everything, Redis can manage lifecycle automatically through expiration.
Business Example
A login flow may store an OTP in Redis with a five-minute TTL. Once the time expires, Redis removes the code automatically, reducing cleanup complexity and improving security.