Middle
Реализуйте highload RPC ручку /weather с нагрузкой 10k RPS, где функция aiWeatherForecast() работает ~1 секунду
sobes.tech AI
Answer from AI
To implement a highload RPC endpoint /weather with a load of 10k RPS and a function aiWeatherForecast() that takes about 1 second, you need to ensure scalability and asynchronicity so as not to block request processing.
Main approaches:
- Asynchronous request handling — use async/await or event loop to avoid blocking the thread while waiting for the result.
- Caching — if forecasts do not change every second, cache results to reduce load.
- Scaling — run multiple instances of the service behind a load balancer.
- Queues and worker pools — if
aiWeatherForecast()is heavy, process requests through a task queue.
Example in Python using asyncio and aiohttp (simplified):
import asyncio
from aiohttp import web
async def aiWeatherForecast():
await asyncio.sleep(1) # simulate long operation
return {"temp": 20, "condition": "sunny"}
async def handle_weather(request):
forecast = await aiWeatherForecast()
return web.json_response(forecast)
app = web.Application()
app.router.add_get('/weather', handle_weather)
if __name__ == '__main__':
web.run_app(app, port=8080)
For 10k RPS, it will be necessary to run multiple server instances, use a load balancer (e.g., Nginx), and possibly optimize aiWeatherForecast() or cache results to avoid running a heavy function on each request.
Also consider using more performant languages or services if Python cannot handle the load.