Redis is commonly used to store temporary information such as active sessions, reservations, verification codes, online users, and recently accessed resources. These records are useful for a limited period, but they should not remain in the database forever.
For example, imagine an online shopping application where customers reserve products before completing checkout. The application stores the reserved product IDs in Redis. If a customer completes the purchase, the reservation can be removed. But what happens when the customer leaves the application without completing the order?
The reservation should expire automatically so that another customer can purchase the product.
Redis already supports expiration at the key level. However, there are situations where several temporary records need to be stored inside one Redis set, and each record has a different expiration time.
Until Redis 8.12, developers generally had to manage this situation themselves. They could create separate keys, maintain expiration information in another data structure, or periodically remove expired members using application code.
Redis 8.12 introduces expiration for individual set members. This means a member can have its own expiration time even when several members belong to the same Redis set.
This feature makes temporary data management easier for applications that need to track records with different lifetimes.
In this article, we will understand how individual set-member expiration works, explore the new Redis commands, and build practical examples for managing temporary reservations and active sessions.
The Problem With Traditional Redis Set Expiration
A Redis set is an unordered collection of unique string values. It is useful when an application needs to maintain a group of related items without storing a separate key for every item.
For example, an application may maintain a set of active user IDs:
SADD active-users user-101 user-102 user-103
The application can check whether a user belongs to the set using SISMEMBER:
SISMEMBER active-users user-101
This works well when all members have the same lifetime or when the application manages membership manually.
The problem appears when every member needs a different expiration time.
Suppose the application stores three active sessions:
SADD active-sessions session-101 session-102 session-103
The first session should expire after five minutes, the second after fifteen minutes, and the third after thirty minutes.
Before Redis 8.12, expiration was associated with the entire key, not individual members. If the application ran this command:
EXPIRE active-sessions 300
Redis would expire the complete active-sessions key after five minutes. All three sessions would disappear together.
That is not what the application needs.
Developers often work around this limitation by storing every session under a separate Redis key:
SET session:101 "user-101" EX 300
SET session:102 "user-102" EX 900
SET session:103 "user-103" EX 1800
This approach is valid, but it changes how the application manages the data. If the application needs set operations to find, compare, or process groups of sessions, it must maintain additional structures or perform more operations.
Redis 8.12 addresses this particular problem by allowing expiration to be assigned to individual set members.
Understanding Set-Member Expiration in Redis 8.12
Redis 8.12 introduces a group of commands that allow developers to set, inspect, and remove expiration times for individual members of a set.
The important difference is that the set can remain available while individual members disappear when their own expiration times are reached.
For example, consider this set:
SADD active-sessions session-101 session-102 session-103
Now assign different expiration times:
SEXPIRE active-sessions 300 MEMBERS 1 session-101
SEXPIRE active-sessions 900 MEMBERS 1 session-102
SEXPIRE active-sessions 1800 MEMBERS 1 session-103
The first session has a lifetime of five minutes, the second fifteen minutes, and the third thirty minutes.
Redis manages these expiration times independently.
When the first session expires, it is treated as no longer belonging to the set. The other two sessions can remain available until their own expiration times are reached.
This is useful when an application stores temporary records together but each record has a different lifetime.
The feature is not limited to session management. It can also be useful for reservations, temporary access permissions, short-lived memberships, and other records that naturally expire at different times.
Add Set Members With an Expiration Using SADDEX
Redis 8.12 introduces the SADDEX command. It allows developers to add members to a set and assign their expiration times in the same command.
Consider an application that stores temporary product reservations.
Instead of first adding members and then assigning expiration times separately, the application can use:
SADDEX reserved-products EX 600 MEMBERS 3 product-101 product-102 product-103
This command adds three members to the reserved-products set and assigns each of them a ten-minute expiration.
The EX option specifies the expiration in seconds.
Redis also supports milliseconds:
SADDEX reserved-products PX 30000 MEMBERS 2 product-104 product-105
In this example, both members expire after thirty seconds.
For applications that need to work with an exact expiration timestamp rather than a relative duration, SADDEX supports EXAT and PXAT.
For example:
SADDEX temporary-access EXAT 1893456000 MEMBERS 2 user-101 user-102
The members expire at the specified Unix timestamp in seconds.
Understand the MNX and MXX options
SADDEX also supports conditions that control whether members can be added or updated.
The MNX option means that the operation succeeds only if none of the specified members already exist.
SADDEX reserved-products MNX EX 300 MEMBERS 2 product-201 product-202
If any specified member already exists, the operation makes no changes.
The MXX option requires all specified members to already exist:
SADDEX reserved-products MXX EX 600 MEMBERS 2 product-201 product-202
If any member does not exist, the command does not make the requested changes.
These conditions are useful when the application needs to avoid accidentally replacing existing reservation information or wants to update the expiration of a group of known members.
Be careful when updating an existing member
One important behavior of SADDEX deserves attention.
When SADDEX is used without an expiration option, an existing member's expiration is removed. If the application wants to preserve existing member expiration times, it should use KEEPTTL.
For example:
SADDEX reserved-products KEEPTTL MEMBERS 2 product-101 product-102
Existing members keep their current expiration times. Newly added members do not receive an expiration.
This matters when different parts of an application update the same Redis set. A developer may think that adding an existing member is harmless, but the command's options can change its expiration behavior.
Regular SADD does not change an existing member's expiration time.
Set Expiration After Adding Members
Sometimes the application adds a member first and decides its expiration later.
Redis 8.12 provides SEXPIRE for this situation.
For example, create a set and add some members:
SADD active-users user-101 user-102 user-103
Now assign a five-minute expiration to one user:
SEXPIRE active-users 300 MEMBERS 1 user-101
The command sets a relative expiration time of 300 seconds for user-101.
You can assign expiration to several members in one command:
SEXPIRE active-users 600 MEMBERS 2 user-102 user-103
Both members receive a ten-minute expiration.
The command also supports conditions that control how the expiration is changed.
Option | Meaning |
|---|
NX | Set expiration only if the member does not already have one |
XX | Set expiration only if the member already has an expiration |
GT | Set expiration only if the new expiration is later than the existing one |
LT | Set expiration only if the new expiration is earlier than the existing one |
For example, suppose a session should receive a new expiration only when it does not already have one.
You can use:
SEXPIRE active-users 300 NX MEMBERS 1 user-101
This avoids replacing an existing expiration time.
The choice of condition depends on the application's rules. A session renewal process, for example, may need to extend an existing expiration rather than assign one only when no expiration exists.
Use Milliseconds When More Precise Expiration Is Required
Some applications need expiration times shorter than one second.
For example, a temporary lock or short-lived reservation may need to expire after 500 milliseconds.
Redis 8.12 provides SPEXPIRE for assigning expiration in milliseconds.
SPEXPIRE temporary-locks 500 MEMBERS 1 lock-101
The member receives an expiration time of 500 milliseconds.
This is useful when the application needs finer control over short-lived records.
The difference between SEXPIRE and SPEXPIRE is the unit of time. SEXPIRE uses seconds, while SPEXPIRE uses milliseconds.
Do not use milliseconds simply because they are available. For ordinary sessions, reservations, and temporary memberships, seconds are usually easier to work with. Milliseconds are more useful when the application has a specific requirement for shorter time intervals.
Check How Much Time a Member Has Left
Setting an expiration is only part of the work. Applications also need to know how much time remains before a member expires.
Redis 8.12 provides STTL for reading the remaining expiration time in seconds.
For example:
STTL reserved-products MEMBERS 3 product-101 product-102 product-103
Redis returns the remaining time for each requested member.
The results can be interpreted as follows:
A positive number means the member has that many seconds remaining.
-1 means the member exists but has no expiration.
-2 means the member does not exist or is no longer available.
For millisecond precision, use SPTTL:
SPTTL reserved-products MEMBERS 2 product-101 product-102
This command returns the remaining time in milliseconds.
These commands are useful when an application displays countdowns, checks whether a reservation is close to expiration, or decides whether a temporary record should be renewed.
Remember that the returned value is a snapshot. Time continues to pass after Redis returns the result, so the application should not treat it as a permanent value.
Work With Exact Expiration Times
Relative expiration is useful when a record should remain available for a fixed duration after it is created.
However, some applications have a fixed business deadline.
For example, a temporary promotion may expire at a specific time regardless of when the user joins it.
Redis provides SEXPIREAT to set an expiration using a Unix timestamp in seconds.
SEXPIREAT promotions 1893456000 MEMBERS 2 offer-101 offer-102
Both members receive the specified expiration timestamp.
For millisecond timestamps, use SPEXPIREAT:
SPEXPIREAT promotions 1893456000000 MEMBERS 1 offer-103
The timestamp should be calculated and validated by the application using the intended time zone and business rules.
An absolute expiration is particularly useful for scheduled promotions, temporary access periods, event registrations, and records that must expire at a known deadline.
To inspect the exact expiration time, Redis provides SEXPIRETIME and SPEXPIRETIME.
For example:
SEXPIRETIME promotions MEMBERS 2 offer-101 offer-102
This returns the expiration timestamps in seconds.
The millisecond version is:
SPEXPIRETIME promotions MEMBERS 2 offer-101 offer-102
These commands help applications display expiration dates without maintaining a separate timestamp for every set member.
Remove Expiration When a Member Should Become Permanent
Business requirements can change.
A temporary reservation may become a confirmed reservation, or a temporary access permission may be extended indefinitely.
In these situations, the application may need to remove the member's expiration without removing the member itself.
Redis 8.12 provides SPERSIST for this purpose.
SPERSIST reserved-products MEMBERS 1 product-101
This removes the expiration from product-101 while keeping the member in the set.
You can verify the result:
STTL reserved-products MEMBERS 1 product-101
The returned value should be -1 when the member exists without an expiration.
This is different from SREM, which removes the member from the set entirely.
Understanding the difference is important because removing an expiration and removing a member are two separate operations.
Build a Temporary Reservation Example
Let's bring these commands together in a practical example.
Suppose an online shopping application allows customers to reserve products for ten minutes while they complete checkout.
The application stores reserved product IDs in one Redis set.
First, add the reservations:
SADDEX reserved-products EX 600 MEMBERS 3 product-101 product-102 product-103
All three products receive a ten-minute expiration.
Now imagine the customer completes checkout for product-101. The application should remove that product from the temporary reservation set:
SREM reserved-products product-101
The remaining products continue to have their own expiration times.
The application can check their remaining time:
STTL reserved-products MEMBERS 2 product-102 product-103
Suppose the customer decides to keep product-102 reserved for another ten minutes. The application can update its expiration:
SEXPIRE reserved-products 600 MEMBERS 1 product-102
If product-103 is no longer temporary and should remain in the set until explicitly removed, remove its expiration:
SPERSIST reserved-products MEMBERS 1 product-103
This example shows how individual expiration can simplify the management of temporary records.
The application does not need to create a separate Redis key for every product merely to give each reservation its own expiration time.
However, the set should not automatically become the source of truth for product availability. The actual inventory and reservation rules should be enforced by the application's backend. Redis can manage temporary membership, but checkout must still verify that a product can be purchased.
Important Behavior When Set Members Expire
Individual member expiration changes how developers should think about Redis set operations.
An expired member is treated as missing by other set commands, even if Redis has not physically reclaimed it yet.
For example, if a member's expiration time has passed, SISMEMBER should no longer treat it as an active member.
There is one important detail with SCARD. Redis documents that SCARD can count expired members that have not yet been physically removed. Therefore, applications should be careful when using SCARD as an exact count of currently active members.
If the application needs to make business decisions based on active members, test the behavior with the commands and Redis version used in production.
There are also differences in how set operations handle expiration information.
Some commands preserve member expiration when moving or creating sets:
SMOVE transfers a member together with its expiration.
SINTERSTORE, SUNIONSTORE, and SDIFFSTORE copy member expiration information to the destination set.
This is useful when applications use set operations to build temporary groups.
However, sorted-set commands that accept sets as input, such as ZUNION, return an error when the input set contains member expirations. Review the command compatibility before mixing expiring sets with other Redis data structures.
These details matter when an application uses Redis sets for more than simple membership checks.
Performance and Application Design Considerations
The new commands make expiration management easier, but developers should still understand their cost.
Redis documents the complexity of SADDEX as O(N), where N is the number of members being added. Commands such as SEXPIRE, SPEXPIRE, STTL, and SPTTL also have O(N) complexity based on the number of members specified in the command.
This means that requesting expiration information for a large number of members requires more work than checking one member.
For normal application operations, processing a small group of members at a time is straightforward. However, avoid repeatedly sending commands containing very large member lists without measuring their effect.
Also, do not confuse expiration with business-level cleanup.
For example, if a reservation expires, Redis can remove the member automatically. But the application may still need to update another system, notify a user, release inventory in a database, or record an audit event.
Redis expiration should not be treated as a replacement for a business workflow that must execute additional actions.
If an operation needs to trigger important work when a reservation expires, use an appropriate event or scheduled processing design. Do not depend on the disappearance of a Redis member alone to complete the business operation.
Common Mistakes to Avoid
Expiring the complete set instead of individual members
Using EXPIRE on the set key removes the entire set after the specified duration.
Use SEXPIRE or SPEXPIRE when different members need different expiration times.
Forgetting the MEMBERS count
The new commands require the number of members to be specified after MEMBERS.
For example:
SEXPIRE active-users 300 MEMBERS 2 user-101 user-102
The number 2 tells Redis how many member names follow.
Make sure this count matches the supplied members.
Accidentally removing existing expiration times
SADDEX without an expiration option can remove expiration from existing members.
Use KEEPTTL when the application needs to preserve existing expiration values.
Treating an expired member as a permanent record
Once its expiration time has passed, a member should be treated as unavailable. Applications should not rely on it remaining accessible because physical cleanup may happen separately.
Assuming Redis expiration completes other business operations
Redis can remove an expired member, but it cannot automatically complete an order, update inventory in an external database, or send a business notification.
Keep these responsibilities in the appropriate application workflow.
Conclusion
Redis 8.12 makes temporary data management more flexible by introducing expiration for individual set members. Developers can now store related values in one Redis set while assigning a different lifetime to each member.
Commands such as SADDEX, SEXPIRE, SPEXPIRE, STTL, SPTTL, and SPERSIST provide the operations needed to add members, assign expiration, inspect remaining time, and remove expiration when business requirements change.
This is particularly useful for temporary reservations, active sessions, short-lived memberships, and other data where individual records expire independently.
The main benefit is simpler data management. Instead of creating separate keys or writing custom cleanup logic for every member, applications can use Redis to manage member expiration directly.
However, expiration should remain part of a larger data management strategy. Important business actions, such as releasing inventory or recording completed transactions, still need to be handled by the application.
Use individual set expiration when related temporary records need different lifetimes. It provides a cleaner way to manage those records while keeping the application logic easier to understand.
Join the conversation! Your thoughts help the community grow.