Load balancing with Nginx

Even at home or in a store, there may be some problems to solve: Imagine in a store, three vendors each use a PC or a tablet, it makes three different sessions. What IP will be sent to the server? The same for all three sessions (IP router)? or the IP address of each device (the sessions are so well insulated)?

sorry, I said a stupidity. Whatever the IP adress, there will be only one per device, so everything is good.

Does anyone have experience to share with the use of a database across multiple instances?

Thanks for the info. Very interesting.

@olivier vidal I have always used the mysql database backend and have stayed away from SQlite except for simple utilities. Mysql is great for multi-client, concurrent use.

On the session question, Although the IPs may be grouped into one by NAT, the sessions are maintained by session identifiers in the URL (as far as I can tell by observation). So the sessions stay separate. The issue is that the load balancing is defeated because all traffic from one NAT ip will be routed to one application instance.

So, this approach would work great for an app that is open to the world. But if you are developing an app for a corporate client and you are not running on a server on their internal network, then the balancing may be defeated for that client by the NAT.

@David Wylie Thanks! - if you use it for anything, let me know what more you discover.

I am considering re-compiling nginx to add mod-sticky upstream. This should remove the NAT/IP/ip_hash limitation. If it works I will post it here.

Could someone explain to me how Xojo’s Session handling works with this? I thought that Xojo maintains the Session information inside the program’s memory - how could it then share this information if it gets several requests on the same session distributed to separate instances of the program? Or does the above mechanism make sure that all requests for the same session are always sent to the same instance?

Say you have 1 machine, 12 cores, 64 bit OS running 32 bit processes
Run multiple instances on the same machine & use all memory & cores

Yes - the same session should always be on the same instance with an Nginx set up like above

@John Joyce Thanks for the instructions above. I’m gonna try to see if I can get that to work this weekend :slight_smile:

@John Joyce ! I’ve got it up and running as per your instructions above. Thanks!
One question though. If I go to my app and it connects and works…Until i press refresh. Then it hangs in the “Launching…” state and never connects again. Any ideas/tips? :slight_smile:

Never mind. Used your excellent instructions for HAproxy instead…All working flawlessly now :smiley:
Thank you.

@Albin Kiland - glad it helped. I think HAproxy is the way to go now. I have had several apps running this way flawlessly. Not toooo painful to set up was it? Once you get the basic system up adding apps is a snap too.

Not too difficult at all :slight_smile:
Thanks again!

Hello all,

It’s worth noting that you can get better results by using session stickiness through cookies. This way if many users share an ip you still get them to different instances. (some cellular networks do, so even in an open env you may get lots of connections from the same ip)

There is another issue that is solved, i.e.: if user is mobile and switches networks/ip but I’m not sure how xojo webapps handle a momentary disconnect.

more info on nginx/cookies on

Also worth noting is that you can use more than one webapp per core, we’ve been doing this and up to 2 per core is fine with about 10-20 connections per second from mobile apps using HandleURL as well as browser clients. (xeon 2.5GHz cores)