swifty - a very fast shared memory cache
For a couple of months while developing my new web service, I have been wondering what would be the most efficient way of caching small data, such as SQL responses or HTML snippets. I looked at Cache::Cache and memcached, but was not satisfied by their design, since I did not have any plans of scaling-out my service to multiple servers, but wanted to squeeze maximum performance out from a single server. So last week, as a little fun while creating my presentation slides for my company, I started to write my own cache library, swifty.
Features of swifty:
- use multiple mmap-ed files for sharing data
- lock-free reads
- writes serialized by using flocks
- data consistency maintained through the use of memory barriers
Since swifty uses a lock-free algorithm, its read performance is very high. On my MacBook (Core 2 Duo @ 2GHz), a single process can perform 7.8 million transactions per second for reading a 1K block (7.8GB/sec). This would be from tens or maybe to hundreds of times faster than other cache systems that use system calls for accessing cached data.
The write performance on the same MacBook was 310 thousand transactions (310MB/sec), the flock system call being the bottleneck. Not as impressive as the read performance, it should still be faster than (or at least equivalent to) other conventional cache systems, since swifty issues only two system calls per a write transaction (one lock and one unlock).
Currently, the development stage is still very preliminaly, and lacks many features, namely as follows. But if you are interested, please have a look.
- write Makefile and configure
- bindings to perl and other languages
- support for processors other than Pentium 4 and its successors
- extension of expiration timer when a single process starts updating
PS. it might be interesting to use swifty for caching memcached responses :-)