Before applying the sidecar proxy pattern, the service implements retry logic and error handling itself, mixing business and networking code. After applying the pattern, the service simply calls the local sidecar proxy endpoint. The sidecar handles retries, load balancing, and other network concerns, keeping the service code clean and focused on business logic.
### Before: Service handles retries and logging internally
import requests
def call_service_b():
for _ in range(3):
try:
response = requests.get('http://service-b/api')
if response.status_code == 200:
return response.json()
except requests.exceptions.RequestException:
pass
raise Exception('Failed after retries')
### After: Service delegates networking to sidecar proxy
import requests
def call_service_b():
response = requests.get('http://localhost:15001/service-b/api') # Sidecar proxy listens locally
response.raise_for_status()
return response.json()