sudoSolidity
sudoSolidity15mo ago

Cold Boot Optimizations

I have a next.js app that uses trcp for all of the routes. I am noticing cold boots are slow. It is likely I am doing something wrong, or is there an optimization to be made? I also want to understand how next.js creates the underlying lambda functions with trcp. Is each router its lambda or something else? 
This is a mono repo I have a services package containing all of the service functions and unit tests. I then have an api package that wraps functions from the service package inside routers. The service package contains classes, for example, userService, notificationsService, and more. Each of these classes takes in a configuration. Almost everyone takes in a database connection, but some take in secrets to interact with external APIs. I have a factory that instantiates all of the classes, takes in every secret, and returns an instance of the services. Example: 
return {
 user: new UserService(databaseURL) notification: new NotificationService(SendridKey, databaseURL), moreServices… } I pass this in the context of trcp and interact with the service I want inside of each router. Example: likeThread: protectedProcedure .input( z.object({ tweetId: z.string(), }), ) .mutation(async ({ ctx, input }) => { const { serviceInternals, session } = ctx; await serviceInternals.user.toggleLike( session.user.walletAddress, input.tweetId, ); }), subscribeToEmailList: protectedProcedure .input( z.object({ email: z.string().email(), }), ) .mutation(async ({ ctx, input }) => { const { serviceInternals } = ctx; await serviceInternals.email.subscribeToMailingList(input.email); }),

 I need to be able to perform unit test on each of the service functions individually (client requires % unit code coverage), and I like having the service layer detached from the router. 

On cold boot, this takes 5-10 seconds to load. After that, it is 1 second.
4 Replies
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
splitting up the endpoint is usually the way
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
GitHub
trpc/examples/soa at main · trpc/trpc
🧙‍♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
sudoSolidity
sudoSolidityOP15mo ago
Wow, I had no idea you could do this super cool. Do you know how next works with the trcp? Does it make new functions for the data in the route, or is each router treated as a function? I'm checking now how much code I am using on boot and if it's all in the same service. I don't think I need to have multiple endpoints. Maybe it would be better to no use a class and instead, just bunch functions together and import them into the router?
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
Each router as a function Your cold starts can also be related to prisma, so look up what you can do there too

Did you find this page helpful?