Azure Functions
medium 31 minAzure 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
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
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
criticalIt runs again at each replay and may answer differently.
Wrongdef flow(context): price = requests.get(URL).json()Rightdef flow(context): price = yield context.call_activity("get_price", None)Swallowing errors
criticalThe platform takes a normal return as success and removes the message.
Wrongtry: save(msg)except Exception as e: log(e)Righttry: save(msg)except Exception as e: log(e) raiseNo wait between tries
majorFive 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
majorYou pay for two, and the chain is cut at 230 seconds.
Wrongdef a(req): return requests.post(FUNCTION_B, json=x)Rightdef 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.