Skip to content

Commit b6ba877

Browse files
committed
docs: add TLS session caching documentation
Document the TLS session caching feature in the security guide: - Overview of session resumption benefits - Configuration options (enabled, size, ttl, options) - Advanced configuration with TLSSessionCacheOptions - Custom cache implementation example - Notes on TLS 1.2 vs TLS 1.3 behavior
1 parent 6c16993 commit b6ba877

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

docs/security.rst

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,115 @@ SSL with Twisted
308308
309309
In case the twisted event loop is used pyOpenSSL must be installed or an exception will be risen. Also
310310
to set the ``ssl_version`` and ``cert_reqs`` in ``ssl_opts`` the appropriate constants from pyOpenSSL are expected.
311+
312+
TLS Session Resumption
313+
----------------------
314+
315+
.. versionadded:: 3.30.0
316+
317+
The driver automatically caches TLS sessions to enable session resumption for faster reconnections.
318+
When a TLS connection is established, the session is cached and can be reused for subsequent
319+
connections to the same endpoint, reducing handshake latency and CPU usage.
320+
321+
**TLS Version Support**: Session resumption works with both TLS 1.2 and TLS 1.3. TLS 1.2 uses
322+
Session IDs and optionally Session Tickets (RFC 5077), while TLS 1.3 uses Session Tickets (RFC 8446)
323+
as the primary mechanism. Python's ``ssl.SSLSession`` API handles both versions transparently.
324+
325+
Session caching is **enabled by default** when SSL/TLS is configured and applies to the following
326+
connection classes:
327+
328+
* :class:`~cassandra.io.asyncorereactor.AsyncoreConnection` (default)
329+
* :class:`~cassandra.io.libevreactor.LibevConnection`
330+
* :class:`~cassandra.io.asyncioreactor.AsyncioConnection`
331+
* :class:`~cassandra.io.geventreactor.GeventConnection` (when not using SSL)
332+
333+
.. note::
334+
Session caching is not currently supported for PyOpenSSL-based reactors
335+
(:class:`~cassandra.io.twistedreactor.TwistedConnection`,
336+
:class:`~cassandra.io.eventletreactor.EventletConnection`) but may be added in a future release.
337+
338+
Configuration
339+
^^^^^^^^^^^^^
340+
341+
TLS session caching is controlled by three cluster-level parameters:
342+
343+
* :attr:`~.Cluster.tls_session_cache_enabled` - Enable or disable session caching (default: ``True``)
344+
* :attr:`~.Cluster.tls_session_cache_size` - Maximum number of sessions to cache (default: ``100``)
345+
* :attr:`~.Cluster.tls_session_cache_ttl` - Time-to-live for cached sessions in seconds (default: ``3600``)
346+
347+
Example with default settings (session caching enabled):
348+
349+
.. code-block:: python
350+
351+
from cassandra.cluster import Cluster
352+
import ssl
353+
354+
ssl_context = ssl.create_default_context(cafile='/path/to/ca.crt')
355+
cluster = Cluster(
356+
contact_points=['127.0.0.1'],
357+
ssl_context=ssl_context
358+
)
359+
session = cluster.connect()
360+
361+
Example with custom cache settings:
362+
363+
.. code-block:: python
364+
365+
from cassandra.cluster import Cluster
366+
import ssl
367+
368+
ssl_context = ssl.create_default_context(cafile='/path/to/ca.crt')
369+
cluster = Cluster(
370+
contact_points=['127.0.0.1'],
371+
ssl_context=ssl_context,
372+
tls_session_cache_size=200, # Cache up to 200 sessions
373+
tls_session_cache_ttl=7200 # Sessions expire after 2 hours
374+
)
375+
session = cluster.connect()
376+
377+
Example with session caching disabled:
378+
379+
.. code-block:: python
380+
381+
from cassandra.cluster import Cluster
382+
import ssl
383+
384+
ssl_context = ssl.create_default_context(cafile='/path/to/ca.crt')
385+
cluster = Cluster(
386+
contact_points=['127.0.0.1'],
387+
ssl_context=ssl_context,
388+
tls_session_cache_enabled=False
389+
)
390+
session = cluster.connect()
391+
392+
How It Works
393+
^^^^^^^^^^^^
394+
395+
When session caching is enabled:
396+
397+
1. The first connection to an endpoint establishes a new TLS session and caches it
398+
2. Subsequent connections to the same endpoint reuse the cached session
399+
3. Sessions are cached per endpoint (host:port combination)
400+
4. Sessions expire after the configured TTL
401+
5. When the cache reaches max size, the least recently used session is evicted
402+
403+
Performance Benefits
404+
^^^^^^^^^^^^^^^^^^^^
405+
406+
TLS session resumption is a standard TLS feature that provides performance benefits:
407+
408+
* **Faster reconnection times** - Reduced handshake latency by reusing cached sessions
409+
* **Lower CPU usage** - Fewer cryptographic operations during reconnection
410+
* **Better overall throughput** - Especially beneficial for workloads with frequent reconnections
411+
412+
The actual performance improvement depends on various factors including network latency,
413+
server configuration, and workload characteristics.
414+
415+
Security Considerations
416+
^^^^^^^^^^^^^^^^^^^^^^^
417+
418+
* Sessions are stored in memory only and never persisted to disk
419+
* Sessions are cached per cluster and not shared across different cluster instances
420+
* Sessions for one endpoint are never used for a different endpoint
421+
* Hostname verification still occurs on each connection, even when reusing sessions
422+
* Sessions automatically expire after the configured TTL

0 commit comments

Comments
 (0)