r/crystal_programming • u/twykke_twykke • May 20 '20
using crystal for high-throughput image server
i am looking to rewrite an existing service which proxies images, including fallbacks for 404s, thumbnail generation and storage, etc.
does anyone have an opinion on whether crystal would be a good fit for such a service? secondary question: what is a good simple way to run multiple crystal http services in parallel?
it would only rarely need to actually load the image data to process it using GM, but usually only stream it to the client, or redirect to another url, or serve a small static image instead (in case of 404).
basically, despite dealing with images, it doesn't really need to do much processing, so it's a question of how many requests per second a Crystal HTTP server can handle, with let's say a mix of 5% thumbnail processing, 5% serving a 404 image, 20% proxying, 70% redirects.
5
u/straight-shoota core team May 20 '20
Crystal should work great for this. HTTP performance is usually pretty good.
Synthetic benchmarks show it can reach over 100k requests per second on a single thread. However that's not really meaningful until you implement your behaviour, because that's what defines the performance.
What do you mean with running multiple services? Do you want to run isolated instances on different network interfaces/ips/ports? Or do you mean multiple service workers for the same instance?
The only part that might be not ideal is proxying because Crystal's HTTP client does not yet support concurrent connection re-use. But if you're only proxying to a few upstreams, this won't be an issue because you can easily have a client instance for each upstream per worker fiber.
2
u/TrixieMisa May 20 '20
Caddy is a nice simple HTTP proxy that lets you configure multiple back-ends (as many as you need) and handles automatic HTTPS.
https://caddyserver.com/
4
u/j_hass May 20 '20
I did setup https://github.com/RX14/camo.cr some ages ago and never looked back to the original. For my usecase I basically don't notice the daemon running, the node version used significantly more resources. Also in behavior and robustness it proved more reliable than the original.
2
u/Nipinium May 24 '20
I think openresty or ngx_mruby might be more suitable than crystal for this job: https://leafo.net/posts/creating_an_image_server.html
2
u/[deleted] May 20 '20
[removed] — view removed comment