Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Patterns and Comparisons

Serialization and Compression

Compare JSON, pickle and compact formats for cached values, measure what zlib compression saves and costs, and handle schema changes and the 1 MB item limit safely.

Memcached stores bytes. It has no idea whether those bytes are a JSON document, a pickled Python object, a PHP array or an HTML page. Your client library converts values to bytes on set and back on get, and records how it did so in a 16-bit flags field stored with the item (for example "this is JSON", "this is gzip-compressed"). The choices you make here decide how big your items are (and so how many fit), how fast reads are, and whether an old cached value breaks the code that reads it after a deployment.

This lesson compares the common formats and compression, shows how to measure them, and covers schema evolution, size limits and a security rule about deserialization. All code is runnable Python standard library.

Choosing a format

Format Size Speed Cross-language Notes
JSON medium fast yes Human readable, limited types (no sets, dates or bytes)
Pickle (Python) medium fast no Handles almost any Python object; unsafe to load from untrusted data
MessagePack, CBOR smaller fast yes Binary JSON-like formats; need a library
Protocol Buffers, Avro, FlatBuffers smallest fast yes Schema-based; great for large volumes and strict contracts
Plain string / HTML as is none yes Best for already rendered fragments

For caching, interoperability often decides: if PHP and Python both read the same keys, use a neutral format such as JSON or MessagePack, not a language-specific one.

Python Same data, different encodings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Small gains per item matter at scale: shaving 20% from 100 million items is 20% more cache without buying RAM. Drop fields you never read before you pick a smarter encoding.

Compression

Compressing values trades CPU for memory and bandwidth. It helps when the values are large and repetitive (JSON, HTML, text) and hurts when they are tiny or already compressed (images, JPEG, zip). A common rule is to compress only values above about 1 KB, and to measure.

Python What does zlib save?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Observe that levels past the default give a diminishing size gain for noticeably more CPU, that tiny values save little in absolute terms (and can grow once a header is added), and that large repetitive JSON shrinks dramatically. Your numbers for timing will differ from machine to machine; sizes are reproducible.

Where to compress

  • In the client: most libraries compress above a threshold and set a flag so they can decompress on read. Easy, but CPU lands on your app servers.
  • Before the cache, in your own code: gives full control over the algorithm (for instance zstd or lz4, which are fast), and the flag lets other readers recognise it.
  • Never compress the whole cluster blindly: the hottest small keys gain nothing, and decompression adds latency to every read.

Schema evolution: old values meet new code

A deployment changes the shape of an object. Old cached copies still have the old shape and the new code reads them. Without care you get missing attributes, wrong types or exceptions in production for as long as the old items live. Solutions:

  1. Put a version in the key: user:v3:42. New code reads new keys; old ones age out. The cost is a cold cache after each version bump.
  2. Store a version inside the value and let readers upgrade or discard old shapes: {"v": 3, "data": {...}}. If the version is unknown, treat it as a miss.
  3. Make changes backward compatible: add optional fields, never rename, and give defaults.
Python Reading old shapes safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Treating anything you cannot parse as a miss is the safest default: the cache is disposable, and a rebuild is better than a crash.

The item size limit

Memcached rejects values larger than the item size limit (1 MB by default, -I). The client sees a failed set, often silently. If you cache lists or big documents:

  • Check the serialized size before storing, and log when a value is over ~512 KB.
  • Split big collections: cache each page of results, or each item individually plus a list of ids.
  • Compress large text first.
  • Remember that large items need larger slab chunks, which wastes memory (see Slab Allocator and Memory Sizing).

A security rule: never unpickle untrusted data

Unpickling executes code chosen by whoever produced the bytes. If an attacker can write to your Memcached (it has no authentication by default, see Cache Anti-Patterns and Security), pickle.loads on a read becomes remote code execution on your application servers. Prefer JSON, MessagePack or schema-based formats for anything that goes through a shared cache.

pickle, PHP's unserialize and Java's native serialization all have a history of code execution bugs when fed attacker-controlled bytes. A cache is shared infrastructure; treat its contents as untrusted input.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Memcached do with the value you store?

    The server stores bytes. Format and compression are client conventions recorded in the flags.
  2. 2. When does compression help the most?

    Large, repetitive data compresses well; tiny or already-compressed values gain little or grow.
  3. 3. How should code treat a cached value it cannot decode or whose version it does not know?

    The cache is disposable; a rebuild is safer than propagating an error.
  4. 4. Why is unpickling data from a shared cache dangerous?

    Deserializing attacker-controlled pickle data can run arbitrary code.

Summary

  • Memcached stores bytes; clients choose the format and record it in the flags.
  • Use a neutral format like JSON or MessagePack when several languages share the cache.
  • Compress large repetitive values (about 1 KB and up) and measure the CPU cost.
  • Version your keys or values so deployments cannot break on old cached shapes; unreadable values are misses.
  • Stay under the item size limit, and never deserialize untrusted data with unsafe formats.

Next: Cache Anti-Patterns and Security