# Ping Requests getting dropped for #ZU2: Concurrent clients

**URL:** <https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [May 4, 2024, 9:53am UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273 "2024-05-04T09:53:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vermaankit484](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/vermaankit484/32/232_2.png) [@vermaankit484](https://forum.codecrafters.io/u/vermaankit484)\
**Post date:** [May 4, 2024, 9:53am UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/1 "2024-05-04T09:53:06Z")

</div>

I’m stuck on Stage 5 (tests failing for stage 4).

I implemented ECHO command, but suddenly the PING command for concurrent clients started giving wierd behaviour. The requests for first two clients are getting served, while before further connection could establish for other clients, the request seem to be dropped out

I’ve tried checking if there’s some shared resource causing the unexpected behaviour, verified the serverSocket object has not closed due to some unexepected exception, verified the executorservice has not shut down due to some exception.

Here are my logs:

```auto
[stage-4] Running tests for Stage #4: Handle concurrent clients
[stage-4] $ ./spawn_redis_server.sh
[your_program] Waiting for connection
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[your_program] Connection established
[your_program] Waiting for connection
[stage-4] client-1: Received bytes: "+PONG\r\n"
[stage-4] client-1: Received RESP value: "PONG"
[stage-4] Received "PONG"
[stage-4] client-2: $ redis-cli PING
[stage-4] client-2: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[your_program] Connection established
[your_program] Waiting for connection
[stage-4] client-2: Received bytes: "+PONG\r\n"
[stage-4] client-2: Received RESP value: "PONG"
[stage-4] Received "PONG"
[stage-4] client-1: $ redis-cli PING
[stage-4] client-1: Sent bytes: "*1\r\n$4\r\nPING\r\n"
[stage-4] Received: "" (no content received)
[stage-4] ^ error
[stage-4] Error: Expected start of a new RESP2 value (either +, -, :, $ or *)
[stage-4] Test failed

```

And here’s a snippet of my code:

```auto
public class Main {
  public static void main(String[] args){
      ExecutorService executorService = null;
      try (ServerSocket serverSocket = runServer()) {
          executorService = Executors.newCachedThreadPool();
          while (true) {
              System.out.println("Waiting for connection");
              Socket socket = serverSocket.accept();
              System.out.println("Connection established");
              executorService.submit(new RequestHandler(socket));
          }
      } catch (Exception exception) {
          System.out.println("Exception received " + exception.getMessage());
      } finally {
          executorService.shutdown();
      }
  }

  private static ServerSocket runServer() throws IOException {
      final ServerSocket serverSocket = new ServerSocket(6379);
      serverSocket.setReuseAddress(true);
      return serverSocket;
  }
}

```

The request from third client seem to be timed out before the connection could be established.  
The RequestHandler class is implementing Runnable interface and there’s no shared resource among threads. Hence not adding code for it.

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 6, 2024, 2:06pm UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/2 "2024-05-06T14:06:09Z")

</div>

@vermaankit484 what does `RequestHandler` looks like? Could you try adding logs there to figure out where it’s stuck when the timeout occurs? Is it waiting on more data from the client?

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 6, 2024, 2:07pm UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/3 "2024-05-06T14:07:17Z")

</div>

Note that the third request here isn’t from a separate client, it is from client-1. So it looks like the bug here is that the program can handle the first ping from a client but not the second (from the same client).

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [May 13, 2024, 12:00pm UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/4 "2024-05-13T12:00:41Z")

</div>

Going to close this for now, please let us know if you still need help!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [May 18, 2024, 12:00pm UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/5 "2024-05-18T12:00:51Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [June 4, 2024, 2:21am UTC](https://forum.codecrafters.io/t/ping-requests-getting-dropped-for-zu2-concurrent-clients/273/6 "2024-06-04T02:21:52Z")

</div>

Note: I’ve updated the title of this post to include the stage ID (#ZU2). You can learn about the stages rename here: [Upcoming change: Stages overhaul](https://forum.codecrafters.io/t/upcoming-change-stages-overhaul/375).
