How to handle slow external APIs in Spring Boot microservices

💡 Day 14 of my 𝗝𝗮𝘃𝗮 𝗕𝗮𝗰𝗸𝗲𝗻𝗱 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝗼𝗳 𝘁𝗵𝗲 𝗗𝗮𝘆 𝘀𝗲𝗿𝗶𝗲𝘀: 🧠 Question: Your Spring Boot microservice calls an external payment API. Sometimes the payment API responds within 200 ms, but during peak hours it starts taking 3–5 seconds. This slowdown is causing your API to become slow, and users are complaining about delays. 👉 As a backend engineer, what practical steps would you take to protect your service from slow downstream APIs? ✅ Answer: To handle a slow external API without affecting your users, apply these strategies: 1. 𝐒𝐞𝐭 𝐚𝐠𝐠𝐫𝐞𝐬𝐬𝐢𝐯𝐞 𝐭𝐢𝐦𝐞𝐨𝐮𝐭𝐬 Never wait 5 seconds for a dependency. Set: • Connection timeout → short • Read timeout → short Example: 500 ms–1 sec 2. 𝐔𝐬𝐞 𝐂𝐢𝐫𝐜𝐮𝐢𝐭 𝐁𝐫𝐞𝐚𝐤𝐞𝐫 (𝐑𝐞𝐬𝐢𝐥𝐢𝐞𝐧𝐜𝐞4𝐣) If the payment service becomes slow or fails repeatedly: • Open the circuit • Stop calling it temporarily • Return fallback immediately This prevents your threads from being blocked. 3. 𝐀𝐝𝐝 𝐁𝐮𝐥𝐤𝐡𝐞𝐚𝐝 𝐩𝐚𝐭𝐭𝐞𝐫𝐧 Isolate payment API calls into a separate thread pool. If payment is slow, it will only block its own pool, not the entire service. 4. 𝐔𝐬𝐞 𝐚𝐬𝐲𝐧𝐜𝐡𝐫𝐨𝐧𝐨𝐮𝐬 𝐜𝐚𝐥𝐥 + 𝐝𝐞𝐟𝐞𝐫𝐫𝐞𝐝 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞 Don’t block your main request thread. Your API can respond quickly with: • “Payment is in processing” • Notify user when complete 5. 𝐂𝐚𝐜𝐡𝐞 𝐬𝐭𝐚𝐛𝐥𝐞 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞𝐬 If API provides static data (token, config), cache it to reduce external calls. 6. 𝐀𝐝𝐝 𝐟𝐚𝐥𝐥𝐛𝐚𝐜𝐤 𝐩𝐥𝐚𝐧 Example: • Retry with exponential backoff • Switch to secondary payment provider • Return partial response ✅ 𝘚𝘮𝘢𝘳𝘵 𝘵𝘪𝘮𝘦𝘰𝘶𝘵 + 𝘤𝘪𝘳𝘤𝘶𝘪𝘵 𝘣𝘳𝘦𝘢𝘬𝘦𝘳 + 𝘣𝘶𝘭𝘬𝘩𝘦𝘢𝘥 → 𝘺𝘰𝘶𝘳 𝘴𝘦𝘳𝘷𝘪𝘤𝘦 𝘴𝘵𝘢𝘺𝘴 𝘧𝘢𝘴𝘵 𝘦𝘷𝘦𝘯 𝘸𝘩𝘦𝘯 𝘥𝘦𝘱𝘦𝘯𝘥𝘦𝘯𝘤𝘪𝘦𝘴 𝘢𝘳𝘦 𝘴𝘭𝘰𝘸. ⚙️ See you tomorrow for Day 15 👋 #Java #SpringBoot #Microservices #Resilience4j #SystemDesign #BackendDeveloper #Performance #ContinuousLearning #QuestionOfTheDay

To view or add a comment, sign in

Explore content categories