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.
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:
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:
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:
| Property | Description | Example Value |
interactive_timeout | Timeout for interactive sessions | 3600 (1 hour) |
wait_timeout | Timeout for non-interactive sessions | 1800 (30 minutes) |
SET @@interactive_timeout | SQL command to set interactive timeout for current session | 3600 |
SET @@wait_timeout | SQL command to set wait timeout for current session | 1800 |
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
- How to import a single table in to mysql database using command line
- How to increase aws dynamodb index limit from 5
- How to increment a counter in Cassandra?
- How to index a list with a TensorFlow tensor?
- How to initialize a PSQL database in docker-compose file?
- How to insert 9 billions records into a database in 2 minutes?
- How to insert a datetime into a Cassandra 1.2 timestamp column
- how to insert datetime into the SQL Database table?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.