# asynchronous and synchronous communications

* Can I do work while waiting?
  * yes -> asynchronous
* History
  * everythign was synchronous

## synchronous IO

* the caller waits for a response from receiver
  * E.g. Asking someone a question in a meeting
* Caller sends a request and blocks Caller
  * must wait for request to finish processing until it continues
  * Caller cannot execute any code while waiting
  * caller/client and receiver/server are in sync
* When the request is made to fetch the data, the cpu is waiting for the hardware/network to do it's thing and return with the data.
* ie IO - files, http requests
* Example
  * Program asks OS to read from disk
  * Program main thread is taken off of the CPU
    * As the cpu is waiting for the file to be read from the hard drive, it can context switch and do something else, but the program is still waiting for the cpu to finish processing
  * Read completes, program can resume execution

## Asynchronous IO

* the response can come whenever. Caller and receiver can do anything meanwhile
* flow
  * Caller sends a request
  * Caller can work until it gets a response (cpu context switches)
  * Caller either:
    * Checks if the response is ready (epoll)
    * Receiver calls back when it's done (io\_uring)
    * Spins up a new thread that blocks, while the main thread carries on processing
      * Thread finish reading and calls back main thread
  * Caller and receiver are not necessary in sync

### Examples

* Asynchronous Programming (promises/futures)
* Asynchronous backend processing
  * flush to queue with job id, and some call (poll, push) checks the job id to see if complete
  * client sends work for backend to do, the client can still carry on processing, but it is still waiting for the backend to comeback with the completed work
    * from client perspective it is aysnc, but the backend is sync
* Asynchronous commits in postgres
  * To do with transactions (executing multiple actions) and the commit confirms all of them if they were all validated and no failures
* Asynchronous IO in Linux (epoll, io\_uring)
* Asynchronous replication
* Asynchronous OS fsync (fs cache)

## Clients

* in past, they were synchronous/blocking
* Now most of them are aysnchronous

## Links

* <https://www.fridayfeedback.com/p/asynchronous-communication/>
* <https://dzone.com/articles/microservices-why-asynchronous-communications>
* <https://dzone.com/articles/communicating-between-microservices>
* <https://danieltammadge.com/2021/01/event-driven-vs-request-driven-architecture-user-registration/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanfak.gitbook.io/workspace/integration/aync-sync-comm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
