Skip to content

Instantly share code, notes, and snippets.

@tysonmote
Created June 16, 2015 23:37
Show Gist options
  • Save tysonmote/1fa32995b25d49baddac to your computer and use it in GitHub Desktop.
Save tysonmote/1fa32995b25d49baddac to your computer and use it in GitHub Desktop.
Delete large Redis sorted sets with Ruby and Resque
$redis = Redis.new
class SafeSortedSetDelete
@queue = :garbage_collection
BATCH_SIZE = 100
# Rename the key and queue for deletion
def self.delete(key)
newkey = "gc:zsets:#{$redis.incr("gc:index")}"
$redis.rename(key, newkey)
Resque.enqueue(self, newkey)
end
# Delete members from the sorted set in batches
def self.perform(key)
while $redis.zcard(key) > 0
$redis.zremrangebyrank(key, 0, BATCH_SIZE - 1)
end
end
end
# Example:
#
# SafeSortedSetDelete.delete("my.large.zset")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment