CASHPOCKET is one TypeScript codebase that serves a browser and a phone. This page lays the system out once: what runs where, how a request is checked, where state lives, how third parties are kept at arm's length, and the decisions behind that shape.

The system on one page

Two clients at the top: a web client on Next.js and React 19 sending a Bearer token, and a mobile app on React Native and Expo for iOS and Android. Below them a pm2 cluster of two instances contains a custom Node server that runs environment checks and a staging guard before handing requests to the Next handler, API routes that serve one /api to both clients, scheduled jobs that run on instance 0 only and call the API with a job key, and services for points, campaigns, approval and notifications. A state band holds a MongoDB replica set used through Prisma and the driver with transactions for points, Redis for the account status cache and refresh-token rotation, and S3 with CloudFront for presigned upload and CDN delivery. On the right, external providers each sit behind an adapter: payments, messaging by KakaoTalk alerts, SMS, email and push, identity and bank verification with social login, coupons and parcel tracking, and social data for the influencer index.
Figure 1. System architecture. Both clients call the same API. The server runs as two pm2 instances, and the scheduler starts in instance 0 only. Third parties are reached through adapters.
ComponentTechnologyResponsibility
Web clientNext.js App Router, React 19, TypeScript, Tailwind, TanStack QueryTester, advertiser and admin surfaces, each under its own layout
Mobile appReact Native 0.81, Expo 54, React Navigation 7Tester surface on iOS and Android, with web views for flows that live on the web
Server entryA custom Node HTTP server in CommonJSLoads the environment, checks it, refuses unsafe staging boots, then hands requests to Next
API routesNext route handlers under one /api treeOne API for both clients, grouped by domain: account, campaigns, points, payments, notifications, admin
ServicesTypeScript modulesPoint kernel, campaign approval and closure, rewards, notifications, chatbot, ad insights
Schedulernode-cron, started inside the serverDaily and periodic jobs, on one instance
DatabaseMongoDB replica set, Prisma plus the native driverAll durable state. The driver is used where a transaction or a pipeline update is needed
CacheRedisAccount status in front of MongoDB, refresh-token hashes, short locks
MediaS3 and CloudFrontPresigned uploads from the client, delivery through the CDN
Process managerpm2 in cluster mode, two instancesRestarts, reloads, memory ceiling
MonitoringSentry on server, edge, browser and appErrors tagged by failing provider and route

The life of a request

A sequence with four participants: a client on the web or the app holding a Bearer token, an API route that verifies the token first, Redis holding account status for one hour, and MongoDB holding users. Step one, the client sends a request with an access token. Step two, the route reads the account status from Redis. Only when the entry is missing: step three, the route loads the user from MongoDB, and step four, stores the status in Redis with a time limit. When an account is blocked or logs out: step five, the route deletes the status entry and the refresh token.
Figure 2. A request and the account check. The token is verified first. The account's current status is then read from Redis, and MongoDB is asked only when the entry is missing.

Both clients send an access token in the Authorization header. A route verifies the signature, rejects tokens that were issued for another purpose, and then asks a second question that the token cannot answer: is this account still allowed in? An admin may have blocked it a minute ago.

That answer comes from a Redis hash holding the account’s blocked, withdrawn and login-allowed flags, its user type and its role. The request check reads the blocked and login-allowed flags. On a miss the route loads the user from MongoDB and stores the hash for an hour. When an admin blocks an account or converts it to an advertiser, or the user logs out, the entry is deleted, so the next request reads fresh state. The admin checks also require the cached user type to be admin and the withdrawn flag to be clear, then load the admin’s user document from MongoDB.

Refresh works through the same store. The details are in Account checks from Redis, and refresh tokens that rotate.

Two processes, one scheduler

pm2 runs the server in cluster mode with two instances. Both accept requests. Scheduled work is different: a job that pays rewards must run once, so the scheduler is started only by the instance whose id is 0. The other instance skips it at boot.

Most jobs do not contain business logic. They call the server’s own API with a job key, and the route does the work. That keeps one implementation for a job whether a schedule or an admin triggers it, and puts every job behind the same authentication check. Scheduled jobs on one of two instances has the schedule.

Where state lives

MongoDB holds everything durable. It runs as a replica set because multi-document transactions require one, and points need transactions. Most of the application reads and writes through Prisma. The point kernel and the payment routes use the native driver, because they need a session shared across several writes and conditional pipeline updates.

Redis holds nothing that cannot be rebuilt. If it is unreachable the service keeps answering.

Media goes to S3. A client asks the server for a presigned upload URL, which is issued only for an allowed content type and extension and expires in minutes. The server records a tracking row for every file it signs. Files are soft-deleted first and removed for good by a nightly job 30 days later. Images are compressed on the device before upload, and the server slices very tall product-detail images into tiles.

Providers behind adapters

The product depends on many third parties: card payments, bank-account verification, identity verification, KakaoTalk alert messages, SMS, email, push, mobile-coupon issuing, parcel tracking, social logins, social-media data and language models. Each is reached through an adapter, and a policy module decides per service whether the adapter talks to the real provider or to a mock.

The policy forces mocks in tests and in CI, and in staging unless a service is switched on deliberately. A test that checks the S3 adapter asserts that mock mode is refused in production outside CI. The policy is there so that a test run or a staging deploy does not send a real KakaoTalk message, issue a real coupon or move real money.

Observability

Sentry runs in four places: the server, the edge runtime, the browser and the app. On the server a hook strips the authorization and cookie headers before an event leaves, drops the framework’s control-flow errors, and tags each event with the provider that failed and the API route it failed in. An alert therefore names the provider and the route before anyone opens the trace, which is where the incident path on the Delivery page starts.

Logs go through the console to pm2’s log files. In production the console is patched at start-up to mask emails, phone numbers and IP addresses. A health route with no authentication and no database call is what the deploy script polls before it decides whether to keep a release.

Decisions

1. One backend for both clients. A separate API for the app was the alternative. The app calls the same routes as the browser and identifies itself by its user agent. The cost is that the API has to stay compatible with app versions that are already installed. The server therefore keeps answering API paths on the address that shipped apps were built with, while browsers on that address are redirected to the canonical host.

2. A custom server entry. It replaces the framework’s default start command. The entry point validates the environment name against an allow-list and runs a staging guard that refuses to boot a staging server pointed at the production database or Redis. It starts the scheduler only after the HTTP server is listening, and loads it lazily so the monitoring hooks are in place before the database driver is first used.

3. The scheduler inside the server, on instance 0. A separate worker service was the alternative. One deployable unit is simpler to operate for a small team, and the jobs are thin calls into the API. The one-instance rule is what makes that safe.

4. A stored balance with a ledger row in the same transaction. The alternative is a balance computed by summing the ledger. A debit is a single conditional update that states the balance it needs, so the database itself refuses an overdraft. The ledger row records the balance before and after, which lets an audit script compare the stored number with the history. The trade-offs are on the point ledger page.

5. A replica set from the start. Transactions were a requirement of the point design, and MongoDB only offers them on a replica set. The local development stack starts one as well, so the same code path runs on a laptop.

6. Account status from a cache. Claims cannot be revoked, and a database read per request is the most frequent query the system would make. A cached status that is deleted when an admin blocks the account makes the block take effect at the next request, for the price of one Redis read.

7. Adapters with a mock policy. Route code does not call provider SDKs directly. The policy is what allows end-to-end tests, an agent opening a draft fix, and a staging server to run the real code paths without real side effects.