

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 클라이언트 모범 사례(Valkey 및 Redis OSS)
<a name="BestPractices.Clients.redis"></a>

일반적인 시나리오의 모범 사례를 알아보고 가장 인기 있는 오픈 소스 Valkey 및 Redis OSS 클라이언트 라이브러리(redis-py, PHPRedis 및 Lettuce)의 코드 예제와 함께 일반적으로 사용되는 오픈 소스 Memcached 클라이언트 라이브러리를 사용하여 ElastiCache 리소스와 상호 작용하기 위한 모범 사례를 따르세요.

**Topics**
+ [많은 수의 연결(Valkey 및 Redis OSS)](BestPractices.Clients.Redis.Connections.md)
+ [클러스터 클라이언트 검색 및 지수 백오프(Valkey 및 Redis OSS)](BestPractices.Clients.Redis.Discovery.md)
+ [클라이언트 측 제한 시간 구성(Valkey 및 Redis OSS)](BestPractices.Clients.Redis.ClientTimeout.md)
+ [서버 측 유휴 제한 시간 구성(Valkey 및 Redis OSS)](BestPractices.Clients.Redis.ServerTimeout.md)
+ [Lua 스크립트](BestPractices.Clients.Redis.LuaScripts.md)
+ [대규모 복합 항목 저장(Valkey 및 Redis OSS)](BestPractices.Clients.Redis.LargeItems.md)
+ [Lettuce 클라이언트 구성(Valkey 및 Redis OSS)](BestPractices.Clients-lettuce.md)
+ [듀얼 스택 클러스터에 선호되는 프로토콜 구성(Valkey 및 Redis OSS)](#network-type-configuring-dual-stack-redis)

## 듀얼 스택 클러스터에 선호되는 프로토콜 구성(Valkey 및 Redis OSS)
<a name="network-type-configuring-dual-stack-redis"></a>

클러스터 모드가 활성화된 Valkey 또는 Redis OSS 클러스터의 경우, 클라이언트가 클러스터 내 노드에 연결하는 데 사용할 프로토콜을 IP Discovery 파라미터로 제어할 수 있습니다. IP Discovery 파라미터는 IPv4 또는 IPv6로 설정할 수 있습니다.

Valkey 또는 Redis OSS 클러스터의 경우, IP Discovery 파라미터가 [cluster slots ()](https://valkey.io/commands/cluster-slots/), [cluster shards ()](https://valkey.io/commands/cluster-shards/), [cluster nodes ()](https://valkey.io/commands/cluster-nodes/) 출력에 사용되는 IP 프로토콜을 설정합니다. 이러한 명령은 클라이언트가 클러스터 토폴로지를 검색하는 데 사용됩니다. 클라이언트는 이들 명령 내의 IP를 사용하여 클러스터 내 다른 노드에 연결합니다.

IP Discovery를 변경해도 연결된 클라이언트는 가동 중지되지 않습니다. 하지만 변경 사항이 전파되려면 다소 시간이 소요됩니다. Valkey 또는 Redis OSS 클러스터에 변경 사항이 완전히 전파된 시점을 확인하려면 `cluster slots`의 출력을 모니터링하면 됩니다. 클러스터 슬롯 명령에서 반환된 모든 노드가 새 프로토콜이 있는 IP를 보고하면, 변경 사항이 완전히 전파된 것입니다.

Redis-Py 사용 예제:

```
cluster = RedisCluster(host="xxxx", port=6379)
target_type = IPv6Address # Or IPv4Address if changing to IPv4

nodes = set()
while len(nodes) == 0 or not all((type(ip_address(host)) is target_type) for host in nodes):
    nodes = set()

   # This refreshes the cluster topology and will discovery any node updates.
   # Under the hood it calls cluster slots
    cluster.nodes_manager.initialize()
    for node in cluster.get_nodes():
        nodes.add(node.host)
    self.logger.info(nodes)

    time.sleep(1)
```

Lettuce 사용 예제:

```
RedisClusterClient clusterClient = RedisClusterClient.create(RedisURI.create("xxxx", 6379));

Class targetProtocolType = Inet6Address.class; // Or Inet4Address.class if you're switching to IPv4

Set<String> nodes;
    
do {
   // Check for any changes in the cluster topology.
   // Under the hood this calls cluster slots
    clusterClient.refreshPartitions();
    Set<String> nodes = new HashSet<>();

    for (RedisClusterNode node : clusterClient.getPartitions().getPartitions()) {
        nodes.add(node.getUri().getHost());
    }

    Thread.sleep(1000);
} while (!nodes.stream().allMatch(node -> {
            try {
                return finalTargetProtocolType.isInstance(InetAddress.getByName(node));
            } catch (UnknownHostException ignored) {}
            return false;
}));
```