0

I need to write a server for stock exchange in Java, the server will get many requests from couple of servers per second, my goal is to write this server and find out how many HTTP GET requests it can handle after executing couple of conditions and send a response in less then 100ms.

Each request contains a token that will be valid for 100ms.

My goal is to fetch data from cache (Redis/Memcached) using the request parameters, execute couple of O(1) conditions, send a response to the same server with the token and a boolean answer in max 100ms and save the request and response in a database (asynchronously).

This server will sit on AWS ec2 at the same region as the requesting servers.

Its needs to be written in Java, low level as it should be, btw, I come from Python/NodeJS world for the past 2 years.

I think its a classic producer-consumer design pattern.

Can anyone guide me to the technical parts ? like.. use "this' for handling requests (SocketInputStream), and use "that" for handling the queues, or maybe use "OpenMQ" framework ? monitor requests with "this" ? any reference for an implementation for similar problem ?

Update 1: Thanks @mp911de, I saw LMAX (Disruptor), someone already invented the wheel but it feels like over-programming for now, I want to check how many 1k json object can be sent and response 100bytes back from simple ec2-server that run this java code I attached ? BTW, how many threads to put in Executors.newFixedThreadPool ?

public class JavaSimpleWebServer {
private static final int fNumberOfThreads = 100;
private static final Executor fThreadPool = Executors.newFixedThreadPool(fNumberOfThreads);
public static final int PORT = 81;

public static void main(String[] args) throws IOException {

    ServerSocket socket = new ServerSocket(PORT);
    while (true)
    {
        final Socket connection = socket.accept();
        Runnable task = new Runnable() {
            @Override
            public void run() {
                HandleRequest(connection);
            }
        };
        fThreadPool.execute(task);
    }
}

private static void HandleRequest(Socket s) {
    BufferedReader in;
    PrintWriter out;
    String request;
    try {
        String webServerAddress = s.getInetAddress().toString();
        System.out.println("New Connection:" + webServerAddress);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        request = in.readLine();
        System.out.println("--- Client request: " + request);
        out = new PrintWriter(s.getOutputStream(), true);
        // do some calculation
        out.println(jsonResponse);
        out.flush();
        out.close();
        s.close();
    }
    catch (IOException e)  {
        System.out.println("Failed respond to client request: " + e.getMessage());
    }
    finally  {
        if (s != null)
        {
            try {
                s.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

}

0xIddo
  • 1
  • 2
  • Google "java producer consumer" for lots of links to examples and explanations. – Gilbert Le Blanc Oct 14 '15 at 17:12
  • HTTP is probably not the best approach when you're heading for a system that has some semantics of a Real-Time system. 100ms are a hard requirement and by running into Garbage Collection you can easily miss the requirement. Take a look at the LMAX architecture and the stuff they did (Martin Thompson). – mp911de Oct 15 '15 at 11:28
  • To answer your question about the threads: It depends! If you have computing-intensive tasks which utilize the CPU and you do not run anything else on the machine, going with the CPU core count is usually a good idea. If you share the machine or you require the CPU's for other processes, use less than that. The other style is to "bridge" I/O time by increasing the thread pool. That means that you initiate a task that does a lot of I/O or waits for the I/O to return and then continue. You can pay with an increased number of threads to "simulate" a seamless processing and better utilize your CPU – mp911de Oct 15 '15 at 18:13

0 Answers0