Video in progress

Services from different teams exchange text data, and from time to time Uzbek or Russian letters come out garbled. How do you establish an encoding policy at the organization level?

"UTF-8 everywhere, and explicitly declared at every boundary": one rule. You make it mandatory in the JVM settings, HTTP headers, the DB and file formats, and verify it with CI and contract tests. Exceptions (for example CSV for Excel) are written down clearly in the docs.

📖 Answer

This kind of corruption usually doesn't happen in one place but at the boundaries: one service writes UTF-8, another reads it as Windows-1251. Each team says "everything is correct on our side", and they are right, because the problem is in between.

The list of boundaries and rules:

  • JVM. On Java 18+ the default is UTF-8. On Java 17 services you put -Dfile.encoding=UTF-8 in the base image, and LANG=C.UTF-8 too. At the code level, getBytes() without a charset, new String(bytes) and FileReader are banned in CI with static analysis (SpotBugs DM_DEFAULT_ENCODING, Error Prone or an ArchUnit rule).
  • HTTP. Content-Type: application/json: JSON is UTF-8 by the standard. ; charset=utf-8 is mandatory on text responses. How an incoming request with no charset is interpreted is written down in the docs.
  • Database. ENCODING 'UTF8' on PostgreSQL. On MySQL it is utf8mb4, not utf8: the old one is 3-byte and fails on emoji. The JDBC URL needs the matching parameter too.
  • Queues. Kafka messages are bytes; serializers use explicit UTF-8, and text fields in the schema have the string type.
  • Files. CSV export has a classic problem: Excel often opens a UTF-8 file without a BOM in the local encoding. So adding a BOM (EF BB BF) to CSVs handed to users is a clearly documented exception. For files between internal systems there is no BOM, otherwise the first column name turns into id.
  • Normalization. Text that looks the same can be different bytes: the Uzbek oʻ (U+02BB), o' (an ASCII apostrophe) and o’ (U+2019). Search and unique keys need a rule using Normalizer and bringing the apostrophes to one form, otherwise the "same" username gets registered twice.

To verify: you add deliberately "hard" text to the contract tests, "Oʻzbekiston — Gʻijduvon — Ümit — 😀" and a word or two in Cyrillic, and check that it passes through every service and comes back byte for byte identical. This one test gives more value than a lot of documentation.