Swift on the Server
By Zef Hemel
- 4 minutes read - 714 wordsItâs happening
Ever since Swift was open sourced by Apple including a release for Linux, people speculated about why Apple would do this. The most popular theory was that Apple itself wanted to use Swift on the server. And these days, even for Appleâââserver equals Linuxâââhence the Linux release. As far as we know, much of Appleâs server infrastructure (at least the stores) is build on WebObjectsâââa technology that is showing its age (to put it mildly). Swift, as a language fully under Appleâs control, could be an obvious foundation for replacing WebObjects within Apple.
But until yesterday there was no server story from Cupertino at all, so the community stepped in:
Diversity is good, but some standardization in the community doesnât hurt.
Luckily, thereâs now going to be an official Server APIs project as part of Swift.org:
My take
I played with Swift and read its spec. Itâs a nice language. Especially if youâre coming from Objective-C, itâs a huge leap forward.
The question isâââdoes a leap forward compared to Objective-C make it and interesting enough language to develop servers in, compared to the dozens of established alternatives? Or is the primary audience for it iOS Swift developers that can then do full-stack Swift development?
Of what Iâve seen of Swift, itâs a very nice, but not a simple language. I know Apple is pushing it as a âmy first programming languageâ for education (with Swift Playgrounds on iPad)âââit has good reasons to do so, but is it as simple as a language can be? No, because a certain amount of cruft needed to be introduced to make it interact seamlessly with âlegacyâ Cocoa and Objective-C code. And of course, that additional complexity is of zero help on the server.
Is that a big deal? I donât know.
To be successful on the server, a ginormous amount of work is going to be required to bring the Swift server ecosystem up to par with its competitionâââJava, Ruby, Python, PHP, Node, Go, and even Rust.
There is already good support for Swift (through Objective-C) for many things, but much of it wonât work on the server, because of dependencies on Cocoa and the Objective-C runtime. As a result itâs actually pretty hard to find libraries today you can use with Swift on the server. IBM has a catalog. Whatâs up with IBMâs obsession with Swift by the way.
What will Swiftâs edge be?
But even if the ecosystem matures, what would be the win of Swift over its alternatives?
As I see itâââthis is what makes it interesting for server development:
- It compiles to native, highly optimized code and doesnât require a VM to runâââso itâs fast and cheap in terms of memory usage.
- If youâre an iOS/Mac developer that already uses Swift for your iOS/Mac appsâââyou get to use the same language (although likely with different libraries) on the server and client. Full-stack Swift!
- Itâs a modern language, and has many modern features like type inference, option types, automatic memory management (without a garbage collector), generic programming, string interpolation, functions as first-class objects, solid unicode support, etc.
HoweverâŚ
We live in post thread-per-request world. Node.js is fully asynchronous and can handle thousands of concurrent requests easily, Go has channels and go-routines multiplexing multiple go-routines on a thread. Rust is close to the OS so only supports threads, but it can actually statically verify that no race conditions and other concurrency bugs can occur. Which is pretty wild.
So whatâs Swiftâs story here? At a language level it has none for now. Is Grand Central Dispatch a competitive answer on the server?
Iâd say âno.â
There are some user-land libraries emerging that implement things like co-routines and channels in Swift, like Venice and Safeâââbut these things typically donât interact well with existing âsynchronousâ APIs. Thereâs much to be said to have language (VM) level support (or at least strong conventions, like Node) for this thing these days (like Go and Erlang have).
But anyway, Swift on server is getting enough excitement to become a real thing. Letâs see if it will pick up outside the world of Apple believers.