TradeLabs AILearn

Redis and PostgreSQL for Trading

How Redis and PostgreSQL work together in trading systems: Redis for live prices, caches and state, PostgreSQL for durable orders, fills and history.

Advanced3 min readUpdated 3 Oct 2026
Markdown
Read firstMessage Queues
Lesson 7 of 16

Two databases appear in a huge number of trading setups, from hobby bots to production services: Redis and PostgreSQL. They solve different problems. Redis is an in memory data store, extremely fast and ideal for the latest prices, caches, counters and short lived state. PostgreSQL is a durable relational database, ideal for orders, fills, account history and anything that must never be lost. Used together, they give a system both speed and a reliable record.

Comparing the two#

RedisPostgreSQL
StorageIn memory, with optional persistenceOn disk, fully durable
SpeedSub millisecond reads and writesFast, but slower per operation
Data modelKeys with strings, hashes, lists, sets, sorted sets, streamsTables with SQL, constraints and transactions
StrengthLive state, caching, pub sub, rate limitsAccuracy, queries, history, integrity
RiskData can be lost if persistence is off or misconfiguredSlower under very high write rates

Typical uses of Redis in trading#

import redis
r = redis.Redis()
r.hset("px:BTCUSDT", mapping={"bid": 64250.1, "ask": 64250.5, "ts": 1790000000123})
latest = r.hgetall("px:BTCUSDT")

Typical uses of PostgreSQL in trading#

Transactions are PostgreSQL's key strength: recording a fill and updating a position happen together or not at all, so the books never disagree with themselves.

Persistence settings in Redis#

OptionBehaviour
No persistenceFastest; data lost on restart
RDB snapshotsPeriodic snapshots; recent changes can be lost
AOF (append only file)Logs every write; configurable sync frequency

Treat Redis as a fast cache and live state store unless you have deliberately configured and tested persistence. Anything financial and important belongs in PostgreSQL too.

Performance tips#

  • Index PostgreSQL tables on the columns you filter by, such as (instrument_id, ts).
  • Batch inserts of market data instead of one row at a time.
  • Use connection pooling for many short connections.
  • Set Redis memory limits and an eviction policy for caches.
  • Monitor both: memory, connections, slow queries and replication lag. See Monitoring and Logging Systems.

Alternatives#

SQLite works well for a single bot on one machine with no server to manage. Specialist time series and columnar databases handle very large tick datasets. Managed cloud versions of Redis and PostgreSQL remove maintenance work at extra cost.

Frequently asked questions#

Why use Redis in a trading system?#

For very fast access to live prices, caches, counters, locks and pub sub messaging shared between processes.

Is PostgreSQL good for trading data?#

Yes. It is reliable for orders, fills and accounts and handles moderate market data well, with extensions such as TimescaleDB for larger time series.

Can I use Redis as my only database?#

It is risky for important records, because in memory data can be lost if persistence is not configured carefully; keep durable records in a database like PostgreSQL.

Next, learn how to watch all of these systems in Monitoring and Logging Systems.

Check your understanding

3 quick questions on this lesson. Get them all right to finish it.

Turn on JavaScript to take the quiz.

Finished this lesson?Sign in to save your progress across devices.
Next lessonMonitoring and Logging SystemsThe tools behind trading observability: structured logs, metrics, dashboards, tracing and alerting with Prometheus, Grafana and log stacks, and how to set them up.

Mentioned in