Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP Clients: silent heroes of distributed systems

Adam Dubiel
September 17, 2017

HTTP Clients: silent heroes of distributed systems

Adam Dubiel

September 17, 2017
Tweet

More Decks by Adam Dubiel

Other Decks in Programming

Transcript

  1. @dubieladam HTTP clients we know RestTemplate restTemplate = new RestTemplate();

    Client client = ClientBuilder.newBuilder().build(); AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build();
  2. @dubieladam HTTP clients about we argue async.getForEntity("https://allegro.pl",Map.class).addCallback( { ResponseEntity<Map> result

    -> /* success! */ }, { Throwable ex -> /* failure :( */ } ) jersey.target("https://allegro.pl").request().async().get();
  3. @dubieladam We need to go deeper RestTemplate.getForEntity -> RestTemplate.doExecute ->

    HttpAccessor.createRequest -> ClientHttpRequestFactory.createRequest -> SimpleClientHttpRequestFactory -> URL.openConnection -> HttpURLConnection
  4. @dubieladam We need to go deeper Client.get -> JerseyInvocation.invoke ->

    ClientRuntime.invoke -> Connector.apply -> HttpUrlConnector._apply -> DefaultConnectionFactory.getConnection -> URL.openConnection() -> HttpURLConnection
  5. @dubieladam Just throw some async on it AsyncRestTemplate.getForEntity -> AsyncRestTemplate.doExecute

    -> AsyncHttpAccessor.createAsyncRequest -> AsyncClientHttpRequestFactory.createAsyncRequest -> ... -> ... -> HttpURLConnection
  6. @dubieladam Just throw some async on it Client.async.get -> JerseyInvocation.submit

    -> ClientRuntime.submit -> Connector.apply -> ... -> ... -> ... -> HttpURLConnection
  7. @dubieladam Start with the basics The network is reliable. Latency

    is zero. Bandwidth is infinite. The network is secure. Topology doesn't change. There is one administrator. Transport cost is zero. The network is homogeneous.
  8. @dubieladam Fallacies of distributed systems The network is reliable. Latency

    is zero. Bandwidth is infinite. The network is secure. Topology doesn't change. There is one administrator. Transport cost is zero. The network is homogeneous.
  9. @dubieladam Latency is zero Is latency zero? When do i

    pay for latency? Can i pay less for connections? What is connection pooling? No With every connection and request Use connection pooling
  10. @dubieladam Costs of connecting conn request / response conn request

    / response conn request / response conn request / response
  11. @dubieladam Costs of connecting conn request / response request /

    response request / response request / response
  12. @dubieladam Fallacies of distributed systems The network is reliable. Latency

    is zero. Bandwidth is infinite. The network is secure. Topology doesn't change. There is one administrator. Transport cost is zero. The network is homogeneous.
  13. @dubieladam Network is reliable Are network or my peers reliable?

    How can i know if they fail? Timeouts? What timeouts? No Set timeouts
  14. @dubieladam Connection timeout conn request / response this used to

    be faster.. should i go get a tea? okay, cancelling any moment.. oh! works again
  15. @dubieladam Connection timeout time between initiating and establishing TCP connection

    default depends on system (might be infinity) sane defaults: 50ms (accounting for hiccups, milage may vary, please measure!)
  16. @dubieladam Socket/read timeout conn request / response request response response

    great, got some data! hello?! waiting for some more..
  17. @dubieladam Socket read/timeout time between receiving first and consecutive chunks

    of data default depends on system (measured in seconds) sane default depends on peer response time
  18. @dubieladam Timeouts == fail fast waiting is wasting resources waste

    enough and your app becomes unresponsive which makes other apps wait for you which makes them unresponsive which...
  19. @dubieladam NIO thread model Remote Server My Thread 1 OS

    IO Thread be careful not to do much here!
  20. @dubieladam For implementation, always ask is there any connection pool

    and how big is it? what are the timeouts? what is the thread model?
  21. @dubieladam Thread model If my code does not block, who

    is waiting for everything to happen?
  22. @dubieladam What are the async APIs hiding? MyThread AsyncRestTemplate Remote

    Server MAGIC... Thread 1 Thread 2 Thread 3 Thread N
  23. @dubieladam For API, always ask on which thread pool is

    my code running? how big is the thread pool and how can i control it?
  24. @dubieladam When choosing HTTP client, always ask is there any

    connection pool? how big is the connection pool? what are the timeouts? what is the thread model? on which thread pool is my code running? how big is the thread pool and how can i control it?
  25. @dubieladam When choosing HTTP client, always ask is there any

    connection pool? how big is the connection pool? what are the timeouts? what is the thread model? on which thread pool is my code running? how big is the thread pool and how can i control it?