Skip to content

Range Key Sharding

yuemori edited this page Jul 31, 2016 · 3 revisions

If you want to range key sharding, Range object set to cluster key.

ActiveRecord::ShardFor.configure do |config|
  config.define_cluster(:user) do |cluster|
    # unique identifier, connection name
    cluster.register(0...100, :production_user_001)
    cluster.register(100...200, :production_user_002)
    cluster.register(200...300, :production_user_003)
    cluster.register(300..Float::INFINITY, :production_user_004)
  end
end

class User < ActiveRecord::Base
  include ActiveRecord::ShardFor::Model
  use_cluster :user, :distkey
  def_distkey :id

  def self.generate_unique_id
    # Implement to generate unique id
  end

  before_put do |attributes|
    attributes[:id] = generate_unique_id unless attributes[:id]
  end
end

More example, sharding key is String:

ActiveRecord::ShardFor.configure do |config|
  config.define_cluster(:user) do |cluster|
    # unique identifier, connection name
    cluster.register('a'..'z', :production_user_001)
    cluster.register('A'..'Z', :production_user_002)
    cluster.register('0'..'9', :production_user_003)
  end
end

class InitialStringRouter < ActiveRecord::ShardFor::ConnectionRouter
  def route(key)
    key.to_s.first
  end
end

ActiveRecord::ShardFor.configure do |config|
  config.register_connection_router(:initial_string, InitialStringRouter)
end

class User < ActiveRecord::Base
  include ActiveRecord::ShardFor::Model
  use_cluster :user, :initial_string
  def_distkey :id

  def self.generate_unique_id
    SecureRandom.uuid
  end

  before_put do |attributes|
    attributes[:id] = generate_unique_id unless attributes[:id]
  end
end
Clone this wiki locally