Jetty Logo
Version: 9.2.11-SNAPSHOT
Contact the core Jetty developers at

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Session Clustering with MongoDB

Configuring the MongoDBSessionIdManager
Configuring a MongoSessionManager

Jetty can support session clustering by persisting sessions into MongoDB. Each Jetty instance locally caches sessions for which it has received requests, writing any changes to the session through to the cluster as the request exits the server. Sessions must obey the Serialization contract, and servlets must call the Session.setAttribute() method to ensure that changes are persisted.

The persistent session mechanism works in conjunction with a load balancer that supports stickiness. Stickiness can be based on various data items, such as source IP address or characteristics of the session ID or a load-balancer specific mechanism. For those load balancers that examine the session ID, the Jetty persistent session mechanism appends a node ID to the session ID, which can be used for routing.

In this type of solution, the traffic on the network needs to be carefully watched and tends to be the bottleneck. You are probably investigating this solution in order to scale to large amount of users and sessions, so careful attention should be paid to your usage scenario. Applications with a heavy write profile to their sessions will consume more network bandwidth than profiles that are predominately read oriented. We recommend using this session manager with largely read based session scenarios.


There are two components to session management in Jetty: a session ID manager and a session manager.

  • The session ID manager ensures that session IDs are unique across all webapps hosted on a Jetty instance, and thus there should only be one session ID manager per Jetty instance.

  • The session manager handles the session lifecycle (create/update/invalidate/expire) on behalf of a web application, so there is one session manager per web application instance.

These managers also cooperate and collaborate with the org.eclipse.jetty.server.session.SessionHandler to enable cross-context dispatch.

Configuring the MongoDBSessionIdManager

You need to configure an org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager instance, either in embedded code or in a jetty.xml file. Here is an example of a jetty.xml setup:

Notice that the MongoSessionIdManager needs access to a mongodb cluster. The default settings are for localhost and default port for mongo db.

The more complex setup is to configure with remote mongodb instances, this is an example of how that would look.

As Jetty configuration files are direct mappings of XML to Java, it is straightforward to see how to do this in code, but here's an example anyway:

The MongoDB Session Id Manager has slightly different options then some of our more traditional session options. The mongodb session id manager has the same scavenge timers which govern the setting of a valid session to invalid after a certain period of inactivity. New to this session id manager is the extra purge setting which governs removal from the mongodb cluster. This can be configured through the 'purge' option. Purge is by default set to true and by default runs daily for each node on the cluster. Also able to be configured is the age in which an invalid session will be retained which is set to 1 day by default. This means that invalid sessions will be removed after lingering in the mongodb instance for a day. There is also an option for purging valid sessions that have not been used recently. The default time for this is 1 week. You can disable these behaviors by setting purge to false.

You must configure the MongoSessionIdManager with a workerName that is unique across the cluster. Typically the name relates to the physical node on which the instance is executing. If this name is not unique, your load balancer might fail to distribute your sessions correctly.

You can also configure how often the persistent session mechanism sweeps the mongo cluster looking for old, expired sessions with the scavengePeriod setting. The default value is 60 seconds. We recommend that you not increase the frequency because doing so increases the load on the database with very little gain; old, expired sessions can harmlessly sit in the database.


How long to delay before periodically looking for sessions to scavenge?


How much time after a scavenge has completed should you wait before doing it again?

purge (Boolean)

Do you want to purge (delete) sessions that are invalid from the session store completely?


How often do you want to perform this purge operation?


How old should an invalid session be before it is eligible to be purged?


How old should a valid session be before it is eligible to be marked invalid and purged? Should this occur at all?


Whether or not to retain all sessions when the session manager stops. Default is true.

Configuring a MongoSessionManager

The way you configure a org.eclipse.jetty.nosql.mongodb.MongoSessionManager depends on whether you're configuring from a context xml file or a jetty-web.xml file or code. The basic difference is how you get a reference to the Jetty org.eclipse.jetty.server.Server instance.

From a context xml file, you reference the Server instance as a Ref:

From a WEB-INF/jetty-web.xml file, you can reference the Server instance directly:

If you're embedding this in code:

See an error or something missing? Contribute to this documentation at Github!(Generated: 2015-04-28T01:01:05+00:00)