> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# replicas

> Creates replicas of an index

export const Setting = ({type, default: defaultValue, defaultNote, scope, min, max, formerly}) => {
  const renderedDefault = defaultValue === '' ? '""' : defaultValue;
  const renderedNote = defaultNote ? `(${defaultNote})` : '';
  return <ul>
      <li><strong>Type:</strong> <code>{type}</code></li>
      <li><strong>Default:</strong> <code>{renderedDefault}</code>{renderedNote}</li>
      {min && <li><strong>Min:</strong> <code>{min}</code></li>}
      {max && <li><strong>Max:</strong> <code>{max}</code></li>}
      <li><strong>Scope:</strong> <a href="/doc/api-reference/api-parameters"><code>{scope}</code></a></li>
      {formerly && <li>
          <strong>Deprecated name:</strong> <code>{formerly}</code>
        </li>}
    </ul>;
};

<Setting type="list<string>" default="[]" defaultNote="no replicas" scope="settings" formerly="slaves" />

Use replicas to create copies of your primary index with a different configuration (settings, synonyms, rules).
Replicas are useful for:

* [Alternative sorting strategies](/doc/guides/managing-results/refine-results/sorting)
* [Multiple indices](/doc/guides/managing-results/refine-results/sorting/how-to/set-settings-and-forward-to-replicas) such as ascending and descending price
* [A/B testing](/doc/guides/ab-testing/what-is-ab-testing).

The data in a replica stays synchronized with its primary index.
All indexing operations on the primary (add, update, delete) are automatically forwarded to its replicas.

A primary index can have multiple replicas,
but each replica must have exactly one primary.
Replicas can't have their own replicas.

The `primary` setting is automatically added to a replica's settings with the name of the primary index.

## Usage

* This setting specifies the complete set of replicas for an index.

* Adding a new replica to this list:

  * Creates a new replica index if it doesn't exist.
  * Overwrites the records of an existing replica index with the same name.
    Its settings remain unchanged.
    To sync the settings from the primary, either copy the settings to the replica or delete the replica index before recreating it.

* Removing a replica from the list detaches it and turns it into a regular index.
  Then, you can delete it like any other index.

## Example

<AccordionGroup>
  <Accordion title="Current API clients" defaultOpen="true">
    <CodeGroup>
      ```cs C# theme={"system"}
      var response = await client.SetSettingsAsync(
        "INDEX_NAME",
        new IndexSettings
        {
          Replicas = new List<string> { "name_of_replica_index1", "name_of_replica_index2" },
        }
      );
      ```

      ```dart Dart theme={"system"}
      final response = await client.setSettings(
        indexName: "INDEX_NAME",
        indexSettings: IndexSettings(
          replicas: [
            "name_of_replica_index1",
            "name_of_replica_index2",
          ],
        ),
      );
      ```

      ```go Go theme={"system"}
      response, err := client.SetSettings(client.NewApiSetSettingsRequest(
        "INDEX_NAME",
        search.NewEmptyIndexSettings().SetReplicas(
          []string{"name_of_replica_index1", "name_of_replica_index2"})))
      if err != nil {
        // handle the eventual error
        panic(err)
      }
      ```

      ```java Java theme={"system"}
      UpdatedAtResponse response = client.setSettings(
        "INDEX_NAME",
        new IndexSettings().setReplicas(Arrays.asList("name_of_replica_index1", "name_of_replica_index2"))
      );
      ```

      ```js JavaScript theme={"system"}
      const response = await client.setSettings({
        indexName: 'theIndexName',
        indexSettings: { replicas: ['name_of_replica_index1', 'name_of_replica_index2'] },
      });
      ```

      ```kotlin Kotlin theme={"system"}
      var response =
        client.setSettings(
          indexName = "INDEX_NAME",
          indexSettings =
            IndexSettings(replicas = listOf("name_of_replica_index1", "name_of_replica_index2")),
        )
      ```

      ```php PHP theme={"system"}
      $response = $client->setSettings(
          'INDEX_NAME',
          ['replicas' => [
              'name_of_replica_index1',

              'name_of_replica_index2',
          ],
          ],
      );
      ```

      ```python Python theme={"system"}
      response = client.set_settings(
          index_name="INDEX_NAME",
          index_settings={
              "replicas": [
                  "name_of_replica_index1",
                  "name_of_replica_index2",
              ],
          },
      )
      ```

      ```ruby Ruby theme={"system"}
      response = client.set_settings(
        "INDEX_NAME",
        Algolia::Search::IndexSettings.new(replicas: ["name_of_replica_index1", "name_of_replica_index2"])
      )
      ```

      ```scala Scala theme={"system"}
      val response = Await.result(
        client.setSettings(
          indexName = "INDEX_NAME",
          indexSettings = IndexSettings(
            replicas = Some(Seq("name_of_replica_index1", "name_of_replica_index2"))
          )
        ),
        Duration(100, "sec")
      )
      ```

      ```swift Swift theme={"system"}
      let response = try await client.setSettings(
          indexName: "INDEX_NAME",
          indexSettings: IndexSettings(replicas: ["name_of_replica_index1", "name_of_replica_index2"])
      )
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="Legacy API clients">
    <CodeGroup>
      ```cs C# theme={"system"}
      IndexSettings settings = new IndexSettings();
      settings.Replicas = new List
      {
          "replica_1",
          "replica_2"
      };

      index.SetSettings(settings);
      ```

      ```go Go theme={"system"}
      res, err := index.SetSettings(search.Settings{
      	Replicas: opt.Replicas(
      		"replica_1",
      		"replica_2",
      	),
      })
      ```

      ```java Java theme={"system"}
      index.setSettings(
        new IndexSettings().setReplicas(Arrays.asList(
          "replica_1",
          "replica_2"
        ))
      );
      ```

      ```js JavaScript theme={"system"}
      index
        .setSettings({
          replicas: ["replica_1", "name_of_replica_index2"],
        })
        .then(() => {
          // done
        });
      ```

      ```kotlin Kotlin theme={"system"}
      val settings = settings {
          replicas {
              +"replica_1"
              +"replica_2"
          }
      }

      index.setSettings(settings)
      ```

      ```php PHP theme={"system"}
      $index->setSettings([
        'replicas' => [
          'replica_1',
          'replica_2'
        ]
      ]);
      ```

      ```python Python theme={"system"}
      index.set_settings({"replicas": ["replica_1", "name_of_replica_index2"]})
      ```

      ```ruby Ruby theme={"system"}
      index.set_settings(
        {
          replicas: [
            "replica_1",
            "replica_2"
          ]
        }
      )
      ```

      ```scala Scala theme={"system"}
      client.execute {
        setSettings of "myIndex" `with` IndexSettings(
          replicas = Some(Seq(
            "replica_1",
            "replica_2"
          ))
        )
      }
      ```

      ```swift Swift theme={"system"}
      let settings = Settings()
        .set(\.replicas, to: [
          "replica_1",
          "replica_2"
        ])

      index.setSettings(settings) { result in
        if case .success(let response) = result {
          print("Response: \(response)")
        }
      }
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>
