How do you scale your Adonis apps?

Just wondering how other scale their adonis apps when their userbase starts to increase the load on it.
I was thinking when i get the stage when i need to scale up adonis/server, a horizontal approach would be best.
Put a load balancer in front of adonis, have all session,app,data within redis and mysql so i can scale that too. Then i can boot up multiple instances of the adonis app when needed.
I was thinking of an internal only microservice for certain tasks if needed, but if i have multiple adonis apps running it might not be needed, and just have it all within adonis.

Hi @PixelPaul!

This is quite interesting topic :slight_smile:

As long as you don’t need to scale up WS it’s really easy. Just spin up more containers / servers.
Easiest would be to use pm2. With PM2 it’s really straightforward. Just tell it to start how many Adonis instances you want. Also Adonis has built in support for WS clustering (https://adonisjs.com/docs/4.1/websocket#_cluster_support). Haven’t ever used it myself tho

When using Docker then another easy way is to use docker-compose built in scale

More complex would be using Kubernetes or I really reccommend learning Rancher for projects that don’t need full multi-cloud support. Rancher is really easy to use on single server. Even tho it supports multi-cloud and multi-cluster etc. it’s easier to get managed Kubernetes from Google, AWS or DigitalOcean (maybe there are more providers) when you reach that scale. Or find sysadmin so you can focus on app, not updating underlying provisioners, load-balancers etc
With Kubernetes / Rancher you also get automatic rolling releases, A/B tests, health checks, autoscaling and a lot of goodies (but it also has quite big learning curve and might be huge overkill, don’t use it just because of hype)

When scaling WS then things might get little bit more complicated. Depends on your structure, but you might need to keep all separate Adonis apps up to date with WS updates. That’s usually done thro Redis or something similar (RethinkDB is also interesting, but it was killed years ago and is slowly revived now).

Also I find it a lot easier to deal with scaling when using JWT as auth, not session. With sessions you have to add sticky rules to your load-balancers or make whole session check go thro Redis / something similar so that all Adonis instances know who is logged in and who is not. With JWT it no such thing is needed.

About Microservices here’s good discussion: MIcroservices with AdonisJS
And I’d reccomend to use Adonis API only or Adonis Slim for microservice :slight_smile:

2 Likes

adonis cluster would be interesting. Do you know of any good articles on setup and getting it working well with PM2? Sounds like that would be the easiest and just put a load balancer in front if it.

There is no need for loadbalancer. PM2 Will do it for you. Just start Adonis with

pm2 start server.js -i max
2 Likes

if you use stateless architecture (token auth …) you can cluster your app using one of those techniques/technlogies
1 - use nodejs cluster and adapt the server.js : https://nodejs.org/api/cluster.html
2 - use pm2 : out of the box cluster/monitoring
3 - deploy your app on kubernetes or docker as service with replicas as you want

good luck

1 Like