TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Redis scripts do not expire keys atomically

67 pointsby stichersover 3 years ago

8 comments

blutackover 3 years ago
Redis doesn&#x27;t guarantee that expiry happens exactly at expiry time ([1] gives up to a 1 millisecond delay), so if your system assumes that all related keys are removed at exactly the same time it is still broken (although the race window is now tighter).<p>Maybe you could store the related keys in a hash or something?<p>I&#x27;m also guilty of trying to use features in redis in ways they weren&#x27;t really designed for - it&#x27;s such a fantastic bit of technology that a lot of the time you can still get away with it but it&#x27;s not redis&#x27;s fault when it doesn&#x27;t work!<p>1: <a href="https:&#x2F;&#x2F;redis.io&#x2F;commands&#x2F;expire" rel="nofollow">https:&#x2F;&#x2F;redis.io&#x2F;commands&#x2F;expire</a>
评论 #30100249 未加载
评论 #30100367 未加载
macspoofingover 3 years ago
&gt;The integrity of a set of related keys requires that either all keys exist, or none exist.<p>Using a Key-Value DB to store relational data eh - that never leads to problems.<p>Their solution isn&#x27;t any better because the API does not guarantee a particular order or atomicity of key expiration - so great that it works for you now[1], but next version may change the behavior.<p>[1] Does it work though? Are you really really sure?
评论 #30101905 未加载
alatiesover 3 years ago
It should be noted that an EXPIREAT triggers a deletion if the timestamp being set is in the past and could lead to an increase in memory and disk pressure during script execution. Worth checking if the behavior there differs in any meaningful way since y&#x27;all made the change. EXPIRE usually just marks a key and relies on either subsequent operations to the same key or garbage collection to actually excise the entry and do the subsequent bookkeeping, I believe.<p>It&#x27;s been a while since I had to deal with this kind of problem though, so please tell me if things are different now.
gorjusborgover 3 years ago
It seems the article&#x27;s implication is that they are relying on redis TTL and expiry to guarantee all keys exists or no keys exist, at all times, for a given key set.<p>I never got the impression that Redis gave guarantees on when expired keys were deleted, just that they would be <i>eventually</i>.<p>If the issue is that you want a consistent <i>all or nothing</i>, why not just use a single key to make that determination?
评论 #30101830 未加载
kristoff_itover 3 years ago
I think the solutions to this problem are:<p>- If the keys are strings, just use a single hashmap and you&#x27;re done.<p>- If the keys have different types, then you need a Redis module that implements something like `MPEXPIREAT time key1 key2 ...` and then you need to either be able to handle the case where you successfully retrieve key1 but not key2 (because they expired half-way through) or you use a lua script to execute the multi-key access atomically. That said, without the lua script the whole point of having the keys expire at the same time makes less sense, IMO.
andydunstallover 3 years ago
Hey, I&#x27;m the author of this blog post. Ask me anything!
评论 #30100221 未加载
评论 #30100146 未加载
评论 #30101504 未加载
matthewaveryusaover 3 years ago
I ran into a similar issue and used a hashset of all the related keys with ttls on the individual keys. the hashset would expire making the keys unreachable, and the TTls basically garbages collects the individual keys. anything that required to transact with the set would do so atomically by being wrapped in a lua script.
jpswainover 3 years ago
Is there a reason that the DEL command wouldn’t work? Is there some semantic difference with expire?