Using Tags with Sidekiq Jobs

2021-06-23

A customer recently opened issue to point out a bug in Sidekiq’s tag filtering support and I realized: I haven’t told people that tags exist. Oops. Here is your notification. ๐Ÿ˜

Sidekiq 6.0.1 added support for per-job tags. Tags are an Array of Strings within the job payload. Maybe you want to tag the sports related to a given job:

class SomeWorker
  include Sidekiq::Worker
  sidekiq_options tags: ['alpha']

There’s plenty of ideas for job tagging:

In the Web UI you can see tags and, in Sidekiq Pro, click on them to see all jobs with that tag.

For example, assume we have a bunch of jobs which are associated with different sports. First we’ll create a bunch of jobs with random tags:

sports = [:โšฝ๏ธ, :๐Ÿˆ, :โ›ณ๏ธ, :โšพ๏ธ, :๐Ÿ€, :๐ŸŽพ, :๐Ÿ, :๐ŸฅŠ, :๐ŸŽณ, :๐Ÿ“]
100.times do
  SimpleWorker.set(tags: sports.sample(2)).perform_async("abc")
end

In Redis, tags are an Array within each job’s JSON payload.

{
  "class": "SimpleWorker",
  "args": ["abc"],
  "tags": ["โšฝ๏ธ", "๐Ÿˆ"],
  "queue": "default"
}

On the Retries and Dead tabs, we can see each tag formatted as a blue label next to the job type.

alljobs

In Sidekiq Pro, the tags are clickable and will use Pro’s filtering support to show just jobs with that tag. Click a soccer ball and you’ll see all jobs tagged with it. The filter isn’t very smart; it looks for any substring match within the job’s JSON payload – make your tags unique or you can get false positives. That’s why I like to use emoji: they are unusual but short and easy to read.

alljobs

Tags can make sets of jobs easier to find and manage within Redis. Have a creative use for tags? Have an idea to improve them? Open a issue and let me know.