AlgoViz

AWS Lambda

medium 25 min

AWS Lambda runs your function when an event arrives and stops charging when it ends.

See it run

Start cold, start warm, count environments, and see who retries when something fails.

Operations

Cold start

No environment exists yet.

Step 1 of 6

Nothing runs

Lambda serviceCallerFront endchecks limitsEnvironmentnoneCode storagecodeLogs

The function exists as code. There is no environment.

Step 1: Nothing runs

def invoke(event):    check_permissions_and_limits()    env = free_environment()    if env is None:        env = create()            # download, start runtime        env.run_init_code()       # outside the handler    result = env.handler(event)    env.freeze()    return result
Extra delay
Under 100 ms to over 1 s
You pay
Init and handler time
Warm start
no init

Your own init code is usually the largest part, and the part you control.

In the real world

The same aws lambda doing real jobs. Pick one and step through it.

The function scales, the database does not.

Step 1 of 4

Morning

Our systemUsersFunction40 environmentsPostgreSQL
Connections used
20%

Forty environments hold forty connections.

Step 1: Morning

Common mistakes

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

  • A client for every call

    major

    Each request pays for a new connection, and the database fills up.

    Wrong
    def handler(e, c):    db = connect()
    Right
    db = connect()def handler(e, c): ...
  • Mark done, then work

    critical

    A crash in between loses the event, because the retry sees the mark.

    Wrong
    mark(event_id)charge(order)
    Right
    charge(order, idempotency_key=event_id)# or effect and mark in one write
  • Expecting reserved to be warm

    minor

    Reserved concurrency is a share and a cap. Cold starts stay.

    Wrong
    reserved = 10  # "no cold starts"
    Right
    provisioned = 10
  • No failure destination

    critical

    After the last retry, an asynchronous event is discarded.

    Wrong
    invoke(type="Event")
    Right
    on_failure = queue_arn

Interview tips

  • How to explain it

    • Code runs when an event arrives.
    • One request per environment, warm if reused.
    • Concurrency is rate times duration.
  • Memory trick

    • Ring: a helper.
    • Apron: the cold start.
    • Full house: 429.
  • Reach for it when

    • Work is short and event-driven.
    • Traffic is uneven.
    • Nobody wants to run servers.
  • Be careful when

    • Load is steady and high.
    • A small database sits behind it.
    • One pool serves several teams.