I'm reading the HAProxy manual and there is a part that I'm not understanding:
request-learn
If this option is specified, then haproxy will be able to learn
the cookie found in the request in case the server does not
specify any in response. This is typically what happens with
PHPSESSID cookies, or when haproxy's session expires before
the application's session and the correct server is selected.
It is recommended to specify this option to improve reliability.
What does it mean "in case server does not specify any in response", specifically what is the server responding to, and what's the point of learning a cookie if the server doesn't specify one?
The issue of "when haproxy's session expires before the application's session" seems an important one but its handling by haproxy doesn't make sense to me here.
Usually when you are using a cookie it comes in a response from the back-end server with a "set-cookie" header. This is usually in response to a login. If this doesn't happen then HAProxy will not be able to stick you to the same back-end server
This option allows HAProxy to "learn" about a cookie based on a HTTP Request not just a HTTP Response with "set-cookie."
To answer your specific questions:
"What is the server responding to?" Any HTTP Request
"What is the point of learning a cookie the server did not provide?" It allows HAProxy to perform stickiness when you have a server that does non-standard things and doesn't/can't provide the cookie as expected. An example he provides is the "PHPSESSID" cookie.
"HAproxy's session expires before the application's session?" HAProxy stores the cookies to perform stickiness. They have a TTL before they are no good anymore. In the case that the TTL for the back-end server is longer than the TTL for HAProxy, HAProxy will forget the cookie before the back-end server. The client will have the Cookie in the request and HAProxy will be able to re-learn the cookie.
Your skepticism is well placed because if your back-end servers don't replicate session information then this option is of limited usefulness in the TTL case. It still applies to the non-standard cookie case scenario however.