TiDB
Session Timeout
Database Management
Server Configuration
Programming

How to implement Session Timeout in TiDB?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In web applications, managing session timeout is a crucial aspect of maintaining security and ensuring that resources are used efficiently. When using TiDB, an open-source, distributed SQL database compatible with MySQL and PostgreSQL, implementing session timeout involves understanding its system variables and configuration properties. This article will guide you through the steps to effectively implement session timeout in TiDB, including some technical insights and examples.

Understanding Session Variables in TiDB

TiDB, much like MySQL, offers various session variables that can be used to control the behavior of database connections. The session timeout setting in TiDB is controlled by the wait_timeout and interactive_timeout system variables:

  • wait_timeout: The number of seconds the server waits for activity on a non-interactive connection before closing it.
  • interactive_timeout: The number of seconds the server waits for activity on an interactive connection before closing it.

It's important to differentiate between interactive and non-interactive sessions. Interactive sessions are typically those initiated by clients like MySQL shell, while non-interactive sessions might be connections from a web application or a service.

Setting the Session Timeout

You can set the session timeout values at server startup or dynamically for existing sessions. Both approaches use the SET command, but the scope (global or session) and impact differ.

For Server Startup:

To set the session timeout globally for all new connections, modify the configuration file (tidb.toml or your custom configuration file), adding or updating the following entries:

toml
[performance]
interactive-timeout = 3600 # interactive timeout in seconds
wait-timeout = 1800 # wait timeout in seconds

These settings ensure that all new interactive and non-interactive sessions will have timeouts of 1 hour and 30 minutes, respectively. After making changes, restart the TiDB server for them to take effect.

For Existing Sessions:

To change the session timeout dynamically for a current session, use the SET command in a SQL query. For example:

sql
SET @@interactive_timeout = 3600; -- sets interactive timeout to 1 hour
SET @@wait_timeout = 1800; -- sets non-interactive timeout to 30 minutes

These commands only affect the current TiDB session. Once the session ends or is closed, these settings revert to the defaults specified in the server configuration.

Handling Timeouts in Application Logic

Implementing session timeouts effectively often requires handling timed-out sessions gracefully in your application code. Ensure that your application can handle exceptions or errors related to lost database connections. This could mean catching the exception and either retrying the connection or providing a meaningful error message to the user.

Monitoring and Testing

After configuring session timeouts, monitor your application and database logs to ensure that sessions are being closed as expected. Unexpectedly long-running sessions might indicate an issue with the session timeout configuration or with your application's connection management logic.

Summary Table

Here's a summary of key points related to implementing session timeout in TiDB:

PropertyDescriptionExample Value
interactive_timeoutTimeout for interactive sessions3600 (1 hour)
wait_timeoutTimeout for non-interactive sessions1800 (30 minutes)
SET @@interactive_timeoutSQL command to set interactive timeout for current session3600
SET @@wait_timeoutSQL command to set wait timeout for current session1800

Conclusion

Implementing session timeout in TiDB is essential for enhanced security and better resource management. By configuring wait_timeout and interactive_timeout, either globally or per session, the system can automatically close idle connections, reducing the risk of cyber-attacks and freeing up resources. Always ensure that your application gracefully handles potential connection disruptions caused by these timeouts.

By paying attention to these details, developers can ensure a robust and secure environment for TiDB-based applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.