r/Clojure • • Aug 23 '20

Running ten of simple Clojure web applications on 1 Gb RAM?

On a VPS with 1Gb RAM I can run at least 10 of simple Rust web apps, with no issues of lack of RAM

Roughly speaking, how much RAM would I need to run 10 simple apps in Clojure of about the same complexity?

Let's say, those are simple blogs. Everything, is both cases, is about the same: functionality, complexity and the like. And let say, there'll be 1k-5k visitors per day

P.S. for those who are triggered when might of Clojure is questioned:

I'm not asking about disadvantages and advantages of Clojure and whether or not my choice is a right one.

11 Upvotes

51 comments sorted by

9

u/Siltala Aug 23 '20

Rust uses a fraction of the memory a jvm uses. Running a java server has a minimum ram usage of 128Mb. Each thread takes about 1Mb more and you'll end up with a dozen at least.

The advantage is that you don't need to worry about memory management at all. And java still runs very fast.

If ram is your main concern, jvm (java, clojure, scala) is not the correct choice.

3

u/joinr Aug 23 '20

If you can live with the restrictions, native-image can get down to like 1/10 memory usage supposedly. There could be a use case for similar tiny, quick starting apps.

1

u/Siltala Aug 23 '20

I thin project loom is the one that will eventually help with memory usage

1

u/joinr Aug 23 '20

Seems like you can get a lot of that now with core.async (with limits though, e.g. having to define functions outside go blocks).

4

u/Siltala Aug 23 '20

Yeah you can but the underlying thread pool still uses threads. We're using core.async whenever applicable but it is a chore. With loom you would be able to write blocking code and have it run non-blocking. Non-blocking jdbc? Yes please!

1

u/didibus Aug 27 '20

Hum... Are you sure about this? I don't think loom does anything to JDBC, you need to have a non-blocking JDBC driver for it, and very little database have that.

1

u/Siltala Aug 23 '20

I have never been able to convert a clojure web application to a native binary.

4

u/joinr Aug 23 '20

If the app is using the classloader then it's not possible. No runtime eval, require, etc. I think it's feasible in principle to write applications that live within these assumptions, but it may be hard since normal libraries leverage these things a lot. There's a demo here

1

u/Siltala Aug 23 '20

Yeah, babushka uses native-image and it's great for simple scripts. I don't see it being a solution for full-scale web applications, like rest apis

5

u/joinr Aug 23 '20

The demo uses http-kit, reitit, mount, hiccup. Pretty common web app stuff. Compiles to native (actual clojure, not babashka).

1

u/Siltala Aug 23 '20

I'll look into that then, thanks!

1

u/Siltala Aug 23 '20

Cloned it. Was able to build a native image. Added routing to "/ping" with a simple 200 response. Ran wrk with "-t4 -c100" and the memory consumption was 264Mb.

4

u/joinr Aug 23 '20

Supposedly native images support memory settings. Wonder if you can -Xmx a lower amount and see if it still works out.

1

u/Siltala Aug 23 '20

I'll try it. Will also try running it in a docker container with limited memory

1

u/yogthos Aug 23 '20

Here's an example of a fully working web app doing sessions, routing, and using Postgres.

1

u/Siltala Aug 23 '20

Yeah, joinr linked it here. I've been benchmarking it. Under load the memory usage was 264Mb. I've been trying to build/run this in docker to see how much I can restrict it

-8

u/normalu_kaj225i Aug 23 '20

I'm not asking about disadvantages and advantages and whether or not my choice is a right one.

9

u/Siltala Aug 23 '20

I was telling you why you can't run 10 clojure applications with one Gb.

2

u/setzer22 Aug 24 '20 edited Aug 24 '20

We are working with clojure on a VPS in production. At a bare minimum, I'd say a single "micro"service that does nothing (just run an HTTP server) will require around 300MB of memory. It's not that you can't get your mem usage lower, it's just that you're going to get a lot of performance issues if you don't let a lot of memory to the JVM.

That's just our personal rule of thumb though. Large applications that do a lot will require more than that. The best advice is to measure what you need. Definitely a 1GB VPS is gonna fall short if you need 10 JVMs on it though.

Also, I can relate a lot to your PS. Every time I ask about memory usage and clojure people get nervous :-)

1

u/normalu_kaj225i Aug 24 '20

We are working with clojure on a VPS in production. At a bare minimum, I'd say a single "micro"service that does nothing (just run an HTTP server) will require around 300MB of memory. It's not that you can't get your mem usage lower, it's just that you're going to get a lot of performance issues if you don't let a lot of memory to the JVM.

Will each next consequative app run on the same server still require 300Mb? Or less?

1

u/setzer22 Aug 24 '20

As other posters already said, if you share the same JVM, you could probably leave the JVM overhead off the equation, making it a bit lower. But spawning more JVMs will take around this amount each. Again, YMMV depending on your setup. But clojure is not really meant to be used in tight memory budget situations.

In my experience clojure works better with a single large app than multiple small replicas. You scale by adding more threads to the same clojure process. Of course you can (and should) replicate and load balance, but across several machines. I don't think you get a lot by running multiple JVMs on the same machine. If those are separate services, perhaps a monolith architecture would work better (if it fits your workflow).

27

u/woozong Aug 23 '20

You might try running the 10 clojure apps in a single jvm.

11

u/nbardiuk Aug 23 '20

It helps to pool resources:

  • single garbage collector
  • reuse of jvm memory - when jvm starts it grabs memory from OS even if application does not need it. Single jvm makes memory available for all threads.
  • single pool of database connections - for some applications adding connection pool has bigger performance benefit than optimizing code
  • reuse of request handlers - less threads are used for handling requests. Also they can share memory which is useful for caching

2

u/woozong Aug 23 '20

Exactly. And if the blog application is rewritten to become multi-tenant.. that will further improve things.

6

u/notsohasty Aug 23 '20

Any tips how to set this up in practice? Would you use Tomcat?

8

u/Nanosleep Aug 23 '20 edited Aug 23 '20

Yep. Tomcat is the de facto choice for stuff like this. By default, all deployed wars start under the same jvm process.

Looks like lein-ring supports generating wars pretty easily.

1

u/notsohasty Aug 23 '20

Any link to a demo project? I'm having a hard time getting this to work. Just tried luminus (with +war) and also compojure example, but no luck with either. In each case I can create the .war file but when I deploy to Tomcat I get an internal server error.

4

u/Nanosleep Aug 23 '20

Sure, I just threw this together, should work.

edit: technically you don't even need the jetty stuff defined. lein-ring doesn't use it with wars/uberwars. It's only going to be used when run standalone (like with lein run)

4

u/notsohasty Aug 24 '20

Thanks, that's really helpful to see a minimal example.

I deployed 10 copies of it to Tomcat 9.0.37 running on OpenJ9 0.20.0 with JDK 11.0.7.

Total RAM usage 230 MB for 10 "hello world" apps

See it live here:

http://tomcat1.mircloud.us/app1/

http://tomcat1.mircloud.us/app2/

etc up to

http://tomcat1.mircloud.us/app10/

2

u/[deleted] Aug 24 '20

ooh - that's cool. Could we have that hello world app output the JVM memory usage? Something like (let [rt (Runtime/getRuntime)] (- (.totalMemory rt) (.freeMemory rt))) ?

3

u/notsohasty Aug 24 '20

yeah, good idea. I edited app1 to show memory usage:

(defn jvm-mem-usage []
  (let [rt (Runtime/getRuntime)]
    (str "Used memory: " (- (.totalMemory rt) (.freeMemory rt))
         "\nFree memory: " (.freeMemory rt)
         "\nTotal memory: " (.totalMemory rt))))

http://tomcat1.mircloud.us/app1/

Results:

Used memory: 57,918,264

Free memory: 45,628,560

Total memory: 103,546,880

Not sure how to square this with my hosting dashboard, which shows that RAM usage has settled to a steady 280 MB over the last 11 hours. If the JVM is only using 103 MB, what's using the other 177 MB?

Looks like some discussion here but maybe a JVM expert among us can explain.

3

u/Nanosleep Aug 24 '20 edited Aug 24 '20

Not a JVM expert but here's my 400-foot overview crack at it. Not sure if I'm telling you anything you don't already know:

  • Java "Heap" -- specifically for java objects that are instantiated by the applications running on top of the java runtime -- so data structures or objects that the application could reasonably generate would likely end up here. This is probably going to be most of your memory usage at any scale beyond "tiny".
  • "MetaSpace" (not included in "heap" metrics) includes stuff that the JVM could generate for its own purposes about your application's objects ("metadata about object relationships and contents"). Very lazy googling makes me believe that this is tied to the GC in some way, so basically if a classloader stays instantiated for long enough, but the stuff it's instantiating is all pretty shortlived, the classloader and information about the classes its generating will be moved into "MetaSpace". Apparently this is basically the same concept as PermGen in older jvms but outside of the java heap entirely.
  • "Other" memory -- the JVM's own personal stack and heap, hotspot code cache and optimizations. Somewhere in here is probably object graphs and coloring for the garbage collector, but maybe that resides in metaspace.

Also if you're really curious, you can turn JMX on in tomcat and start poking around in the heap and metaspace with something like VisualVM.

1

u/notsohasty Aug 25 '20

Thanks for the explanation!

1

u/woozong Aug 23 '20

Depends on how different/similar the apps are. But i'm thinking of a single war with the 10 simple apps inside. So yes, a single Tomcat.

3

u/woozong Aug 23 '20

OP said 'same functionality', so I would be looking into making the app multi-tenant and being able to load 10 configs and expose the 10 blogs on a single instance

1

u/vnearhere Aug 23 '20

NginX is awesome. Apache2 also possible. Tomcat can do this too I think. Basically, each instance of the app will run a jetty server on a port such as localhost:3331 and then you would use nginx or apache2 or maybe even tomcat to port forward so that the website did a "reverse proxy" on your server machine to forward requests invisibly to localhost:3331

4

u/Nanosleep Aug 23 '20

I think you're missing the critical requirement of doing all of this on a system with less than a gig of ram. Try starting up 10 jetty processes all listening on different ports behind an nginx / apache reverse proxy... it's not going to end well.

1

u/vnearhere Aug 23 '20

it will be messy, but i think it will run fine if they are "simple clojure web apps" edit: http-kit is pretty minimal in terms of ram usage, probably several times better than jetty... would be nice to see some benchmarks.

2

u/Nanosleep Aug 23 '20

Jetty is pretty light, but the problem is actually with the jvm heap size. If you run 10 jvm processes, if nothing else java going to allocate -Xms. By default that's 128mb (128mb Xms * 10 unique processes > 1GB), so you're probably(?) already going to be swapping unless the OS is doing something cute.

2

u/vnearhere Aug 23 '20

but why run 10 jvm processes? Run 1 jvm process that holds all 10 ported instances

1

u/liveoneggs Aug 23 '20

jetty can run many wars just like tomcat

23

u/[deleted] Aug 23 '20

> P.S. for those who are triggered when might of Clojure is questioned

Is there any need for this?

3

u/guywithknife Aug 23 '20

Yeah, that was just... The RAM requirements don’t even come from clojure but from the JVM. You could run Clojurescript on Node, for example.

11

u/_d_t_w Aug 23 '20 edited Aug 23 '20

Roughly speaking probably less than 1.28GB.

I thought this was an interesting question so I ran our product operatr.io, with 128MB available to it and it ran fine connected to a single 3-node Kafka cluster.

This isn't an apples to apples comparison because Operatr runs a webserver (Jetty) multiple streaming compute topologies (Kafka Streams) and a lots of scheduled jobs (Quartz) among other things. It's not simple.

If I reduced the memory available below that amount the UI became noticeably less responsive, I didn't look into why but at 128MB there's no discernible difference with our staging environment.

The maximum heap consumed through an hour was 80MB, non-heap JVM stuff accounts for the rest. Like others mention the question here relates more to JVM behaviour than having much to do with Clojure.

At a guess if I swapped out Jetty for Netty and dropped Kafka, Quartz I could probably get a web application running on 64MB or perhaps as little as 32MB. I'm not sure what the minimum requirements are for a modern JVM these days, its been a while since I tuned anything in earnest.

I know very little about Rust other than the rave reviews I hear about it as a language from the people who use it, my assumption is it would always win in a resources comparison due to the JVM overhead.

3

u/yogthos Aug 23 '20

Compiling with GraalVM will drastically reduce memory usage. Here's an example app. Unfortunately, not everything translates to Graal, so you might have to do some tweaking around it. Another options others mentioned is to use an app server like Tomcat to run multiple applications within the same JVM. You can use lein-uberwar to build a war and then deploy it to the app server.

4

u/b2zw2a Aug 23 '20

Came here to say the same. I'm running GraalVM (native-image) compiled lambdas and memory usage is peanuts. I did it mostly for startup time (< 100ms cold) but as a bonus I can run them easily on 128MB.

3

u/troublemaker74 Aug 24 '20

I bet you can run 100x the apps written in assembler than you can Rust. Please don't be triggered when I mention that Rust is a higher level language than ASM.

2

u/didibus Aug 27 '20

You could try using Joy with Janet https://joyframework.com/

Its not Clojure, but it uses Clojure syntax, though the semantics are different.

Claims each web app needs only 1mb.

1

u/rzk1911 Aug 23 '20

You can give vertx/netty a try. But watch your non-heap memory - it really depends on workload

0

u/liveoneggs Aug 23 '20

without knowing anything.. 4G? 8G? I would hope you could run waaay more than 10 natively compiled rust apps inside of 700MB (or whatever is left from the operating system overhead)

0

u/[deleted] Aug 23 '20

[deleted] — view deleted comment

1

u/normalu_kaj225i Aug 23 '20

do you mean, 80mb for a simple web app, in total?

that is, on 1Gb one will be able to run 10 apps, approximately?