-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign patterns
More file actions
85 lines (70 loc) · 3.41 KB
/
Copy pathdesign patterns
File metadata and controls
85 lines (70 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
singleton design pattern
prototype design pattern
starglor design pattern
ACID
SOLID
saga design pattern
MVC design pattern
Reposetry design pattern (DAO)
observer design pattern
factory design pattern
Statergy design pattern
-- over data base design pattern were it helps a web application switching from one data base to another
ie .. oracle to mySQL etc
Proxy design apttern -- AOP
======================================================================================================================================================================================================
circuit breaker -- (resourse for understanding) https://github.com/akash-coded/spring-framework/discussions/178
CODE ::
resilience4j.circuitbreaker:
instances:
productCatalog:
failureRateThreshold: 50
waitDurationInOpenState: 5000
ringBufferSizeInHalfOpenState: 5
ringBufferSizeInClosedState: 10
---------------
@Service
public class TransactionService {
// ...
@CircuitBreaker(name = "creditScoreService", fallbackMethod = "fallbackCreditScore")
public CreditScore getCreditScore(Customer customer) {
// Call to External Credit Score Service
}
private CreditScore fallbackCreditScore(Customer customer, Throwable t) {
// Fallback logic
}
}
------------------------------------------------
-- can be implemented in 2 cases :
-- synchronus
-- inter service communication were we can prevent cascading failures, here each service is waiting for other to respond,
so we can quickly fail and respond with a responce.
-- Asynchronus
-- here we mostly communicate with the message queues, working on this queues we can mannage this queue and Re-Queue or redirect it other queues if implemented.
implementation of the circuit breaker should be thoughful considering the casees like
-- inter local communication.
-- multi region implementation.
-- multi service implemenattion
lets say we have chaining communication between the three services and each of it should have its own implementation of circuit breaker.
so that when its failing individually they can have there own failure response and handled.
sates of circuit breaker :
==========================
Closed: The normal state where requests are routed through. If failures reach a certain threshold, the circuit breaker transitions to the open state.
Open: In this state, requests are not made to the service; instead, a fallback method is invoked. After a predefined "sleep" duration, it transitions to a half-open state to test if the underlying problem is resolved.
Half-Open: A limited number of requests are allowed through. If these requests are successful, the circuit breaker moves back to the closed state. If not, it returns to the open state.
retry mechanisum
@Service
public class PaymentService {
@Autowired
private RestTemplate restTemplate;
@Retryable(maxAttempts = 3, backoff = @Backoff(delay = 1000))
public PaymentResponse charge(PaymentRequest request) {
return restTemplate.postForObject(
"https://api.payment.com/charge",
request,
PaymentResponse.class
);
}
}
======================================================================================================================================================================================================
Rate limiting -- Resilience4j