uvloop makes asyncio fast. In fact, it is at least 2x faster than nodejs, gevent, as well as any other Python asynchronous framework. The performance of uvloop-based asyncio is close to that of Go programs.
At Paxos, we make heavy use of asyncio because it’s more performant and also because it’s a better fit for our architecture. Our product synchronizes transaction data from many different clients and is inherently asynchronous because it’s a distributed system.
asyncio is great for maximizing throughput, but we also care about reducing latency. For latency, your database can be a major bottleneck. When using best practices like a proper index and SSDs, a ~10 ms response time for a database query is considered good.
RAM is orders of magnitude faster, but simple key-value stores don’t allow for many of the features software engineers rely on. Multiple indices and basic type checking are very useful. ORDER BY, LIMIT, and OFFSET, are nice to have as well.
We’ve been impressed with redis due to its performance, advanced features and documentation. So, we wrote a redis-backed object mapper called subconscious that relies on asyncio and redis’ powerful primitives. You get the benefits of a database, with the performance of RAM! It’s like a bare-bones version of SQLAlchemy that will never support JOIN operations.
You can define a model like this (for more details, check out the official repo):
class Gender(enum.Enum): MALE = 'male' FEMALE = 'female' class User(RedisModel): uuid = Column(primary_key=True) name = Column(required=True) age = Column(index=True, type=int, sort=True, required=True) gender = Column(index=True, enum=Gender) country_code = Column(index=True)
Then you can query and use your model like this:
some_user = await User.load(db, some_uuid) if some_user.age > 65: # apply senior citizen discount price -= 10.00
You’ll also find lots of features you’ve come to expect from traditional ORMs. Here’s how you’d send a promotion to your US, college-age, male users:
async for user in User.filter_by( db=db, age=[18, 19, 20, 21, 22], country_code='USA', gender=Gender.MALE, ): await send_promo_email(user)
In order to demonstrate how powerful and easy to use this is in practice, we’ve thrown together a pastebin-like website in ~100 lines of code:
We load-tested this app on Microsoft Azure’s cheapest instance (called an F1, which has 1 core and 2 GB RAM), and the numbers are great:
- 10k pageviews took ~41s
- Median pageview was ~19ms
- 90% of pageviews return in < 22ms
Redis can be saved to disk, but has much weaker persistence vs a traditional database. Transactions and rollbacks are complicated, and there are less guarantees when it comes to data integrity. Some common use-cases:
- Ephemeral data that can be replayed from a log (so persistence isn’t a big concern)
- Frequently-updated data: writing to RAM is *much* faster than writing to disk
- When latency is extremely important
- When the same data is queried regularly, the volume of data is low, and/or you have a large hardware budget
For low-throughput applications or applications that lend themselves to horizontal scaling, these features may not provide enough benefit to get excited about. But, for high-throughput apps where latency matters, the performance increase can be substantial.
Subconscious is open-sourced using an MIT license, so feel free to use it for your own projects!
Interested in working with these technologies (and more)? We’re hiring software engineers.
Originally published at www.paxos.com on April 27, 2017.