Course contentsShow
← Node.js
Lesson 569 of 1,9806. Networking and HTTP FundamentalsFree lesson

TLS Session Resumption

Enable session tickets to reduce the latency penalty of repeated TLS handshakes for returning users.

In this lesson, you will learn how to optimize your Node.js servers by enabling TLS session tickets to eliminate redundant handshake overhead for returning visitors.

The Cost of the TLS Handshake

As we covered in our session on TLS/SSL Handshake Basics, establishing a secure connection is "expensive." Each time a client connects, they must exchange several messages to negotiate encryption keys, perform certificate validation, and establish trust. For a user visiting your site multiple times, repeating this entire handshake process adds significant latency to every new connection.

Introducing Session Tickets

Instead of performing a full handshake every time, we can use TLS Session Resumption via "Session Tickets."

Think of a session ticket like a "coat check" token at a restaurant. The first time the client connects, the server performs the full, complex handshake and then issues a ticket—an encrypted blob of data containing the negotiated session details. The client stores this token. When the user returns, they present the ticket. If the server can successfully decrypt and validate it, it skips the expensive handshake calculations and instantly resumes the encrypted session.

Enabling Tickets in Node.js

In Node.js, the https module provides a way to manage these session tickets. While the server handles much of this automatically, you can explicitly manage the ticket keys if you are running a cluster of servers, ensuring that a user can resume their session even if they hit a different server instance.

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert'),
  // Ensure ticket support is enabled (default in modern Node.js)
  ticketKeys: crypto.randomBytes(48) 
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('Securely connected!');
}).listen(443);

By providing ticketKeys, you allow the server to encrypt and decrypt the session state. Without this, a user might experience a "cache miss" if they land on a different server node, forcing a full handshake again. By enabling session resumption, you drastically improve performance for recurring users while maintaining the same security standards.

Key Takeaway: TLS Session Resumption uses tickets to bypass the computationally heavy handshake process, significantly reducing latency for returning users.