DEV Community

aafrey
aafrey

Posted on

ELI5: v8 Isolates and Contexts

I'm struggling to find clear information on Isolates vs Contexts vs Processes.

In the v8 JavaScript runtime, what is the role of an Isolate? What is the role of a Context?

How does an Isolate or Context compare to a Unix process?

Oldest comments (9)

Collapse
 
zspencer profile image
Zee

This is a very good question. Let's start first with understanding a Unix process: A place that a particular chunk of code is being executed by the systems processing units, either their CPU or GPU or more. A process is where the Operating System makes space for:

  • Priority - Knowing how important the process is to the operating system, so that as the processor has idle cycles it can apply them in the most valuable order
  • Memory Allocation - Gaining access to memory so the code can store variables, with some affordances for marking the memory as available the data in the variables isn't relevant anymore.
  • Memory Isolation - Confidence that other bits of code cannot access the data you store in your codes variables means malicious actors can't grab the users password from your memory.

This is, of course, an over-simplification. Process prioritization, memory allocation, and memory isolation are all very deeply complex and nuanced topics that fly right over my head most of the time.

The other piece of Unix infrastructure that's worth wrapping ones head around to understand v8 Isolates and Contexts is the notion of a thread.

Threads, like processes, take responsibility for memory isolation and allocation and priority. However, they run within a parent process. The outer operating system isn't expected to know about or have to think about them. It only has to think about the outer process.

This makes starting and terminating threads potentially cheaper computationally; and provides in-application affordances for managing the work being done in those threads potentially independent of the underlying operating system. You don't have to worry about whether the Unix kernel manages memory in this way and Windows kernel manages it in another way, or Android having a different process prioritization schema than iOS. Hooray! This saves cognitive load on developers at the cost of an orchestration layer with some, but hopefully minimal, performance impact.

So what's happening in v8 with Isolates and Contexts?

The role of v8 in the Node/JavaScript ecosystem is approximately that of the "operating system." In fact, you may hear of v8 as being called a JavaScript "Virtual Machine." It's not, but it's pretty darn close.

An Isolate is a mechanism for v8 to isolate and allocate memory to the code that is running. Code running within an isolate cannot read data from code running within another isolate.

A Context is very very similar. However, it appears that multiple chunks of running code may share a context.

Essentially, Isolates are closer to the Processes in Unix, whereas contexts are closer to Threads.

Hopefully this is close enough to ELI 5 :).

Collapse
 
aafrey profile image
aafrey

@zee thank you for this very well crafted response.

My understanding was that an Isolate, to put this in Unix terms, would be an in-process thread, and that you can spawn as many Isolates as you need inside a true Unix process. But when viewed that way, I am not sure where Contexts fit, since you can have many Context inside an Isolate. A lot of this thinking came from research I did, after posting this question.

If I have a program with v8 embedded inside of it, running that program would create a true process in Unix, but inside that process my program might spawn many Isolates that can run in parallel, within the same unix process. These Isolates might then spawn multiple Contexts within an Isolate. Isolates can be run in parallel, but Contexts can not be parallelized within an Isolate, but they can share memory. I guess I'm just not sure what having multiple Contexts in an Isolate will give you?

Does this explanation jive with how you understand v8?

I very much appreciate your response and looking forward to more discussion!

Collapse
 
zspencer profile image
Zee

Hmm, I wasn't aware that a Context is a singleton within an Isolate, but I guess that makes sense that they're a mechanism for sharing data across code in an isolate.

Maybe that's how Chrome winds up sharing state between web pages and web workers on the page?

Thread Thread
 
aafrey profile image
aafrey

@Zee Maybe they aren't? That's just my understanding which could certainly be incorrect. It's difficult to find info outside of the limited description in the v8 API docs of both classes. It seems NodeJS briefly supported Isolates, but no longer, and no Chrome or v8 documentation that I've seen really discusses them in detail even though they seem to be a really huge idea inside v8.

Here is some documentation I found, but it was surprisingly hard to locate!!
v8docs.nodesource.com/node-10.6/d5...
v8docs.nodesource.com/node-10.6/df...

Thread Thread
 
zspencer profile image
Zee

I wonder if this is an instance where a more concrete example of the use case you're trying to apply with Isolates or Contexts would be helpful?

I'm definitely tilting towards the edge of my understanding and competence though, but hopefully I can at least quack a bit and the rubber duck will go on :)

Thread Thread
 
aafrey profile image
aafrey

No particular use-case at the moment, but I was reading more about the Cloudflare Worker runtime which explains how they are using v8 Isolates to create sandboxed serverless workers. Some details here: blog.cloudflare.com/serverless-per...

Thread Thread
 
aafrey profile image
aafrey

@Zee i found some additional examples in NodeJS source, using Isolates in the experimental Workers API: github.com/nodejs/node/blob/master...

Thread Thread
 
aafrey profile image
aafrey

some more details provided by a member of the v8 team, Yang Guo.

twitter.com/hashseed/status/106875...

Thread Thread
 
zspencer profile image
Zee

Nice, that feels like it clears things up a bit more!