Distributed design patterns
Circuit breaker
This pattern prevents a retry storm when a service is degraded and can't recover because it's clients use Retry pattern and create extra load to the service. A circuit breaker acts as a proxy for operations that might fail. The proxy can be in the following states:
- Closed: the Circuit Breaker allows to execute all requests but it counts number of failures. If number of failures exceed a given threshold for some period of time then the Circuit Breaker goes to Open state and starts a timer. When the timer ends the Circuit Breaker will go to Half-open state. The timeout gives time to the service to recover.
- Open: a request to the service fails immediately.
- Half-open: a very limited number of requests is allowed to be executed and if they are successful then the Circuit Breaker goes to Closed state otherwise it goes to Open state.
Index table
NoSQL database may support only primary key indexing.
If we need to implement a fast search by a non-primary key field we can create an "index table" which has the field as a primary key and has a pointer to the main table.
Retry with backoff
An API call may fail due to different transient problems: network connectivity issues or temporal service unavailability.
To solve the issue a service can implement several attempts to retry an API call with a delay between the attempts. The delay may be random in a given range or exponential (also in a given range).
Retries may create a retry storm and to avoid it you may need to implement a Circuit Breaker.
Always specify a timeout for a network request.
Your API calls should be idempotent.