Slide 1

Slide 1 text

FAULT-TOLERANT CLIENTS Implementando clients e services mais resilientes e tolerantes a falhas

Slide 2

Slide 2 text

O que é um Sistema Tolerante a Falhas? 


Slide 3

Slide 3 text

https://deniseyu.io/art/

Slide 4

Slide 4 text

https://deniseyu.io/art/

Slide 5

Slide 5 text

https://deniseyu.io/art/

Slide 6

Slide 6 text

Desenhando sistemas distribuídos…

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Spring Boot App

Slide 12

Slide 12 text

Spring Boot App

Slide 13

Slide 13 text

Spring Boot App Client Service

Slide 14

Slide 14 text

Spring Boot App Client Service

Slide 15

Slide 15 text

Spring Boot App Client Service Local Calls

Slide 16

Slide 16 text

Spring Boot App Client Service Local Calls

Slide 17

Slide 17 text

Spring Boot App Client Service Local Calls

Slide 18

Slide 18 text

Spring Boot App Client Service Local Calls Mas não aqui!

Slide 19

Slide 19 text

Spring Boot App Client Service Local Calls Mas não aqui!

Slide 20

Slide 20 text

Spring Boot App Client Service Local Calls

Slide 21

Slide 21 text

Em contrapartida…

Slide 22

Slide 22 text

Spring Boot App Spring Boot App

Slide 23

Slide 23 text

Spring Boot App Spring Boot App Client

Slide 24

Slide 24 text

Spring Boot App Spring Boot App Client Service

Slide 25

Slide 25 text

Spring Boot App Spring Boot App Client Service 
 Internet

Slide 26

Slide 26 text

Spring Boot App Spring Boot App Client Service 
 Internet

Slide 27

Slide 27 text

Spring Boot App Spring Boot App Client Service 
 Internet Remote Calls

Slide 28

Slide 28 text

Spring Boot App Spring Boot App Client Service 
 Internet Remote Calls

Slide 29

Slide 29 text

Spring Boot App Client Spring Boot App Service 
 Internet Remote Calls

Slide 30

Slide 30 text

Spring Boot App Client Spring Boot App Service 
 Internet Remote Calls

Slide 31

Slide 31 text

Spring Boot App Client Spring Boot App Service 
 Internet Remote Calls

Slide 32

Slide 32 text

Spring Boot App Client Spring Boot App Service 
 Internet Remote Calls

Slide 33

Slide 33 text

Spring Boot App Client Spring Boot App Service 
 Internet Remote Calls

Slide 34

Slide 34 text

Spring Boot App Spring Boot App 
 Internet Client Service Remote Calls

Slide 35

Slide 35 text

https://deniseyu.io/art/

Slide 36

Slide 36 text

https://deniseyu.io/art/

Slide 37

Slide 37 text

https://deniseyu.io/art/

Slide 38

Slide 38 text

https://deniseyu.io/art/

Slide 39

Slide 39 text

https://deniseyu.io/art/

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Por que isso importa?

Slide 42

Slide 42 text

Spring Boot App Spring Boot App Spring Boot App 
 Local Calls Remote Calls

Slide 43

Slide 43 text

Spring Boot App Spring Boot App Spring Boot App 
 Centralized System Distributed System

Slide 44

Slide 44 text

Spring Boot App Spring Boot App Spring Boot App 
 Centralized System Distributed System

Slide 45

Slide 45 text

— Martin Fowler First Law of Distributed Object Design: 
 "don’t distribute your objects"

Slide 46

Slide 46 text

Mas se não tiver jeito…

Slide 47

Slide 47 text

Spring Boot App Client Spring Boot App Service 
 Internet Distributed System

Slide 48

Slide 48 text

Spring Boot App Client Spring Boot App Service 
 Internet Distributed System

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Distributed System Spring Boot App Client Spring Boot App Service 
 Internet

Slide 51

Slide 51 text

Distributed System Spring Boot App Client Spring Boot App Service 
 Internet

Slide 52

Slide 52 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 53

Slide 53 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 54

Slide 54 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 55

Slide 55 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 56

Slide 56 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 57

Slide 57 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 58

Slide 58 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 59

Slide 59 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 60

Slide 60 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 61

Slide 61 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 62

Slide 62 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 63

Slide 63 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 64

Slide 64 text

Design for Failures

Slide 65

Slide 65 text

Rafael Ponte @rponte

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Fortaleza - Terra do Sol

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

FAULT-TOLERANT CLIENTS Implementando clients e services mais resilientes e tolerantes a falhas

Slide 70

Slide 70 text

Implementação Ingênua

Slide 71

Slide 71 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 72

Slide 72 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 73

Slide 73 text

@RestController public class CalculadoraDeFretesController { @GetMapping(path="/fretes/calcula") public ResponseEntity calcula(@RequestParam String cep) { // consulta frete em outro microsserviço String url = "https://ms.fast-fretes.com/calcula-frete/" ; 
 ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();
 return ResponseEntity.ok(frete); } }

Slide 74

Slide 74 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 75

Slide 75 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 76

Slide 76 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 77

Slide 77 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Pode ser qualquer HTTP Client!

Slide 78

Slide 78 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 79

Slide 79 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 80

Slide 80 text

ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 81

Slide 81 text

O problema dessa implementação…

Slide 82

Slide 82 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 83

Slide 83 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 84

Slide 84 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 85

Slide 85 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 86

Slide 86 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 87

Slide 87 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 88

Slide 88 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 89

Slide 89 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 90

Slide 90 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 91

Slide 91 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 92

Slide 92 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 93

Slide 93 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 94

Slide 94 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 95

Slide 95 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 96

Slide 96 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 97

Slide 97 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 98

Slide 98 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

Tudo isso porque esperamos demais…

Slide 102

Slide 102 text

Timeout

Slide 103

Slide 103 text

ZupClientConfig config = // ... ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 104

Slide 104 text

ZupClientConfig config = // ... .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>() ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 105

Slide 105 text

ZupClientConfig config = // ... .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 106

Slide 106 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 107

Slide 107 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 108

Slide 108 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 109

Slide 109 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Depende do caso de uso e contexto

Slide 110

Slide 110 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 111

Slide 111 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 112

Slide 112 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 113

Slide 113 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 114

Slide 114 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 115

Slide 115 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 116

Slide 116 text

Fail Fast

Slide 117

Slide 117 text

…mas não para Transient Failures

Slide 118

Slide 118 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 119

Slide 119 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 120

Slide 120 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 121

Slide 121 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 122

Slide 122 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 123

Slide 123 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 124

Slide 124 text

Não podemos jogar esse problema no usuário…

Slide 125

Slide 125 text

Retry

Slide 126

Slide 126 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 127

Slide 127 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy( ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 128

Slide 128 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom().build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 129

Slide 129 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 130

Slide 130 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Transient Failures

Slide 131

Slide 131 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .build() ) .build(); ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 132

Slide 132 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .build() ) .build(); ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Depende do caso de uso e contexto

Slide 133

Slide 133 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 134

Slide 134 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 135

Slide 135 text

Spring Boot App Client Spring Boot App Service 
 Internet Original Request:

Slide 136

Slide 136 text

Spring Boot App Client Spring Boot App Service 
 Internet Original Request:

Slide 137

Slide 137 text

Spring Boot App Client Spring Boot App Service 
 Internet Retry #1: Original Request:

Slide 138

Slide 138 text

Spring Boot App Client Spring Boot App Service 
 Internet Retry #1: Retry #2: Original Request:

Slide 139

Slide 139 text

Spring Boot App Client Spring Boot App Service 
 Internet Retry #1: Retry #2: Original Request:

Slide 140

Slide 140 text

Clients são EGOISTAS

Slide 141

Slide 141 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 142

Slide 142 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 143

Slide 143 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 144

Slide 144 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 145

Slide 145 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 146

Slide 146 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 147

Slide 147 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 148

Slide 148 text

Spring Boot App Client Spring Boot App Service 
 Internet

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

RETRY STORM 
 Vamo surrar esse servidor ae!

Slide 151

Slide 151 text

Não demos tempo pro serviço se recuperar

Slide 152

Slide 152 text

Retry with Backo f

Slide 153

Slide 153 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 154

Slide 154 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS) ) .build() ) .build(); ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 155

Slide 155 text

100ms
 100ms
 100ms
 100ms
 …
 Retry #1 Retry #2 Retry #3 Retry #4

Slide 156

Slide 156 text

Não resolvemos o problema, somente "empurramos" ele 100ms pra frente…

Slide 157

Slide 157 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS) ) .build() ) .build(); ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 158

Slide 158 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS), 2 ) .build() ) .build(); ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 159

Slide 159 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS), 2 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Delay Factor

Slide 160

Slide 160 text

100ms
 200ms
 400ms
 800ms
 …
 Retry #1 Retry #2 Retry #3 Retry #4 Dobramos Dobramos Dobramos Dobramos

Slide 161

Slide 161 text

Retry with Exponential Backo f

Slide 162

Slide 162 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS), 2 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 163

Slide 163 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS), 2 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 164

Slide 164 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(Duration.of(100, MILLIS), 2 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 165

Slide 165 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(Duration.of(100, MILLIS) ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 166

Slide 166 text

Demos folêgo pro servidor ter tempo para se recuperar, porém…

Slide 167

Slide 167 text

No content

Slide 168

Slide 168 text

SINCRONIA ENTRE CLIENTS

Slide 169

Slide 169 text

Precisamos diminuir as chances dessa sincronização…

Slide 170

Slide 170 text

Retry with Backo f f and Jitter

Slide 171

Slide 171 text

jitter /ˈdʒɪtə/ n. 
 the deviation from true periodicity of a presumably periodic signal.

Slide 172

Slide 172 text

jitter /ˈdʒɪtə/ n. 
 add randomness to the periodicity of a periodic signal.

Slide 173

Slide 173 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(Duration.of(100, MILLIS) ) .build() ) .build();
 ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 174

Slide 174 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(Duration.of(100, MILLIS) ) .withJitter(0.25 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute();

Slide 175

Slide 175 text

ZupClientConfig config = ZupClientConfig.custom( ) .withRequestTimeout(Duration.of(5, SECONDS) ) .withRetryPolicy(RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(Duration.of(100, MILLIS) ) .withJitter(0.25 ) .build() ) .build() ; ZupHttpClient client = new ZupHttpClient<>(config) ; Frete frete = client.get(url ) .withParameter("cep", cep ) .execute(); Jitter Factor

Slide 176

Slide 176 text

delay random(0, delay*0.25) + -

Slide 177

Slide 177 text

delay random(0, delay*0.25) + -

Slide 178

Slide 178 text

delay random(0, delay*0.25) + -

Slide 179

Slide 179 text

delay random(0, delay*0.25) + -

Slide 180

Slide 180 text

delay random(0, delay*0.25) Jitter + -

Slide 181

Slide 181 text

100ms
 200ms
 400ms
 800ms
 …
 Retry #1 Retry #2 Retry #3 Retry #4

Slide 182

Slide 182 text

121ms +21ms 
 203ms +3ms 
 393ms -7ms 
 788ms -12ms 
 …
 Retry #1 Retry #2 Retry #3 Retry #4

Slide 183

Slide 183 text

Não só evitamos a sincronização…

Slide 184

Slide 184 text

… como também distribuímos melhor os requests entre os intervalos

Slide 185

Slide 185 text

Retry with Exponential Backo f https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/

Slide 186

Slide 186 text

Retry with Exponential Backo f https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/ Intervalos longos 
 e ociosos

Slide 187

Slide 187 text

Intervalos longos 
 e ociosos Retry with Exponential Backo f Picos ainda podem ocorrer https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/

Slide 188

Slide 188 text

Retry with Exponential Backo f f and Jitter https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/

Slide 189

Slide 189 text

Retry with Exponential Backo f f and Jitter https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/ Distribui as requisições entre os intervalos

Slide 190

Slide 190 text

Distribui as requisições entre os intervalos Retry with Exponential Backo f f and Jitter Menor incidência de picos https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/

Slide 191

Slide 191 text

Distribui as requisições entre os intervalos Retry with Exponential Backo f f and Jitter Menor incidência de picos https://aws.amazon.com/blogs/architecture/exponential-backo f f -and-jitter/

Slide 192

Slide 192 text

Pra entender melhor como tudo isso funciona…

Slide 193

Slide 193 text

Efeitos de Particionamento de Rede

Slide 194

Slide 194 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(ofMillis(100)) .build( ) (Rede particionada) https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf

Slide 195

Slide 195 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(ofMillis(100)) .build( ) https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf (Rede particionada)

Slide 196

Slide 196 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(ofMillis(100)) .withJitter(0.25)
 .build() https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf (Rede particionada)

Slide 197

Slide 197 text

Efeitos de falhas parciais

Slide 198

Slide 198 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withDelay(ofMillis(100)) .build( ) (taxa de falha de 25 %) https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf

Slide 199

Slide 199 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(ofMillis(100)) .build( ) (taxa de falha de 25 %) https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf

Slide 200

Slide 200 text

RetryPolicy.custom( ) .retryOn(HttpStatus5xxException.class ) .withMaxAttempts(3 ) .withExponentialBackoff(ofMillis(100)) .withJitter(0.25)
 .build() (taxa de falha de 25 %) https://www.usenix.org/sites/default/ f i les/conference/protected- f i les/srecon18asia_slides_goh.pdf

Slide 201

Slide 201 text

Concluindo…

Slide 202

Slide 202 text

Sistemas Distribuídos são complexos por natureza

Slide 203

Slide 203 text

Qualquer chamada remota é um potencial ponto de falha

Slide 204

Slide 204 text

Pontos de atenção

Slide 205

Slide 205 text

• Sempre de f i na Timeouts Pontos de atenção

Slide 206

Slide 206 text

• Sempre de f i na Timeouts • Não faça Retry (por default) Pontos de atenção

Slide 207

Slide 207 text

• Sempre de f i na Timeouts • Não faça Retry (por default) • Se f i zer Retry, faça Backo f f (exponential) Pontos de atenção

Slide 208

Slide 208 text

• Sempre de f i na Timeouts • Não faça Retry (por default) • Se f i zer Retry, faça Backo f f (exponential) • Sempre use Jitter Pontos de atenção

Slide 209

Slide 209 text

OBRIGADO!

Slide 210

Slide 210 text

@rponte Rafael Ponte