Skip to main content

Posts

Fallbacks Are Overrated - Architecting For Resilience

Abstract Fallbacks in circuit breakers replace failure with some pre-configured response so that the scope of the malfunction is limited and hidden from the end user. However, in real life, a naïve fallback is often too simple and either confusing or unacceptable. I suggest a more robust approach to handling failures, compensating for broken transactions in the future.

Reading time: 8 minutes.

What is a circuit breaker? A circuit breaker is a layer between your code and external dependencies that have a high risk of failure. Every time you call another service, database, or even touch your own disk, there is a possibility of failure. Without circuit breakers such a simple error quickly escalates, bubbling up to your end user. All too often minor dependency brings down a huge system, resulting in 503 HTTP responses or slowness. Circuit breaker discovers raised error levels or elevated response times quickly. Rather than slowing the whole system, it cuts off entire dependency temporari…

Map.merge() - One method to rule them all

Russian translation available: Map.merge () - метод, чтобы управлять всеми остальными

I don’t often explain a single method in JDK, but when I do, it’s about Map.merge(). Probably the most versatile operation in the key-value universe. And also rather obscure and rarely used. merge() can be explained as follows: it either puts new value under the given key (if absent) or updates existing key with a given value (UPSERT). Let’s start with the most basic example: counting unique word occurrences. Pre-Java 8 (read: pre-2014!) code was quite messy and the essence was lost in implementation details:

var map = new HashMap<String, Integer>(); words.forEach(word -> { var prev = map.get(word); if (prev == null) { map.put(word, 1); } else { map.put(word, prev + 1); } }); However, it works and for given input produces desired output:

var words = List.of("Foo", "Bar", "Foo", "Buzz", "Foo", "Buzz", &…

Designing Data-Intensive Applications: my favourite book of last year

Martin Kleppmann, the author of Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems wrote a wonderful, comprehensive book. I consider this to be my most valuable reading of 2018, even though the book is almost 2 years old now. Martin proves that great bestsellers in the programming industry aren’t about shiny new frameworks and buzzwords. Data-Intensive Applications is a solid piece about the fundamentals of computer systems, especially from the data manipulation perspective.

This book introduces and explains all topics related to data storage, retrieval and transmission. That doesn’t sound very exciting, does it? However, expect very thorough (600+ pages!) and enjoyable journey through databases, protocols, algorithms and distributed systems.

In the first chapter: “Reliable, Scalable, and Maintainable Applications” the author describes the environment in which our systems live nowadays. What are the possible failure modes (softwar…

RxJava vs Reactor

Summary:Stick to whichever library you already have on your CLASSPATH.If you get a choice, Reactor is preferable, RxJava 2.x is still a good alternativeIn case you’re on Android, then RxJava 2.x is your only choice Table of contents:APIType-safetyChecked exceptionsTestingDebuggingSpring supportAndroid developmentMaturitySummary Many people ask me, which library to use in their new projects (if any). Some are concerned that they learned RxJava 1.x and then 2.x came along, and the Reactor. Which one should they use? And some also wonder, what’s with this new java.util.concurrent.Flow? Let me clarify a few things. First of all, both versions of RxJava and Reactor are quite similar from a functional perspective. If you know 1.x or 2.x, Reactor will be very familiar, though you still have to learn about the differences. Secondly, Flow class (a set of interfaces, to be precise) is part of a reactive streams specification, bundled into JDK. This specification dictates that various reactive …

Brute-forcing a seemingly simple number puzzle

Something was bothering me for almost two decades. It was a pen and paper game that I learned when I was around 13. The rules are simple: on an empty 10x10 grid (100 squares in total) you put a number 1 on an arbitrary square. Starting from that square you can move horizontally or vertically jumping over two squares or diagonally jumping over one square. There you can place number 2. Your task is to reach number 100, filling all squares. You can not visit already visited squares. Here is an example of a solved game with a reduced 5x5 grid, starting at top-left corner:

12414225162158201310182311471536172212919 On the other hand, if the program makes bad choices, we might get stuck without reaching the perfect score of 25 (on a reduced 5x5 grid):

18291613517141047153619121811 Notice how we got stuck at number 19, unable to move anywhere and fill six remaining gaps. On an original 10x10 grid I never managed to reach the perfect score of 100. Countless hours wasted at school, of trial and …

Thread pool self-induced deadlocks

Summary (reading time: 10 minutes)Deadlocks are caused by many threads locking the same resourcesDeadlocks can also occur if thread pool is used inside a task running in that poolModern libraries like RxJava/Reactor are also susceptible A deadlock is a situation where two or more threads are waiting for resources acquired by each other. For example thread A waits for lock1 locked by thread B, whereas thread B waits for lock2, locked by thread A. In worst case scenario, the application freezes for an indefinite amount of time. Let me show you a concrete example. Imagine there is a Lumberjack class that holds references to two accessory locks:

import com.google.common.collect.ImmutableList; import lombok.RequiredArgsConstructor; import java.util.concurrent.locks.Lock; @RequiredArgsConstructor class Lumberjack { private final String name; private final Lock accessoryOne; private final Lock accessoryTwo; void cut(Runnable work) { try { accessoryOne.…

Sneak peek at spring-cloud-function serverless project

Almost a year ago Spring team announced spring-cloud-function umbrella project. It's basically a Spring's approach to serverless (I prefer the term function-as-a-service) programming. Function<T, R> becomes the smallest building block in a Spring application. Functions defined as Spring beans are automatically exposed e.g. via HTTP in RPC style. Just a quick example how it looks:

@SpringBootApplication public class FaasApplication { public static void main(String[] args) throws Exception { SpringApplication.run(FaasApplication.class, args); } @Bean Function<Long, Person> personById(PersonRepository repo) { return repo::findById; } } @Component interface PersonRepository { Person findById(long id); Mono<Person> findByIdAsync(long id); } The implementation of PersonRepository is irrelevant here. This is a valid Spring Boot application. But once you put spring-cloud-function dependency, beans of Function type come …