Skip to content
Latchkey

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.

memcached
CLIENT_ERROR bad data chunk

Common 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

  1. Prefer pymemcache or a mature client that frames values correctly.
  2. Store bytes, letting the client compute the byte length.
  3. Avoid hand-writing the memcached text protocol.
python
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.

python
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.

Frequently asked questions

What causes ""CLIENT_ERROR bad data chunk""?
The length prefix was computed from character count, not byte count, so multibyte UTF-8 values overrun or underrun the frame.
How do I fix "CLIENT_ERROR bad data chunk"?
Use a maintained client library

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card