-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved redis performance in large keyspace, added pagination, and a…
…uto-expiration This commit makes several interrelated changes: 1. Replaced the redis key scan to find job keys in `Client#find_workflow` with job class name persistence in `Workflow#to_hash`. This significantly improves performance when loading many workflows because it avoids n key scans. 2. Added `Client#workflow_ids` with sorting by creation timestamp and pagination as an alternative to `Client#all_workflows`, which has performance issues in large keyspaces and returns unwieldy amounts of data given a large number of workflows. 3. Added workflow and job indexes by `created_at` and `expires_at` timestamps. The former is necessary for paging through sorted workflow ids, and the latter is necessary to remove data on expiration. 4. Replace use of redis key TTL with explicit expiration via `Client#expire_workflows`, since there's no other way to remove data from the indexes. 5. Added a migration file (and infrastructure) to migrate to the new indexes and expiration format. Given a redis instance with 10,000 workflows, this set of changes allows a page of the most recent 100 workflows to be loaded in 0.1 seconds, whereas previously `all_workflows` would take hours to return data. (Or, for a less extreme example of 1000 workflows, we can load 100 workflows in 0.1 seconds compared to `all_workflows` taking 42 seconds).
- Loading branch information
Showing
11 changed files
with
549 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Gush | ||
class IndexWorkflowsByCreatedAtAndExpiresAt < Gush::Migration | ||
def self.version | ||
1 | ||
end | ||
|
||
def up | ||
redis.scan_each(match: "gush.workflows.*").map do |key| | ||
id = key.sub("gush.workflows.", "") | ||
workflow = client.find_workflow(id) | ||
|
||
ttl = redis.ttl(key) | ||
redis.persist(key) | ||
workflow.jobs.each { |job| redis.persist("gush.jobs.#{id}.#{job.klass}") } | ||
|
||
client.persist_workflow(workflow) | ||
client.expire_workflow(workflow, ttl.positive? ? ttl : -1) | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Gush | ||
class Migration | ||
def migrate | ||
return if migrated? | ||
|
||
up | ||
migrated! | ||
end | ||
|
||
def up | ||
# subclass responsibility | ||
end | ||
|
||
def version | ||
self.class.version | ||
end | ||
|
||
def migrated? | ||
redis.sismember("gush.migration.schema_migrations", version) | ||
end | ||
|
||
private | ||
|
||
def migrated! | ||
redis.sadd("gush.migration.schema_migrations", version) | ||
end | ||
|
||
def client | ||
@client ||= Client.new | ||
end | ||
|
||
def redis | ||
Gush::Client.redis_connection(client.configuration) | ||
end | ||
end | ||
end |
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
Oops, something went wrong.