AlgoViz

Synchronous communication

easy 24 min

In synchronous communication the caller sends a request and needs the response before that piece of work can go on.

See it run

Ask, wait, continue. Then what happens with chains, parallel calls and full pools.

Operations

One request

A client asks and cannot go on until the answer comes.

Step 1 of 5

Client idle

ClientreadyServiceidle

The client is ready to make a request.

Step 1: Client idle

response = session.get(url, timeout=(0.5, 2))   # waits hereresponse.raise_for_status()show(response.json())
User waits
One call
Needs up
One service

Say what waits: the user's request, the thread, or both.

In the real world

The same synchronous communication doing real jobs. Pick one and step through it.

The user cannot go on without the answer.

Step 1 of 2

Send

POST /auth/loginClientwaitingServicechecks password

POST /auth/login goes to the login service.

Step 1: Send

Common mistakes

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

  • No timeout

    critical

    One hanging server holds threads until the pool is empty.

    Wrong
    requests.get(url)
    Right
    requests.get(url, timeout=(0.5, 2))
  • Sequential calls that are independent

    major

    The delays add up for no reason.

    Wrong
    user = get_user(id)orders = get_orders(id)
    Right
    user, orders = run_parallel(    lambda: get_user(id),    lambda: get_orders(id))
  • Waiting for work nobody needs now

    major

    A slow mail server delays, or fails, the sign-up.

    Wrong
    save(user)send_email(user)         # 2 sreturn "welcome"
    Right
    save(user)queue.add("email", user.id)return "welcome"
  • Treating two calls as a transaction

    critical

    The charge can succeed and the order fail.

    Wrong
    payments.charge(order)orders.create(order)     # fails: charged, no order
    Right
    order = orders.create(order, status="pending")payments.charge(order, key=order.id)orders.confirm(order)    # repair pending ones later

Interview tips

  • How to explain it

    • The caller needs the answer to go on.
    • Simple, immediate, tightly coupled.
    • Waiting is not the same as blocking a thread.
  • Memory trick: SYNC

    • Send the request.
    • Yield your turn, No other work.
    • Complete response needed.
  • Use it for

    • Login, search, validation.
    • Anything a person waits for.
    • Short work with clear errors.
  • Watch

    • Chains: delays add.
    • Pools: arrivals x time.
    • P99 values do not add up.