Marcio Cunha

How to secure microservices with mTLS and why to drop tokens

Learn how certificate-based mTLS authentication secures microservices communication at the transport layer without relying exclusively on JWT tokens.

Marcio Cunha•5 min
Also available in:EspañolPortuguês
Summary
  • End-to-end encryption at the transport layer validates both callers and receivers in internal networks
  • Digital certificates eliminate the heavy computational overhead of decoding cryptographic keys per request
  • Public key infrastructure requires strict governance to prevent systemic failures from expired certificates
  • Automated credential issuance and rotation eliminates critical manual errors in distributed environments
  • Network architecture gains robust isolation, preventing unauthorized access to internal services during breaches

Why traditional token models fail in microservices

When building systems divided into smaller pieces called microservices, security becomes complex. Previously, a single monolithic application handled everything within its own memory boundaries. Today, each small feature runs isolated, communicating with others through the internal company network. The most common model to protect this conversation is access tokens based on JSON Web Tokens, known as JWT. In practice, this means every request sent from one service to another carries a signed digital ticket proving who the user is or which system is making the call.

The problem is that these tokens travel through the air or cables every millisecond, demanding constant processing to check if the digital signature is still valid. Furthermore, if an attacker intercepts this ticket, they can impersonate the legitimate system until the token expires. To solve this vulnerability at its root, many engineers turn to transport-layer approaches, ensuring identity is validated long before any ticket is read. To understand how to maintain integrity and prevent operational failures under heavy load, check out our guide on how to fix connection pools and avoid bottlenecks during high traffic.

What mTLS is and how it works in practice

The acronym mTLS stands for Mutual Transport Layer Security. To understand the concept, think of an exclusive party where entry is permitted only upon presenting an official photo ID. In the traditional internet world, standard TLS encryption only ensures you are talking to the correct server, but the server usually does not require you to prove who you are before opening the page. With mTLS enabled, a rigorous two-way street happens: the server presents its digital certificate to the client, and the client does exactly the same for the server.

In practice, this means neither side agrees to talk to strangers. Before any data travels, both programs exchange invisible cryptographic keys and validate if they trust the same issuing authority. If either side lacks a valid, recognized certificate, the connection drops immediately without opening the door to an intruder. This check happens at the network layer, lifting a huge burden from application code, which no longer needs to manually validate credentials on every called route.

The real benefits of dropping token dependency

Abandoning exclusive reliance on application tokens brings profound operational advantages for engineering teams. When we rely solely on keys sent in request headers, any flaw in code validation logic can open catastrophic breaches. Additionally, tokens must be repeatedly generated, signed, and decoded by software libraries, consuming precious processing cycles on servers handling millions of requests per second.

With mTLS operating at the transport layer, encryption and identity checks are delegated to specialized tools or the operating system itself, freeing the programming language to focus purely on business rules. Another notable gain is internal code simplification. Developers no longer need to scatter complex token-based permission checks across dozens of different microservices, since the network topology itself blocks intruders trying to bypass official infrastructure barriers.

How to structure public key infrastructure

Implementing this technology requires creating an internal Certificate Authority, often called a CA. This entity acts as a private digital notary inside the company, responsible for signing and issuing identity certificates for every existing microservices. Without this organized foundation, manually managing hundreds of key files and certificates on ephemeral servers becomes an impossible mission highly prone to human error.

To keep the system running without unwanted interruptions, automation is indispensable. Modern tools manage automatic rotation of these credentials before they expire, preventing sudden outages of entire systems due to expired certificates. Integrating this security flow with infrastructure automation tools gives teams agility and predictability. For deeper insights into operational network automation, read our article on how to automate switches with Ansible in local networks.

Basic step-by-step to configure mutual trust

Configuring this security in practice involves creating root certificates, generating service keys, and adjusting proxy servers bridging them. Below are the main steps to establish this infrastructure in a development environment:

  1. Generate the root Certificate Authority using the OpenSSL command to sign subsequent certificates:
    openssl genrsa -out ca.key 4096 && openssl req -new -x509 -days 365 -key ca.key -out ca.crt
  2. Create the private key and signing request for the specific microservice:
    openssl genrsa -out service.key 2048 && openssl req -new -key service.key -out service.csr
  3. Sign the microservice certificate using the root CA created in the first step:
    openssl x509 -req -days 365 -in service.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out service.crt

Operational challenges and common pitfalls

Despite all obvious advantages, adopting certificate-based security requires operational maturity. The biggest daily challenge is credential lifecycle management. Since certificates have strict expiration dates for security reasons, forgetting to automate renewal can take entire microservices offline in the middle of the night, generating stressful incidents for the on-call team.

Another critical point is troubleshooting when a connection is refused. Since certificate exchange happens behind the scenes before data transmission, error traces are often cryptic, pointing only to generic transport layer handshake failures. Teams must have proper monitoring tools to inspect connection status and quickly identify whether problems stem from a corrupted key, unsynchronized server clocks, or outdated trusted authorities.

Final considerations

Adopting mTLS certificate-based authentication represents a mature leap in modern microservices architecture. By shifting identification and encryption responsibilities from the application to the transport layer, we eliminate fragile token dependencies and reduce the risk of internal network data interception. While demanding rigorous discipline in key automation and certificate management, gains in robustness, performance, and isolation amply justify the implementation effort.

Ultimately, building resilient systems means shielding foundations before worrying about the surface. Integrating this security layer with appropriate observability and automation tools ensures your engineering is ready to scale with confidence and stability under any scenario.