Created
September 18, 2018 07:44
-
-
Save nwjlyons/0db53723fd60f0872db87f23e375797b to your computer and use it in GitHub Desktop.
Redis v SQL benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from django.core.cache import cache | |
from django.contrib.auth.models import User | |
def benchmark_redis(n=1): | |
for _ in range(n): | |
cache.get("blank") | |
def benchmark_sql(n=1): | |
for _ in range(n): | |
User.objects.first() |
Difference between returning a small and big payload from Redis
def benchmark_redis(n=1, key="blank"):
for _ in range(n):
cache.get(key)
cache.set("small", b"a" * 100) # 100 bytes
cache.set("big", b"a" * 10_000) # 10,000 bytes
timeit.timeit(lambda: benchmark_redis(key="small"), number=10_000)
>>> 1.791828200017335
timeit.timeit(lambda: benchmark_redis(key="big"), number=10_000)
>>> 1.9380456000217237
There is a slight difference, but not much.
It is faster to return a larger payload than it is to run two redis queries. It seems like the biggest overhead is going over the network.
Latency numbers https://gist.github.com/hellerbarde/2843375
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Redis benchmarks
SQL benchmarks
Analysis
Redis is almost five times faster than SQL.
In both redis and sql benchmarks, running two queries doubles the execution time.