Skip to main content

nautilus_infrastructure/redis/
cache.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Redis-backed cache database for the system.
17//!
18//! # Architecture
19//!
20//! Uses two Redis connections with distinct roles:
21//! - **READ** (`self.con`): synchronous queries (`keys`, `read`, `load_all`),
22//!   owned by the main struct.
23//! - **WRITE**: owned by a background task on `get_runtime()`, receives
24//!   commands via an unbounded `tokio::sync::mpsc` channel.
25//!
26//! All write operations (`insert`, `update`, `delete`, `flush`) are routed
27//! through the command channel so they execute on the WRITE connection. This
28//! avoids cross-runtime I/O issues since the WRITE connection is always
29//! created on the Nautilus runtime.
30//!
31//! Synchronous callers (`close`, `flushdb_sync`) use `std::sync::mpsc` reply
32//! channels to block until the background task confirms completion. When
33//! called from the Nautilus runtime itself, `block_in_place` is used
34//! automatically to avoid stalling the worker thread.
35
36use std::{
37    collections::VecDeque,
38    fmt::{Debug, Write as _},
39    ops::ControlFlow,
40    pin::Pin,
41    sync::mpsc::{self, SyncSender},
42    time::Duration,
43};
44
45use ahash::AHashMap;
46use anyhow::Context;
47use base64::{Engine, prelude::BASE64_STANDARD};
48use bytes::Bytes;
49#[cfg(feature = "defi")]
50use nautilus_common::defi::cache::InstrumentMigration;
51use nautilus_common::{
52    cache::{
53        CacheConfig,
54        database::{
55            CacheDatabaseAdapter, CacheDatabaseFactory, CacheMap, register_loaded_currencies,
56        },
57    },
58    enums::SerializationEncoding,
59    live::{block_on_nautilus_with, get_runtime},
60    logging::{log_task_awaiting, log_task_started, log_task_stopped},
61    signal::Signal,
62};
63use nautilus_core::{UUID4, UnixNanos, correctness::check_slice_not_empty};
64use nautilus_cryptography::providers::install_cryptographic_provider;
65use nautilus_model::{
66    accounts::AccountAny,
67    data::{
68        Bar, CustomData, DataType, FundingRateUpdate, HasTsInit, InstrumentClose, QuoteTick,
69        TradeTick,
70    },
71    events::{
72        AccountState, OrderEventAny, OrderFilled, OrderSnapshot,
73        position::snapshot::PositionSnapshot,
74    },
75    identifiers::{
76        AccountId, ActorId, ClientId, ClientOrderId, InstrumentId, PositionId, StrategyId,
77        TraderId, VenueOrderId,
78    },
79    instruments::{Instrument, InstrumentAny, SyntheticInstrument},
80    orderbook::OrderBook,
81    orders::{Order, OrderAny},
82    position::Position,
83    types::{Currency, Money},
84};
85use redis::{AsyncCommands, Pipeline, aio::ConnectionManager};
86use serde::{Deserialize, Serialize};
87use ustr::Ustr;
88
89use super::{REDIS_DELIMITER, REDIS_FLUSHDB, get_index_key};
90use crate::redis::{
91    RedisConnectionConfig, create_redis_connection,
92    queries::{CurrencyRecord, DatabaseQueries},
93};
94
95// Task and connection names
96const CACHE_READ: &str = "cache-read";
97const CACHE_WRITE: &str = "cache-write";
98const CACHE_PROCESS: &str = "cache-process";
99
100// Error constants
101const FAILED_TX_CHANNEL: &str = "Failed to send to channel";
102
103// Collection keys
104const INDEX: &str = "index";
105const GENERAL: &str = "general";
106const CURRENCIES: &str = "currencies";
107const INSTRUMENTS: &str = "instruments";
108const INSTRUMENT_CLOSES: &str = "instrument_closes";
109const SYNTHETICS: &str = "synthetics";
110const ACCOUNTS: &str = "accounts";
111const ORDERS: &str = "orders";
112const POSITIONS: &str = "positions";
113const ACTORS: &str = "actors";
114const STRATEGIES: &str = "strategies";
115const SNAPSHOTS: &str = "snapshots";
116const HEALTH: &str = "health";
117const CUSTOM: &str = "custom";
118
119// Index keys
120const INDEX_ORDER_IDS: &str = "index:order_ids";
121const INDEX_ORDER_POSITION: &str = "index:order_position";
122const INDEX_ORDER_CLIENT: &str = "index:order_client";
123const INDEX_ORDERS: &str = "index:orders";
124const INDEX_ORDERS_OPEN: &str = "index:orders_open";
125const INDEX_ORDERS_CLOSED: &str = "index:orders_closed";
126const INDEX_ORDERS_EMULATED: &str = "index:orders_emulated";
127const INDEX_ORDERS_INFLIGHT: &str = "index:orders_inflight";
128const INDEX_POSITIONS: &str = "index:positions";
129const INDEX_POSITIONS_OPEN: &str = "index:positions_open";
130const INDEX_POSITIONS_CLOSED: &str = "index:positions_closed";
131
132/// Configuration for a Redis-backed cache database.
133///
134/// Redis 6.2 or higher is required for correct operation.
135#[cfg_attr(
136    feature = "python",
137    expect(
138        clippy::unsafe_derive_deserialize,
139        reason = "config deserializes plain fields; unsafe methods come from generated PyO3 integration"
140    )
141)]
142#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
143#[serde(default, deny_unknown_fields)]
144#[cfg_attr(
145    feature = "python",
146    pyo3::pyclass(module = "nautilus_trader.infrastructure", from_py_object)
147)]
148#[cfg_attr(
149    feature = "python",
150    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.infrastructure")
151)]
152pub struct RedisCacheConfig {
153    /// The Redis host address. If `None`, `127.0.0.1` is used.
154    pub host: Option<String>,
155    /// The Redis port. If `None`, `6379` is used.
156    pub port: Option<u16>,
157    /// The Redis account username.
158    pub username: Option<String>,
159    /// The Redis account password.
160    pub password: Option<String>,
161    /// If Redis should use an SSL-enabled connection.
162    pub ssl: bool,
163    /// The timeout (in seconds) to wait for a new connection.
164    pub connection_timeout: u16,
165    /// The timeout (in seconds) to wait for a response.
166    pub response_timeout: u16,
167    /// The number of retry attempts with exponential backoff for connection attempts.
168    pub number_of_retries: usize,
169    /// The base value for exponential backoff calculation.
170    pub exponent_base: u64,
171    /// The maximum delay between retry attempts (in seconds).
172    pub max_delay: u64,
173    /// The multiplication factor for retry delay calculation.
174    pub factor: u64,
175}
176
177impl Debug for RedisCacheConfig {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        let redacted = self.password.as_ref().map(|_| "***");
180        f.debug_struct(stringify!(RedisCacheConfig))
181            .field("host", &self.host)
182            .field("port", &self.port)
183            .field("username", &self.username)
184            .field("password", &redacted)
185            .field("ssl", &self.ssl)
186            .field("connection_timeout", &self.connection_timeout)
187            .field("response_timeout", &self.response_timeout)
188            .field("number_of_retries", &self.number_of_retries)
189            .field("exponent_base", &self.exponent_base)
190            .field("max_delay", &self.max_delay)
191            .field("factor", &self.factor)
192            .finish()
193    }
194}
195
196impl Default for RedisCacheConfig {
197    fn default() -> Self {
198        Self {
199            host: None,
200            port: None,
201            username: None,
202            password: None,
203            ssl: false,
204            connection_timeout: 20,
205            response_timeout: 20,
206            number_of_retries: 100,
207            exponent_base: 2,
208            max_delay: 1000,
209            factor: 2,
210        }
211    }
212}
213
214impl RedisConnectionConfig for RedisCacheConfig {
215    fn host(&self) -> Option<&str> {
216        self.host.as_deref()
217    }
218
219    fn port(&self) -> Option<u16> {
220        self.port
221    }
222
223    fn username(&self) -> Option<&str> {
224        self.username.as_deref()
225    }
226
227    fn password(&self) -> Option<&str> {
228        self.password.as_deref()
229    }
230
231    fn ssl(&self) -> bool {
232        self.ssl
233    }
234
235    fn connection_timeout(&self) -> u16 {
236        self.connection_timeout
237    }
238
239    fn response_timeout(&self) -> u16 {
240        self.response_timeout
241    }
242
243    fn number_of_retries(&self) -> usize {
244        self.number_of_retries
245    }
246
247    fn exponent_base(&self) -> u64 {
248        self.exponent_base
249    }
250
251    fn max_delay(&self) -> u64 {
252        self.max_delay
253    }
254
255    fn factor(&self) -> u64 {
256        self.factor
257    }
258}
259
260/// A type of database operation.
261#[derive(Clone, Debug)]
262pub enum DatabaseOperation {
263    #[cfg(feature = "defi")]
264    Defi {
265        codes: Vec<String>,
266        references: Vec<String>,
267        migration: Option<InstrumentMigration>,
268        reply: SyncSender<anyhow::Result<()>>,
269    },
270    Insert,
271    Update,
272    UpdateOrder,
273    ReplaceList,
274    Delete,
275    DeletePositionIndex,
276    Flush(SyncSender<()>),
277    Close,
278}
279
280/// Represents a database command to be performed which may be executed in a task.
281#[derive(Clone, Debug)]
282pub struct DatabaseCommand {
283    /// The database operation type.
284    pub op_type: DatabaseOperation,
285    /// The primary key for the operation.
286    pub key: Option<String>,
287    /// The data payload for the operation.
288    pub payload: Option<Vec<Bytes>>,
289}
290
291impl DatabaseCommand {
292    /// Creates a new [`DatabaseCommand`] instance.
293    #[must_use]
294    pub const fn new(op_type: DatabaseOperation, key: String, payload: Option<Vec<Bytes>>) -> Self {
295        Self {
296            op_type,
297            key: Some(key),
298            payload,
299        }
300    }
301
302    /// Initialize a `Close` database command, this is meant to close the database cache channel.
303    #[must_use]
304    pub const fn close() -> Self {
305        Self {
306            op_type: DatabaseOperation::Close,
307            key: None,
308            payload: None,
309        }
310    }
311}
312
313pub struct RedisCacheDatabase {
314    pub con: ConnectionManager,
315    pub trader_id: TraderId,
316    pub trader_key: String,
317    pub encoding: SerializationEncoding,
318    pub bulk_read_batch_size: Option<usize>,
319    timestamps_as_iso8601: bool,
320    tx: tokio::sync::mpsc::UnboundedSender<DatabaseCommand>,
321    handle: Option<tokio::task::JoinHandle<()>>,
322}
323
324impl Debug for RedisCacheDatabase {
325    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
326        f.debug_struct(stringify!(RedisCacheDatabase))
327            .field("trader_id", &self.trader_id)
328            .field("encoding", &self.encoding)
329            .finish_non_exhaustive()
330    }
331}
332
333impl RedisCacheDatabase {
334    /// Creates a Redis cache database from cache behavior and Redis connection settings.
335    ///
336    /// # Errors
337    ///
338    /// Returns an error if:
339    /// - Establishing the Redis connection fails.
340    /// - The command processing task cannot be spawned.
341    pub async fn new(
342        trader_id: TraderId,
343        instance_id: UUID4,
344        config: CacheConfig,
345        database: RedisCacheConfig,
346    ) -> anyhow::Result<Self> {
347        install_cryptographic_provider();
348
349        let con = create_redis_connection(CACHE_READ, &database).await?;
350
351        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<DatabaseCommand>();
352        let trader_key = get_trader_key(trader_id, instance_id, &config);
353        let trader_key_clone = trader_key.clone();
354        let encoding = config.encoding;
355        let bulk_read_batch_size = config.bulk_read_batch_size;
356        let timestamps_as_iso8601 = config.timestamps_as_iso8601;
357
358        let handle = get_runtime().spawn(async move {
359            if let Err(e) =
360                process_commands(rx, trader_key_clone, config.clone(), database.clone()).await
361            {
362                log::error!("Error in task '{CACHE_PROCESS}': {e}");
363            }
364        });
365
366        Ok(Self {
367            con,
368            trader_id,
369            trader_key,
370            encoding,
371            bulk_read_batch_size,
372            timestamps_as_iso8601,
373            tx,
374            handle: Some(handle),
375        })
376    }
377
378    #[must_use]
379    pub const fn get_encoding(&self) -> SerializationEncoding {
380        self.encoding
381    }
382
383    #[must_use]
384    pub fn get_trader_key(&self) -> &str {
385        &self.trader_key
386    }
387
388    pub fn close(&mut self) {
389        log::debug!("Closing");
390
391        let Some(handle) = self.handle.take() else {
392            log::debug!("Already closed");
393            return;
394        };
395
396        if let Err(e) = self.tx.send(DatabaseCommand::close()) {
397            log::debug!("Error sending close command: {e:?}");
398        }
399
400        log_task_awaiting(CACHE_PROCESS);
401
402        let (tx, rx) = mpsc::sync_channel(1);
403
404        get_runtime().spawn(async move {
405            if let Err(e) = handle.await {
406                log::error!("Error awaiting task '{CACHE_PROCESS}': {e:?}");
407            }
408            let _ = tx.send(());
409        });
410        let _ = blocking_recv(&rx);
411
412        log::debug!("Closed");
413    }
414
415    pub async fn flushdb(&mut self) {
416        if let Err(e) = redis::cmd(REDIS_FLUSHDB)
417            .query_async::<()>(&mut self.con)
418            .await
419        {
420            log::error!("Failed to flush database: {e:?}");
421        }
422    }
423
424    /// Sends a flush command through the background task channel and blocks
425    /// until it completes. Safe to call from any runtime context.
426    ///
427    /// # Errors
428    ///
429    /// Returns an error if the command channel is closed or the reply is lost.
430    pub fn flushdb_sync(&self) -> anyhow::Result<()> {
431        let (reply_tx, reply_rx) = mpsc::sync_channel(1);
432        let cmd = DatabaseCommand {
433            op_type: DatabaseOperation::Flush(reply_tx),
434            key: None,
435            payload: None,
436        };
437        self.tx
438            .send(cmd)
439            .map_err(|e| anyhow::anyhow!("{FAILED_TX_CHANNEL}: {e}"))?;
440        blocking_recv(&reply_rx).map_err(|e| anyhow::anyhow!("Failed to flush database: {e}"))?;
441        Ok(())
442    }
443
444    /// Retrieves all keys matching the given `pattern` from Redis for this trader.
445    ///
446    /// # Errors
447    ///
448    /// Returns an error if the underlying Redis scan operation fails.
449    pub async fn keys(&mut self, pattern: &str) -> anyhow::Result<Vec<String>> {
450        let pattern = format!("{}{REDIS_DELIMITER}{pattern}", self.trader_key);
451        DatabaseQueries::scan_keys(&mut self.con, pattern).await
452    }
453
454    /// Reads the value(s) associated with `key` for this trader from Redis.
455    ///
456    /// # Errors
457    ///
458    /// Returns an error if the underlying Redis read operation fails.
459    pub async fn read(&mut self, key: &str) -> anyhow::Result<Vec<Bytes>> {
460        DatabaseQueries::read(&self.con, &self.trader_key, key).await
461    }
462
463    /// Reads multiple values using bulk operations for efficiency.
464    ///
465    /// # Errors
466    ///
467    /// Returns an error if the underlying Redis read operation fails.
468    pub async fn read_bulk(&mut self, keys: &[String]) -> anyhow::Result<Vec<Option<Bytes>>> {
469        match self.bulk_read_batch_size {
470            Some(batch_size) => {
471                DatabaseQueries::read_bulk_batched(&self.con, keys, batch_size).await
472            }
473            None => DatabaseQueries::read_bulk(&self.con, keys).await,
474        }
475    }
476
477    /// Loads custom data from Redis matching the given `data_type` (blocking).
478    ///
479    /// Spawns the async query on the global Nautilus runtime and blocks until
480    /// the result arrives via a channel. Safe from any thread context (Python,
481    /// test runtimes, plain threads).
482    ///
483    /// # Errors
484    ///
485    /// Returns an error if the query fails or the reply channel is closed.
486    pub fn load_custom_data(&self, data_type: &DataType) -> anyhow::Result<Vec<CustomData>> {
487        let con = self.con.clone();
488        let trader_key = self.trader_key.clone();
489        let data_type = data_type.clone();
490        let batch_size = self.bulk_read_batch_size.unwrap_or(usize::MAX);
491        let (tx, rx) = mpsc::channel();
492
493        get_runtime().spawn(async move {
494            let result = DatabaseQueries::load_custom_data_batched(
495                &con,
496                &trader_key,
497                &data_type,
498                batch_size,
499            )
500            .await;
501
502            if let Err(e) = tx.send(result) {
503                log::error!("Failed to send custom data result for '{data_type}': {e:?}");
504            }
505        });
506
507        blocking_recv(&rx).map_err(|e| anyhow::anyhow!("load_custom_data channel closed: {e}"))?
508    }
509
510    /// Sends an insert command for `key` with optional `payload` to Redis via the background task.
511    ///
512    /// # Errors
513    ///
514    /// Returns an error if the command cannot be sent to the background task channel.
515    pub fn insert(&self, key: String, payload: Option<Vec<Bytes>>) -> anyhow::Result<()> {
516        let op = DatabaseCommand::new(DatabaseOperation::Insert, key, payload);
517        match self.tx.send(op) {
518            Ok(()) => Ok(()),
519            Err(e) => anyhow::bail!("{FAILED_TX_CHANNEL}: {e}"),
520        }
521    }
522
523    /// Stores custom data in Redis (key format: `custom:<ts_init_020>:<uuid>`, value: full JSON).
524    ///
525    /// Uses the registered JSON format independently of cache encoding and timestamp options.
526    ///
527    /// # Errors
528    ///
529    /// Returns an error if serialization fails or the insert command cannot be sent.
530    pub fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()> {
531        let json_bytes = serde_json::to_vec(data)
532            .map_err(|e| anyhow::anyhow!("CustomData serialization failed: {e}"))?;
533        let ts_init = data.ts_init().as_u64();
534        let key = format!(
535            "{CUSTOM}{REDIS_DELIMITER}{:020}{REDIS_DELIMITER}{}",
536            ts_init,
537            UUID4::new()
538        );
539        self.insert(key, Some(vec![Bytes::from(json_bytes)]))
540    }
541
542    fn serialize_payload<T: Serialize>(&self, payload: &T) -> anyhow::Result<Vec<u8>> {
543        DatabaseQueries::serialize_payload_with_timestamps(
544            self.encoding,
545            payload,
546            self.timestamps_as_iso8601,
547        )
548    }
549
550    #[cfg(feature = "defi")]
551    fn write_defi(
552        &self,
553        key: String,
554        codes: Vec<String>,
555        payloads: Vec<Bytes>,
556        references: Vec<String>,
557        migration: Option<InstrumentMigration>,
558    ) -> anyhow::Result<()> {
559        let (reply, receiver) = mpsc::sync_channel(1);
560
561        let command = DatabaseCommand::new(
562            DatabaseOperation::Defi {
563                codes,
564                references,
565                migration,
566                reply,
567            },
568            key,
569            Some(payloads),
570        );
571
572        self.tx.send(command)?;
573        blocking_recv(&receiver)?
574    }
575
576    /// Sends an update command for `key` with optional `payload` to Redis via the background task.
577    ///
578    /// # Errors
579    ///
580    /// Returns an error if the command cannot be sent to the background task channel.
581    pub fn update(&mut self, key: String, payload: Option<Vec<Bytes>>) -> anyhow::Result<()> {
582        let op = DatabaseCommand::new(DatabaseOperation::Update, key, payload);
583        match self.tx.send(op) {
584            Ok(()) => Ok(()),
585            Err(e) => anyhow::bail!("{FAILED_TX_CHANNEL}: {e}"),
586        }
587    }
588
589    /// Sends a delete command for `key` with optional `payload` to Redis via the background task.
590    ///
591    /// # Errors
592    ///
593    /// Returns an error if the command cannot be sent to the background task channel.
594    pub fn delete(&mut self, key: String, payload: Option<Vec<Bytes>>) -> anyhow::Result<()> {
595        let op = DatabaseCommand::new(DatabaseOperation::Delete, key, payload);
596        match self.tx.send(op) {
597            Ok(()) => Ok(()),
598            Err(e) => anyhow::bail!("{FAILED_TX_CHANNEL}: {e}"),
599        }
600    }
601
602    /// Delete the given order from the database with full index cleanup.
603    ///
604    /// # Errors
605    ///
606    /// Returns an error if the command cannot be sent to the background task channel.
607    pub fn delete_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<()> {
608        let order_id_bytes = Bytes::from(client_order_id.to_string());
609
610        // Delete the order itself
611        let key = format!("{ORDERS}{REDIS_DELIMITER}{client_order_id}");
612        let op = DatabaseCommand::new(DatabaseOperation::Delete, key, None);
613        self.tx
614            .send(op)
615            .map_err(|e| anyhow::anyhow!("Failed to send delete order command: {e}"))?;
616
617        self.delete_key(format!(
618            "{SNAPSHOTS}{REDIS_DELIMITER}{ORDERS}{REDIS_DELIMITER}{client_order_id}"
619        ))?;
620
621        // Delete from all order indexes
622        let index_keys = [
623            INDEX_ORDER_IDS,
624            INDEX_ORDERS,
625            INDEX_ORDERS_OPEN,
626            INDEX_ORDERS_CLOSED,
627            INDEX_ORDERS_EMULATED,
628            INDEX_ORDERS_INFLIGHT,
629        ];
630
631        for index_key in &index_keys {
632            let key = (*index_key).to_string();
633            let payload = vec![order_id_bytes.clone()];
634            let op = DatabaseCommand::new(DatabaseOperation::Delete, key, Some(payload));
635            self.tx
636                .send(op)
637                .map_err(|e| anyhow::anyhow!("Failed to send delete order index command: {e}"))?;
638        }
639
640        // Delete from hash indexes
641        let hash_indexes = [INDEX_ORDER_POSITION, INDEX_ORDER_CLIENT];
642        for index_key in &hash_indexes {
643            let key = (*index_key).to_string();
644            let payload = vec![order_id_bytes.clone()];
645            let op = DatabaseCommand::new(DatabaseOperation::Delete, key, Some(payload));
646            self.tx.send(op).map_err(|e| {
647                anyhow::anyhow!("Failed to send delete order hash index command: {e}")
648            })?;
649        }
650
651        Ok(())
652    }
653
654    /// Delete the given position from the database with full index cleanup.
655    ///
656    /// # Errors
657    ///
658    /// Returns an error if the command cannot be sent to the background task channel.
659    pub fn delete_position(&self, position_id: &PositionId) -> anyhow::Result<()> {
660        let position_id_bytes = Bytes::from(position_id.to_string());
661
662        // Delete the position itself
663        let key = format!("{POSITIONS}{REDIS_DELIMITER}{position_id}");
664        let op = DatabaseCommand::new(DatabaseOperation::Delete, key, None);
665        self.tx
666            .send(op)
667            .map_err(|e| anyhow::anyhow!("Failed to send delete position command: {e}"))?;
668
669        self.delete_key(format!(
670            "{SNAPSHOTS}{REDIS_DELIMITER}{POSITIONS}{REDIS_DELIMITER}{position_id}"
671        ))?;
672        self.delete_key(format!(
673            "{GENERAL}{REDIS_DELIMITER}position_oms:{position_id}"
674        ))?;
675
676        // Delete from all position indexes
677        let index_keys = [
678            INDEX_POSITIONS,
679            INDEX_POSITIONS_OPEN,
680            INDEX_POSITIONS_CLOSED,
681        ];
682
683        for index_key in &index_keys {
684            let key = (*index_key).to_string();
685            let payload = vec![position_id_bytes.clone()];
686            let op = DatabaseCommand::new(DatabaseOperation::Delete, key, Some(payload));
687            self.tx.send(op).map_err(|e| {
688                anyhow::anyhow!("Failed to send delete position index command: {e}")
689            })?;
690        }
691
692        self.tx
693            .send(DatabaseCommand::new(
694                DatabaseOperation::DeletePositionIndex,
695                INDEX_ORDER_POSITION.to_string(),
696                Some(vec![position_id_bytes]),
697            ))
698            .map_err(|e| anyhow::anyhow!("Failed to send delete position mapping command: {e}"))?;
699
700        Ok(())
701    }
702
703    fn delete_key(&self, key: String) -> anyhow::Result<()> {
704        self.tx
705            .send(DatabaseCommand::new(DatabaseOperation::Delete, key, None))
706            .map_err(|e| anyhow::anyhow!("Failed to send delete command: {e}"))
707    }
708
709    /// Logs an account-event deletion request and retains the full history for replay.
710    ///
711    /// Always returns success.
712    #[expect(
713        clippy::missing_errors_doc,
714        reason = "account-event deletion is a logged no-op"
715    )]
716    pub fn delete_account_event(
717        &self,
718        account_id: &AccountId,
719        event_id: &str,
720    ) -> anyhow::Result<()> {
721        log::warn!(
722            "Deleting account events currently a no-op (pending redesign), {account_id}: {event_id}"
723        );
724        Ok(())
725    }
726}
727
728/// Receives a reply, handing off the worker first when called from the Nautilus runtime.
729///
730/// The check is whether a runtime handle is current, not whether this thread is a runtime worker.
731/// Both branches block the caller, so a caller that must not block, such as a live node driven by
732/// a host event loop, cannot use these paths at all and is rejected before it reaches them.
733fn blocking_recv<T>(rx: &mpsc::Receiver<T>) -> Result<T, mpsc::RecvError> {
734    let on_nautilus_runtime =
735        tokio::runtime::Handle::try_current().is_ok_and(|h| h.id() == get_runtime().handle().id());
736
737    if on_nautilus_runtime {
738        tokio::task::block_in_place(|| rx.recv())
739    } else {
740        rx.recv()
741    }
742}
743
744async fn process_commands(
745    mut rx: tokio::sync::mpsc::UnboundedReceiver<DatabaseCommand>,
746    trader_key: String,
747    config: CacheConfig,
748    database: RedisCacheConfig,
749) -> anyhow::Result<()> {
750    log_task_started(CACHE_PROCESS);
751
752    let mut con = create_redis_connection(CACHE_WRITE, &database).await?;
753
754    // Buffering
755    let mut buffer: VecDeque<DatabaseCommand> = VecDeque::new();
756    let buffer_interval = Duration::from_millis(config.buffer_interval_ms.unwrap_or(0) as u64);
757
758    // A sleep used to trigger periodic flushing of the buffer.
759    // When `buffer_interval` is zero we skip using the timer and flush immediately
760    // after every message.
761    let flush_timer = tokio::time::sleep(buffer_interval);
762    tokio::pin!(flush_timer);
763
764    // Continue to receive and handle messages until channel is hung up
765    loop {
766        tokio::select! {
767            maybe_cmd = rx.recv() => {
768                let result = handle_command(
769                    maybe_cmd,
770                    &mut buffer,
771                    buffer_interval,
772                    &mut con,
773                    &trader_key,
774                    config.encoding,
775                ).await;
776
777                if result.is_break() {
778                    break;
779                }
780            }
781            () = &mut flush_timer, if !buffer_interval.is_zero() => {
782                flush_buffer(
783                    &mut buffer,
784                    &mut con,
785                    &trader_key,
786                    config.encoding,
787                    &mut flush_timer,
788                    buffer_interval,
789                ).await;
790            }
791        }
792    }
793
794    // Drain any remaining messages
795    if !buffer.is_empty() {
796        drain_buffer(&mut con, &trader_key, config.encoding, &mut buffer).await;
797    }
798
799    log_task_stopped(CACHE_PROCESS);
800    Ok(())
801}
802
803async fn handle_command(
804    maybe_cmd: Option<DatabaseCommand>,
805    buffer: &mut VecDeque<DatabaseCommand>,
806    buffer_interval: Duration,
807    con: &mut ConnectionManager,
808    trader_key: &str,
809    encoding: SerializationEncoding,
810) -> ControlFlow<()> {
811    let Some(cmd) = maybe_cmd else {
812        log::debug!("Command channel closed");
813        return ControlFlow::Break(());
814    };
815
816    log::trace!("Received {cmd:?}");
817
818    match cmd.op_type {
819        #[cfg(feature = "defi")]
820        DatabaseOperation::Defi {
821            codes,
822            references,
823            migration,
824            reply,
825        } => {
826            if !buffer.is_empty() {
827                drain_buffer(con, trader_key, encoding, buffer).await;
828            }
829
830            let result = match (cmd.key, cmd.payload) {
831                (Some(key), Some(payloads)) => {
832                    DatabaseQueries::write_defi(
833                        con,
834                        trader_key,
835                        encoding,
836                        &key,
837                        &codes,
838                        &payloads,
839                        &references,
840                        migration.as_ref(),
841                    )
842                    .await
843                }
844                _ => Err(crate::defi::integrity_error("Missing DeFi write payload")),
845            };
846
847            let _ = reply.send(result);
848            return ControlFlow::Continue(());
849        }
850        DatabaseOperation::Close => {
851            if !buffer.is_empty() {
852                drain_buffer(con, trader_key, encoding, buffer).await;
853            }
854            return ControlFlow::Break(());
855        }
856        DatabaseOperation::Flush(reply_tx) => {
857            if !buffer.is_empty() {
858                drain_buffer(con, trader_key, encoding, buffer).await;
859            }
860
861            if let Err(e) = redis::cmd(REDIS_FLUSHDB).query_async::<()>(con).await {
862                log::error!("Failed to flush database: {e:?}");
863            }
864            let _ = reply_tx.send(());
865            return ControlFlow::Continue(());
866        }
867        _ => {}
868    }
869
870    buffer.push_back(cmd);
871
872    if buffer_interval.is_zero() {
873        drain_buffer(con, trader_key, encoding, buffer).await;
874    }
875
876    ControlFlow::Continue(())
877}
878
879async fn flush_buffer(
880    buffer: &mut VecDeque<DatabaseCommand>,
881    con: &mut ConnectionManager,
882    trader_key: &str,
883    encoding: SerializationEncoding,
884    flush_timer: &mut Pin<&mut tokio::time::Sleep>,
885    buffer_interval: Duration,
886) {
887    if !buffer.is_empty() {
888        drain_buffer(con, trader_key, encoding, buffer).await;
889    }
890    flush_timer
891        .as_mut()
892        .reset(tokio::time::Instant::now() + buffer_interval);
893}
894
895async fn drain_buffer(
896    conn: &mut ConnectionManager,
897    trader_key: &str,
898    encoding: SerializationEncoding,
899    buffer: &mut VecDeque<DatabaseCommand>,
900) {
901    let mut pipe = redis::pipe();
902    pipe.atomic();
903    let mut has_pending_ops = false;
904
905    for msg in buffer.drain(..) {
906        let Some(key) = msg.key else {
907            log::error!("Null key found for message: {msg:?}");
908            continue;
909        };
910        let collection = match get_collection_key(&key) {
911            Ok(collection) => collection,
912            Err(e) => {
913                log::error!("{e}");
914                continue;
915            }
916        };
917
918        let key = format!("{trader_key}{REDIS_DELIMITER}{key}");
919
920        let result = match msg.op_type {
921            DatabaseOperation::UpdateOrder | DatabaseOperation::DeletePositionIndex => {
922                flush_pending_pipeline(conn, &mut pipe, &mut has_pending_ops).await;
923                execute_state_command(conn, trader_key, encoding, &key, msg.op_type, msg.payload)
924                    .await
925            }
926            #[cfg(feature = "defi")]
927            DatabaseOperation::Defi { .. } => unreachable!("DeFi writes execute before buffering"),
928            DatabaseOperation::Close => panic!("Close command should not be drained"),
929            DatabaseOperation::Flush(_) => panic!("Flush command should not be drained"),
930            operation => {
931                match append_pipeline_command(&mut pipe, collection, &key, &operation, msg.payload)
932                {
933                    Ok(()) => {
934                        has_pending_ops = true;
935                        Ok(())
936                    }
937                    Err(e) => Err(e),
938                }
939            }
940        };
941
942        if let Err(e) = result {
943            log::error!("{e}");
944        }
945    }
946
947    flush_pending_pipeline(conn, &mut pipe, &mut has_pending_ops).await;
948}
949
950fn append_pipeline_command(
951    pipe: &mut Pipeline,
952    collection: &str,
953    key: &str,
954    operation: &DatabaseOperation,
955    payload: Option<Vec<Bytes>>,
956) -> anyhow::Result<()> {
957    if matches!(operation, DatabaseOperation::Delete) {
958        return delete(pipe, collection, key, payload);
959    }
960
961    let payload = payload.ok_or_else(|| anyhow::anyhow!("Null `payload` for {operation:?}"))?;
962    log::debug!("Processing {operation:?} for collection: {collection}, key: {key}");
963
964    match operation {
965        DatabaseOperation::Insert => insert(pipe, collection, key, &payload),
966        DatabaseOperation::Update => update(pipe, collection, key, &payload),
967        DatabaseOperation::ReplaceList => replace_list_operation(pipe, collection, key, &payload),
968        _ => unreachable!("only pipeline operations are appended"),
969    }
970}
971
972async fn execute_state_command(
973    conn: &mut ConnectionManager,
974    trader_key: &str,
975    encoding: SerializationEncoding,
976    key: &str,
977    operation: DatabaseOperation,
978    payload: Option<Vec<Bytes>>,
979) -> anyhow::Result<()> {
980    let payload = payload.ok_or_else(|| anyhow::anyhow!("Null `payload` for {operation:?}"))?;
981
982    match operation {
983        DatabaseOperation::UpdateOrder => {
984            log::debug!("Processing UPDATE_ORDER for key: {key}");
985            update_order_event_log(conn, trader_key, encoding, key, &payload).await
986        }
987        DatabaseOperation::DeletePositionIndex => {
988            let position_id = payload
989                .first()
990                .ok_or_else(|| anyhow::anyhow!("Empty position mapping payload"))?;
991            delete_position_index(conn, key, position_id).await
992        }
993        _ => unreachable!("only stateful operations require a pipeline drain"),
994    }
995}
996
997async fn flush_pending_pipeline(
998    conn: &mut ConnectionManager,
999    pipe: &mut Pipeline,
1000    has_pending_ops: &mut bool,
1001) {
1002    if !*has_pending_ops {
1003        return;
1004    }
1005
1006    if let Err(e) = pipe.query_async::<()>(conn).await {
1007        log::error!("{e}");
1008    }
1009
1010    *pipe = redis::pipe();
1011    pipe.atomic();
1012    *has_pending_ops = false;
1013}
1014
1015async fn delete_position_index(
1016    conn: &mut ConnectionManager,
1017    key: &str,
1018    position_id: &Bytes,
1019) -> anyhow::Result<()> {
1020    let mappings: Vec<(String, String)> = conn.hgetall(key).await?;
1021
1022    let orders: Vec<_> = mappings
1023        .into_iter()
1024        .filter_map(|(order, position)| {
1025            (position.as_bytes() == position_id.as_ref()).then_some(order)
1026        })
1027        .collect();
1028
1029    if !orders.is_empty() {
1030        conn.hdel::<_, _, ()>(key, orders).await?;
1031    }
1032
1033    Ok(())
1034}
1035
1036async fn update_order_event_log(
1037    conn: &mut ConnectionManager,
1038    trader_key: &str,
1039    encoding: SerializationEncoding,
1040    key: &str,
1041    value: &[Bytes],
1042) -> anyhow::Result<()> {
1043    check_slice_not_empty(value, stringify!(value))?;
1044
1045    let result: Vec<Bytes> = conn.lrange(key, 0, -1).await?;
1046    if result.is_empty() {
1047        log::warn!("Cannot update order in Redis, no existing state at {key}");
1048        return Ok(());
1049    }
1050
1051    let mut append_pipe = redis::pipe();
1052    append_pipe.atomic();
1053    update_list(&mut append_pipe, key, value[0].as_ref());
1054    append_pipe.query_async::<()>(conn).await?;
1055
1056    let mut payloads = result;
1057    payloads.push(value[0].clone());
1058
1059    let order = DatabaseQueries::deserialize_order(encoding, &payloads).with_context(|| {
1060        format!("Order event append succeeded for {key}, but index replay failed rebuilding order")
1061    })?;
1062
1063    let mut pipe = redis::pipe();
1064    pipe.atomic();
1065    update_order_indexes(&mut pipe, trader_key, &order);
1066    pipe.query_async::<()>(conn).await?;
1067
1068    Ok(())
1069}
1070
1071fn insert(pipe: &mut Pipeline, collection: &str, key: &str, value: &[Bytes]) -> anyhow::Result<()> {
1072    check_slice_not_empty(value, stringify!(value))?;
1073
1074    match collection {
1075        INDEX => insert_index(pipe, key, value),
1076        GENERAL | CURRENCIES | INSTRUMENTS | INSTRUMENT_CLOSES | SYNTHETICS | ACTORS
1077        | STRATEGIES | HEALTH | CUSTOM => {
1078            insert_string(pipe, key, value[0].as_ref());
1079            Ok(())
1080        }
1081        ACCOUNTS | ORDERS | POSITIONS | SNAPSHOTS => {
1082            insert_list(pipe, key, value[0].as_ref());
1083            Ok(())
1084        }
1085        _ => anyhow::bail!("Unsupported operation: `insert` for collection '{collection}'"),
1086    }
1087}
1088
1089fn insert_index(pipe: &mut Pipeline, key: &str, value: &[Bytes]) -> anyhow::Result<()> {
1090    let index_key = get_index_key(key)?;
1091    match index_key {
1092        INDEX_ORDER_IDS
1093        | INDEX_ORDERS
1094        | INDEX_ORDERS_OPEN
1095        | INDEX_ORDERS_CLOSED
1096        | INDEX_ORDERS_EMULATED
1097        | INDEX_ORDERS_INFLIGHT
1098        | INDEX_POSITIONS
1099        | INDEX_POSITIONS_OPEN
1100        | INDEX_POSITIONS_CLOSED => {
1101            insert_set(pipe, key, value[0].as_ref());
1102            Ok(())
1103        }
1104        INDEX_ORDER_POSITION => {
1105            insert_hset(pipe, key, value[0].as_ref(), value[1].as_ref());
1106            Ok(())
1107        }
1108        INDEX_ORDER_CLIENT => {
1109            if !value.len().is_multiple_of(2) {
1110                anyhow::bail!(
1111                    "Invalid hash index payload for '{index_key}': expected field-value pairs"
1112                );
1113            }
1114
1115            let entries = value
1116                .as_chunks::<2>()
1117                .0
1118                .iter()
1119                .map(|entry| (entry[0].as_ref(), entry[1].as_ref()))
1120                .collect::<Vec<(&[u8], &[u8])>>();
1121            pipe.hset_multiple(key, &entries);
1122            Ok(())
1123        }
1124        _ => anyhow::bail!("Index unknown '{index_key}' on insert"),
1125    }
1126}
1127
1128fn insert_string(pipe: &mut Pipeline, key: &str, value: &[u8]) {
1129    pipe.set(key, value);
1130}
1131
1132fn insert_set(pipe: &mut Pipeline, key: &str, value: &[u8]) {
1133    pipe.sadd(key, value);
1134}
1135
1136fn insert_hset(pipe: &mut Pipeline, key: &str, name: &[u8], value: &[u8]) {
1137    pipe.hset(key, name, value);
1138}
1139
1140fn insert_list(pipe: &mut Pipeline, key: &str, value: &[u8]) {
1141    pipe.rpush(key, value);
1142}
1143
1144fn replace_list(pipe: &mut Pipeline, key: &str, value: &[u8]) {
1145    pipe.del(key);
1146    pipe.rpush(key, value);
1147}
1148
1149fn replace_list_operation(
1150    pipe: &mut Pipeline,
1151    collection: &str,
1152    key: &str,
1153    value: &[Bytes],
1154) -> anyhow::Result<()> {
1155    check_slice_not_empty(value, stringify!(value))?;
1156
1157    match collection {
1158        ACCOUNTS | ORDERS | POSITIONS => {
1159            replace_list(pipe, key, value[0].as_ref());
1160            Ok(())
1161        }
1162        _ => anyhow::bail!("Unsupported operation: `replace_list` for collection '{collection}'"),
1163    }
1164}
1165
1166fn update(pipe: &mut Pipeline, collection: &str, key: &str, value: &[Bytes]) -> anyhow::Result<()> {
1167    check_slice_not_empty(value, stringify!(value))?;
1168
1169    match collection {
1170        ACCOUNTS | ORDERS | POSITIONS => {
1171            update_list(pipe, key, value[0].as_ref());
1172            Ok(())
1173        }
1174        _ => anyhow::bail!("Unsupported operation: `update` for collection '{collection}'"),
1175    }
1176}
1177
1178fn update_list(pipe: &mut Pipeline, key: &str, value: &[u8]) {
1179    pipe.rpush_exists(key, value);
1180}
1181
1182fn delete(
1183    pipe: &mut Pipeline,
1184    collection: &str,
1185    key: &str,
1186    value: Option<Vec<Bytes>>,
1187) -> anyhow::Result<()> {
1188    log::debug!(
1189        "delete: collection={}, key={}, has_payload={}",
1190        collection,
1191        key,
1192        value.is_some()
1193    );
1194
1195    match collection {
1196        INDEX => delete_from_index(pipe, key, value),
1197        ORDERS | POSITIONS | ACCOUNTS | ACTORS | STRATEGIES | SNAPSHOTS | GENERAL => {
1198            delete_string(pipe, key);
1199            Ok(())
1200        }
1201        _ => anyhow::bail!("Unsupported operation: `delete` for collection '{collection}'"),
1202    }
1203}
1204
1205fn delete_from_index(
1206    pipe: &mut Pipeline,
1207    key: &str,
1208    value: Option<Vec<Bytes>>,
1209) -> anyhow::Result<()> {
1210    let value = value.ok_or_else(|| anyhow::anyhow!("Empty `payload` for `delete` '{key}'"))?;
1211    let index_key = get_index_key(key)?;
1212
1213    match index_key {
1214        INDEX_ORDER_IDS
1215        | INDEX_ORDERS
1216        | INDEX_ORDERS_OPEN
1217        | INDEX_ORDERS_CLOSED
1218        | INDEX_ORDERS_EMULATED
1219        | INDEX_ORDERS_INFLIGHT
1220        | INDEX_POSITIONS
1221        | INDEX_POSITIONS_OPEN
1222        | INDEX_POSITIONS_CLOSED => {
1223            remove_from_set(pipe, key, value[0].as_ref());
1224            Ok(())
1225        }
1226        INDEX_ORDER_POSITION | INDEX_ORDER_CLIENT => {
1227            remove_from_hash(pipe, key, value[0].as_ref());
1228            Ok(())
1229        }
1230        _ => anyhow::bail!("Unsupported index operation: remove from '{index_key}'"),
1231    }
1232}
1233
1234fn remove_from_set(pipe: &mut Pipeline, key: &str, member: &[u8]) {
1235    pipe.srem(key, member);
1236}
1237
1238fn remove_from_hash(pipe: &mut Pipeline, key: &str, field: &[u8]) {
1239    pipe.hdel(key, field);
1240}
1241
1242fn delete_string(pipe: &mut Pipeline, key: &str) {
1243    pipe.del(key);
1244}
1245
1246fn full_redis_key(trader_key: &str, key: &str) -> String {
1247    format!("{trader_key}{REDIS_DELIMITER}{key}")
1248}
1249
1250fn update_order_indexes(pipe: &mut Pipeline, trader_key: &str, order: &OrderAny) {
1251    let client_order_id = order.client_order_id();
1252    let order_id_bytes = client_order_id.to_string();
1253
1254    insert_set(
1255        pipe,
1256        &full_redis_key(trader_key, INDEX_ORDERS),
1257        order_id_bytes.as_bytes(),
1258    );
1259
1260    if order.venue_order_id().is_some() {
1261        insert_set(
1262            pipe,
1263            &full_redis_key(trader_key, INDEX_ORDER_IDS),
1264            order_id_bytes.as_bytes(),
1265        );
1266    }
1267
1268    if order.is_inflight() {
1269        insert_set(
1270            pipe,
1271            &full_redis_key(trader_key, INDEX_ORDERS_INFLIGHT),
1272            order_id_bytes.as_bytes(),
1273        );
1274    } else {
1275        remove_from_set(
1276            pipe,
1277            &full_redis_key(trader_key, INDEX_ORDERS_INFLIGHT),
1278            order_id_bytes.as_bytes(),
1279        );
1280    }
1281
1282    if order.is_open() {
1283        remove_from_set(
1284            pipe,
1285            &full_redis_key(trader_key, INDEX_ORDERS_CLOSED),
1286            order_id_bytes.as_bytes(),
1287        );
1288        insert_set(
1289            pipe,
1290            &full_redis_key(trader_key, INDEX_ORDERS_OPEN),
1291            order_id_bytes.as_bytes(),
1292        );
1293    } else if order.is_closed() {
1294        remove_from_set(
1295            pipe,
1296            &full_redis_key(trader_key, INDEX_ORDERS_OPEN),
1297            order_id_bytes.as_bytes(),
1298        );
1299        insert_set(
1300            pipe,
1301            &full_redis_key(trader_key, INDEX_ORDERS_CLOSED),
1302            order_id_bytes.as_bytes(),
1303        );
1304    }
1305
1306    if order.emulation_trigger().is_some() && !order.is_closed() {
1307        insert_set(
1308            pipe,
1309            &full_redis_key(trader_key, INDEX_ORDERS_EMULATED),
1310            order_id_bytes.as_bytes(),
1311        );
1312    } else {
1313        remove_from_set(
1314            pipe,
1315            &full_redis_key(trader_key, INDEX_ORDERS_EMULATED),
1316            order_id_bytes.as_bytes(),
1317        );
1318    }
1319}
1320
1321fn format_timestamp(timestamp: UnixNanos) -> String {
1322    format!("{:.9}", timestamp.to_datetime_utc())
1323}
1324
1325fn get_trader_key(trader_id: TraderId, instance_id: UUID4, config: &CacheConfig) -> String {
1326    let mut key = String::new();
1327
1328    if config.use_trader_prefix {
1329        key.push_str("trader-");
1330    }
1331
1332    key.push_str(trader_id.as_str());
1333
1334    if config.use_instance_id {
1335        key.push(REDIS_DELIMITER);
1336        write!(key, "{instance_id}").expect("writing to String cannot fail");
1337    }
1338
1339    key
1340}
1341
1342fn get_collection_key(key: &str) -> anyhow::Result<&str> {
1343    key.split_once(REDIS_DELIMITER)
1344        .map(|(collection, _)| collection)
1345        .ok_or_else(|| {
1346            anyhow::anyhow!("Invalid `key`, missing a '{REDIS_DELIMITER}' delimiter, was {key}")
1347        })
1348}
1349
1350#[derive(Debug)]
1351pub struct RedisCacheDatabaseAdapter {
1352    pub database: RedisCacheDatabase,
1353}
1354
1355impl RedisCacheDatabaseAdapter {
1356    #[cfg(feature = "defi")]
1357    fn write_token_instrument(
1358        &self,
1359        instrument: &InstrumentAny,
1360        migration: Option<InstrumentMigration>,
1361    ) -> anyhow::Result<()> {
1362        use nautilus_model::defi::currency;
1363        let codes = currency::instrument_codes(instrument).map_err(crate::defi::integrity_error)?;
1364        let mut payloads = Vec::with_capacity(codes.len() + 1);
1365        for code in &codes {
1366            let (currency, _) = currency::lookup(Ustr::from(code)).ok_or_else(|| {
1367                crate::defi::integrity_error(format!("Missing token definition for {code}"))
1368            })?;
1369
1370            let record = CurrencyRecord::from(&currency);
1371            payloads.push(Bytes::from(self.database.serialize_payload(&record)?));
1372        }
1373
1374        let payload = if codes.is_empty() {
1375            self.database.serialize_payload(instrument)?
1376        } else {
1377            let record = crate::defi::InstrumentRecord {
1378                defi_currency_codes: codes.clone(),
1379                instrument: serde_json::to_value(instrument)?,
1380            };
1381
1382            self.database.serialize_payload(&record)?
1383        };
1384
1385        payloads.push(Bytes::from(payload));
1386        self.database.write_defi(
1387            format!("instruments:{}", instrument.id()),
1388            codes,
1389            payloads,
1390            crate::defi::currency_references(instrument),
1391            migration,
1392        )
1393    }
1394
1395    fn encoding(&self) -> SerializationEncoding {
1396        self.database.get_encoding()
1397    }
1398
1399    fn send_command(
1400        &self,
1401        op_type: DatabaseOperation,
1402        key: String,
1403        payload: Option<Vec<Bytes>>,
1404    ) -> anyhow::Result<()> {
1405        let op = DatabaseCommand::new(op_type, key, payload);
1406        self.database
1407            .tx
1408            .send(op)
1409            .map_err(|e| anyhow::anyhow!("{FAILED_TX_CHANNEL}: {e}"))
1410    }
1411
1412    fn append_list(&self, key: String, payload: Bytes) -> anyhow::Result<()> {
1413        self.send_command(DatabaseOperation::Update, key, Some(vec![payload]))
1414    }
1415
1416    fn serialize_account_event(&self, account: &AccountAny) -> anyhow::Result<Bytes> {
1417        let event: AccountState = account.last_event().ok_or_else(|| {
1418            anyhow::anyhow!("Cannot persist account with no events: {}", account.id())
1419        })?;
1420
1421        let payload = self.database.serialize_payload(&event)?;
1422        Ok(Bytes::from(payload))
1423    }
1424
1425    fn serialize_order_event(&self, order_event: &OrderEventAny) -> anyhow::Result<Bytes> {
1426        let payload = self.database.serialize_payload(order_event)?;
1427        Ok(Bytes::from(payload))
1428    }
1429
1430    fn serialize_position_event(&self, position: &Position) -> anyhow::Result<Bytes> {
1431        let event: OrderFilled = position.last_event().ok_or_else(|| {
1432            anyhow::anyhow!("Cannot persist position with no events: {}", position.id)
1433        })?;
1434
1435        let payload = self.database.serialize_payload(&event)?;
1436        Ok(Bytes::from(payload))
1437    }
1438
1439    fn load_state(&self, key: String) -> anyhow::Result<AHashMap<String, Bytes>> {
1440        let mut con = self.database.con.clone();
1441        let trader_key = self.database.trader_key.clone();
1442        let encoding = self.encoding();
1443        let (tx, rx) = mpsc::channel();
1444
1445        get_runtime().spawn(async move {
1446            let result = async {
1447                let full_key = format!("{trader_key}{REDIS_DELIMITER}{key}");
1448                let value: Option<Bytes> = con.get(&full_key).await?;
1449                let Some(value) = value else {
1450                    return Ok(AHashMap::new());
1451                };
1452
1453                Self::deserialize_state(encoding, &value)
1454            }
1455            .await;
1456
1457            if let Err(e) = tx.send(result) {
1458                log::error!("Failed to send state load result for '{key}': {e:?}");
1459            }
1460        });
1461
1462        blocking_recv(&rx).map_err(|e| anyhow::anyhow!("load_state channel closed: {e}"))?
1463    }
1464
1465    fn deserialize_state(
1466        encoding: SerializationEncoding,
1467        value: &[u8],
1468    ) -> anyhow::Result<AHashMap<String, Bytes>> {
1469        match encoding {
1470            SerializationEncoding::MsgPack => Ok(rmp_serde::from_slice(value)?),
1471            SerializationEncoding::Json => {
1472                let values: AHashMap<String, serde_json::Value> = serde_json::from_slice(value)?;
1473                values
1474                    .into_iter()
1475                    .map(|(key, value)| {
1476                        let bytes = match value {
1477                            serde_json::Value::String(value) => {
1478                                Bytes::from(BASE64_STANDARD.decode(value)?)
1479                            }
1480                            value => serde_json::from_value(value)?,
1481                        };
1482
1483                        Ok((key, bytes))
1484                    })
1485                    .collect()
1486            }
1487            SerializationEncoding::Sbe => {
1488                anyhow::bail!("SBE encoding is not supported for Redis cache payloads")
1489            }
1490        }
1491    }
1492
1493    fn update_state(&self, key: String, state: &AHashMap<String, Bytes>) -> anyhow::Result<()> {
1494        let payload = self.database.serialize_payload(state)?;
1495        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1496    }
1497
1498    fn replace_list(&self, key: String, payload: Bytes) -> anyhow::Result<()> {
1499        self.send_command(DatabaseOperation::ReplaceList, key, Some(vec![payload]))
1500    }
1501}
1502
1503#[async_trait::async_trait]
1504impl CacheDatabaseFactory for RedisCacheConfig {
1505    async fn create(
1506        &self,
1507        trader_id: TraderId,
1508        instance_id: UUID4,
1509        config: CacheConfig,
1510    ) -> anyhow::Result<Box<dyn CacheDatabaseAdapter>> {
1511        let database =
1512            RedisCacheDatabase::new(trader_id, instance_id, config, self.clone()).await?;
1513        Ok(Box::new(RedisCacheDatabaseAdapter { database }))
1514    }
1515}
1516
1517#[async_trait::async_trait]
1518impl CacheDatabaseAdapter for RedisCacheDatabaseAdapter {
1519    fn close(&mut self) -> anyhow::Result<()> {
1520        self.database.close();
1521        Ok(())
1522    }
1523
1524    fn flush(&mut self) -> anyhow::Result<()> {
1525        self.database.flushdb_sync()
1526    }
1527
1528    async fn load_all(&self) -> anyhow::Result<CacheMap> {
1529        log::debug!("Loading all data");
1530
1531        DatabaseQueries::load_all_batched(
1532            &self.database.con,
1533            self.encoding(),
1534            &self.database.trader_key,
1535            self.database.bulk_read_batch_size.unwrap_or(usize::MAX),
1536        )
1537        .await
1538    }
1539
1540    fn load(&self) -> anyhow::Result<AHashMap<String, Bytes>> {
1541        let con = self.database.con.clone();
1542        let trader_key = self.database.trader_key.clone();
1543        let batch_size = self.database.bulk_read_batch_size.unwrap_or(usize::MAX);
1544        let (tx, rx) = mpsc::channel();
1545
1546        get_runtime().spawn(async move {
1547            let result = async {
1548                let pattern = format!("{trader_key}{REDIS_DELIMITER}{GENERAL}:*");
1549                let mut con_scan = con.clone();
1550                let keys = DatabaseQueries::scan_keys(&mut con_scan, pattern).await?;
1551                if keys.is_empty() {
1552                    return Ok(AHashMap::new());
1553                }
1554
1555                let values = DatabaseQueries::read_bulk_batched(&con, &keys, batch_size).await?;
1556                let prefix = format!("{trader_key}{REDIS_DELIMITER}{GENERAL}{REDIS_DELIMITER}");
1557                let mut general = AHashMap::new();
1558
1559                for (key, value) in keys.into_iter().zip(values) {
1560                    let Some(value) = value else {
1561                        continue;
1562                    };
1563
1564                    if let Some(clean_key) = key.strip_prefix(&prefix) {
1565                        general.insert(clean_key.to_string(), value);
1566                    }
1567                }
1568
1569                Ok(general)
1570            }
1571            .await;
1572
1573            if let Err(e) = tx.send(result) {
1574                log::error!("Failed to send general load result: {e:?}");
1575            }
1576        });
1577
1578        blocking_recv(&rx).map_err(|e| anyhow::anyhow!("load channel closed: {e}"))?
1579    }
1580
1581    async fn load_currencies(&self) -> anyhow::Result<AHashMap<Ustr, Currency>> {
1582        DatabaseQueries::load_currencies_batched(
1583            &self.database.con,
1584            &self.database.trader_key,
1585            self.encoding(),
1586            self.database.bulk_read_batch_size.unwrap_or(usize::MAX),
1587        )
1588        .await
1589    }
1590
1591    async fn load_instruments(&self) -> anyhow::Result<AHashMap<InstrumentId, InstrumentAny>> {
1592        DatabaseQueries::load_instruments(
1593            &self.database.con,
1594            &self.database.trader_key,
1595            self.encoding(),
1596        )
1597        .await
1598    }
1599
1600    async fn load_instrument_closes(
1601        &self,
1602    ) -> anyhow::Result<AHashMap<InstrumentId, InstrumentClose>> {
1603        DatabaseQueries::load_instrument_closes_batched(
1604            &self.database.con,
1605            &self.database.trader_key,
1606            self.encoding(),
1607            self.database.bulk_read_batch_size.unwrap_or(usize::MAX),
1608        )
1609        .await
1610    }
1611
1612    async fn load_synthetics(&self) -> anyhow::Result<AHashMap<InstrumentId, SyntheticInstrument>> {
1613        DatabaseQueries::load_synthetics(
1614            &self.database.con,
1615            &self.database.trader_key,
1616            self.encoding(),
1617        )
1618        .await
1619    }
1620
1621    async fn load_accounts(&self) -> anyhow::Result<AHashMap<AccountId, AccountAny>> {
1622        DatabaseQueries::load_accounts(
1623            &self.database.con,
1624            &self.database.trader_key,
1625            self.encoding(),
1626        )
1627        .await
1628    }
1629
1630    async fn load_orders(&self) -> anyhow::Result<AHashMap<ClientOrderId, OrderAny>> {
1631        DatabaseQueries::load_orders(
1632            &self.database.con,
1633            &self.database.trader_key,
1634            self.encoding(),
1635        )
1636        .await
1637    }
1638
1639    async fn load_positions(&self) -> anyhow::Result<AHashMap<PositionId, Position>> {
1640        DatabaseQueries::load_positions(
1641            &self.database.con,
1642            &self.database.trader_key,
1643            self.encoding(),
1644        )
1645        .await
1646    }
1647
1648    fn load_index_order_position(&self) -> anyhow::Result<AHashMap<ClientOrderId, PositionId>> {
1649        let con = self.database.con.clone();
1650        let trader_key = self.database.trader_key.clone();
1651        let (tx, rx) = mpsc::channel();
1652
1653        get_runtime().spawn(async move {
1654            let result = DatabaseQueries::load_index_order_position(&con, &trader_key).await;
1655            if let Err(e) = tx.send(result) {
1656                log::error!("Failed to send load_index_order_position result: {e:?}");
1657            }
1658        });
1659
1660        blocking_recv(&rx)
1661            .map_err(|e| anyhow::anyhow!("load_index_order_position channel closed: {e}"))?
1662    }
1663
1664    fn load_index_order_client(&self) -> anyhow::Result<AHashMap<ClientOrderId, ClientId>> {
1665        let con = self.database.con.clone();
1666        let trader_key = self.database.trader_key.clone();
1667        let (tx, rx) = mpsc::channel();
1668
1669        get_runtime().spawn(async move {
1670            let result = DatabaseQueries::load_index_order_client(&con, &trader_key).await;
1671            if let Err(e) = tx.send(result) {
1672                log::error!("Failed to send load_index_order_client result: {e:?}");
1673            }
1674        });
1675
1676        blocking_recv(&rx)
1677            .map_err(|e| anyhow::anyhow!("load_index_order_client channel closed: {e}"))?
1678    }
1679
1680    async fn load_currency(&self, code: &Ustr) -> anyhow::Result<Option<Currency>> {
1681        DatabaseQueries::load_currency(
1682            &self.database.con,
1683            &self.database.trader_key,
1684            code,
1685            self.encoding(),
1686        )
1687        .await
1688    }
1689
1690    async fn load_instrument(
1691        &self,
1692        instrument_id: &InstrumentId,
1693    ) -> anyhow::Result<Option<InstrumentAny>> {
1694        load_instrument(
1695            &self.database.con,
1696            &self.database.trader_key,
1697            instrument_id,
1698            self.encoding(),
1699            self.database.bulk_read_batch_size.unwrap_or(usize::MAX),
1700        )
1701        .await
1702    }
1703
1704    async fn load_synthetic(
1705        &self,
1706        instrument_id: &InstrumentId,
1707    ) -> anyhow::Result<Option<SyntheticInstrument>> {
1708        DatabaseQueries::load_synthetic(
1709            &self.database.con,
1710            &self.database.trader_key,
1711            instrument_id,
1712            self.encoding(),
1713        )
1714        .await
1715    }
1716
1717    fn load_instrument_sync(
1718        &self,
1719        instrument_id: &InstrumentId,
1720    ) -> anyhow::Result<Option<InstrumentAny>> {
1721        let con = self.database.con.clone();
1722        let trader_key = self.database.trader_key.clone();
1723        let encoding = self.encoding();
1724        let instrument_id = *instrument_id;
1725        let batch_size = self.database.bulk_read_batch_size.unwrap_or(usize::MAX);
1726        block_on_nautilus_with(move || async move {
1727            load_instrument(&con, &trader_key, &instrument_id, encoding, batch_size).await
1728        })
1729    }
1730
1731    fn load_synthetic_sync(
1732        &self,
1733        instrument_id: &InstrumentId,
1734    ) -> anyhow::Result<Option<SyntheticInstrument>> {
1735        let con = self.database.con.clone();
1736        let trader_key = self.database.trader_key.clone();
1737        let encoding = self.encoding();
1738        let instrument_id = *instrument_id;
1739        block_on_nautilus_with(move || async move {
1740            DatabaseQueries::load_synthetic(&con, &trader_key, &instrument_id, encoding).await
1741        })
1742    }
1743
1744    async fn load_account(&self, account_id: &AccountId) -> anyhow::Result<Option<AccountAny>> {
1745        DatabaseQueries::load_account(
1746            &self.database.con,
1747            &self.database.trader_key,
1748            account_id,
1749            self.encoding(),
1750        )
1751        .await
1752    }
1753
1754    async fn load_order(
1755        &self,
1756        client_order_id: &ClientOrderId,
1757    ) -> anyhow::Result<Option<OrderAny>> {
1758        DatabaseQueries::load_order(
1759            &self.database.con,
1760            &self.database.trader_key,
1761            client_order_id,
1762            self.encoding(),
1763        )
1764        .await
1765    }
1766
1767    async fn load_position(&self, position_id: &PositionId) -> anyhow::Result<Option<Position>> {
1768        DatabaseQueries::load_position(
1769            &self.database.con,
1770            &self.database.trader_key,
1771            position_id,
1772            self.encoding(),
1773        )
1774        .await
1775    }
1776
1777    fn load_actor(&self, actor_id: &ActorId) -> anyhow::Result<AHashMap<String, Bytes>> {
1778        let key = format!("{ACTORS}{REDIS_DELIMITER}{actor_id}{REDIS_DELIMITER}state");
1779        self.load_state(key)
1780    }
1781
1782    fn load_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<AHashMap<String, Bytes>> {
1783        let key = format!("{STRATEGIES}{REDIS_DELIMITER}{strategy_id}{REDIS_DELIMITER}state");
1784        self.load_state(key)
1785    }
1786
1787    fn load_signals(&self, _name: &str) -> anyhow::Result<Vec<Signal>> {
1788        anyhow::bail!("Loading signals from Redis cache adapter not supported")
1789    }
1790
1791    fn load_custom_data(&self, data_type: &DataType) -> anyhow::Result<Vec<CustomData>> {
1792        self.database.load_custom_data(data_type)
1793    }
1794
1795    fn load_order_snapshot(
1796        &self,
1797        _client_order_id: &ClientOrderId,
1798    ) -> anyhow::Result<Option<OrderSnapshot>> {
1799        anyhow::bail!("Loading order snapshots from Redis cache adapter not supported")
1800    }
1801
1802    fn load_position_snapshot(
1803        &self,
1804        _position_id: &PositionId,
1805    ) -> anyhow::Result<Option<PositionSnapshot>> {
1806        anyhow::bail!("Loading position snapshots from Redis cache adapter not supported")
1807    }
1808
1809    fn load_quotes(&self, _instrument_id: &InstrumentId) -> anyhow::Result<Vec<QuoteTick>> {
1810        anyhow::bail!("Loading quote data for Redis cache adapter not supported")
1811    }
1812
1813    fn load_trades(&self, _instrument_id: &InstrumentId) -> anyhow::Result<Vec<TradeTick>> {
1814        anyhow::bail!("Loading market data for Redis cache adapter not supported")
1815    }
1816
1817    fn load_funding_rates(
1818        &self,
1819        _instrument_id: &InstrumentId,
1820    ) -> anyhow::Result<Vec<FundingRateUpdate>> {
1821        anyhow::bail!("Loading market data for Redis cache adapter not supported")
1822    }
1823
1824    fn load_bars(&self, _instrument_id: &InstrumentId) -> anyhow::Result<Vec<Bar>> {
1825        anyhow::bail!("Loading market data for Redis cache adapter not supported")
1826    }
1827
1828    fn add(&self, key: String, value: Bytes) -> anyhow::Result<()> {
1829        let key = format!("{GENERAL}{REDIS_DELIMITER}{key}");
1830        self.database.insert(key, Some(vec![value]))
1831    }
1832
1833    fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> {
1834        #[cfg(feature = "defi")]
1835        if nautilus_model::defi::currency::check(*currency)
1836            .map_err(crate::defi::integrity_error)?
1837            .is_some()
1838            || currency.code.contains('@')
1839        {
1840            let record = CurrencyRecord::from(currency);
1841            let payload = self.database.serialize_payload(&record)?;
1842            return self.database.write_defi(
1843                format!("currencies:{}", currency.code),
1844                vec![currency.code.to_string()],
1845                vec![Bytes::from(payload)],
1846                Vec::new(),
1847                None,
1848            );
1849        }
1850
1851        let key = format!("{CURRENCIES}{REDIS_DELIMITER}{}", currency.code);
1852        let record = CurrencyRecord::from(currency);
1853        let payload = self.database.serialize_payload(&record)?;
1854        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1855    }
1856
1857    fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> {
1858        #[cfg(feature = "defi")]
1859        {
1860            self.write_token_instrument(instrument, None)
1861        }
1862
1863        #[cfg(not(feature = "defi"))]
1864        {
1865            let key = format!("{INSTRUMENTS}{REDIS_DELIMITER}{}", instrument.id());
1866            let payload = self.database.serialize_payload(instrument)?;
1867            self.database.insert(key, Some(vec![Bytes::from(payload)]))
1868        }
1869    }
1870
1871    #[cfg(feature = "defi")]
1872    fn add_defi_instrument(
1873        &self,
1874        instrument: &InstrumentAny,
1875        migration: InstrumentMigration,
1876    ) -> anyhow::Result<()> {
1877        self.write_token_instrument(instrument, Some(migration))
1878    }
1879
1880    fn add_instrument_close(&self, close: &InstrumentClose) -> anyhow::Result<()> {
1881        let key = format!(
1882            "{INSTRUMENT_CLOSES}{REDIS_DELIMITER}{}",
1883            close.instrument_id
1884        );
1885        let payload = self.database.serialize_payload(close)?;
1886        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1887    }
1888
1889    fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()> {
1890        let key = format!("{SYNTHETICS}{REDIS_DELIMITER}{}", synthetic.id);
1891        let payload = self.database.serialize_payload(synthetic)?;
1892        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1893    }
1894
1895    fn add_account(&self, account: &AccountAny) -> anyhow::Result<()> {
1896        let account_id = account.id();
1897        let key = format!("{ACCOUNTS}{REDIS_DELIMITER}{account_id}");
1898
1899        let payload = self.serialize_account_event(account)?;
1900        self.database.insert(key, Some(vec![payload]))
1901    }
1902
1903    fn add_order(&self, order: &OrderAny, client_id: Option<ClientId>) -> anyhow::Result<()> {
1904        let client_order_id = order.client_order_id();
1905        let key = format!("{ORDERS}{REDIS_DELIMITER}{client_order_id}");
1906
1907        let event = OrderEventAny::Initialized(order.init_event().clone());
1908        let payload = self.serialize_order_event(&event)?;
1909        self.replace_list(key, payload)?;
1910
1911        let order_id_bytes = Bytes::from(client_order_id.to_string());
1912        self.database
1913            .insert(INDEX_ORDERS.to_string(), Some(vec![order_id_bytes.clone()]))?;
1914
1915        if order.emulation_trigger().is_some() {
1916            self.database.insert(
1917                INDEX_ORDERS_EMULATED.to_string(),
1918                Some(vec![order_id_bytes.clone()]),
1919            )?;
1920        }
1921
1922        if let Some(client_id) = client_id {
1923            self.database.insert(
1924                INDEX_ORDER_CLIENT.to_string(),
1925                Some(vec![order_id_bytes, Bytes::from(client_id.to_string())]),
1926            )?;
1927        }
1928
1929        Ok(())
1930    }
1931
1932    fn add_order_snapshot(&self, snapshot: &OrderSnapshot) -> anyhow::Result<()> {
1933        let key = format!(
1934            "{SNAPSHOTS}{REDIS_DELIMITER}{ORDERS}{REDIS_DELIMITER}{}",
1935            snapshot.client_order_id
1936        );
1937        let payload = self.database.serialize_payload(snapshot)?;
1938        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1939    }
1940
1941    fn add_position(&self, position: &Position) -> anyhow::Result<()> {
1942        let position_id = position.id;
1943        let key = format!("{POSITIONS}{REDIS_DELIMITER}{position_id}");
1944
1945        let payload = self.serialize_position_event(position)?;
1946        self.replace_list(key, payload)?;
1947
1948        if position.requires_replay_state() {
1949            self.add_position_snapshot(&PositionSnapshot::from_replay_state(position, None))?;
1950        }
1951
1952        let position_id_bytes = Bytes::from(position_id.to_string());
1953        self.database.insert(
1954            INDEX_POSITIONS.to_string(),
1955            Some(vec![position_id_bytes.clone()]),
1956        )?;
1957        self.database.insert(
1958            INDEX_POSITIONS_OPEN.to_string(),
1959            Some(vec![position_id_bytes.clone()]),
1960        )?;
1961        self.send_command(
1962            DatabaseOperation::Delete,
1963            INDEX_POSITIONS_CLOSED.to_string(),
1964            Some(vec![position_id_bytes]),
1965        )?;
1966
1967        Ok(())
1968    }
1969
1970    fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()> {
1971        let key = format!(
1972            "{SNAPSHOTS}{REDIS_DELIMITER}{POSITIONS}{REDIS_DELIMITER}{}",
1973            snapshot.position_id
1974        );
1975        let payload = self.database.serialize_payload(snapshot)?;
1976        self.database.insert(key, Some(vec![Bytes::from(payload)]))
1977    }
1978
1979    fn add_order_book(&self, _order_book: &OrderBook) -> anyhow::Result<()> {
1980        anyhow::bail!("Saving market data for Redis cache adapter not supported")
1981    }
1982
1983    fn add_signal(&self, _signal: &Signal) -> anyhow::Result<()> {
1984        anyhow::bail!("Saving signals for Redis cache adapter not supported")
1985    }
1986
1987    fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()> {
1988        self.database.add_custom_data(data)
1989    }
1990
1991    fn add_quote(&self, _quote: &QuoteTick) -> anyhow::Result<()> {
1992        anyhow::bail!("Saving market data for Redis cache adapter not supported")
1993    }
1994
1995    fn add_trade(&self, _trade: &TradeTick) -> anyhow::Result<()> {
1996        anyhow::bail!("Saving market data for Redis cache adapter not supported")
1997    }
1998
1999    fn add_funding_rate(&self, _funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {
2000        anyhow::bail!("Saving market data for Redis cache adapter not supported")
2001    }
2002
2003    fn add_bar(&self, _bar: &Bar) -> anyhow::Result<()> {
2004        anyhow::bail!("Saving market data for Redis cache adapter not supported")
2005    }
2006
2007    fn delete_actor(&self, actor_id: &ActorId) -> anyhow::Result<()> {
2008        let key = format!("{ACTORS}{REDIS_DELIMITER}{actor_id}{REDIS_DELIMITER}state");
2009        let op = DatabaseCommand::new(DatabaseOperation::Delete, key, None);
2010        self.database
2011            .tx
2012            .send(op)
2013            .map_err(|e| anyhow::anyhow!("{FAILED_TX_CHANNEL}: {e}"))
2014    }
2015
2016    fn delete_strategy(&self, component_id: &StrategyId) -> anyhow::Result<()> {
2017        let key = format!("{STRATEGIES}{REDIS_DELIMITER}{component_id}{REDIS_DELIMITER}state");
2018        let op = DatabaseCommand::new(DatabaseOperation::Delete, key, None);
2019        self.database
2020            .tx
2021            .send(op)
2022            .map_err(|e| anyhow::anyhow!("{FAILED_TX_CHANNEL}: {e}"))
2023    }
2024
2025    fn delete_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<()> {
2026        self.database.delete_order(client_order_id)
2027    }
2028
2029    fn delete_position(&self, position_id: &PositionId) -> anyhow::Result<()> {
2030        self.database.delete_position(position_id)
2031    }
2032
2033    fn delete_account_event(&self, account_id: &AccountId, event_id: &str) -> anyhow::Result<()> {
2034        self.database.delete_account_event(account_id, event_id)
2035    }
2036
2037    fn index_venue_order_id(
2038        &self,
2039        client_order_id: ClientOrderId,
2040        venue_order_id: VenueOrderId,
2041    ) -> anyhow::Result<()> {
2042        self.database.insert(
2043            INDEX_ORDER_IDS.to_string(),
2044            Some(vec![Bytes::from(client_order_id.to_string())]),
2045        )?;
2046        log::debug!("Indexed {client_order_id:?} -> {venue_order_id:?}");
2047        Ok(())
2048    }
2049
2050    fn index_order_position(
2051        &self,
2052        client_order_id: ClientOrderId,
2053        position_id: PositionId,
2054    ) -> anyhow::Result<()> {
2055        self.database.insert(
2056            INDEX_ORDER_POSITION.to_string(),
2057            Some(vec![
2058                Bytes::from(client_order_id.to_string()),
2059                Bytes::from(position_id.to_string()),
2060            ]),
2061        )
2062    }
2063
2064    fn index_order_clients(&self, claims: &[(ClientOrderId, ClientId)]) -> anyhow::Result<()> {
2065        if claims.is_empty() {
2066            return Ok(());
2067        }
2068
2069        let mut payload = Vec::with_capacity(claims.len() * 2);
2070        for (client_order_id, client_id) in claims {
2071            payload.push(Bytes::from(client_order_id.to_string()));
2072            payload.push(Bytes::from(client_id.to_string()));
2073        }
2074
2075        self.database
2076            .insert(INDEX_ORDER_CLIENT.to_string(), Some(payload))
2077    }
2078
2079    fn update_actor(
2080        &self,
2081        actor_id: &ActorId,
2082        state: &AHashMap<String, Bytes>,
2083    ) -> anyhow::Result<()> {
2084        let key = format!("{ACTORS}{REDIS_DELIMITER}{actor_id}{REDIS_DELIMITER}state");
2085        self.update_state(key, state)
2086    }
2087
2088    fn update_strategy(
2089        &self,
2090        strategy_id: &StrategyId,
2091        state: &AHashMap<String, Bytes>,
2092    ) -> anyhow::Result<()> {
2093        let key = format!("{STRATEGIES}{REDIS_DELIMITER}{strategy_id}{REDIS_DELIMITER}state");
2094        self.update_state(key, state)
2095    }
2096
2097    fn update_account(&self, account: &AccountAny) -> anyhow::Result<()> {
2098        let account_id = account.id();
2099        let key = format!("{ACCOUNTS}{REDIS_DELIMITER}{account_id}");
2100        let payload = self.serialize_account_event(account)?;
2101        self.append_list(key, payload)
2102    }
2103
2104    fn update_order(&self, order_event: &OrderEventAny) -> anyhow::Result<()> {
2105        let client_order_id = order_event.client_order_id();
2106        let key = format!("{ORDERS}{REDIS_DELIMITER}{client_order_id}");
2107        let payload = self.database.serialize_payload(order_event)?;
2108
2109        let op = DatabaseCommand::new(
2110            DatabaseOperation::UpdateOrder,
2111            key,
2112            Some(vec![Bytes::from(payload)]),
2113        );
2114        self.database
2115            .tx
2116            .send(op)
2117            .map_err(|e| anyhow::anyhow!("{FAILED_TX_CHANNEL}: {e}"))
2118    }
2119
2120    fn update_position(&self, position: &Position) -> anyhow::Result<()> {
2121        let position_id = position.id;
2122        if position.requires_replay_state() {
2123            self.add_position_snapshot(&PositionSnapshot::from_replay_state(position, None))?;
2124        } else {
2125            let key = format!("{POSITIONS}{REDIS_DELIMITER}{position_id}");
2126            let payload = self.serialize_position_event(position)?;
2127            self.append_list(key, payload)?;
2128        }
2129
2130        let position_id_bytes = Bytes::from(position_id.to_string());
2131
2132        if position.is_open() {
2133            self.database.insert(
2134                INDEX_POSITIONS_OPEN.to_string(),
2135                Some(vec![position_id_bytes.clone()]),
2136            )?;
2137            self.send_command(
2138                DatabaseOperation::Delete,
2139                INDEX_POSITIONS_CLOSED.to_string(),
2140                Some(vec![position_id_bytes]),
2141            )?;
2142        } else if position.is_closed() {
2143            self.database.insert(
2144                INDEX_POSITIONS_CLOSED.to_string(),
2145                Some(vec![position_id_bytes.clone()]),
2146            )?;
2147            self.send_command(
2148                DatabaseOperation::Delete,
2149                INDEX_POSITIONS_OPEN.to_string(),
2150                Some(vec![position_id_bytes]),
2151            )?;
2152        }
2153
2154        Ok(())
2155    }
2156
2157    fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> {
2158        let snapshot = OrderSnapshot::from(order.clone());
2159        self.add_order_snapshot(&snapshot)
2160    }
2161
2162    fn snapshot_position_state(
2163        &self,
2164        position: &Position,
2165        ts_snapshot: UnixNanos,
2166        unrealized_pnl: Option<Money>,
2167    ) -> anyhow::Result<()> {
2168        let mut snapshot = PositionSnapshot::from(position, unrealized_pnl);
2169        snapshot.ts_init = ts_snapshot;
2170        self.add_position_snapshot(&snapshot)
2171    }
2172
2173    fn heartbeat(&self, timestamp: UnixNanos) -> anyhow::Result<()> {
2174        let timestamp = format_timestamp(timestamp);
2175        self.database.insert(
2176            format!("{HEALTH}{REDIS_DELIMITER}heartbeat"),
2177            Some(vec![Bytes::from(timestamp)]),
2178        )
2179    }
2180}
2181
2182async fn load_instrument(
2183    con: &ConnectionManager,
2184    trader_key: &str,
2185    instrument_id: &InstrumentId,
2186    encoding: SerializationEncoding,
2187    batch_size: usize,
2188) -> anyhow::Result<Option<InstrumentAny>> {
2189    let mut currencies =
2190        DatabaseQueries::load_currencies_batched(con, trader_key, encoding, batch_size).await?;
2191    register_loaded_currencies(&mut currencies)?;
2192    DatabaseQueries::load_instrument(con, trader_key, instrument_id, encoding).await
2193}
2194
2195#[cfg(test)]
2196mod tests {
2197    use rstest::rstest;
2198
2199    use super::*;
2200
2201    #[rstest]
2202    fn test_get_trader_key_with_prefix_and_instance_id() {
2203        let trader_id = TraderId::from("tester-123");
2204        let instance_id = UUID4::new();
2205        let config = CacheConfig {
2206            use_instance_id: true,
2207            ..Default::default()
2208        };
2209
2210        let key = get_trader_key(trader_id, instance_id, &config);
2211        assert!(key.starts_with("trader-tester-123:"));
2212        assert!(key.ends_with(&instance_id.to_string()));
2213    }
2214
2215    #[rstest]
2216    fn test_get_collection_key_valid() {
2217        let key = "collection:123";
2218        assert_eq!(get_collection_key(key).unwrap(), "collection");
2219    }
2220
2221    #[rstest]
2222    fn test_get_collection_key_invalid() {
2223        let key = "no_delimiter";
2224        assert!(get_collection_key(key).is_err());
2225    }
2226}