Created
June 16, 2015 23:30
-
-
Save tysonmote/8c9f4bae25aa8b57dbf3 to your computer and use it in GitHub Desktop.
Delete large Redis sorted sets with Ruby and Sidekiq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$redis = Redis.new | |
class SafeSortedSetDelete | |
include Sidekiq::Worker | |
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) | |
self.perform_async(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