Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Resilience with Hystrix
An introduction

Uwe Friedrichsen, codecentric AG, 2014
@ufried
Uwe Friedrichsen | uwe.friedrichsen@codecentric.de | http://slideshare.net/ufried | http://ufried.tumblr.com
It‘s all about production!
Production
Availability
Resilience
Fault Tolerance
re•sil•ience (rɪˈzɪl yəns) also re•sil′ien•cy, n.

1.  the power or ability to return to the original form, position,
etc....
Implemented patterns






•  Timeout
•  Circuit breaker
•  Load shedder
Supported patterns

•  Bulkheads

(a.k.a. Failure Units)
•  Fail fast
•  Fail silently
•  Graceful degradation of service
...
That‘s been enough theory
Give us some code – now!
Basics
Hello, world!
public class HelloCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
private ...
@Test
public void shouldGreetWorld() {
String result = new HelloCommand("World").execute();
assertEquals("Hello, World", r...
Synchronous execution
@Test
public void shouldExecuteSynchronously() {
String s = new HelloCommand("Bob").execute();
assertEquals("Hello, Bob", ...
Asynchronous execution
@Test
public void shouldExecuteAsynchronously() {
Future<String> f = new HelloCommand("Alice").queue();
String s = null;
t...
Reactive execution (simple)
@Test
public void shouldExecuteReactiveSimple() {
Observable<String> observable =
new HelloCommand("Alice").observe();
Str...
Reactive execution (full)
public class CommandHelloObserver implements Observer<String> {
// Needed for communication between observer and test case...
@Test
public void shouldExecuteReactiveFull() {
// Needed for communication between observer and test case
AtomicReference...
Fallback
public class FallbackCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
publi...
@Test
public void shouldGetFallbackResponse() {
String s = new FallbackCommand().execute();
assertEquals("Powered by fallb...
Error propagation
public class ErrorPropagationCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default...
@Test
public void shouldGetFallbackResponse() {
String s = null;
try {
s = new ErrorPropagationCommand().execute();
fail()...
How it works
Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
Configuration
Configuration

•  Based on Archaius by Netflix

which extends the Apache Commons Configuration Library
•  Four levels of p...
Configuration – Some examples

•  hystrix.command.*.execution.isolation.strategy

Either “THREAD” or “SEMAPHORE” (Default:...
Configuration – More examples

•  hystrix.threadpool.*.coreSize

Maximum number of concurrent requests when using thread p...
Patterns
Timeout
public class LatentResource {
private final long latency;
public LatentResource(long latency) {
this.latency = ((latency <...
public class TimeoutCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
privat...
@Test
public void shouldGetNormalResponse() {
LatentResource resource = new LatentResource(50L);
TimeoutCommand command = ...
Circuit breaker
public class CircuitBreakerCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";...
@Test
public void shouldExecuteWithCircuitBreakerClosed() {
CircuitBreakerCommand command = new
CircuitBreakerCommand("Clo...
@Test
public void shouldExecuteWithCircuitBreakerOpen() {
CircuitBreakerCommand command = new
CircuitBreakerCommand("OpenB...
Load shedder (Thread pool)
public class LatentCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
private...
@Test
public void shouldShedLoad() {
List<Future<String>> l = new LinkedList<>();
LatentResource resource = new LatentReso...
Load shedder (Semaphore)
public class SemaphoreCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
priv...
private class SemaphoreCommandInvocation implements
java.util.concurrent.Callable<String> {
private final LatentResource r...
@Test
public void shouldShedLoad() {
List<Future<String>> l = new LinkedList<>();
LatentResource resource = new LatentReso...
Fail fast
public class FailFastCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
priva...
@Test
public void shouldSucceed() {
FailFastCommand command = new FailFastCommand(true);
String s = command.execute();
ass...
Fail silent
public class FailSilentCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";
pri...
@Test
public void shouldSucceed() {
FailSilentCommand command = new FailSilentCommand(true);
String s = command.execute();...
Static fallback
public class StaticFallbackCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "default";...
@Test
public void shouldSucceed() {
StaticFallbackCommand command = new StaticFallbackCommand(true);
String s = command.ex...
Cache fallback
public class CacheClient {
private final Map<String, String> map;
public CacheClient() {
map = new ConcurrentHashMap<>();
...
public class CacheCommand extends HystrixCommand<String> {
private static final String COMMAND_GROUP = "primary";
private ...
…
@Override
protected String getFallback() {
return "Cached: " + new FBCommand(cache, req).execute();
}
private static cla...
@Test
public void shouldExecuteWithoutCache() {
CacheClient cache = new CacheClient();
String s = new CacheCommand(cache, ...
… and so on
Advanced features
Advanced Features

•  Request Caching
•  Request Collapsing
•  Plugins

Event Notifier, Metrics Publisher, Properties Stra...
Source: https://github.com/Netflix/Hystrix/wiki/Dashboard
Source: https://github.com/Netflix/Hystrix/wiki/Dashboard
Further reading

1.  Hystrix Wiki,

https://github.com/Netflix/Hystrix/wiki
2.  Michael T. Nygard, Release It!,
Pragmatic ...
Wrap-up

•  Resilient software design becomes a must
•  Hystrix is a resilience library
•  Provides isolation and latency ...
It‘s all about production!
@ufried
Uwe Friedrichsen | uwe.friedrichsen@codecentric.de | http://slideshare.net/ufried | http://ufried.tumblr.com
Resilience with Hystrix
Resilience with Hystrix
Upcoming SlideShare
Loading in …5
×

Resilience with Hystrix

20,904 views

Published on

Resilience becomes a must. Actually resilience is an issue for a long time, but with ever-increasing distribution it becomes a must. But how can we implement resilience into our applications? Hystrix, an open-source library developed by Netflix provides a nice starting point. It provides downstream isolation by implementing patterns like timeout, circuit breaker and load shedder. This presentation gives an introduction into Hystrix, providing lots of code examples.

  • Dating for everyone is here: ♥♥♥ http://bit.ly/369VOVb ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ♥♥♥ http://bit.ly/369VOVb ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THIS BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THIS can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THIS is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THIS Helped Ignite a Movement,-- Atomic Habits: An Easy &amp; Proven Way to Build Good Habits &amp; Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THIS the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THIS Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THAT BOOKS INTO AVAILABLE FORMAT (2019 Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { http://shorturl.at/mzUV6 } ......................................................................................................................... Download Full EPUB Ebook here { http://shorturl.at/mzUV6 } ......................................................................................................................... Download Full doc Ebook here { http://shorturl.at/mzUV6 } ......................................................................................................................... Download PDF EBOOK here { http://shorturl.at/mzUV6 } ......................................................................................................................... Download EPUB Ebook here { http://shorturl.at/mzUV6 } ......................................................................................................................... Download doc Ebook here { http://shorturl.at/mzUV6 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book that can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer that is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBooks .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story That Helped Ignite a Movement,-- Atomic Habits: An Easy &amp; Proven Way to Build Good Habits &amp; Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money That the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths that Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Resilience with Hystrix

  1. 1. Resilience with Hystrix An introduction Uwe Friedrichsen, codecentric AG, 2014
  2. 2. @ufried Uwe Friedrichsen | uwe.friedrichsen@codecentric.de | http://slideshare.net/ufried | http://ufried.tumblr.com
  3. 3. It‘s all about production!
  4. 4. Production Availability Resilience Fault Tolerance
  5. 5. re•sil•ience (rɪˈzɪl yəns) also re•sil′ien•cy, n. 1.  the power or ability to return to the original form, position, etc., after being bent, compressed, or stretched; elasticity. 2.  ability to recover readily from illness, depression, adversity, or the like; buoyancy. Random House Kernerman Webster's College Dictionary, © 2010 K Dictionaries Ltd. Copyright 2005, 1997, 1991 by Random House, Inc. All rights reserved. http://www.thefreedictionary.com/resilience
  6. 6. Implemented patterns •  Timeout •  Circuit breaker •  Load shedder
  7. 7. Supported patterns •  Bulkheads
 (a.k.a. Failure Units) •  Fail fast •  Fail silently •  Graceful degradation of service •  Failover •  Escalation •  Retry •  ...
  8. 8. That‘s been enough theory Give us some code – now!
  9. 9. Basics
  10. 10. Hello, world!
  11. 11. public class HelloCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final String name; public HelloCommand(String name) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.name = name; } @Override protected String run() throws Exception { return "Hello, " + name; } }
  12. 12. @Test public void shouldGreetWorld() { String result = new HelloCommand("World").execute(); assertEquals("Hello, World", result); }
  13. 13. Synchronous execution
  14. 14. @Test public void shouldExecuteSynchronously() { String s = new HelloCommand("Bob").execute(); assertEquals("Hello, Bob", s); }
  15. 15. Asynchronous execution
  16. 16. @Test public void shouldExecuteAsynchronously() { Future<String> f = new HelloCommand("Alice").queue(); String s = null; try { s = f.get(); } catch (InterruptedException | ExecutionException e) { // Handle Future.get() exceptions here } assertEquals("Hello, Alice", s); }
  17. 17. Reactive execution (simple)
  18. 18. @Test public void shouldExecuteReactiveSimple() { Observable<String> observable = new HelloCommand("Alice").observe(); String s = observable.toBlockingObservable().single(); assertEquals("Hello, Alice", s); }
  19. 19. Reactive execution (full)
  20. 20. public class CommandHelloObserver implements Observer<String> { // Needed for communication between observer and test case private final AtomicReference<String> aRef; private final Semaphore semaphore; public CommandHelloObserver(AtomicReference<String> aRef, Semaphore semaphore) { this.aRef = aRef; this.semaphore = semaphore; } @Override public void onCompleted() { // Not needed here } @Override public void onError(Throwable e) { // Not needed here } @Override public void onNext(String s) { aRef.set(s); semaphore.release(); } }
  21. 21. @Test public void shouldExecuteReactiveFull() { // Needed for communication between observer and test case AtomicReference<String> aRef = new AtomicReference<>(); Semaphore semaphore = new Semaphore(0); Observable<String> observable = new HelloCommand("Bob").observe(); Observer<String> observer = new CommandHelloObserver(aRef, semaphore); observable.subscribe(observer); // Wait until observer received a result try { semaphore.acquire(); } catch (InterruptedException e) { // Handle exceptions here } String s = aRef.get(); assertEquals("Hello, Bob", s); }
  22. 22. Fallback
  23. 23. public class FallbackCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; public FallbackCommand() { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); } @Override protected String run() throws Exception { throw new RuntimeException("I will always fail"); } @Override protected String getFallback() { return "Powered by fallback"; } }
  24. 24. @Test public void shouldGetFallbackResponse() { String s = new FallbackCommand().execute(); assertEquals("Powered by fallback", s); }
  25. 25. Error propagation
  26. 26. public class ErrorPropagationCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; protected ErrorPropagationCommand() { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); } @Override protected String run() throws Exception { throw new HystrixBadRequestException("I fail differently", new RuntimeException("I will always fail")); } @Override protected String getFallback() { return "Powered by fallback"; } }
  27. 27. @Test public void shouldGetFallbackResponse() { String s = null; try { s = new ErrorPropagationCommand().execute(); fail(); // Just to make sure } catch (HystrixBadRequestException e) { assertEquals("I will fail differently", e.getMessage()); assertEquals("I will always fail", e.getCause().getMessage()); } assertNull(s); // Fallback is not triggered }
  28. 28. How it works
  29. 29. Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
  30. 30. Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
  31. 31. Source: https://github.com/Netflix/Hystrix/wiki/How-it-Works
  32. 32. Configuration
  33. 33. Configuration •  Based on Archaius by Netflix
 which extends the Apache Commons Configuration Library •  Four levels of precedence 1.  Global default from code 2.  Dynamic global default property 3.  Instance default from code 4.  Dynamic instance property Provides many configuration & tuning options
  34. 34. Configuration – Some examples •  hystrix.command.*.execution.isolation.strategy
 Either “THREAD” or “SEMAPHORE” (Default: THREAD) •  hystrix.command.*.execution.isolation.thread.timeoutInMilliseconds
 Time to wait for the run() method to complete (Default: 1000) •  h.c.*.execution.isolation.semaphore.maxConcurrentRequests
 Maximum number of concurrent requests when using semaphores (Default: 10) •  hystrix.command.*.circuitBreaker.errorThresholdPercentage
 Error percentage at which the breaker trips open (Default: 50) •  hystrix.command.*.circuitBreaker.sleepWindowInMilliseconds
 Time to wait before attempting to reset the breaker after tripping (Default: 5000) * must be either “default” or the command key name
  35. 35. Configuration – More examples •  hystrix.threadpool.*.coreSize
 Maximum number of concurrent requests when using thread pools (Default: 10) •  hystrix.threadpool.*.maxQueueSize
 Maximum LinkedBlockingQueue size - -1 for using SynchronousQueue (Default: -1) •  hystrix.threadpool.default.queueSizeRejectionThreshold
 Queue size rejection threshold (Default: 5) … and many more * must be either “default” or the thread pool key name
  36. 36. Patterns
  37. 37. Timeout
  38. 38. public class LatentResource { private final long latency; public LatentResource(long latency) { this.latency = ((latency < 0L) ? 0L : latency); } public String getData() { addLatency(); // This is the important part return "Some value”; } private void addLatency() { try { Thread.sleep(latency); } catch (InterruptedException e) { // We do not care about this here } } }
  39. 39. public class TimeoutCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final LatentResource resource; public TimeoutCommand(int timeout, LatentResource resource) { super(Setter.withGroupKey( HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter() .withExecutionIsolationThreadTimeoutInMilliseconds( timeout))); this.resource = resource; } @Override protected String run() throws Exception { return resource.getData(); } @Override protected String getFallback() { return "Resource timed out"; } }
  40. 40. @Test public void shouldGetNormalResponse() { LatentResource resource = new LatentResource(50L); TimeoutCommand command = new TimeoutCommand(100, resource); String s = command.execute(); assertEquals("Some value", s); } @Test public void shouldGetFallbackResponse() { LatentResource resource = new LatentResource(150L); TimeoutCommand command = new TimeoutCommand(100, resource); String s = command.execute(); assertEquals("Resource timed out", s); }
  41. 41. Circuit breaker
  42. 42. public class CircuitBreakerCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; public CircuitBreakerCommand(String name, boolean open) { super(Setter.withGroupKey( HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)) .andCommandKey(HystrixCommandKey.Factory.asKey(name)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter() .withCircuitBreakerForceOpen(open))); } @Override protected String run() throws Exception { return("Some result"); } @Override protected String getFallback() { return "Fallback response"; } }
  43. 43. @Test public void shouldExecuteWithCircuitBreakerClosed() { CircuitBreakerCommand command = new CircuitBreakerCommand("ClosedBreaker", false); String s = command.execute(); assertEquals("Some result", s); HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory .getInstance(command.getCommandKey()); assertTrue(breaker.allowRequest()); }
  44. 44. @Test public void shouldExecuteWithCircuitBreakerOpen() { CircuitBreakerCommand command = new CircuitBreakerCommand("OpenBreaker", true); String s = command.execute(); assertEquals("Fallback response", s); HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory .getInstance(command.getCommandKey()); assertFalse(breaker.allowRequest()); }
  45. 45. Load shedder (Thread pool)
  46. 46. public class LatentCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final LatentResource resource; public LatentCommand(LatentResource resource) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.resource = resource; } @Override protected String run() throws Exception { return resource.getData(); } @Override protected String getFallback() { return "Fallback triggered"; } }
  47. 47. @Test public void shouldShedLoad() { List<Future<String>> l = new LinkedList<>(); LatentResource resource = new LatentResource(500L); // Make latent // Use up all available threads for (int i = 0; i < 10; i++) l.add(new LatentCommand(resource).queue()); // Call will be rejected as thread pool is exhausted String s = new LatentCommand(resource).execute(); assertEquals("Fallback triggered", s); // All other calls succeed for (Future<String> f : l) assertEquals("Some value", get(f)); // wrapper for f.get() }
  48. 48. Load shedder (Semaphore)
  49. 49. public class SemaphoreCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final LatentResource resource; public SemaphoreCommand(LatentResource resource) { super(Setter.withGroupKey( HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)) .andCommandPropertiesDefaults( HystrixCommandProperties.Setter() .withExecutionIsolationStrategy(HystrixCommandProperties .ExecutionIsolationStrategy.SEMAPHORE))); this.resource = resource; } @Override protected String run() throws Exception { return resource.getData(); } @Override protected String getFallback() { return "Fallback triggered"; } }
  50. 50. private class SemaphoreCommandInvocation implements java.util.concurrent.Callable<String> { private final LatentResource resource; public SemaphoreCommandInvocation(LatentResource resource) { this.resource = resource; } @Override public String call() throws Exception { return new SemaphoreCommand(resource).execute(); } }
  51. 51. @Test public void shouldShedLoad() { List<Future<String>> l = new LinkedList<>(); LatentResource resource = new LatentResource(500L); // Make latent ExecutorService e = Executors.newFixedThreadPool(10); // Use up all available semaphores for (int i = 0; i < 10; i++) l.add(e.submit(new SemaphoreCommandInvocation(resource))); // Wait a moment to make sure all commands are started pause(250L); // Wrapper around Thread.sleep() // Call will be rejected as all semaphores are in use String s = new SemaphoreCommand(resource).execute(); assertEquals("Fallback triggered", s); // All other calls succeed for (Future<String> f : l) assertEquals("Some value", get(f)); }
  52. 52. Fail fast
  53. 53. public class FailFastCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final boolean preCondition; public FailFastCommand(boolean preCondition) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.preCondition = preCondition; } @Override protected String run() throws Exception { if (!preCondition) throw new RuntimeException(("Fail fast")); return "Some value"; } }
  54. 54. @Test public void shouldSucceed() { FailFastCommand command = new FailFastCommand(true); String s = command.execute(); assertEquals("Some value", s); } @Test public void shouldFailFast() { FailFastCommand command = new FailFastCommand(false); try { String s = command.execute(); fail("Did not fail fast"); } catch (Exception e) { assertEquals(HystrixRuntimeException.class, e.getClass()); assertEquals("Fail fast", e.getCause().getMessage()); } }
  55. 55. Fail silent
  56. 56. public class FailSilentCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final boolean preCondition; public FailSilentCommand(boolean preCondition) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.preCondition = preCondition; } @Override protected String run() throws Exception { if (!preCondition) throw new RuntimeException(("Fail fast")); return "Some value"; } @Override protected String getFallback() { return null; // Turn into silent failure } }
  57. 57. @Test public void shouldSucceed() { FailSilentCommand command = new FailSilentCommand(true); String s = command.execute(); assertEquals("Some value", s); } @Test public void shouldFailSilent() { FailSilentCommand command = new FailSilentCommand(false); String s = "Test value"; try { s = command.execute(); } catch (Exception e) { fail("Did not fail silent"); } assertNull(s); }
  58. 58. Static fallback
  59. 59. public class StaticFallbackCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "default"; private final boolean preCondition; public StaticFallbackCommand(boolean preCondition) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.preCondition = preCondition; } @Override protected String run() throws Exception { if (!preCondition) throw new RuntimeException(("Fail fast")); return "Some value"; } @Override protected String getFallback() { return "Some static fallback value"; // Static fallback } }
  60. 60. @Test public void shouldSucceed() { StaticFallbackCommand command = new StaticFallbackCommand(true); String s = command.execute(); assertEquals("Some value", s); } @Test public void shouldProvideStaticFallback() { StaticFallbackCommand command = new StaticFallbackCommand(false); String s = null; try { s = command.execute(); } catch (Exception e) { fail("Did not fail silent"); } assertEquals("Some static fallback value", s); }
  61. 61. Cache fallback
  62. 62. public class CacheClient { private final Map<String, String> map; public CacheClient() { map = new ConcurrentHashMap<>(); } public void add(String key, String value) { map.put(key, value); } public String get(String key) { return map.get(key); } }
  63. 63. public class CacheCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "primary"; private final CacheClient cache; private final boolean failPrimary; private final String req; public CacheCommand(CacheClient cache, boolean failPrimary, String request) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.cache = cache; this.failPrimary = failPrimary; this.req = request; } @Override protected String run() throws Exception { if (failPrimary) throw new RuntimeException("Failure of primary"); String s = req + "-o"; cache.add(req, s); return(s); } …
  64. 64. … @Override protected String getFallback() { return "Cached: " + new FBCommand(cache, req).execute(); } private static class FBCommand extends HystrixCommand<String> { private static final String COMMAND_GROUP = "fallback"; private final CacheClient cache; private final String request; public FBCommand(CacheClient cache, String request) { super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP)); this.cache = cache; this.request = request; } @Override protected String run() throws Exception { return cache.get(request); } } }
  65. 65. @Test public void shouldExecuteWithoutCache() { CacheClient cache = new CacheClient(); String s = new CacheCommand(cache, false, "ping”).execute(); assertEquals("ping-o", s); } @Test public void shouldRetrieveValueFromCache() { CacheClient cache = new CacheClient(); new CacheCommand(cache, false, "ping").execute(); String s = new CacheCommand(cache, true, "ping”).execute(); assertEquals("Cached: ping-o", s); } @Test public void shouldNotRetrieveAnyValue() { CacheClient cache = new CacheClient(); String s = new CacheCommand(cache, true, "ping”).execute(); assertEquals("Cached: null", s); }
  66. 66. … and so on
  67. 67. Advanced features
  68. 68. Advanced Features •  Request Caching •  Request Collapsing •  Plugins
 Event Notifier, Metrics Publisher, Properties Strategy, Concurrency Strategy, Command Execution Hook •  Contributions
 Metrics Event Stream, Metrics Publisher, Java Annotations, Clojure Bindings, … •  Dashboard
  69. 69. Source: https://github.com/Netflix/Hystrix/wiki/Dashboard
  70. 70. Source: https://github.com/Netflix/Hystrix/wiki/Dashboard
  71. 71. Further reading 1.  Hystrix Wiki,
 https://github.com/Netflix/Hystrix/wiki 2.  Michael T. Nygard, Release It!, Pragmatic Bookshelf, 2007 3.  Robert S. Hanmer,
 Patterns for Fault Tolerant Software, Wiley, 2007 4.  Andrew Tanenbaum, Marten van Steen, Distributed Systems – Principles and Paradigms,
 Prentice Hall, 2nd Edition, 2006
  72. 72. Wrap-up •  Resilient software design becomes a must •  Hystrix is a resilience library •  Provides isolation and latency control •  Easy to start with •  Yet some learning curve •  Many fault-tolerance patterns implementable •  Many configuration options •  Customizable •  Operations proven Become a resilient software developer!
  73. 73. It‘s all about production!
  74. 74. @ufried Uwe Friedrichsen | uwe.friedrichsen@codecentric.de | http://slideshare.net/ufried | http://ufried.tumblr.com

×