Memcached "CLIENT_ERROR bad data chunk" in CI
The memcached text protocol declares a byte length before the value, and if the bytes sent do not match, the server replies "CLIENT_ERROR bad data chunk." In CI this usually comes from a byte-vs-character length mismatch (multibyte data) or a mismatched client library.
What this error means
A store command fails with "CLIENT_ERROR bad data chunk" when the value contains multibyte characters or when a raw socket protocol is hand-written.
CLIENT_ERROR bad data chunkCommon causes
Declared length does not match the byte length
The length prefix was computed from character count, not byte count, so multibyte UTF-8 values overrun or underrun the frame.
A hand-rolled or mismatched protocol implementation
Custom socket code or an incompatible client mis-frames the value, breaking the text protocol.
How to fix it
Use a maintained client library
- Prefer pymemcache or a mature client that frames values correctly.
- Store bytes, letting the client compute the byte length.
- Avoid hand-writing the memcached text protocol.
from pymemcache.client.base import Client
c = Client(("localhost", 11211))
c.set(b"key", "caf\u00e9".encode("utf-8"))Compute length in bytes, not characters
If you must speak the protocol directly, encode first and use the byte length in the command.
data = value.encode("utf-8")
# length is len(data), the byte count, not len(value)How to prevent it
- Let a real client library handle framing and lengths.
- Encode to bytes and use byte length for the protocol.
- Do not hand-write the memcached text protocol in tests.