liveBook · Manning

Thread local

As explained in Grokking Software Architecture

A variable whose value is maintained separately for each thread of execution.

Thread-local storage (TLS) gives each thread its own independent copy of a variable. Unlike a global variable, a thread-local value stays private to the thread that writes it. section 14.4.1

For observability, Mapped Diagnostic Context (MDC) uses thread-local storage to associate request details—such as a tenantId, orderId, or correlationId—with the active thread. Logging can then add that context to messages throughout the call stack without passing telemetry values through business method signatures. section 14.4.1

Figure 14.6 Mapped Diagnostic Context gives every execution thread a private sandbox.

A private-sandbox diagram illustrates how each execution thread keeps its own context. section 14.4.1

Scope checkout context

A checkout operation can place its orderId in MDC for the duration of a try-with-resources block. Logs emitted within that block, including deeper calls, carry the identifier; leaving the block removes it to prevent leaks or contamination of later work. section 14.7.2

How Grokking Software Architecture covers it

How deep it goes
Defines per-thread isolation and traces MDC metadata through logging and cleanup, but does not detail runtime implementation internals.
Examples
Uses tenantId and orderId metadata, checkout logs, and a test that checks context at a service boundary.
What you can do after
Apply MDC to attach transaction identifiers to logs and remove the context when the operation ends.
How it connects
Connects thread-local storage to Mapped Diagnostic Context, logging, observability, and request-context propagation.

Also explained in

Compare how 2 books explain thread local →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.