Foxx Microservices
Traditionally, server-side projects have been developed as standalone applicationsthat guide the communication between the client-side frontend and the databasebackend. This has led to applications that were either developed as singlemonoliths or that duplicated data access and domain logic across all servicesthat had to access the database. Additionally, tools to abstract away theunderlying database calls could incur a lot of network overhead when using remotedatabases without careful optimization.
ArangoDB allows application developers to write their data access and domain logicas microservices running directly within the database with native access toin-memory data. The Foxx microservice framework makes it easy to extendArangoDB’s own REST API with custom HTTP endpoints using modern JavaScript runningon the same V8 engine you know from Node.js and the Google Chrome web browser.
Unlike traditional approaches to storing logic in the database (like storedprocedures), these microservices can be written as regular structured JavaScriptapplications that can be easily distributed and version controlled. Depending onyour project’s needs Foxx can be used to build anything from optimized RESTendpoints performing complex data access to entire standalone applicationsrunning directly inside the database.
How it works
Foxx services consist of JavaScript code running in the V8 JavaScript runtimeembedded inside ArangoDB. Each service is mounted in each available V8 context(the number of contexts can be adjusted in the server configuration).Incoming requests are distributed across these contexts automatically.
If you’re coming from another JavaScript environment like Node.js this issimilar to running multiple Node.js processes behind a load balancer:you should not rely on server-side state (other than the database itself)between different requests as there is no way of making sure consecutiverequests will be handled in the same context.
Because the JavaScript code is running inside the database another differenceis that all Foxx and ArangoDB APIs are purely synchronous and should beconsidered blocking. This is especially important for transactions,which in ArangoDB can execute arbitrary code but may have to lockentire collections (effectively preventing any data to be written)until the code has completed.
Compatibility caveats
Unlike JavaScript in browsers or Node.js, the JavaScript environmentin ArangoDB is synchronous. This means any code that depends on asynchronousbehavior like promises or setTimeout
will not behave correctly inArangoDB or Foxx. Additionally, ArangoDB does not support native extensionsunlike Node.js. All code has to be implemented in pure JavaScript.
While ArangoDB provides a lot of compatibility code to support code writtenfor Node.js, some Node.js built-in modules can not be provided by ArangoDB.For a closer look at the Node.js modules ArangoDB does ordoes not provide check outthe appendix on JavaScript modules.
When using bundled node modules keep in mindthat these restrictions not only apply to the modules themselves but alsothe node dependencies of those modules. As a rule of thumb:
Modules written to work in Node.js and the browser that do notrely on async behavior should generally work
Modules that rely on network or filesystem I/O or make heavy useof async behavior most likely will not