The short answer
MariaDB and a Redis-compatible store such as Valkey are not two takes on the same thing. MariaDB is a relational database: it keeps your data on disk in tables, answers questions about it in SQL, and guarantees that a transaction either happens completely or not at all. A Redis-compatible store keeps keys and values in memory and answers simple requests about one key in well under a millisecond.
So the question is rarely which one to pick. Use MariaDB for the data your business is made of: customers, orders, invoices, anything you would have to explain if it were lost or wrong. Use a Redis-compatible store for what makes the application fast or coordinates its parts: caches, sessions, rate limits, counters, queues and locks. Most applications of any size end up with both.
What MariaDB is good at
MariaDB began as a community fork by the database's original developers. It speaks the same protocol and almost the same SQL, so MySQL clients, drivers and most MySQL applications connect to it unchanged.
- Relationships and questions you did not plan for. Tables, foreign keys and joins let you ask new questions of old data: which customers ordered twice last month, which products were never returned.
- Transactions. Moving money between two accounts, or reserving stock while creating an order, either commits as a whole or not at all, even when two requests arrive at the same moment.
- Data larger than memory. Tables live on disk, with the most used parts cached in memory, so a database can hold far more than the server's RAM.
- Durability you can rely on. A commit is written to disk before it is acknowledged, and with a replica in semi-synchronous mode it is also on a second server.
The cost is speed per request and effort per change: a query that joins three tables takes milliseconds, not microseconds, and the schema has to be designed and migrated as the application grows.
What a Redis-compatible store is good at
Valkey is an open-source, BSD-licensed key-value server, continued by its community when the original project changed its license. It speaks the same protocol, so Redis clients and commands work with it as they are.
- Speed. Every key is in memory, and most commands touch one key, so they finish in microseconds and a single server handles a very large number of them each second.
- Data structures, not just strings. Lists, hashes, sets and sorted sets make a queue, a leaderboard or a set of online users one command each.
- Expiry built in. A key can be given a lifetime, which is exactly what caches, sessions, one-time codes and rate limits need.
- Atomic counters and locks. Increments and set-if-absent are single atomic commands, so several application servers can share a counter or a lock without a race.
The cost is that the whole dataset must fit in memory, which is dearer than disk, and that there are no tables, joins or SQL: you look things up by key, so you decide in advance how each piece of data will be found.
Side by side
- Data model: MariaDB has tables, rows and SQL; a Redis-compatible store has keys holding strings, lists, hashes, sets and sorted sets.
- Where the data lives: MariaDB on disk with a cache in memory; a Redis-compatible store in memory, with a log on disk to recover from.
- Typical request: milliseconds for a SQL query; microseconds for a key lookup.
- Size: MariaDB holds as much as its disk; a Redis-compatible store holds what fits in memory.
- Transactions: full multi-statement transactions in MariaDB; single atomic commands, short command batches and scripts in a Redis-compatible store.
- Losing a server: on our high-availability plans, MariaDB's semi-synchronous replica loses no acknowledged commit; our key-value databases can bound the loss to about 1 or 5 seconds of writes, or replicate asynchronously.
Using both together
The most common pattern is a cache in front of the database. The application asks the key-value store first; on a miss it reads MariaDB, stores the answer with an expiry, and returns it. Writes go to MariaDB, which stays the source of truth, and remove the cached copy so the next read fetches the new one:
def get_product(product_id):
key = f"product:{product_id}"
cached = kv.get(key)
if cached is not None:
return json.loads(cached)
row = db.execute("SELECT * FROM products WHERE id = %s", (product_id,)).fetchone()
kv.set(key, json.dumps(row), ex=300) # five minutes
return row
def update_price(product_id, price):
db.execute("UPDATE products SET price = %s WHERE id = %s", (price, product_id))
db.commit()
kv.delete(f"product:{product_id}")Other jobs the two share well:
- Sessions in the key-value store with an expiry, users and their settings in MariaDB.
- Rate limits as a counter per client and minute in the key-value store, so they never load the database.
- Background jobs queued in a list, with each job's outcome recorded in MariaDB.
- Leaderboards and counts kept live in a sorted set, and written to MariaDB now and then for reports.
When one of them is enough
- MariaDB alone is enough while the database answers fast enough under real load. Indexes and a bigger plan go a long way before a cache is worth the extra moving part and the stale reads it can cause.
- A Redis-compatible store alone is enough when everything is looked up by key and can be rebuilt: a cache for an external API, a rate limiter, a feature-flag store.
Running both on ElectraDB
Both run in the same account and region, with the same high availability, encrypted backups and restore to any second of the last 7 days. A managed MariaDB database and a managed Redis-compatible database each get one hostname with TLS, and both are on the pricing page. Moving existing data is covered in migrating a database to MariaDB and moving a Redis-compatible dataset.
More articles
- Coming soon: databases that move closer to your applicationTell us where your application runs and we will place your database, and over time move it, to the servers with the lowest latency to it.
- Moving a Redis-compatible dataset to ElectraDBCopy every key, value and expiry from a Redis-compatible server into an ElectraDB key-value database with a short script, over verified TLS.
- Migrating a MySQL or MariaDB database to ElectraDBDump a MySQL or MariaDB database, load it into ElectraDB's MariaDB over verified TLS, and switch your application over with a short write freeze.
- All articlesEvery article, newest first.