Which session handler minimizes latency for concurrent AJAX requests in Moodle?
0 reputation · 30 Apr 2022, 07:33 UTC
0 reputation · 30 Apr 2022, 07:33 UTC
Moodle relies on PHP session management to maintain user state. By default, file-based session handlers lock the session file for the duration of a request. This behavior can cause subsequent concurrent requests from the same user session—such as multiple simultaneous AJAX calls for dashboard blocks—to queue sequentially, increasing perceived latency.
While Moodle supports alternative handlers via $CFG->session_handler_class, including database-backed storage or external caches like Redis, the choice involves trade-offs between locking efficiency, database overhead, and infrastructure complexity.
What are the performance implications of using the \core\session\database handler compared to Redis when mitigating session lock contention? Does Moodle provide a native way to implement session_write_close() across all standard AJAX endpoints to release locks early?
35275 reputation · 30 Apr 2022, 18:57 UTC
The \core\session\redis handler minimizes latency for concurrent AJAX requests. Unlike file or database handlers, Redis utilizes atomic operations to manage session state, significantly reducing the lock contention that typically causes simultaneous requests from the same user to queue sequentially.
\core\session\database)SELECT ... FOR UPDATE) on the sessions table.\core\session\redis)SET NX EX) for lock acquisition with a defined Time-To-Live (TTL).Moodle does not provide a native, global mechanism to implement session_write_close() across all standard AJAX endpoints. While Moodle manages session closure via \core\session\manager::write_close() during the request shutdown phase, the lock is held for the entire duration of the execution.
For custom AJAX endpoints, you can manually release the lock to allow other concurrent requests to proceed once session data is no longer needed:
require_once(__DIR__ . '/../../config.php');
require_login();
// Read necessary session data
$userdata = \core\session\manager::instance()->get_userdata();
// Release the lock early
session_write_close();
// Perform expensive processing or API calls
$result = perform_heavy_task();
echo json_encode($result);
To implement the Redis handler, update your config.php:
$CFG->session_handler_class = '\\core\\session\\redis';
$CFG->session_redis_config = [
'host' => '127.0.0.1',
'port' => 6379,
'prefix' => 'moodle_sess_',
'database' => 0,
'acquire_lock_timeout' => 5.0,
'lock_expire' => 7200,
];
Verification:
/admin/phpinfo.php by searching for session.handler.ab) to simulate concurrent requests with the same session cookie and compare the p95 response times between the database and Redis handlers.What is the average number of concurrent AJAX requests triggered per user page load? If the count is consistently above 3-5, the transition to Redis is critical; otherwise, the database handler may be sufficient.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.