View a markdown version of this page

將讀取分散至複本 (停用叢集模式) - Amazon ElastiCache

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將讀取分散至複本 (停用叢集模式)

對於具有複本的叢集模式停用叢集,您可以使用 Lettuce 的 StaticMasterReplicaTopologyProvider搭配 ReadFrom設定,將寫入導向主節點,並將讀取分散到複本。這可透過從主要 卸載讀取來改善讀取輸送量。

使用主要端點進行寫入,並使用讀取器端點進行讀取。讀取器端點會自動負載平衡可用複本之間的連線。

// Primary endpoint for writes, reader endpoint for read distribution RedisURI primaryURI = RedisURI.Builder.redis("primary-endpoint.cache.amazonaws.com") .withPort(6379).withSsl(true).build(); RedisURI readerURI = RedisURI.Builder.redis("reader-endpoint.cache.amazonaws.com") .withPort(6379).withSsl(true).build(); RedisClient redisClient = RedisClient.create(clientResources); redisClient.setOptions(clientOptions); // Open a primary-replica connection using the StaticMasterReplicaTopologyProvider StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica.connect(redisClient, StringCodec.UTF8, Arrays.asList(primaryURI, readerURI)); // Direct reads to replicas, writes always go to the primary connection.setReadFrom(ReadFrom.REPLICA_PREFERRED);

下列ReadFrom選項可供使用:

  • REPLICA_PREFERRED – 可用時從複本讀取,並回到主要 (建議用於大多數使用案例)。

  • REPLICA – 僅從複本讀取。如果沒有可用的複本,則失敗。

  • PRIMARY_PREFERRED – 從主要 讀取,回到複本。

  • ANY_REPLICA – 從任何複本節點讀取。

  • PRIMARY – 僅從主節點讀取。需要強式一致性時使用 。

注意

從複本讀取的資料可能會稍微過時,因為複寫是非同步的。如果您的應用程式需要強式一致性,請使用 ReadFrom.PRIMARYReadFrom.PRIMARY_PREFERRED