Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
Created September 18, 2018 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjlyons/0db53723fd60f0872db87f23e375797b to your computer and use it in GitHub Desktop.
Save nwjlyons/0db53723fd60f0872db87f23e375797b to your computer and use it in GitHub Desktop.
Redis v SQL benchmarks
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()
@nwjlyons
Copy link
Author

nwjlyons commented Sep 18, 2018

Redis benchmarks

timeit.timeit(lambda: benchmark_redis(), number=10_000)
>>> 1.6047718999907374
timeit.timeit(lambda: benchmark_redis(2), number=10_000)
>>> 3.1935179000138305

SQL benchmarks

timeit.timeit(lambda: benchmark_sql(), number=10_000)
>>> 7.684576300001936
timeit.timeit(lambda: benchmark_sql(2), number=10_000)
>>> 15.491334499994991

Analysis

Redis is almost five times faster than SQL.

In both redis and sql benchmarks, running two queries doubles the execution time.

@nwjlyons
Copy link
Author

nwjlyons commented Sep 18, 2018

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.

@nwjlyons
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment