Fast Perfomance WebSocket Server

TsgcWebSocketServer and TsgcWebSocketHTTPServer are based on Indy library, so every connection is handled by a thread, so if you have 1000 concurrent connections, you will have, at least, 1000 threads to handle these connections. When performance is important, you must do some "tweaks" to increase performance and improve server work.

 Use the following tips to increase server performance.


1. Set in Server component property NotifyEvents := neNoSync. This means that events are raised in the context of connection thread, so there is no synchronization mechanism. If you must access to VCL controls or shared objects, use your own synchronization mechanisms.


2. Set in Server component property Optimizations.Connections.Enabled := True. If you plan to have more than 1000 concurrent connections in your server, and you call Server.WriteData method a lot, enable this property. Basically it saves connections in a cache list where searches are faster than accessing to Indy connections list.

    2.1 CacheSize: is the number of connections stored in a fast cache.

    2.2 GroupLevel: creates internally several lists splitted by first character, so if you have lots of connections, searches are faster.


3.  Set in Server component property Optimizations.Channels.Enabled := True. Enabling this property, channels are saved in list where searches are faster than previous method.


4.  Set in Server component property Optimizations.ConnectionsFree.Enabled := True. If this property is enabled, every time there is a disconnection, instead of destroy TsgcWSConnection, object is stored in a List and every X seconds, all objects stored in this list are destroyed.


5. By default, sgcWebSockets uses Critical Sections to protect access to shared objects. But you can use TMonitor or SpinLocks instead of critical sections. Just compile your project with one of the following compiler defines




6. Use latest FastMM4, you can download from:

    FastMM4 is a very good memory manager, but sometimes doesn't scales well with multi threaded applications. Use the following compiler define in your application:

     {$DEFINE UseReleaseStack}

   Then, add FastMM4 as the first unit in your project uses and compile again. For a high concurrent server, you will note an increase in performance.

   This tweak does following: If a block cannot be released immediately during a FreeMem call the block will added to a list of blocks that will be freed later, either in the background cleanup thread or during the next call to FreeMem.