# ERR 协议错误：无效的批量长度

- **ID:** `redis/err-protocol-bad-int`
- **领域:** redis
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Redis 服务器收到了格式错误的 RESP 协议消息，其中包含无效的批量字符串长度，通常由网络损坏或客户端实现错误导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Redis 6.2.6 | active | — | — |
| Redis 7.0.11 | active | — | — |
| ioredis 5.3.2 | active | — | — |
| Jedis 4.3.1 | active | — | — |

## 解决方案

1. ```
   Enable Redis connection pooling with health checks to automatically drop corrupted connections. Example in Node.js using ioredis: `const redis = new Redis({ maxRetriesPerRequest: 3, retryStrategy: (times) => Math.min(times * 50, 2000) });`
   ```
2. ```
   Add a proxy or load balancer that validates RESP protocol before forwarding to Redis, such as using HAProxy with a custom TCP health check.
   ```
3. ```
   Upgrade the client library to the latest version that includes better RESP parsing. For Java Jedis: `jedis.close(); jedis = new Jedis(host, port);` after catching the error.
   ```

## 无效尝试

- **** — The error is about incoming data, not outgoing buffers. (95% 失败率)
- **** — TLS may add overhead but is not the cause of malformed bulk lengths. (80% 失败率)
