[001.3] Serialization of Arguments

Considerations for data types used in arguments in your workers.

Subscribe now

Serialization of Arguments [05.13.2016]

In the last episode we built a simple Sidekiq worker. We used a case statement to switch on the argument passed in, like this:

class OurWorker
  include Sidekiq::Worker

  def perform(complexity)
    case complexity
    when "super_hard"
      sleep 20
      puts "Really took quite a bit of effort"
    when "hard"
      sleep 10
      puts "That was a bit of work"
      sleep 1
      puts "That wasn't a lot of effort"

This case statement is the sort of thing I would typically use a symbol for. So why didn't we? Sidekiq uses the JSON data format to store all job data in Redis and JSON doesn't support symbols and other Ruby-specific data types. You're welcome to try it yourself to get a feel for it, but symbols are converted to Strings when the client API pushes the job to Redis.

The bottom line? Use simple job parameters and avoid serializing complex objects to Redis in your workers. You'll end up much happier!