Asynchronous read replicas can lag behind the primary. Routing every read to a replica may therefore violate read-your-writes consistency when a user reads immediately after writing.
Design a ReplicaRouter class:
ReplicaRouter(String[] replicas) creates a router. Every replica starts at applied log sequence number, or LSN, 0.void recordWrite(String session, int lsn) records that session has observed a primary write at lsn. A session's required LSN never moves backward.void advanceReplica(String replica, int lsn) reports replication progress. A replica's applied LSN never moves backward.String routeRead(String session, String mode) returns the selected replica or "primary".
When mode is "eventual", every replica is eligible. When it is "read-your-writes", a replica is eligible only if its applied LSN is at least the greatest write LSN recorded for the session. A session with no recorded write requires LSN 0.
Choose the eligible replica with the greatest applied LSN, breaking ties by alphabetically smaller replica name. If no replica satisfies read-your-writes, return "primary".
Example 1:
Input:
Output:
Explanation: Replica A has reached the session's LSN 4. After replica B advances to LSN 8, it becomes the freshest target for an eventual read.
Example 2:
Input:
Output:
Explanation: Neither replica has applied LSN 8, so the consistent read uses primary. Eventual mode can use replica B at LSN 5.
Constraints
1 <= replicas.length <= 500- Replica names are unique non-empty lowercase strings.
0 <= lsn <= 10^9mode is "eventual" or "read-your-writes".- Every name passed to
advanceReplica belongs to replicas. - At most
10^5 operations are performed.