State

All of a users' functions have access to shared state. This state is implemented asa simple key-value store and accessed at a low level through the Faasm host interface,and through higher level language-specific libraries.

The Faasm runtime ensures this state is shared efficiently across the cluster, takingcare of all underlying replication and synchronisation.

Under the hood state is stored as byte arrays, making it language-agnostic and easy to integratewith WebAssembly.

Faasm provides some simple wrappers around state operations, e.g. in C++:

  1. #include "faasm/faasm.h"
  2.  
  3. FAASM_MAIN_FUNC() {
  4. const char *key = "my_state_key";
  5.  
  6. // Read the state into a buffer
  7. long stateSize = 123;
  8. uint8_t *myState = new uint8_t[stateSize];
  9. faasmReadState(key, newState, stateSize);
  10.  
  11. // Do something useful, modify state
  12.  
  13. // Write the updated state
  14. faasmWriteState(key, myState, stateSize);
  15.  
  16. return 0;
  17. }

and Python:

  1. from pyfaasm.code import faasm_main
  2. from pyfaasm.state import get_state, set_state
  3.  
  4. @faasm_main
  5. def my_func():
  6. # Read the state
  7. key = "myKey"
  8. state_val = get_state(key)
  9.  
  10. # Do something useful
  11.  
  12. # Set an updated value
  13. set_state(key, state_val)

Offset state

When operating in parallel on larger state values, it may be unnecessary to loadthe full value into memory for every function instance. For example, many functionsoperating in parallel on a large matrix may only access a few rows or columns each.In this scenario it's unnecessarily expensive and slow to load the full matrix intoevery function.

To cater for this, Faasm state values are byte-addressable, i.e. each function canexplicitly access only a subsection of the value, and the Faasm runtime will ensureonly the necessary data is transferred. This can improve performance and reducecost in large data-intensive applications.

The low-level offset state operations are part of theFaasm host interface, and explained in more detail inour paper.