wedosoftware logo
Insights

Running user scripts on GraalVM: keeping them fast, flexible and safe

Letting specialists write their own logic can turn a backend into a platform, or make a fast system slow and a safe one unsafe. Practical lessons for running user scripts on GraalVM.

Key takeaways

  • Pass values to scripts as bindings and never paste them into the source: the compile cache keeps working and script injection is prevented.
  • Share a single engine and cache compiled scripts, since compiling costs much more than running.
  • Use a bounded, recycled pool of contexts, and run nested script calls inside the caller’s own context to avoid deadlocks.
  • Choose statement limits and host access deliberately: unrestricted host access amounts to remote code execution as a feature.

Sooner or later every configurable backend hears the same request: “Could we write a bit of logic ourselves, without waiting for a release?” A pricing exception, a validation rule, a mapping for a single partner. Letting business specialists write small scripts that run inside the platform is extremely powerful. It is also one of the simplest ways to turn a fast system into a slow one, or a safe system into an unsafe one.

When user-written scripts sit on the request path, they can easily run millions of times a day. GraalVM is a solid foundation for this, and the lessons below are the ones that count most when you embed it.

Why GraalVM

GraalVM allows a Java application to run JavaScript (and other languages) in the same process, using a modern, standards-compliant JavaScript engine and a just-in-time compiler that speeds up hot scripts. Compared with older embedded engines, it offers:

  • Up-to-date JavaScript that your users already know.
  • Genuine performance once scripts are warm, approaching native code for tight logic.
  • Fine-grained control over what a script may access and how long it may run.
  • Interoperability with Java objects, whenever you decide to permit it.

It is open source as well. Still, it does not make scripting fast or safe on its own; the way you embed it does.

Lesson 1: compile once, run many times

Parsing and compiling a script costs far more than executing it. Cache compiled scripts and share a single engine across the application, so every part of the system benefits from code that has already been parsed and optimised.

The trap: never assemble a script’s source text from data. If a value is pasted into the code, each request yields a “new” script, the cache never hits and the compiler works overtime. Pass values in as variables (bindings) instead. The same rule also shuts the door on script injection.

Lesson 2: pool execution contexts, and put limits on the pool

A GraalVM context is the sandbox in which a script runs, and creating one is not cheap. Keep a bounded pool: each evaluation borrows a context, runs, then returns it. Several details matter a great deal:

  • Bound both the pool and the wait. When every context is busy, callers wait for a limited time and then fail with a clear error, instead of stacking up threads.
  • Recycle contexts after a set number of evaluations, so nothing a script leaves behind can grow without end.
  • Nothing may outlive the lease. Values coming out of a script should be converted to plain Java objects before the context returns to the pool. Holding a reference into a returned context is a bug waiting to happen.
  • Watch out for scripts that call scripts. If a running script calls another one, and that call tries to borrow a second context from a full pool, you can deadlock under load. Nested calls should run in the context the caller already holds.

Lesson 3: slow I/O inside a script costs more than you expect

When a script calls a database or an HTTP service, it keeps its context for the entire wait. Under load this appears not as “slow scripts” but as timeouts while waiting for a context. Keep slow work out of scripts wherever you can, and make it visible where you cannot.

Lesson 4: restrict what a script can do

Decide explicitly how far you trust script authors, and configure GraalVM accordingly:

  • Resource limits. A statement limit prevents an accidental endless loop from tying up a thread forever.
  • Host access. GraalVM can expose the whole of Java, a chosen list of classes and methods, or nothing at all. Scripts from your own trusted specialists may need more; anything an outside user can submit should receive as little as possible: no file system, no threads, no process creation, no arbitrary class lookup.
  • Who may write scripts is a permission like any other, checked on save, with every change versioned and auditable. More on this in keeping AI agents in bounds.

Embedding a scripting engine with full host access amounts to shipping remote code execution as a product feature. That may be acceptable, but only if it was a deliberate decision and you know exactly who can use it.

Lesson 5: reusable code without copy and paste

Once people are writing scripts, they want to share functions between them. Versioned libraries work well: shared code that a script pins to one exact published version. Give the library to the script as a variable rather than concatenating it into the source, so caching keeps working. If published versions never change, nothing compiled from them ever needs to be invalidated.

Lesson 6: measure it like any other hot path

Benchmark script evaluation (JMH is the standard tool on the JVM) and track pool usage, wait times and compile counts in production. In our experience most scripting speed-ups come from these numbers rather than guesswork: a cache that misses because of string concatenation, a conversion running for every value, a pool sized for yesterday’s traffic.

If you are adding scripting to your platform

  • Values enter as bindings, never as part of the source text.
  • One shared engine, with compiled scripts cached.
  • A bounded pool of contexts, with a wait timeout and recycling.
  • Statement limits, and host access configured deliberately.
  • Script authoring as a permission, with versioning and an audit trail.
  • Benchmarks and metrics from the very beginning.

FAQ

Is GraalVM JavaScript fast enough for scripts on the request path?

Yes, once the scripts are warm. GraalVM compiles hot JavaScript just in time, so tight logic runs close to native speed. The usual bottlenecks lie elsewhere: scripts recompiled because data was pasted into their source, too few execution contexts, or slow I/O inside a script.

How do I keep a user script from running forever?

Give each context a statement limit. GraalVM halts the script once it passes that limit, so an accidental endless loop fails fast instead of holding a thread.

Should scripts be able to use Java classes?

Only as far as their authors are trusted. Scripts written by your own specialists may need a few selected classes; anything that outside users can submit should have no host access whatsoever: no file system, threads, processes or arbitrary class lookup.

Why do my scripts slow down under load even though each one is fast?

Usually because they are waiting for a free context. A script that calls a database or HTTP service holds its context for the whole call, so the pool runs dry and callers time out. Monitor pool usage and wait times, and keep slow I/O out of scripts.

Custom scripting turns a backend into a platform that people can shape themselves, and done carefully it is both fast and safe. Are you embedding scripting in your own systems? We would like to hear which engine you chose and what surprised you.

Want a second pair of eyes on your platform?

Tell us what you run. You will get a straight answer from an engineer, not a sales pitch.