Why does a cached WordPress course page show seats that are already taken?
A page cache shows every visitor the seat count from the moment it stored the page, until that stored copy expires. The count is right only if the page fetches the number of seats left uncached after loading and the server checks again on submit.
How long does a page cache keep an old seat count?
Until the stored page expires or someone clears the cache. In our measurement on a course site in Switzerland with Cache Enabler 1.8.17, the page cache expired after 2 hours. Every visitor who opened the course page in that window saw the count from the moment of storing.
A new registration leaves the stored page as it is. Cache Enabler clears the cache when a post is saved. A registration stored in a table of its own is not a post. A plugin can clear the cache of a single page on purpose with the Cache Enabler hook cache_enabler_clear_page_cache_by_post (KeyCDN documentation). In version 1.8.17 the hook only works for published posts whose address contains no question mark.
Excluding the course page from the cache is the other option. WordPress then renders the page on every request, including for visitors who only read it.
How can a page show the live count while it stays cached?
The page fetches the number of seats left after loading, with a separate request that the cache does not store. Cache Enabler has excluded REST API requests from the cache since version 1.5.0, according to its changelog in the WordPress plugin directory ("Add cache bypass method for Ajax, REST API, and XMLRPC requests"). In our measurement, version 1.8.17 never stored such a request. That held for a request recognized as REST with REST_REQUEST and for a request with an Accept header without text/html.
The page stays in the cache and loads fast. Only the number comes fresh from the server:
| Part of the page | Where it comes from |
|---|---|
| Course page with text and sessions | from the cache, up to 2 hours old on the measured site |
| Seats left after loading | fresh via the REST API, never stored |
| Check on submit | fresh on the server, with the session row locked |
One way to do this: Coursehall requests coursehall/v1/availability after loading. The response holds the seats left, the number of seats and the status of each session. It contains no personal data. Without JavaScript the page shows the cached state. The display alone is therefore not enough. The list of all sessions in the demo shows the seat count for each session.
What happens when two people book the last seat at the same moment?
Only one person gets the seat if the server counts the seats in a transaction and locks the session row while counting. In our measurement on a test installation with MySQL, two simultaneous registrations for the last seat produced exactly one registration. The other one was rejected.
Without a lock, both requests read the same last seat and both write a registration. Coursehall reads the session with SELECT … FOR UPDATE. It writes the registration only if a seat is still free after that. Otherwise the transaction rolls everything back. When the lock ran into a timeout in the test, the data stayed unchanged. All three Coursehall tables ran on InnoDB in that test.
What must the server check again on submit?
Everything the page displayed, because the display may come from the cache:
- whether the session belongs to the course and is not canceled
- whether a seat is still free, counted under the lock
- whether the same registration has already arrived once
- whether the required fields are filled in, with the same rules as in the browser
A cached page can still show "2 free" after the session has filled up. Coursehall then rejects the registration as "fully booked". This applies to its own form and to Gravity Forms. With the Pro waitlist switched on, the built-in form and Gravity Forms put the registration on the waitlist instead. How the seat count and the form work together is shown under limit course seats in WordPress.