Home > Back-end >  How do I determine how many 3rd party web api calls my background task will make in production?
How do I determine how many 3rd party web api calls my background task will make in production?

Time:12-11

My ASP.NET Core MVC web application runs a background task that requests crypto market data from the CoinGecko Api on a set interval. I'm using SignalR to create an open connection between the client and server, so that the displayed data is always up-to-date without the client having to manually request from the server.

CoinGecko has a rate limit of 50 calls/minute. I want to display data for 4 specific coins. Based on the data that I want to display, I'm estimating that I'll have to make 25 calls to update all info. I'll break down the calls:

  • 1 call to /coin/markets to get things like market cap, circulating supply, etc. for all 4 coins
  • 24 calls to /coins/{id}/market_chart to get the 1hour, 1day, 7day, 30day, 90day, and 1year price charts for all 4 coins (4 coins x 6 time intervals)

Assuming I make all 25 calls every minute to keep my data updated, will the number of clients affect the number of API calls I make? I'm guessing that it won't because the data is requested in the back end and then served to all the clients through a SignalR hub.

CryptoHttpListener.cs (the background task):

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                HttpResponseMessage response = client.GetAsync(client.BaseAddress   "/simple/price?ids=bitcoin,ethereum,cardano,shiba-inu&vs_currencies=usd").Result;
                if (response.IsSuccessStatusCode)
                {
                    string data = response.Content.ReadAsStringAsync().Result;
                    _logger.LogInformation("{data}", data);
                    cryptoData = JsonConvert.DeserializeObject<CryptoDataModel>(data);
                    await SendMessage(cryptoData);
                }
                else
                {
                    _logger.LogError("API call failed");
                }
                await Task.Delay(10*1000, stoppingToken);
            }
        }

        public async Task SendMessage(CryptoDataModel cryptoData)
        {
            decimal Bitcoin = cryptoData.Bitcoin.Usd;
            decimal Ethereum = cryptoData.Ethereum.Usd;
            decimal Cardano = cryptoData.Cardano.Usd;
            decimal ShibaInu = cryptoData.ShibaInu.Usd;
            await _hubContext.Clients.All.CryptoPriceUpdated(Bitcoin, Ethereum, Cardano, ShibaInu);
        }

SignalR Hub:

public class CryptoPriceHub : Hub<ICryptoPriceClient>
    {

    }

public interface ICryptoPriceClient
    {
        Task CryptoPriceUpdated(decimal Bitcoin, decimal Ethereum, decimal Cardano, decimal ShibaInu);
    }

Index.cshtml

<p id="bitcoin">placeholder text</p>
<p id="ethereum">placeholder text</p>
<p id="cardano">placeholder text</p>
<p id="shibainu">placeholder text</p>

@section Scripts {
    <script src="~/lib/aspnet/signalr/dist/browser/signalr.min.js"></script>
    <script type="text/javascript">
        var connection = new signalR.HubConnectionBuilder().withUrl("/hub").build();
        connection.on("CryptoPriceUpdated", function (Bitcoin, Ethereum, Cardano, ShibaInu) {
            //Update the DOM
            console.log(Bitcoin   ' '   Ethereum   ' '   Cardano   ' '   ShibaInu);
            document.getElementById("bitcoin").innerText = Bitcoin;
            document.getElementById("ethereum").innerText = Ethereum;
            document.getElementById("cardano").innerText = Cardano;
            document.getElementById("shibainu").innerText = ShibaInu;
        });
        connection.start();

    </script>
}

CodePudding user response:

tldr; You need to make sure your backend server is correctly caching the API calls.

If the API results are cached on the server, and the cached results are used for the public facing requests, then only the API calls in the background will count towards the rate limit.

However, if each public facing request causes an API call, then you will hit the 50/min rate limit after 2 requests.

To Summarize: make sure your server is correctly caching the API calls.

  • Related