Synchronous communication
easy 24 minIn 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
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/login goes to the login service.
Step 1: Send
Common mistakes
The bugs interviewers watch for, with the fix beside each.
No timeout
criticalOne hanging server holds threads until the pool is empty.
Wrongrequests.get(url)Rightrequests.get(url, timeout=(0.5, 2))Sequential calls that are independent
majorThe delays add up for no reason.
Wronguser = get_user(id)orders = get_orders(id)Rightuser, orders = run_parallel( lambda: get_user(id), lambda: get_orders(id))Waiting for work nobody needs now
majorA slow mail server delays, or fails, the sign-up.
Wrongsave(user)send_email(user) # 2 sreturn "welcome"Rightsave(user)queue.add("email", user.id)return "welcome"Treating two calls as a transaction
criticalThe charge can succeed and the order fail.
Wrongpayments.charge(order)orders.create(order) # fails: charged, no orderRightorder = 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.