Skip to content

Instantly share code, notes, and snippets.



Last active Jul 30, 2018
What would you like to do?
Trying out async FaaS with NATS

You can now evaluate asynchronous functions with FaaS backed by the NATS Streaming server.

  • Checkout the branch and build the image: functions/gateway:latest-dev
$ git clone
$ git checkout update_async

Optionally also build the branch:

$ ./
  • Deploy the stack
$ docker stack rm func
$ ./

You'll now see the familiar gateway at http://localhost:8080

Next check the health of the functions - make sure they all have at least 1 replica running

$ docker service ls

Next invoke one of the functions in the sample stack:

$ curl localhost:8080/function/func_markdown/ -d "## Testing"

The response will be returned to the terminal

  • Now invoke it asynchronously:
$ curl -v localhost:8080/async-function/func_markdown/ -d "## Testing"
< HTTP/1.1 202 Accepted

You won't see a response here - just a status 202 to say that the gateway accepted your request.

To try out any of the other functions you can replace "function" in the route with "async-function".

  • Check the logs of the queue worker
$ docker service logs -f func_queue-worker

func_queue-worker.1.s23hrotlqllx@moby    | Listening on [faas-request], clientID=[faas-worker-e2534c0ea217], qgroup=[faas] durable=[]
func_queue-worker.1.s23hrotlqllx@moby    | [#1] Received on [faas-request]: 'sequence:1 subject:"faas-request" data:"{\"Header\":{\"Accept\":[\"*/*\"],\"Content-Length\":[\"10\"],\"Content-Type\":[\"application/x-www-form-urlencoded\"],\"User-Agent\":[\"curl/7.43.0\"]},\"Body\":\"IyMgVGVzdGluZw==\",\"Method\":\"POST\",\"CallbackURL\":{\"Scheme\":\"\",\"Opaque\":\"\",\"User\":null,\"Host\":\"\",\"Path\":\"\",\"RawPath\":\"\",\"ForceQuery\":false,\"RawQuery\":\"\",\"Fragment\":\"\"},\"QueryString\":\"\",\"Function\":\"func_markdown\"}" timestamp:1499942388625813443 '
func_queue-worker.1.s23hrotlqllx@moby    | Request for func_markdown.
func_queue-worker.1.s23hrotlqllx@moby    | <h2>Testing</h2>
func_queue-worker.1.s23hrotlqllx@moby    | 
func_queue-worker.1.s23hrotlqllx@moby    | 
func_queue-worker.1.s23hrotlqllx@moby    | 200 OK <nil>

You should see the request and response from your calls appearing above.

  • Provide a callback-URL

Now head over to and create a new request bin.

Recall your earlier curl statement, and modify it to pass an additional header:

$ curl -v localhost:8080/async-function/func_hubstats/ -d "alexellis2" -H "X-Callback-Url:"

Now refresh your Request Bin page. You should see that FaaS called into it after completing the task.

  • Scale the queue-worker
$ docker service scale func_queue-worker=3

Now use a loop to invoke the function - passing in an integer each time so we can easily identify the request.

You will see that each request is picked up by a separate worker in a round-robbin fashion.

$ for i in {0..20} ; do echo $i && curl localhost:8080/async-function/func_markdown/ -d $i ; done

Check the logs again:

func_queue-worker.3.u0egkphip73n@moby    | <p>2</p>
func_queue-worker.1.s23hrotlqllx@moby    | <p>0</p>
func_queue-worker.2.qfmmizq0pheh@moby    | <p>1</p>
func_queue-worker.2.qfmmizq0pheh@moby    | <p>4</p>
func_queue-worker.3.u0egkphip73n@moby    | <p>5</p>
func_queue-worker.1.s23hrotlqllx@moby    | <p>3</p>


The work was not processed in the order that it was queued - this is perfectly normal. The queue is being depleted by three separate workers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment