This content originally appeared on Bits and Pieces - Medium and was authored by Kamini Kamal
What is the retry pattern — and how to implement it in a microservices architecture?
While dealing with a cloud environment, we come across software, hardware, and network errors which ultimately led to delays, timeouts, and failures in the system. In case of transient failures such as network loss, temporary unavailability of a microservice, timeouts, etc, which are self-recoverable and temporary, we have the option to retry and check if the requests are served.
Strategies for implementing Retry mechanism
- Fixed Delay: Setting a fixed interval to send retry requests every time we get an error. For example, RetryDelay(i) = 100 [ms] for each subsequent retry.
- Incremental Delay: Incrementally increasing the delay with each retry. For example, RetryDelay(i) = 100*i [ms] which increases for subsequent requests.
3. Exponential BackOff: Exponentially setting the delay based on the requirement of the system. For example, RetryDelay(i) = t = 100 * 2 ^(i-1) [ms]
Considerations
- Errors should be transient, temporary, and recoverable.
- The delay or backoff strategy to be used depends on the requirements of the microservice.
- Randomization/Jitter between the retries can be introduced to distribute traffic more evenly in healthy instances.
- It is safe to retry on the idempotent requests to trace the errors.
Ways to implement Retry mechanism
- Library to be shared among microservices
💡 Besides creating a library for the retry logic, you can also use an open-source toolchain like Bit for sharing and managing the retry logic as reusable components making it easier to share, version, and test the code. Moreover, you can also integrate the retry logic with other reusable components from the Bit ecosystem, and update the code more easily across multiple microservices. Find out more here, and here.
Creating one common library and sharing among the services can be a way to reuse the logic. In this case, the services will decide which retry strategy to follow at the application runtime.
2. Ambassador Sidecar pattern on each individual microservices
In this case, each microservice can implement its own retry logic and deploy it as a sidecar. The individual microservices are responsible for the maintenance of the logic as there is no shared instance for the retry implementation.
Accounting the failures and monitoring the requests
The requests sent to different services can be subject to different policies and the application should log the details of faults and failing operations. At times, the monitoring tools might get flooded with failure alerts, we need a mechanism to log early failures. It is best to log early failures as informational entries and only the failure of the last of the retry attempts as an actual error.
If a service is constantly busy or failing possibly because the allocated resources got exhausted, it is high time to scale out the service.
References
Summary
In this article, we learned about the Retry pattern and also the ways to implement it on the microservices architecture. We will continue exploring more in future articles.
Thank you ❤️ for reading this article, feel free to Subscribe, clap👏🏻 and share it.
Build Apps with reusable components, just like Lego
Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Learn more:
- How We Build Micro Frontends
- How we Build a Component Design System
- How to reuse React components across your projects
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
Understanding the Retry Pattern was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Kamini Kamal
Kamini Kamal | Sciencx (2023-02-17T14:31:30+00:00) Understanding the Retry Pattern. Retrieved from https://www.scien.cx/2023/02/17/understanding-the-retry-pattern/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.