Hystrix Circuit Breaker: Microservices Resilience සඳහා Sinhala Guide

මචංලා, කොහොමද? අද අපි කතා කරන්න යන්නේ ටිකක් serious කතාවක්. ඒ තමයි Microservices ලෝකේදි අපිට හැමදාම වගේ මුහුණ දෙන්න වෙන ලොකුම අභියෝගයක් - system failures ගැන. ඒ වගේම ඒවට විසඳුමක් විදියට පාවිච්චි කරන්න පුළුවන් Circuit Breaker pattern එකයි, ඒක Implement කරන්න උදව් වෙන Netflix Hystrix framework එකයි ගැන. මම දන්නවා සමහරවිට මේ නම් ටික අහලා තියෙන්න පුළුවන්, හැබැයි මේක හරියටම වැඩ කරන්නේ කොහොමද කියලා කීයෙන් කීදෙනාද දන්නේ? අන්න ඒකයි අද අපේ කතාව.
අපි හිතමුකෝ, ඔයාලා e-commerce system එකක් හදනවා කියලා Microservices වලින්. Product Details service එක, Order service එක, Payment service එක, Stock service එක වගේ ගොඩක් පොඩි පොඩි services එකට එකතු වෙලා තමයි මේ මුළු system එකම වැඩ කරන්නේ. මේකේ තියෙන ලොකුම වාසිය තමයි එක service එකක පොඩි අවුලක් ආවම මුළු system එකම down වෙන්නේ නැති එක. හැබැයි එහෙම වෙන්නෙත් නැහැ කියලා 100%ක් කියන්න බැහැ, මොකද එක service එකක අවුලක් ආවම ඒකෙන් අනිත් services වලටත් බලපෑම් ඇති වෙන්න පුළුවන්. අන්න ඕකට තමයි අපි cascading failures කියන්නේ.
උදාහරණයක් විදියට, Order service එක Stock service එකට call එකක් දෙනවා කියලා හිතමු. Stock service එකේ මොකක් හරි network issue එකක් හරි, database issue එකක් හරි වෙලා ඒක respond කරන්නේ නැතුව ගියා කියමු. එතකොට Order service එක, Stock service එක reply කරනකම් බලාගෙන ඉඳලා, නිකරුණේ threads ගොඩක් block කරගන්නවා. මේක ටික වෙලාවක් යනකොට, Order service එකට එන requests ගොඩක් block වෙලා, අන්තිමට Order service එකත් down වෙනවා. අන්න එහෙම වුණාම තමයි cascading failure එකක් කියන්නේ. මේ වගේ අවස්ථා වලට විසඳුමක් විදියට තමයි Circuit Breaker pattern එක අපිට උදව් කරන්නේ.
මොකක්ද මේ Circuit Breaker කියන්නේ? 🤔
නම ඇහුවම හිතට එන පළවෙනි දේ තමයි ගෙදර wiring වල තියෙන circuit breaker එක. ඒක මොකද කරන්නේ? Current එක වැඩියෙන් ගලාගෙන යනකොට හෝ short circuit එකක් ආවම, ඒක automate විදියට circuit එක break කරනවා. ඒ කියන්නේ, අනතුරක් වෙන්න කලින් system එක ආරක්ෂා කරනවා. අපේ software world එකේ Circuit Breaker pattern එකත් හරියටම ඒ වගේ වැඩක් තමයි කරන්නේ.
සරලවම කිව්වොත්, Circuit Breaker එක කියන්නේ proxy එකක් වගේ. අපි පිටතින් call කරන service එකක් fail වෙන්න ගත්තොත්, Circuit Breaker එක detect කරනවා ඒක fail වෙනවා කියලා, ඊටපස්සේ ඒ service එකට යන calls ටික temporarily නවත්වනවා. මේකෙන් වෙන්නේ, failing service එකට තවදුරටත් request යවලා ඒක further overload කරන එක නවත්වන එකයි, ඒ වගේම අපේ calling service එකේ resources නිකරුණේ block වීම නවත්වන එකයි.
Circuit Breaker එකට ප්රධාන states තුනක් තියෙනවා:
- CLOSED: මේක තමයි default state එක. මේකේදී requests ටික සාමාන්ය විදියට remote service එකට යනවා. හැබැයි, යම්කිසි error threshold එකකට වඩා failures වැඩි වුණොත් (උදා: requests 10න් 5ක් fail වුණොත්), Circuit Breaker එක OPEN state එකට යනවා.
- OPEN: මේ state එකේදී, Circuit Breaker එක remote service එකට යන හැම request එකක්ම නවත්වනවා. මේ requests වලට කෙලින්ම error එකක් දෙනවා හෝ fallback mechanism එකක් activate කරනවා. මේක කරන්නේ යම්කිසි timeout period එකකට (sleep window එකකට) විතරයි. මේ timeout එක ඉවර වුණාම, Circuit Breaker එක HALF-OPEN state එකට යනවා.
- HALF-OPEN: මේ state එක ඩිංගක් පරිස්සමින් වැඩ කරන state එකක්. මේකේදී Circuit Breaker එක එක request එකක් විතරක් remote service එකට යවලා බලනවා. ඒ request එක සාර්ථක වුණොත්, ඒ කියන්නේ remote service එක ආයෙත් වැඩ කරනවා නම්, Circuit Breaker එක ආයෙත් CLOSED state එකට යනවා. හැබැයි, ඒ request එකත් fail වුණොත්, ඒක ආයෙත් OPEN state එකට යනවා, ඊටපස්සේ අලුත් sleep window එකක් පටන් ගන්නවා.
මේ pattern එකෙන් වෙන්නේ, failing service එකකට නැවත යථා තත්ත්වයට පත් වෙන්න කාලය දෙන එකයි, ඒ වගේම අපේ system එකේ resilience එක වැඩි කරන එකයි. නියමයි නේද?
ඇයි Hystrix? (Why Hystrix?) 🚀
අපි Circuit Breaker pattern එක ගැන ඉගෙන ගත්තා. හැබැයි මේක අපේ code එකට implement කරන්නේ කොහොමද? අන්න එතනදී තමයි Netflix Hystrix කියන framework එක අපිට උදව්වට එන්නේ. Netflix එකේ අය තමන්ගේ Microservices architecture එකේදී මේ වගේ cascading failures වලට ගොඩක් මුහුණ දීලා තියෙනවා. ඒකට විසඳුමක් විදියට තමයි Hystrix එක හදලා තියෙන්නේ, පස්සේ ඒක open-source කළා. Hystrix එක දැන් maintenance mode එකේ තිබ්බත් (අලුත් development නැහැ), තවමත් ගොඩක් production systems වල මේක පාවිච්චි කරනවා, ඒ වගේම මේකෙන් ගන්න පුළුවන් අත්දැකීම් resilience patterns ගැන ඉගෙන ගන්න අතිශයින් වැදගත්.
Hystrix එකේ ප්රධානම features කීපයක් තියෙනවා:
- Latency and Fault Tolerance: ඕනෑම dependent service එකක latency හෝ failure එකකින් සිදුවන බලපෑම system එකේ අනෙකුත් කොටස් වලට පැතිරීම වළක්වනවා.
- Circuit Breaker Implementation: අපි උඩ කතා කරපු Circuit Breaker logic එක automate විදියට implement කරනවා.
- Fallback Mechanisms: Main operation එක fail වුණොත් call කරන්න පුළුවන් alternative path එකක් (fallback method එකක්) define කරන්න පුළුවන්. උදාහරණයක් විදියට, Stock service එක down නම්, fallback එකක් විදියට default stock message එකක් පෙන්වන්න පුළුවන්.
- Resource Isolation (Bulkheads): විවිධ dependencies වලට වෙන වෙනම thread pools වෙන් කරනවා. මේකෙන් වෙන්නේ, එක service එකක් slow වුණාම, ඒක අනිත් services වලට බලපාන්නේ නැති එකයි. හරියට නැවක මැද තියෙන bulkheads වගේ - එක compartment එකක වතුර පිරුණාම, අනිත් compartments වලට වතුර යන්නේ නැහැ.
- Monitoring and Metrics: Hystrix එක අපේ commands වල performance ගැන detailed metrics generate කරනවා. මේවා Hystrix Dashboard එක වගේ tools වලින් බලන්න පුළුවන්, real-time මොනිටරින් වලට මේක ගොඩක් වටිනවා.
- Request Caching and Collapsing: මේකෙන් requests ප්රමාණය අඩු කරලා, system එකේ overall performance එක වැඩි කරනවා.
මේ features නිසා Hystrix එක Microservices architecture එකක resilience එක වැඩි කරන්න තියෙන ඉතාම ප්රබල tool එකක් විදියට හඳුන්වන්න පුළුවන්.
Hystrix Implement කරමු! 💻
Hystrix implement කරන එක ඇත්තටම හිතන තරම් අමාරු නැහැ, විශේෂයෙන්ම ඔයාලා Spring Boot එක්ක වැඩ කරනවා නම්. අපි පොඩි conceptual example එකක් බලමු.
මුලින්ම, ඔයාලගේ Spring Boot project එකට Hystrix dependency එක එකතු කරගන්න ඕනේ. Maven පාවිච්චි කරනවා නම්, pom.xml
එකට මේක දාන්න:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
ඊටපස්සේ, main application class එකේ @EnableHystrix
annotation එක දාන්න:
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableHystrix
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
දැන් ඔයාලට Hystrix commands හදන්න පුළුවන්. සාමාන්යයෙන්, ඔයාලා external service එකකට call කරන method එකට උඩින් @HystrixCommand
annotation එක දානවා.
හිතමු, අපි ProductService
එකෙන් StockService
එකේ availability එක check කරනවා කියලා:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ProductService {
private final RestTemplate restTemplate;
public ProductService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@HystrixCommand(fallbackMethod = "getFallbackProductAvailability")
public String getProductAvailability(String productId) {
// Simulate calling an external service that might fail
System.out.println("Attempting to get product availability for: " + productId);
// Replace with actual API call to StockService
return restTemplate.getForObject("http://stock-service/products/" + productId + "/availability", String.class);
}
// Fallback method - will be called if getProductAvailability fails
public String getFallbackProductAvailability(String productId) {
System.out.println("Fallback: Could not get product availability for: " + productId);
return "Product availability information is currently unavailable. Please try again later.";
}
}
මේ @HystrixCommand
annotation එකේ තියෙන fallbackMethod = "getFallbackProductAvailability"
කියන එකෙන් වෙන්නේ, getProductAvailability
method එක fail වුණොත් (timeout වුණොත්, exception එකක් දැම්මොත්, circuit එක open වුණොත්), getFallbackProductAvailability
method එක automate විදියට call කරන එකයි. මේක තමයි අපේ resilience එකේ හරි අඩක්.
Hystrix Configuration (Circuit Breaker Settings)
Hystrix වලට තියෙනවා configure කරන්න පුළුවන් ගොඩක් parameters. මේවා ඔයාලගේ application.properties
(හෝ .yml
) file එකේ දාන්න පුළුවන්.
# Hystrix Command Properties for a specific command (e.g., default or by command key)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=10
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.default.metrics.rollingStats.numberOfBuckets=10
# Hystrix Thread Pool Properties (for resource isolation)
hystrix.threadpool.default.coreSize=10
hystrix.threadpool.default.maxQueueSize=100
execution.isolation.thread.timeoutInMilliseconds
: මේකෙන් කියන්නේ Hystrix command එකක් execute වෙන්න උපරිම කීයක් milliseconds ද දෙන්නේ කියලා. මේ කාලය ඇතුළත response එකක් නොලැබුණොත්, command එක timeout වෙලා, fallback එකට යනවා.circuitBreaker.requestVolumeThreshold
: මේක තමයි circuit breaker එක CLOSED state එකේ ඉඳන් OPEN state එකට යන්න කලින්, failure rate එක calculate කරන්න ඕන අවම requests ගාන. උදා: requests 10ක් අඩුවෙන් ආවොත්, failure percentage එක calculate කරන්නේ නැහැ.circuitBreaker.errorThresholdPercentage
: මේකෙන් කියන්නේ failures percentage එක කීයක් වුණොත් circuit breaker එක OPEN කරන්න ඕනෙද කියලා. 50% කියන්නේ, requests 10න් 5ක් fail වුණොත් circuit එක OPEN වෙනවා.circuitBreaker.sleepWindowInMilliseconds
: මේක තමයි circuit breaker එක OPEN state එකේ ඉන්න කාලය. මේ කාලය ඉවර වුණාම circuit එක HALF-OPEN වෙනවා.metrics.rollingStats.timeInMilliseconds
සහmetrics.rollingStats.numberOfBuckets
: මේවා පාවිච්චි කරන්නේ Hystrix එක metrics collect කරන විදිය configure කරන්න.threadpool.default.coreSize
සහthreadpool.default.maxQueueSize
: මේවා තමයි Hystrix එකේ thread pool එකේ size එක සහ queue size එක. මේවා resource isolation (bulkhead) වලට ගොඩක් වැදගත්.
මේ configurations ටික ඔයාලගේ system එකේ අවශ්යතාවයට අනුව modify කරගන්න පුළුවන්. හැබැයි මේවා හරියට තේරුම් අරන් modify කරන එක ගොඩක් වැදගත්, මොකද වැරදි configuration එකක් මුළු system එකටම බලපෑම් කරන්න පුළුවන්.
Circuit Breaker එක වැඩ කරන හැටි (Hystrix එක්ක) ⚙️
දැන් අපි බලමු Hystrix මේ states තුන අතර මාරු වෙන්නේ කොහොමද කියලා.
HALF-OPEN State: පරිස්සමෙන් බලමු!
HALF-OPEN state එකට ආවම, Hystrix එක ඊළඟට එන එක request එකක් විතරක් dependent service එකට යවලා බලනවා (probe request එකක් වගේ). මේ probe request එක success වුණොත්, Hystrix එක හිතනවා dependent service එක ආයෙත් වැඩ කරනවා කියලා, ඊටපස්සේ Circuit Breaker එක ආයෙත් CLOSED state එකට යනවා. හැබැයි, මේ probe request එකත් fail වුණොත්, Hystrix එක හිතනවා dependent service එක තවමත් problem එකක කියලා, ඊටපස්සේ Circuit Breaker එක ආයෙත් OPEN state එකට යනවා, අලුත් sleepWindowInMilliseconds
එකක් පටන් ගන්නවා.
OPEN State: වැඩ නවත්වයි!
Circuit Breaker එක OPEN වුණාම, ඊළඟට එන හැම request එකක්ම කෙලින්ම reject කරනවා (හෝ fallback method එකට යවනවා). ඒ කියන්නේ dependent service එකට කිසිම request එකක් යවන්නේ නැහැ. මේක කරන්නේ sleepWindowInMilliseconds
එකෙන් define කරපු කාලයක් යනකම්. මේ කාලය ඇතුළත dependent service එකට යථා තත්ත්වයට පත් වෙන්න කාලය ලැබෙනවා. මේ කාලය ඉවර වුණාම, Circuit Breaker එක HALF-OPEN state එකට යනවා.
CLOSED State: සාමාන්ය වැඩ
මුලින්ම, ඔයාලගේ Hystrix command එක CLOSED state එකේ තමයි තියෙන්නේ. මේකේදී හැම request එකක්ම සාමාන්ය විදියට dependent service එකට යවනවා. මේ අතරතුර Hystrix එක requests වල success/failure rate එක monitoring කරනවා. යම්කිසි rolling window එකක් (උදා: අවසන් තත්පර 10) ඇතුළත, requests ගණන requestVolumeThreshold
එකට වඩා වැඩි වෙලා, error percentage එක errorThresholdPercentage
එකට වඩා වැඩි වුණොත්, Circuit Breaker එක OPEN state එකට යනවා.
මේ විදියට Hystrix, Circuit Breaker pattern එක implement කරලා, අපේ Microservices system එක fault-tolerant සහ resilient කරන්න උදව් කරනවා. මේකෙන් වෙන්නේ එක service එකක අවුලක් ආවම මුළු system එකම down වෙන එක වළක්වන එකයි, ඒ වගේම user experience එකටත් එච්චරම හානියක් නොවී system එක දිගටම run කරන්න පුළුවන් වෙන එකයි.
මතක තියාගන්න, Hystrix එක monitoring කරන එකත් ගොඩක් වැදගත්. Hystrix Dashboard එකෙන් අපිට real-time බලාගන්න පුළුවන් circuit breaker එකේ state එක, request rates, error rates, latency වගේ දේවල්. මේවා production environments වලදී troubleshooting සහ optimizing වලට අතිශයින් වැදගත්.
අවසාන වශයෙන් (Let's Wrap Up!) 🎯
ඉතින් මචංලා, මේක නිකන් බයිලා ටිකක් නෙවෙයි, Microservices development වලදී අනිවාර්යයෙන්ම දැනගෙන ඉන්න ඕන concept එකක්. Circuit Breaker pattern එකයි, ඒක Hystrix වගේ framework එකකින් implement කරන විදියයි දැනගැනීමෙන් ඔයාලට පුළුවන් robust සහ resilient systems හදන්න. මේකෙන් වෙන්නේ,
- System Stability: එක service එකක් down වුණාම මුළු system එකම down වෙන එක වළක්වනවා.
- Improved User Experience: Users ලට හිස් error pages වෙනුවට fallback messages වගේ ප්රයෝජනවත් දේවල් පෙන්වන්න පුළුවන්.
- Faster Recovery: Failing service එකකට නැවත යථා තත්ත්වයට පත් වෙන්න කාලය දෙනවා.
- Easier Debugging: Failure points isolate කරන්න උදව් කරනවා.
Hystrix අලුතෙන් development වෙන්නේ නැති වුණත්, මේකෙන් ගන්න පුළුවන් concepts සහ patterns අතිශයින්ම වැදගත්. දැන් Hystrix වලට වඩා අලුත් විකල්ප (උදා: Resilience4j) ආවත්, Hystrix කියන්නේ resilience patterns වලට හොඳම introduction එකක්. මේ ගැන දැනගැනීමෙන් ඔයාලට අනිත් frameworks වුණත් ඉක්මනින්ම තේරුම් ගන්න පුළුවන්.
ඉතින්, මේ concept එක ඔයාලගේ next project එකට apply කරලා බලන්න. මොකද හිතෙන්නේ? ඔයාලා Hystrix පාවිච්චි කරලා තියෙනවද? නැත්නම් Resilience4j වගේ වෙන framework එකක්ද? ඔයාලගේ අත්දැකීම් පහල comments වලින් කියන්න. මොකද අපිට එකිනෙකාගෙන් ඉගෙන ගන්න ගොඩක් දේවල් තියෙනවා!
ආයෙත් හම්බවෙමු! Happy Coding! 🎉