Frank: "Sharing session information between separate JVMs (be they on the same physical machine or on different machines) is essentially the same problem as sharing any data between separate JVMs."
That's true. What differs is what "qualities of service" you are interested in. For example, should updates be transactional? Should they at least be coordinated? Or can they occur asynchronously? Should the data be in sync among the servers? Or is it OK to approximate coherency?
Frank: "Common solutions include: persisting session data to a ... database"
This is the most common but unfortunately scales very poorly. For performance, it is better to keep the data close to the logic, and the logic is in the application tier, so that is where the data should be (if possible). For scalability, sending traffic from all app servers to the database provides an obvious bottleneck. Even with only one or two app servers, the load on the database can be staggering; the popular site "TheServerSide.com" just ran into this when they implemented a cluster for their site -- they ended up with so much load on the database that it died!
Frank: "All these solutions require that you ensure all data placed in the session is serializable. This is actually a requirement in the servlet specification, but many application servers are lax about enforcing it."
I would add one other issue that we've seen: If you do a "getAttribute" on the session and you change it, you must call "setAttribute" or the change may not be stored (
JDBC) or replicated (clustering technology) because the app server doesn't know that the data changed. This is a common problem.
Frank: "However you do this, bear in mind that it adds a lot of overhead and complexity to your applications. If you start sharing session data, you need to make extra sure that you keep an eye on what data gets put in the session. Repeatedly transferring too many large objects about can offset or even defeat the benefits gained from adding extra servers."
That can be true for a replicated cache, but for a distributed (partitioned) cache, it will scale indefinitely. It also has to do with the granularity; some app servers persist the entire session every request, which is expensive, but some implementations (like Coherence) cluster-replicate just modified attributes and properties.