hands-on: hystrix - inovex€¦ · hands-on: hystrix best practices & pitfalls. hystrix...what?...

22
Hands-On: Hystrix Best practices & pitfalls

Upload: others

Post on 25-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Hands-On: HystrixBest practices & pitfalls

Page 2: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Hystrix...What?

built, heavily tested & used in production by Net�ixJava library, implementation of resilience patternsGoals: fault tolerant/robust self-healing applications,zero downtime

Page 3: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Which cases

Page 4: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Distributed systemExternal dependenciesLimited resourcesHigh AvailabilityAvoid incidents

Page 5: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Availability99,9% (three nines) = 9 hour downtime per year5 dependencies with 99,9% availability = 99,9%^5 =99,5% = 9 days downtime per year

Page 6: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

HowDecoupling by asynchronous executionSafety �rst: bulkheads, fail fastCircuit BreakerFallback, graceful degradation

Page 7: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

WhereClose to the actionIsolate each dependency separatly2+ external calls per single client API call: wrap alldependencies additionally

Page 8: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Example: Synchrounous Mail delivery@EnableHystrix

@SpringBootApplication

public class ShopApplication {}

[RegisterService]

@Transactional

public void register(UserDto userDto) {

User user = userRepo.save(map(userDto));

mailService.sendRegisterSuccessMail(map(user));

}

[MailService]

@HystrixCommand

public void sendRegisterSuccessMail(UserDto user) {

mailGateway.sendMail('register_success', user);

}

3rd Party Libs: Spring Boot, Spring Cloud Net�ix (autosetup- & shutdown-routines)

Page 9: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Example: Delayed Order

@HystrixCommand(fallbackMethod = "placeOrderFallback")

public OrderResult placeOrder(Order order) {

long orderId = orderService.placeOrder(order);

order.setOrderId(orderId);

order.setStatus('RUNNING');

return OrderResult.ok(orderId);

}

public OrderResult placeOrderFallback(Order order) {

// place order async (e.g. cron job)

return OrderResult.OK_DELAYED;

}

Page 10: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Example: Plain Hystrixpublic class OrderCommand extends HystrixCommand {

public OrderCommand(OrderService orderService, Order order) {

super(...config...);

this.orderService = orderService;

this.order = order;

}

@Override

protected void run() {

long orderId = orderService.placeOrder(order);

return OrderResult.ok(orderId);

}

}

new OrderCommand(orderService, order).execute();

Page 11: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Hystrix by Annotations: JavanicaSmaller con�guration effortLess boiler plate codeAutomatic unwrapping ofHystrixBadRequestExceptionsAspect != Aspect

Possibly skip other advicesbuggy: v1.5.4 faulty unwrapping ofHystrixRuntimeExceptions

Page 12: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Semaphores & thread-poolsSemaphore

concurrency & error controlremains in the caller threadtimeout != timeout

Thread-poolHystrix (& Net�ix internal) standardreal decouplingasync execution �ow

Page 13: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of
Page 14: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of
Page 15: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

TimeoutsHTTP connection/socket timeoutsconnection poolsSQL driverHystrix timeouts & limitstimeout values: It depends!

Page 16: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Protecting threadsThread interruption is a desire, not a constraintUse non-blocking API (NIO Channel, Netty, ...)Handle thread interruption appropriately

Check: Thread.currentThread().isInterrupted()Restore state after catching InterruptedException:Thread.currentThread().interrupt();

Page 17: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Decoupling not for freeminimal overhead, butThreadLocal powered functionality missing

log context (MDC, NDC)DB transaction statussecurity contextthread bound DI scopes (request, session, thread)

Page 18: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Solution: Custom Strategypublic class CustomStrategy extends HystrixConcurrencyStrategy {

@Override

public <T> Callable<T> wrapCallable(final Callable<T> callable) {

final Map context = MDC.getContext();

final RequestAttributes attr = RequestContextHolder.get();

return super.wrapCallable(new Callable<T>() {

@Override public T call() throws Exception {

try {

MDC.replaceAll(context);

RequestContextHolder.set(attr);

return callable.call();

} finally {

RequestContextHolder.resetRequestAttributes();

MDC.clear();

} ...

HystrixPlugins.getInstance()

.registerConcurrencyStrategy(new CustomStrategy());

Page 19: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Hystrix DashboardStandalone/selfcontainedversion(https://git.io/vPH8g)Consumes Hystrix metricsstream from each appAggregated metrics streamwith Hystrix Turbine

Page 20: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Demo time!API Gateway (JHipster)

Angular JS / BootstrapSpring BootHystrix

3 simple microservicesMonitoring

Hystrix dashboardKibana dashboard(ELK)

Page 21: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

More HystrixHystrixObservableCommand: Wrapping non-blockingcode (semaphore only)Hystrix command collapsingHystrix request cache

Page 22: Hands-On: Hystrix - inovex€¦ · Hands-On: Hystrix Best practices & pitfalls. Hystrix...What? built, heavily tested & used in production by Netix Java library, implementation of

Vielen Dank!Gerrit Brehmer Software Architekt/SeniorDeveloper

inovex GmbH Of�ce Karlsruhe Ludwig-Erhard-Allee 6 76131 Karlsruhe

Mobil: 01733181007 Mail: [email protected]