Jump to content

Replica Placement Strategies When Using Cassandra

+ 1
  chco's Photo
Posted Jan 22 2011 01:11 PM

There are several strategies one can implement for replica placement when working with Cassandra. The following excerpt from Cassandra: The Definitive Guide offers some insight into choosing which one to work with.
In its simplest terms, to satisfy the replica placement strategy for the configuration file, you just need to provide the name of a Java class that extends the org.apache.cassandra.locator.AbstractReplicationStrategy class. The purpose of this setting is to configure the way that the node picker works.

Note: For determining replica placement, Cassandra implements the Gang of Four Strategy pattern. The strategy is outlined in the common abstract class, allowing different implementations of an algorithm (different strategies for accomplishing the same work). Each algorithm implementation is encapsulated inside a single class that adheres to the interface. So you can provide the strategy framework a variety of different implementations of the algorithm, all of which are interchangeable to the runtime. Clients do not have to be concerned about which strategy is used. A common use of the Strategy pattern is for sorting. Imagine a SortStrategy interface with a single sort operation and different implementations for Quicksort, Mergesort, and so on. Each sorts in different ways, depending on your needs.

The interface provides 11 public methods that require implementation; if your strategy can make use of some of the method implementations provided in the abstract parent, then you can override only the methods you want. Of course, you don’t have to implement this yourself. Out of the box, Cassandra provides three implementations of this interface (extensions of the abstract class): Rack-Aware Strategy, Rack-Unaware Strategy, and Data Center Shard Strategy.

Choosing the right replication strategy is important because the strategy determines which nodes are responsible for which key ranges. The implication is that you’re also determining which nodes should receive which write operations, which can have a big impact on efficiency in different scenarios. If you set up your cluster such that all writes are going to two data centers—one in Australia and one in Reston, Virginia—you will see a matching performance degradation. The variety of pluggable strategies allows you greater flexibility, so that you can tune Cassandra according to your network topology and needs.

The first replica will always be the node that claims the range in which the token falls, but the remainder of the replicas are placed according to the replica placement strategy you use.

Note: As of the time of this excerpt's publication, the names of the strategies are changing. The new names in 0.7 will be SimpleStrategy (formerly known as RackUnawareStrategy), OldNetworkTopologyStrategy (formerly known as RackAwareStrategy), and NetworkTopologyStrategy (formerly known as DatacenterShardStrategy).

Simple Strategy

Simple Strategy is the new name for Rack-Unaware Strategy.

The strategy used by default in the configuration file is org.apache.cassandra.locator.RackUnawareStrategy. This strategy only overrides the calculateNaturalEndpoints method from the abstract parent implementation. This strategy places replicas in a single data center, in a manner that is not aware of their placement on a data center rack. This means that the implementation is theoretically fast, but not if the next node that has the given key is in a different rack than others. This is shown below.

The Simple Strategy places replicas in a single data center, without respect to topology

Attached Image

What’s happening here is that the next N nodes on the ring are chosen to hold replicas, and the strategy has no notion of data centers. A second data center is shown in the diagram to highlight the fact that the strategy is unaware of it.

Old Network Topology Strategy

The second strategy for replica placement that Cassandra provides out of the box is org.apache.cassandra.locator.RackAwareStrategy, now called Old Network Topology Strategy. It’s mainly used to distribute data across different racks in the same data center. Like RackUnawareStrategy, this strategy only overrides the calculateNaturalEndpoints method from the abstract parent implementation. This class, as the original name suggests, is indeed aware of the placement in data center racks.

Say you have two data centers, DC1 and DC2, and a set of Cassandra servers. This strategy will place some replicas in DC1, putting each in a different available rack. It will also ensure that another replica is placed in DC2. The Rack-Aware Strategy is specifically for when you have nodes in the same Cassandra cluster spread over two data centers and you are using a replication factor of 3. This strategy is depicted below.

The Old Network Topology Strategy places the second replica in a different data center and others on different racks in the same data center

Attached Image

Use this strategy when you want to ensure higher availability at the potential cost of some additional latency while the third node in the alternate data center is contacted. There is no point in using Rack-Aware Strategy if you are only running Cassandra in a single data center. Cassandra is optimized for running in different data centers, however, and taking advantage of its high availability may be an important consideration for you. If your Cassandra cluster does span multiple data centers, consider using this strategy.

If you use the Rack-Aware Strategy, you must also use the Rack-Aware Snitch.

Network Topology Strategy

This strategy, included with the 0.7 release of Cassandra, allows you to specify more evenly than the RackAwareStrategy how replicas should be placed across data centers. To use it, you supply parameters in which you indicate the desired replication strategy for each data center. This file is read and executed by the org.apache.cassandra.locator.DataCenterShardStrategy class, so the implementation is somewhat flexible. The data center shard strategy is depicted below.

The Network Topology Strategy places some replicas in another data center and the remainder in other racks in the first data center, as specified

Attached Image

This strategy used to employ a file called datacenter.properties. But as of 0.7, this metadata is attached directly to the keyspace and strategy as a map of configuration options.

Cassandra: The Definitive Guide

Learn more about this topic from Cassandra: The Definitive Guide.

With this hands-on guide, you'll learn how Apache Cassandra handles hundreds of terabytes of data while remaining highly available across multiple data centers -- capabilities that have attracted Facebook, Twitter, and other data-intensive companies. Cassandra: The Definitive Guide provides the technical details and practical examples you need to assess this database management system and put it to work in a production environment.

See what you'll learn

0 Replies