AlgoViz

Azure Functions

medium 31 min

Azure Functions runs small pieces of code when something happens: a request, a message, a timer, a stored file.

See it run

Follow a trigger through its bindings, scale on a backlog, lose a message safely and replay a durable flow.

Operations

Trigger and bindings

An order is accepted and saved by two functions.

Step 1 of 4

HTTP trigger

Our AzureUsersacceptHTTP triggerorders queueemptyworkqueue triggerPoison queueemptyCosmos DB

A request starts accept, and nothing else.

Step 1: HTTP trigger

@app.route(route="orders")@app.queue_output(arg_name="msg", queue_name="orders")def accept(req, msg):    msg.set(req.get_body())        # the binding sends it    return HttpResponse(status_code=202)@app.queue_trigger(arg_name="msg", queue_name="orders")def work(msg):    save(msg)
Time
milliseconds, then later
What you need
one queue

One trigger, any number of bindings.

In the real world

The same azure functions doing real jobs. Pick one and step through it.

More instances make it worse.

Step 1 of 3

Slow

Our AzureUsersacceptHTTP triggerorders queue500 waitingworkqueue triggerPoison queueemptyCosmos DBslow

Cosmos DB answers slowly, so messages wait longer.

Step 1: Slow

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Network call in an orchestrator

    critical

    It runs again at each replay and may answer differently.

    Wrong
    def flow(context):    price = requests.get(URL).json()
    Right
    def flow(context):    price = yield context.call_activity("get_price", None)
  • Swallowing errors

    critical

    The platform takes a normal return as success and removes the message.

    Wrong
    try:    save(msg)except Exception as e:    log(e)
    Right
    try:    save(msg)except Exception as e:    log(e)    raise
  • No wait between tries

    major

    Five tries are used up within a moment, and a short outage fills the poison queue.

    Wrong
    "queues": {}
    Right
    "queues": { "visibilityTimeout": "00:00:30" }
  • Waiting for another function over HTTP

    major

    You pay for two, and the chain is cut at 230 seconds.

    Wrong
    def a(req):    return requests.post(FUNCTION_B, json=x)
    Right
    def flow(context):    x = yield context.call_activity("a", None)    yield context.call_activity("b", x)

Interview tips

  • How to explain it

    • One trigger per function.
    • Bindings move the data.
    • The plan decides scale.
  • Memory trick

    • 230 seconds for HTTP.
    • Five tries, then poison.
    • Orchestrator plans, activity acts.
  • Reach for it when

    • Azure services raise events.
    • A process has waits and undo.
    • Load has quiet hours.
  • Be careful with

    • Python concurrency of one.
    • Changing a running flow.
    • Scaling into a weak database.