Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
Load balancing using setTimeout

I know it's a clickbait title, but this is a serious question. Recently, in Sri Lanka, the government deployed https://fuelpass.gov.lk/ application where a Sri Lankan can register and check the fuel available for the registered vehicle. (The system was designed to control the amount of fuel available to an individual as fuel imports have slowed down due to the economic crisis).

When I checked the system yesterday I realized that they have deployed the dev build to production. As I was going through the code, I noticed that in the Login component, there is a setTimeout call on the form submit to show a loading animation.

The login component looked something like this:

export default () => {
    const [waiting, setWaiting] = useState(false)

    return(
        <Loading showLoading = {waiting}>
            <form onSubmit = {() => {
                setWaiting(true)

                setTimeout(async () => {
                    await loginRequestCall()
                    setWaiting(false)
                }, Math.floor(Math.random() * 5000)
            }}>
                <input />
                <button>Send OTP</button>
            </form>
        </Loading>
    )
}

Screenshot of the source code: enter image description here

The important part is, when user clicks on the Send OTP, It shows the loading screen, only after waiting a random amount of time it sends the request.

I created a video about this and it kinda went viral in Sri Lanka. According to the comments, some of the justifications (for using setTimeout with a random amount of time to wait) were following.

  1. It is a mechanism to prevent DDos attacks
  2. Way to prevent users spamming Send OTP button causing multiple requests
  3. It is a cheap way of load balancing (way to reduce the number of OTP requests)

I wouldn't think if someone wanted to DDos they would be dumb enough to use UI automation. There is no need of setTimeout to prevent spam also.

Which leave me with the last point.

So my questions are,

  1. Is it possible to load balance using setTimeout?
  2. If so, how exactly would it distribute the load?
  3. Is there a valid reason to add a setTimout?
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Is it possible to load balance using setTimeout?

Load balancing is the process of distributing network traffic across multiple servers.

Since load balancing is not reducing the number of calls but distributing them, it is not possible to do load balancing using setTimeout

If so, how exactly would it distribute the load?

As I mentioned, it is impossible to do so with setTimeout, at least not in a non-hacky and inefficient manner.

Is there a valid reason to add a setTimout?

In my opinion, this setTimeout might be intended to be used as a debounce function to avoid the user clicking on the OTP button multiple times if the feedback is not provided immediately (but still not doing the work since all calls will be done anyway).

So the answer is NO, in my opinion, there is not any valid reason for this setTimeout

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda