Skip to content
tchandy edited this page Sep 14, 2010 · 18 revisions

Config File

To enable shard, you need to create a file named shards.yml in your config directory inside your rails application. If you want to use octopus in a plain ruby application, create a folder named config, and put the file inside them. The shards.yml file should looks like this:

  production:
    shards:
      shard_five:
        adapter: mysql
        host: localhost
        database: octopus_shard5

      shard_one:
        adapter: mysql
        host: localhost
        database: octopus_shard1

In this example, you have two shards, named shard_five and shard_one. You could also have a group of shards, and your config file will look like this:

  production:
    shards:
      history_shards:
        aug2009:
          adapter: mysql
          host: localhost
          database: octopus_shard2
        aug2010:
          adapter: mysql
          host: localhost
          database: octopus_shard3
        aug2011:
          adapter: mysql
          host: localhost
          database: octopus_shard4

      country_shards:
        canada:
          adapter: mysql
          host: localhost
          database: octopus_shard5
        brazil:
          adapter: mysql
          host: localhost
          database: octopus_shard6
        russia:
          adapter: mysql
          host: localhost
          database: octopus_shard7

So, this enables you to send queries to a group of shards. if you want replication, you need to add some lines, like this:

production:
  replicated: true
  shards:
    slave1:
      adapter: mysql
      host: localhost
      database: octopus_shard2
    slave2:
      adapter: mysql
      host: localhost
      database: octopus_shard3
    slave3:
      adapter: mysql
      host: localhost
      database: octopus_shard4
    slave4:
      adapter: mysql
      host: localhost
      database: octopus_shard5

If Octopus finds the directive replicated: true, it will assume all shards as slaves, and the database specified in database.yml as master database. So, all writes queries will be sent to master, and all reads queries to slaves. if you have some shards specified inside a group, octopus will see them as normal slaves.

Octopus also supports different shards that runs on different databases, like if you have a PostgreSQL Shard, and the master database is MySQL, no problem, Octopus will handle this for you ;-), just specify in the config file and all will work:

production:
  shards:
    postgres_shard:
      adapter: postgresql
      username: postgres
      password:
      database: octopus_shard1
      encoding: unicode

If you have duplicated shards names or groups names, Octopus will raise a exception, helping the programmer to fix the problem.

Clone this wiki locally