Best design practices for AppHost

Hi,

My project is geting rather large and I am looking for the best approach for good multi-threading performance on a WIndows server.

I am currently implementing AppHostBase. Is that the best class to use?

My ServiceInterfaces implements the Service class. Caching is done on most endpoints using Request.ToOptimizedResultUsingCache.

Non-cached endpoints look like this:

public object Get(GetSomething request)
{
    return BusinessLogic.Get(request);
}

Cached ones look like this:

return base.Request.ToOptimizedResultUsingCache(LocalCache, cacheKey, expireInTimespan, () =>
{
    var response = BusinessLogic.Get(request, session);
    Response.AddHeaderLastModified(DateTime.UtcNow);
    Response.AddHeader("Cache-Control", $"max-age={cacheSeconds}");
    return response;
});

The (quite large) BusinessLogic code looks is using static methods. Is this the best way to go?

public static object Get(GetSomething request) { .. }

Any other suggestions?

Thanks!

All ASP .NET hosts are already multi-threaded web servers, note ServiceStack doesn’t spawn any threads itself to handle requests, i.e. it uses the same ASP .NET Request Worker thread to handle the request.

I’ll then instead assume you’re just interested in what you can do to improve performance, the most important of which is to use the latest ASP .NET Core runtime, i.e. .NET 6.

ServiceStack provides 2 different caching options:

  • Service Response Caching - to cache the service response to prevent it from being re-executed for the same request
  • HTTP Caching - control how the HTTP Client should cache the response

They’re different strategies and you’re trying to combine both in the same request which wont work as you expect since the create cache delegate only gets executed once whilst the cache is valid, i.e. your HTTP Caching headers wont be returned on cached responses.

Choose one or the other, if you want to improve performance of multiple clients making the same request use Service Response caching which will save service execution time by returning the cache response.

If you want to improve performance from the same clients making the same requests, (e,g. full page requests making the same API calls or clients polling same APIs for latest info), use HTTP Caching as you can control how long clients can cache the response for and can save returning entire response payloads by returning NotModified when client has the latest cache.

If the Service execution takes a long time, I’d recommend response caching since cache hits save execution times. But if the Service returns large responses I’d recommend using HttpCaching instead and a short-circuiting strategy like ETag or LastModified which you’d use to determine if the client has the latest cached response.

I don’t understand what’s the context for this as ServiceStack only executes instance methods. If you’re asking about general strategy I would use a singleton instead of static methods, both are allocation-free but singletons can have substitutable implementations.


Note: to format source code, use code blocks with the appropriate highlightjs language, e.g. for C#:

```csharp
// C# Code
```
1 Like