# Chapter 8

### Single Node

* Single computer system, result is deterministic, hardware problem - > blue screen, this is a good choice(deliberate), we prefer to a computer to crash completely rather than returning a wrong result

### Scientific Computing: a giant compter

### Commercial Data Center:

* Cost efficiency matters, use cheap commodity hardwares
* something is always broken

### Unreliable network

* request may have been lost

* request may be waiting in a queue

* the remote node may have failed

* the remote node may have temporarily stopped

* no absolute reliable fault detector

* widely acceptable indicator is timeout, could not tell if it's node failure or network failure, retry few times when possible

* a request could be called several times and results in disastrous outcomes. **Idempotent**. <https://www.i3geek.com/archives/841> call 多次，返回相同结果

* 如何让一个system 变得idempotent？设计API的时候

* network partitions: 写到A， 读B

* without testing, the network failure could break the fundamental assumptions

* network partition naturally happens, we can only resolve it when it happens, we can not prevent it from happening. CAP theory -> CP or AP (consistency, availability and partition)

### network delays are chosen by design

* traditionally telephones lines are almost guaranteed no delays, focus on latency
* network use patterns are burst mode, system design is focusing on the maximum throughput / resource utilization
* similar thing happens with retail time OS

### Unreliable Clock

1. has this request time out yet?
2. what's the 99th percentile response time of this service?  <https://engineering.linkedin.com/performance/who-moved-my-99th-percentile-latency>
3. how many queries per second did this service handle on average in the last five minutes?
4. how long did the user spend on our site?
5. when was this article published?
6. at what date and time should the reminder email be sent?
7. when does this cache entry expire?
8. what is the timestamp on this error message in the log file?

Most commonly used mechanism is the Network Time Protocol (NTP) Individual NTP is not reliable.

### Two type of clock

1. time-of-day. It returns the current date and time according to some calendar. Java System.currentTimeMillis() and Linux: clock\_*gettime(CLOCK\_REALTIME*)
   1. highly unreliable, could jump forward, backward.
   2. in google spanner, time drift assumption is 30 secs per 24 hrs
2. . monotonic clock is suitable for measuring a duration (time interval).&#x20;
   1. clock\_gettime(CLOCK\_MONOTONIC) on linux and  System.nanoTime() in Java
   2. duration could be measured relatively accurate

### Why assumption based on clock is very dangerous

* most of the components in distributed system provides accurate deterministic result of crash. Not clock

### Caveat of last write win (Cassandra, can not used for bank transaction) / DynamoDB can write client config resolution

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdMm86vWqEHwDkBLjS%2F-MMdNMsopQt_sA0ZSE6O%2Fimage.png?alt=media\&token=2f1dadae-947c-4897-8bf5-eccc51b04a25)

* 后来的request的time clock比其他的慢，request会被覆盖
* distributed id generator (UUID) -> very difficult, mac id + machine local time, different machine has different time clock&#x20;

### Process pause

* Java virtual machine has a garbage collector which occasionally needs to stop all running threads. "Stop-the-world" GC pauses have sometimes been know to last for several minutes.
* When the OS context-switches to another thread, or when the hypervisor switches to a different virtual machine, the current running thread can be paused at any arbitrary point in the code.
* if the application performs synchronous disk access, a thread may be paused waiting for a slot disk I/O operation to complete, Java class loader lazy load first time.
* Thrashing: swapping to disk (paging)
* real time OS: every library call has a worst time guaranteed.

### Redlock: fault tolerant distributed locks&#x20;

how to do distributed locking: martin kleppmann

### A real case in HBase (Redis)

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdMm86vWqEHwDkBLjS%2F-MMdNFaPyazGNIDfeamx%2Fimage.png?alt=media\&token=43ca52b9-db77-406b-9dc5-d67f61774330)

ZooKeeper transaction ID

### Truths

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdOVpw6K_hqgC68oqK%2F-MMdOeY0GW3ID91lnrlf%2Fimage.png?alt=media\&token=8f19e692-1087-4d9c-8988-534700a14cb0)

Quorum&#x20;

### Lies: Byzantine failures - unreliable node / network

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdOVpw6K_hqgC68oqK%2F-MMdP2_7CKUUcWtQyzaI%2Fimage.png?alt=media\&token=bb25503d-4023-4e48-abbc-16a8d4ddf4c8)

### Different models in distributed system

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdOVpw6K_hqgC68oqK%2F-MMdPbOP1926e4DWbPAG%2Fimage.png?alt=media\&token=873d1e09-c750-491f-b7b4-eb70d65f68ae)

![](https://2878046910-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIBLMn_afS_V7H-sgLo%2F-MMdPo062VIZF1xIr00U%2F-MMdPyLIsCuGwU3WlWOS%2Fimage.png?alt=media\&token=3edfabaf-89b0-42db-ba9c-b2cbb4fce061)
