Question

I am developing a web application which basically lets the user create a graph in his browser (in the server the graph is basically a List of nodes and each node has a List of edges). Each node or edge that the user creates or deletes is communicated to the server via Ajax, so the server can construct an equal graph represented by Java Objects. This application is a web version of a desktop one. So I would like to offer to the user the option of "save" the graph. My initial idea was persist the object in a database when the user clicks the save button, but for this I need to keep my graph in the user's session.

So can I store a graph (which is a collection of nodes and edges) inside the http session or there is a better way to achieve what I want?

Obs: I was thinking of manipulating the graph directly in the database, that is, the graph is always persisted and if the user session expires I delete the graph of the database if it has not been saved yet.

Was it helpful?

Solution

Answer depends on the scale of your app.

If you have only one server with enough memory to hold sessions for all your concurrent users

Storing info in the session is the easiest thing to do. Make sure that your graph is serializable and Servlet (or J2EE) container will take of the rest. You can help with httpsession persistence if you implement some custom algo for your data.

If you grew big

That is if you have more than one server with a load balancer in front or your graphs do not fit in memory. Store them in some shared storage - either DB or a filer and retrieve by some key lookup. Consider using sticky sessions to minimize DB/file lookups and cache recent graphs in memory

Fancy trick

If you find a way to compress your whole graph into 1-2KB put it entirely into http cookie, browser will send it back to you on every request, your server will be stateless, no DB, no session affinity issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top