

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

# 클라이언트 측 제한 시간 구성(Valkey 및 Redis OSS)
<a name="BestPractices.Clients.Redis.ClientTimeout"></a>

**클라이언트 측 제한 시간 구성**

서버가 요청을 처리하고 응답을 생성하는 데 충분한 시간을 할애할 수 있도록 클라이언트 측 제한 시간을 적절하게 구성합니다. 또한 서버와의 연결을 설정할 수 없는 경우 빠른 실패로 이어질 수 있습니다. 특정 Valkey 또는 Redis OSS 명령은 다른 명령보다 계산 비용이 더 많이 들 수 있습니다. 세부적으로 실행해야 하는 여러 명령이 포함된 Lua 스크립트 또는 MULTI/EXEC 트랜잭션을 예로 들 수 있습니다. 일반적으로 서버로부터 응답을 받기 전에 클라이언트 제한 시간이 초과되지 않도록 하려면 다음을 포함하여 클라이언트 측 제한 시간을 높게 설정하는 것이 좋습니다.
+ 여러 키에서 명령 실행
+ 여러 개별 Valkey 또는 Redis OSS 명령으로 구성된 MULTI/EXEC 트랜잭션 또는 Lua 스크립트 실행
+ 큰 값 읽기
+ 차단 작업 수행(예: BLPOP)

BLPOP 등 차단 작업의 경우 모범 사례는 명령 제한 시간을 소켓 제한 시간보다 낮은 숫자로 설정하는 것입니다.

다음은 redis-py, PHPRedis 및 Lettuce에서 클라이언트 측 제한 시간을 구현하는 코드 예제입니다.

**제한 시간 구성 샘플 1: redis-py**

다음은 redis-py를 사용한 코드 예제입니다.

```
# connect to Redis server with a 100 millisecond timeout
# give every Redis command a 2 second timeout
client = redis.Redis(connection_pool=redis.BlockingConnectionPool(host=HOST, max_connections=10,socket_connect_timeout=0.1,socket_timeout=2))

res = client.set("key", "value") # will timeout after 2 seconds
print(res)                       # if there is a connection error

res = client.blpop("list", timeout=1) # will timeout after 1 second
                                      # less than the 2 second socket timeout
print(res)
```

**제한 시간 구성 샘플 2: PHPRedis**

다음은 PHPRedis를 사용한 코드 예제입니다.

```
// connect to Redis server with a 100ms timeout
// give every Redis command a 2s timeout
$client = new Redis();
$timeout = 0.1; // 100 millisecond connection timeout
$retry_interval = 100; // 100 millisecond retry interval
$client = new Redis();
if($client->pconnect($HOST, $PORT, 0.1, NULL, 100, $read_timeout=2) != TRUE){
	return; // ERROR: connection failed
}
$client->set($key, $value);

$res = $client->set("key", "value"); // will timeout after 2 seconds
print "$res\n";                      // if there is a connection error

$res = $client->blpop("list", 1); // will timeout after 1 second
print "$res\n";                   // less than the 2 second socket timeout
```

**제한 시간 구성 샘플 3: Lettuce**

다음은 Lettuce를 사용한 코드 예제입니다.

```
// connect to Redis server and give every command a 2 second timeout
public static void main(String[] args)
{
	RedisClient client = null;
	StatefulRedisConnection<String, String> connection = null;
	try {
		client = RedisClient.create(RedisURI.create(HOST, PORT));
		client.setOptions(ClientOptions.builder()
	.socketOptions(SocketOptions.builder().connectTimeout(Duration.ofMillis(100)).build()) // 100 millisecond connection timeout
	.timeoutOptions(TimeoutOptions.builder().fixedTimeout(Duration.ofSeconds(2)).build()) // 2 second command timeout 
	.build());

		// use the connection pool from above example

		commands.set("key", "value"); // will timeout after 2 seconds
		commands.blpop(1, "list"); // BLPOP with 1 second timeout
	} finally {
		if (connection != null) {
			connection.close();
		}

		if (client != null){
			client.shutdown();
		}
	}
}
```